def to_problem(self) -> GenericProblem:
     p = GenericProblem(["A A"], ["A A"])
     p.id = self.id
     p.active_constraints = self.active_constraints
     p.passive_constraints = self.passive_constraints
     p.leaf_constraints = self.leaf_constraints
     p.root_constraints = self.root_constraints
     alphabet = p.get_alphabet()
     p.leaf_allow_all = (set(flatten(p.leaf_constraints)) -
                         {" "}) == set(alphabet)
     p.root_allow_all = (set(flatten(p.root_constraints)) -
                         {" "}) == set(alphabet)
     p.flags = ProblemFlags(
         is_tree=self.is_tree,
         is_cycle=self.is_cycle,
         is_path=self.is_path,
         is_directed_or_rooted=self.is_directed_or_rooted,
         is_regular=self.is_regular,
     )
     return p
Example #2
0
def store_problem_and_get_with_id(p: GenericProblem) -> GenericProblem:
    with get_db_cursor(commit=True) as cur:
        problem_data = (
            p.get_active_degree(),
            p.get_passive_degree(),
            len(p.get_alphabet()),
            each_constr_is_homogeneous(p.active_constraints),
            each_constr_is_homogeneous(p.passive_constraints),
            list(p.active_constraints),
            list(p.passive_constraints),
            list(p.leaf_constraints),
            list(p.root_constraints),
            p.flags.is_tree,
            p.flags.is_cycle,
            p.flags.is_path,
            p.flags.is_directed_or_rooted,
            p.flags.is_regular,
        )
        cur.execute(
            """
            INSERT INTO problems (
            active_degree,
            passive_degree,
            label_count,
            actives_all_same,
            passives_all_same,

            active_constraints,
            passive_constraints,
            root_constraints,
            leaf_constraints,
            is_tree,
            is_cycle,
            is_path,
            is_directed_or_rooted,
            is_regular
            ) VALUES (
                %s, %s, %s, %s, %s,
                %s, %s, %s, %s, %s,
                %s, %s, %s, %s
            ) ON CONFLICT DO NOTHING RETURNING id;""",
            problem_data,
        )

        res = cur.fetchone()
        if res is None:
            cur.execute(
                """
                SELECT id FROM problems
                WHERE
                    active_degree = %s AND
                    passive_degree = %s AND
                    label_count = %s AND
                    actives_all_same = %s AND
                    passives_all_same = %s AND

                    active_constraints = %s AND
                    passive_constraints = %s AND
                    root_constraints = %s AND
                    leaf_constraints = %s AND
                    is_tree = %s AND
                    is_cycle = %s AND
                    is_path = %s AND
                    is_directed_or_rooted = %s AND
                    is_regular = %s;
            """,
                problem_data,
            )
            res = cur.fetchone()

        p.id = res["id"]

    return p