Example #1
0
class TestUtils(unittest.TestCase):

    def test_conversion_to_regex(self):
        self.assertEqual(sorted(turn_functions_into_regex(self.functions)),
                         sorted("((r1)|(r2))*"))

    def test_deduce_uid(self):
        uids = [
            UID("r1", "r2"),
            UID("r2", "r3"),
            UID("r3", "r4")
        ]
        new_uids = deduce_all_uids(uids)
        self.assertEqual(len(new_uids), 6)
        self.assertIn(UID("r1", "r4"), new_uids)
        self.assertIn(UID("r2", "r4"), new_uids)
        self.assertIn(UID("r1", "r3"), new_uids)
        for uid in uids:
            self.assertIn(uid, new_uids)

    def test_generate_schema(self):
        uids = [
            UID("hasChild", "hasChild-"),
            UID("birthday", "hasChild-")
        ]
        functions = []
        function = Function("GetParent")
        function.add_atom("hasChild-", "x", "y")
        functions.append(function)
        function = Function("GetGrandChildrenBirthday")
        function.add_atom("hasChild", "x", "y")
        function.add_atom("hasChild", "y", "z")
        function.add_atom("birthday", "z", "t")
        function.set_existential_variable("y")
        functions.append(function)
        relations = ["hasChild", "hasChild-", "birthday", "birthday-"]
        schema = get_schema_xml(functions, relations, uids)
        self.assertIn("schema", schema)

    def setUp(self):
        self.f0 = Function()
        self.f0.add_atom("r1", "x", "y")
        self.f1 = Function()
        self.f1.add_atom("r2", "z", "t")
        self.functions = [self.f0, self.f1]

    def test_get_all_relations(self):
        self.assertEqual(get_all_relations([]), set())
        self.assertEqual(get_all_relations(self.functions), {"r1", "r1-", "r2", "r2-"})
Example #2
0
 def solve(self, query_relation, max_length=-1):
     if query_relation not in self.relations:
         return None
     if self.prev_relation == query_relation:
         cfg = self.prev_cfg
     else:
         query = Function()
         query.add_atom(query_relation, "x", "y")
         deter = utils.get_dfa_from_functions(self.functions,
                                              query_relation)
         cfg = query.get_longest_query_grammar(self.relations, self.uids)
         cfg = cfg.intersection(deter)
         self.prev_relation = query_relation
         self.prev_cfg = cfg
     if not cfg.is_empty():
         for word in cfg.get_words(max_length=max_length):
             return utils.get_translation(self.fst, word)
         raise ExplorationNotDeepEnough("The depth " + str(max_length) +
                                        " given was not high enough.")
     return None
Example #3
0
 def test_simple(self):
     f = Function()
     f.add_atom("r", "x", "y")
     f.set_input_variable("x")
     solver = Solver([f])
     res = solver.solve("r")
     self.assertIsNotNone(res)
     self.assertEqual(len(res), 1)
Example #4
0
from smart_plan.function import Function
from smart_plan import utils

function0 = Function()
function0.add_atom("12", "x", "y")
function0.set_input_variable("x")

function1 = Function()
function1.add_atom("14", "x", "y")
function1.set_input_variable("x")

function2 = Function()
function2.add_atom("14-", "x", "y")
function2.add_atom("12-", "y", "z")
function2.add_atom("13", "z", "t")
function2.set_input_variable("x")

functions = [function0, function1, function2]


linear_paths = utils.get_all_linear_paths(functions)
print(linear_paths)


enfa = utils.get_enfa_from_functions(functions)
new_enfa = utils.get_folded_automaton(enfa)


deter = utils.get_dfa_from_functions(functions)

relation = "13"
 def setUp(self) -> None:
     self.function = Function()
class TestFunction(unittest.TestCase):
    def setUp(self) -> None:
        self.function = Function()

    def test_create_empty_function(self):
        self.assertEqual(self.function.get_number_variables(), 0)
        self.assertEqual(self.function.get_number_atoms(), 0)

    def test_add_atom(self):
        self.function.add_atom("r", "x", "x")
        self.assertEqual(self.function.get_number_variables(), 1)
        self.assertEqual(self.function.get_number_atoms(), 1)

        self.function.add_atom("r", "y", "y")
        self.assertEqual(self.function.get_number_variables(), 2)
        self.assertEqual(self.function.get_number_atoms(), 2)

        self.function.add_atom("r", "x", "y")
        self.assertEqual(self.function.get_number_variables(), 2)

        self.function.add_atom("r", "t", "u")
        self.assertEqual(self.function.get_number_variables(), 4)

    def test_get_linear_paths(self):
        self.assertEqual(self.function.get_linear_paths(), [])

        self.function.add_atom("r1", "x", "y")
        self.assertEqual(sorted(self.function.get_linear_paths()),
                         sorted([["r1"]]))

        self.function.add_atom("r2", "y", "z")
        self.assertEqual(sorted(self.function.get_linear_paths()),
                         sorted([["r1"], ["r1", "r2"]]))

        self.function.add_atom("r3", "x", "z")
        self.assertEqual(sorted(self.function.get_linear_paths()),
                         sorted([["r1"], ["r1", "r2"], ["r3"], ["r3", "r2-"]]))

        self.function.add_atom("r4-", "z", "y")
        self.assertEqual(
            sorted(self.function.get_linear_paths()),
            sorted([["r1"], ["r1", "r2"], ["r3"], ["r3", "r2-"], ["r1", "r4"],
                    ["r3", "r4-"]]))

    def test_real_linear(self):
        self.function.add_atom("r1", "x", "y")
        self.function.add_atom("r2", "y", "z")
        self.assertTrue(self.function.is_linear())

    def test_false_linear(self):
        self.function.add_atom("r1", "x", "y")
        self.function.add_atom("r2", "y", "z")
        self.function.add_atom("r2", "x", "z")
        self.assertFalse(self.function.is_linear())

    def test_false_linear2(self):
        self.function.add_atom("r1", "x", "y")
        self.function.add_atom("r2", "z", "t")
        self.assertFalse(self.function.is_linear())

    def test_get_all_linear_subfunctions(self):
        self.function.add_atom("r1", "x", "y")
        self.function.add_atom("r2", "y", "z")
        self.function.add_atom("r3", "y", "t")
        self.assertEqual(len(self.function.get_linear_subfunctions()), 3)
        for function in self.function.get_linear_subfunctions():
            self.assertTrue(function.is_linear())

    def test_get_xml_relation(self):
        self.function.name = "A"
        self.function.add_atom("r1", "xxx", "yyy")
        self.function.add_atom("r2", "yyy", "zzz")
        self.function.add_atom("r2", "zzz", "ttt")
        self.function.set_existential_variable("zzz")
        xml = self.function.get_xml_relation()
        self.assertIn("View_A", xml)
        self.assertIn("xxx", xml)
        self.assertIn("yyy", xml)
        self.assertNotIn("zzz", xml)
        self.assertIn("ttt", xml)

    def test_get_xml_dependencies(self):
        self.function.name = "A"
        self.function.add_atom("r1", "xxx", "yyy")
        self.function.add_atom("r2", "yyy", "zzz")
        self.function.add_atom("r2", "zzz", "ttt")
        self.function.set_existential_variable("zzz")
        xml = self.function.get_xml_dependencies()
        self.assertEqual(xml.count("View_A"), 2)
        self.assertEqual(xml.count("xxx"), 4)
        self.assertEqual(xml.count("yyy"), 6)
        self.assertEqual(xml.count("ttt"), 4)
        self.assertEqual(xml.count("zzz"), 4)

    def test_existential(self):
        self.assertEqual(self.function.get_number_existential_variables(), 0)

        self.function.add_atom("r1", "x", "y")
        self.function.set_existential_variable("y")

        self.assertEqual(self.function.get_linear_paths(), [])

        self.function.add_atom("r2", "y", "z")
        self.assertEqual(sorted(self.function.get_linear_paths()),
                         sorted([["r1", "r2"]]))

    def test_set_input_variable(self):
        self.function.add_atom("r1", "x", "y")
        self.function.set_input_variable("y")
        self.assertEqual(self.function.get_linear_paths(), [["r1-"]])

    def test_get_longest_query(self):
        self.assertEqual(self.function.get_longest_query(), [])

        self.function.add_atom("r1", "x", "y")
        self.assertEqual(self.function.get_longest_query(), ["r1"])

    def test_grammar(self):
        self.function.add_atom("r1", "x", "y")
        relations = ["r1", "r2", "r1-", "r2-"]
        uids = [UID("r1", "r2")]
        cfg = self.function.get_longest_query_grammar(relations, uids)
        self.assertTrue(cfg.contains(["r1"]))
        self.assertTrue(cfg.contains(["r2", "r2-", "r1"]))
        self.assertFalse(cfg.contains(["r2", "r1"]))

    def test_get_relations_and_inverses(self):
        self.assertEqual(self.function.get_relations_and_inverses(), set())

        self.function.add_atom("r1", "x", "y")
        self.assertEqual(self.function.get_relations_and_inverses(),
                         {"r1", "r1-"})
Example #7
0
        relations = sorted(utils.get_all_relations(functions))
        print("Relations", len(relations))

        # enfa = utils.get_enfa_from_functions(functions)
        # enfa = utils.get_folded_automaton(enfa)

        fst = utils.get_transducer_parser(functions)

        answered = 0
        sizes = []

        for relation in relations:
            logging.info("Processing %s", relation)

            query = Function()
            query.add_atom(relation, "x", "y")
            deter = utils.get_dfa_from_functions(functions, relation)
            cfg = query.get_longest_query_grammar(relations, uids)
            cfg = cfg.intersection(deter)
            if not cfg.is_empty():
                logging.info("%s can be answered.", relation)
                answered += 1
                for word in cfg.get_words():
                    logging.info(str(word))
                    logging.info(str(utils.get_translation(fst, word)))
                    sizes.append(len(word))
                    break
            else:
                logging.info("%s CANNOT be answered", relation)
Example #8
0
 def setUp(self):
     self.f0 = Function()
     self.f0.add_atom("r1", "x", "y")
     self.f1 = Function()
     self.f1.add_atom("r2", "z", "t")
     self.functions = [self.f0, self.f1]
Example #9
0
 def test_generate_schema(self):
     uids = [
         UID("hasChild", "hasChild-"),
         UID("birthday", "hasChild-")
     ]
     functions = []
     function = Function("GetParent")
     function.add_atom("hasChild-", "x", "y")
     functions.append(function)
     function = Function("GetGrandChildrenBirthday")
     function.add_atom("hasChild", "x", "y")
     function.add_atom("hasChild", "y", "z")
     function.add_atom("birthday", "z", "t")
     function.set_existential_variable("y")
     functions.append(function)
     relations = ["hasChild", "hasChild-", "birthday", "birthday-"]
     schema = get_schema_xml(functions, relations, uids)
     self.assertIn("schema", schema)
 def return_function_if_single_input(self, function):
     is_single_output = self.counter == 1
     if not is_single_output:
         return Function()
     return function
 def create_new_function(self, name="NONAME"):
     function = Function(name=name)
     self.counter = 0
     return function
         (1, 4, 10, 15, 0.2),
         (1, 4, 15, 15, 0.2)
        ]

experiments_setups = experiments_setups[::-1]

if __name__ == '__main__':

    if not os.path.exists("../benedikt_schemas"):
        os.makedirs("../benedikt_schemas")

    while True:
        for min_length, max_length, n_relation, n_function, proba_existential in experiments_setups:

            relations = get_random_relations(n_relation)
            functions = [Function.get_random_function(relations, min_length, max_length, proba_existential, str(i))
                         for i in range(n_function)]

            relations = sorted(utils.get_all_relations(functions))
            linear_paths = utils.get_all_linear_paths(functions)

            uids = utils.get_nicoleta_assumption_uids(linear_paths)

            schema = get_schema_xml(get_all_linear_subfunctions(functions),
                                    relations,
                                    uids)

            #enfa = utils.get_enfa_from_functions(functions)
            #new_enfa = utils.get_folded_automaton(enfa)

            answered_us = 0
Example #13
0
 def test_four_function_deeper(self):
     f0 = Function()
     f0.add_atom("u", "x", "y")
     f0.add_atom("r", "y", "z")
     f0.set_input_variable("x")
     f1 = Function()
     f1.add_atom("r-", "x", "y")
     f1.add_atom("s", "y", "z")
     f1.set_input_variable("x")
     f2 = Function()
     f2.add_atom("s-", "x", "y")
     f2.add_atom("t", "y", "z")
     f2.set_input_variable("x")
     f3 = Function()
     f3.add_atom("t-", "x", "y")
     f3.add_atom("u-", "y", "z")
     f3.add_atom("q", "z", "z'")
     f3.set_input_variable("x")
     solver = Solver([f0, f1, f2, f3])
     res = solver.solve("q")
     self.assertIsNotNone(res)
     self.assertEqual(len(res), 4)
Example #14
0
 def test_three_functions(self):
     f0 = Function()
     f0.add_atom("r", "x", "y")
     f0.set_input_variable("x")
     f1 = Function()
     f1.add_atom("r-", "x", "y")
     f1.add_atom("s", "y", "z")
     f1.set_input_variable("x")
     f2 = Function()
     f2.add_atom("s-", "x", "y")
     f2.add_atom("q", "y", "z")
     f2.set_input_variable("x")
     solver = Solver([f0, f1, f2])
     res = solver.solve("q")
     self.assertIsNotNone(res)
     self.assertEqual(len(res), 3)
Example #15
0
 def test_five_function_fabian_missing_function(self):
     f0 = Function()
     f0.add_atom("hasChild-", "x", "y")
     f0.add_atom("r", "y", "z")
     f0.set_input_variable("x")
     f1 = Function()
     f1.add_atom("r-", "x", "y")
     f1.add_atom("s", "y", "z")
     f1.set_input_variable("x")
     f2 = Function()
     f2.add_atom("s-", "x", "y")
     f2.add_atom("u", "y", "z")
     f2.set_input_variable("x")
     f3 = Function()
     f3.add_atom("u-", "x", "y")
     f3.add_atom("t", "y", "z")
     f3.set_input_variable("x")
     solver = Solver([f0, f1, f2, f3])
     res = solver.solve("hasBirthday")
     self.assertIsNone(res)
Example #16
0
 def test_five_function_fabian(self):
     f0 = Function()
     f0.add_atom("hasChild-", "x", "y")
     f0.add_atom("r", "y", "z")
     f0.set_input_variable("x")
     f1 = Function()
     f1.add_atom("r-", "x", "y")
     f1.add_atom("s", "y", "z")
     f1.set_input_variable("x")
     f2 = Function()
     f2.add_atom("s-", "x", "y")
     f2.add_atom("u", "y", "z")
     f2.set_input_variable("x")
     f3 = Function()
     f3.add_atom("u-", "x", "y")
     f3.add_atom("t", "y", "z")
     f3.set_input_variable("x")
     f4 = Function()
     f4.add_atom("t-", "x", "y")
     f4.add_atom("hasChild", "y", "z")
     f4.add_atom("hasBirthday", "z", "z'")
     f4.set_input_variable("x")
     solver = Solver([f0, f1, f2, f3, f4])
     res = solver.solve("hasBirthday")
     self.assertIsNotNone(res)
     self.assertEqual(len(res), 5)