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)
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)
def test_two_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("q", "y", "z") f1.set_input_variable("x") solver = Solver([f0, f1]) res = solver.solve("q") self.assertIsNotNone(res) self.assertEqual(len(res), 2)
def test_four_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("t", "y", "z") f2.set_input_variable("x") f3 = Function() f3.add_atom("t-", "x", "y") f3.add_atom("q", "y", "z") f3.set_input_variable("x") solver = Solver([f0, f1, f2, f3]) res = solver.solve("q") self.assertIsNotNone(res) self.assertEqual(len(res), 4)
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)
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"
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-"})