Ejemplo n.º 1
0
    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")
Ejemplo n.º 2
0
    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")
Ejemplo n.º 3
0
    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")
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    def test_atom_5(self):
        wff = "Person(f())"
        expr = Predicate("Person", [Func("f", [])])

        tree = FoplParser.parse(wff)

        self.assertEqual(tree.expr, expr)
Ejemplo n.º 7
0
    def test_atom_1(self):
        wff = "P()"
        expr = Predicate("P", [])

        tree = FoplParser.parse(wff)

        self.assertEqual(tree.expr, expr)
Ejemplo n.º 8
0
    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"])
Ejemplo n.º 9
0
    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"])
Ejemplo n.º 10
0
    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"])
Ejemplo n.º 11
0
    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")
Ejemplo n.º 12
0
    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"])
Ejemplo n.º 13
0
    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"])
Ejemplo n.º 14
0
    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, [])
Ejemplo n.º 15
0
    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"])
Ejemplo n.º 16
0
    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")
Ejemplo n.º 17
0
    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"])
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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"])
Ejemplo n.º 20
0
    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"])
Ejemplo n.º 21
0
    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)
Ejemplo n.º 22
0
def parse(expr):
    if expr.startswith('[') and expr.endswith(']'):
        expr = expr[1:-1]
    expr = FoplParser.parse(expr).expr
    ConstConverter(expr)
    return expr
Ejemplo n.º 23
0
def parse(expr: str):
    return FoplParser.parse(expr).expr
Ejemplo n.º 24
0
def parse(expr):
    expr = FoplParser.parse(expr).expr
    ConstConverter(expr)
    return expr