def test_impl_1(self): expected_permutations = [ "A()->B()", ] for expr_str in expected_permutations: expr = Parser.parse(expr_str).expr permutations = expr.permute() self.assertEqual(len(permutations), 1) expr = Parser.parse(expr_str).expr self.assertIn(expr, permutations, f"{expr_str} not in perms")
def test_eq_1(self): expected_permutations = [ "A()<->B()", ] for expr in expected_permutations: expr = Parser.parse(expr).expr permutations = expr.permute() self.assertEqual(len(permutations), len(expected_permutations)) for expected_permutation in expected_permutations: expr = Parser.parse(expected_permutation).expr self.assertIn(expr, permutations, f"{expected_permutation} missing")
def test_or_3(self): expected_permutations = [ "(A()|B())|C()", "A()|(B()|C())", ] for expr in expected_permutations: expr = Parser.parse(expr).expr permutations = expr.permute() self.assertEqual(len(permutations), len(expected_permutations)) for expected_permutation in expected_permutations: expr = Parser.parse(expected_permutation).expr self.assertIn(expr, permutations, f"{expected_permutation} missing")
def test_ex_quant_1(self): wff = "(E)a Person(a)" expr = ExistentialQuantor(Var("a"), Predicate("Person", [Var("a")])) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr)
def test_all_quant_1(self): wff = "(A)a Person(a)" expr = AllQuantor(Var("a"), Predicate("Person", [Var("a")])) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr)
def test_atom_5(self): wff = "Person(f())" expr = Predicate("Person", [Func("f", [])]) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr)
def test_atom_1(self): wff = "P()" expr = Predicate("P", []) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr)
def test_clamp_2(self): wff = "( Person(A) )" expr = Predicate("Person", [Const("A")]) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["A"])
def test_not_3(self): wff = "!!Person(A)" expr = Not(Not(Predicate("Person", [Const("A")]))) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["A"])
def test_atom_4(self): wff = "Person( X, Y, Z)" expr = Predicate("Person", [Const("X"), Const("Y"), Const("Z")]) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["X", "Y", "Z"])
def test_and_4(self): expected_permutations = [ "A()&(B()&(C()&D()))", "A()&((B()&C())&D())", "((A()&B())&C())&D()", "(A()&(B()&C()))&D()", "(A()&B())&(C()&D())", ] for expr in expected_permutations: expr = Parser.parse(expr).expr permutations = expr.permute() self.assertEqual(len(permutations), len(expected_permutations)) for expected_permutation in expected_permutations: expr = Parser.parse(expected_permutation).expr self.assertIn(expr, permutations, f"{expected_permutation} missing")
def test_eq_3(self): wff = "Person(A) <-> Person(B)" expr = Eq(Predicate("Person", [Const("A")]), Predicate("Person", [Const("B")])) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["A", "B"])
def test_atom_8(self): wff = "(A)a Person(a,B)" expr = AllQuantor(Var("a"), Predicate("Person", [Var("a"), Const("B")])) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["B"])
def test_atom_9(self): wff = "(A)a,b Person(a,b)" expr = AllQuantor( Var("a"), AllQuantor(Var("b"), Predicate("Person", [Var("a"), Var("b")]))) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, [])
def test_complex_4(self): wff = "P(A)|P(B)&P(C)|P(D)" expr = Or( Or(Predicate("P", [Const("A")]), And(Predicate("P", [Const("B")]), Predicate("P", [Const("C")]))), Predicate("P", [Const("D")])) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["A", "B", "C", "D"])
def test_and_6(self): expr = Or(And(Predicate("A", []), Predicate("B", [])), Predicate("C", [])) expected_permutations = [ "A()&B()|C()", ] permutations = expr.permute() self.assertEqual(len(permutations), len(expected_permutations)) for expected_permutation in expected_permutations: expr = Parser.parse(expected_permutation).expr self.assertIn(expr, permutations, f"{expected_permutation} missing")
def test_atom_7(self): wff = "Person(A,f(X,g(Y,Z)),B)" expr = Predicate("Person", [ Const("A"), Func("f", [Const("X"), Func("g", [Const("Y"), Const("Z")])]), Const("B") ]) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["A", "X", "Y", "Z", "B"])
def test_complex_1(self): wff = "(A)a (E)b (Person(a)&Person(b)->Parent(a,b))" expr = AllQuantor( Var("a"), ExistentialQuantor( Var("b"), Impl( And(Predicate("Person", [Var("a")]), Predicate("Person", [Var("b")])), Predicate("Parent", [Var("a"), Var("b")])))) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr)
def test_complex_3(self): wff = "(P(A)->K(f(A,B))&P(C))<->!(E(C,D)|K(A))" expr = Eq( Impl( Predicate("P", [Const("A")]), And(Predicate("K", [Func("f", [Const("A"), Const("B")])]), Predicate("P", [Const("C")]))), Not( Or(Predicate("E", [Const("C"), Const("D")]), Predicate("K", [Const("A")])))) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["A", "B", "C", "D"])
def test_complex_5(self): wff = "(A)a (P(a)->K(f(a,B))&P(C)<->!(E(C,D)|K(a)))" expr = AllQuantor( Var("a"), Eq( Impl( Predicate("P", [Var("a")]), And(Predicate("K", [Func("f", [Var("a"), Const("B")])]), Predicate("P", [Const("C")]))), Not( Or(Predicate("E", [Const("C"), Const("D")]), Predicate("K", [Var("a")]))))) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr) self.assertEqual(tree.constants, ["B", "C", "D"])
def test_complex_2(self): wff = "(A)a,b (E)c (Person(a)&Person(b)&Person(c)&Parent(a,b)&Parent(b,c)->Grandparent(a,c))" expr = AllQuantor( Var("a"), AllQuantor( Var("b"), ExistentialQuantor( Var("c"), Impl( And( And( And( And(Predicate("Person", [Var("a")]), Predicate("Person", [Var("b")])), Predicate("Person", [Var("c")])), Predicate("Parent", [Var("a"), Var("b")])), Predicate("Parent", [Var("b"), Var("c")])), Predicate("Grandparent", [Var("a"), Var("c")]))))) tree = FoplParser.parse(wff) self.assertEqual(tree.expr, expr)
def parse(expr): if expr.startswith('[') and expr.endswith(']'): expr = expr[1:-1] expr = FoplParser.parse(expr).expr ConstConverter(expr) return expr
def parse(expr: str): return FoplParser.parse(expr).expr
def parse(expr): expr = FoplParser.parse(expr).expr ConstConverter(expr) return expr