예제 #1
0
 def test_cnf_action_alpha(self):
     g1 = Generalization("or", [Formula("&", Formula("X"), Formula("Y"))])
     exp = [
         Generalization("or", [Formula("X")]),
         Generalization("or", [Formula("Y")])
     ]
     self.assertEqual(exp, g1.cnf_action())
예제 #2
0
 def test_components_binary(self):
     formula = Formula("&", self.a1, self.a2)
     (comp1, comp2) = formula.components()
     self.assertIsNot(comp1, self.a1)
     self.assertIsNot(comp2, self.a2)
     self.assertEqual(comp1, self.a1)
     self.assertEqual(comp2, self.a2)
 def test_components_binary(self):
     formula = Formula("&", self.a1, self.a2)
     (comp1, comp2) = formula.components()
     self.assertIsNot(comp1, self.a1)
     self.assertIsNot(comp2, self.a2)
     self.assertEqual(comp1, self.a1)
     self.assertEqual(comp2, self.a2)
예제 #4
0
 def test_has_non_literal(self):
     g1 = Generalization("and", [self.f1, Formula("Z"), self.f2])
     g2 = Generalization("or", [Formula("Z"), Formula("!", Formula("Y"))])
     g3 = Generalization("or", [])
     self.assertTrue(g1.has_non_literal)
     self.assertFalse(g3.has_non_literal())
     self.assertFalse(g2.has_non_literal())
 def test_is_alpha_primary(self):
     self.assertTrue(self.fand1.is_alpha())
     self.assertFalse(self.for1.is_alpha())
     f1 = Formula("!", self.fand1)
     self.assertFalse(f1.is_alpha())
     f2 = Formula("!", self.for1)
     self.assertTrue(f2.is_alpha())
예제 #6
0
 def test_nnf_unary(self):
     sub = Formula("&", self.a1, self.a2)
     formula = Formula("!", sub)
     exp = Formula("|", Formula("!", Formula("X")),
                   Formula("!", Formula("Y")))
     self.assertEqual(exp, formula.nnf())
     self.assertIsNot(self.a1, formula.nnf().subformula1.subformula1)
     self.assertIsNot(self.a2, formula.nnf().subformula2.subformula1)
 def test_nnf_binary(self):
     sub1 = Formula("!", Formula("!", self.a1))
     sub2 = Formula("!", Formula("!", self.a2))
     formula = Formula("&", sub1, sub2)
     exp = Formula("&", self.a1, self.a2)
     self.assertEqual(exp, formula.nnf())
     self.assertIsNot(self.a1, formula.nnf().subformula1)
     self.assertIsNot(self.a2, formula.nnf().subformula2)
 def test_nnf_unary(self):
     sub = Formula("&", self.a1, self.a2)
     formula = Formula("!", sub)
     exp = Formula("|",
                   Formula("!", Formula("X")),
                   Formula("!", Formula("Y")))
     self.assertEqual(exp, formula.nnf())
     self.assertIsNot(self.a1, formula.nnf().subformula1.subformula1)
     self.assertIsNot(self.a2, formula.nnf().subformula2.subformula1)
예제 #9
0
 def setUp(self):
     self.a1 = Formula("X")
     self.a2 = Formula("Y")
     self.l1 = Formula("!", Formula("X"))
     self.fand1 = Formula("&", Formula("X"), Formula("Y"))
     self.for1 = Formula("|", Formula("X"), Formula("Y"))
     self.taut1 = parser.parse("((P->Q)&(Q->R))->-(-R&P)")
     self.taut2 = parser.parse("(-P->Q)->((P->Q)->Q)")
     self.taut3 = parser.parse("((P->Q)->P)->P")
     self.taut4 = parser.parse("(P->(Q->R))->((P->Q)->(P->R))")
예제 #10
0
 def test_nnf_binary(self):
     sub1 = Formula("!", Formula("!", self.a1))
     sub2 = Formula("!", Formula("!", self.a2))
     formula = Formula("&", sub1, sub2)
     exp = Formula("&", self.a1, self.a2)
     self.assertEqual(exp, formula.nnf())
     self.assertIsNot(self.a1, formula.nnf().subformula1)
     self.assertIsNot(self.a2, formula.nnf().subformula2)
예제 #11
0
 def test_cnf(self):
     g1 = Generalization(
         "or", [Formula("&", Formula("X"), Formula("!", Formula("Y")))])
     g2 = Generalization("and", [
         Generalization(
             "or", [Formula("&", Formula("X"), Formula("!", Formula("Y")))])
     ])
     self.assertEqual(Generalization("and", g1.cnf_action()), g2.cnf())
 def test_is_alpha_none(self):
     # atomic
     self.assertIsNone(self.a1.is_alpha())
     # literal
     self.assertIsNone(self.l1.is_alpha())
     # secondary connectives
     s1 = Formula("<=>", self.a1, self.a2)
     s2 = Formula("!=", self.a1, self.a2)
     self.assertIsNone(s1.is_alpha())
     self.assertIsNone(s2.is_alpha())
     # not not
     f1 = self.l1.negate().negate()
     self.assertIsNone(f1.is_alpha())
     f2 = self.fand1.negate().negate()
     self.assertIsNone(f2.is_alpha())
     f3 = self.for1.negate().negate()
     self.assertIsNone(f3.is_alpha())
예제 #13
0
 def test_is_alpha_primary(self):
     self.assertTrue(self.fand1.is_alpha())
     self.assertFalse(self.for1.is_alpha())
     f1 = Formula("!", self.fand1)
     self.assertFalse(f1.is_alpha())
     f2 = Formula("!", self.for1)
     self.assertTrue(f2.is_alpha())
예제 #14
0
 def test_remove_every(self):
     g1 = Generalization(
         "and", [self.f1, Formula("Z"), self.f2,
                 Formula("Z")])
     exp1 = Generalization("and", [self.f1, self.f2])
     g2 = Generalization(
         "and",
         [self.f1, Formula("Z"), self.f1,
          Formula("Z"), self.f2])
     exp2 = Generalization("and", [Formula("Z"), Formula("Z"), self.f2])
     g1.remove_every(Formula("Z"))
     self.assertEqual(exp1, g1)
예제 #15
0
 def test_cnf_particular(self):
     # components
     p = Formula("P")
     np = Formula("!", p)
     q = Formula("Q")
     nq = Formula("!", q)
     r = Formula("R")
     # formulas
     f1s = "-(P-> (Q-> R)) -> ((P->Q) -> (P -> R))"
     f1 = parser.parse(f1s)
     f2s = "-(P&Q) -> R"
     f2 = parser.parse(f2s)
     # expected
     exp1 = Generalization("and", [
         Generalization("or", [np, nq, r, p, np, r]),
         Generalization("or", [np, nq, r, nq, np, r])
     ])
     exp2 = Generalization(
         "and",
         [Generalization("or", [p, r]),
          Generalization("or", [q, r])])
     # assertions
     self.assertEqual(exp1, f1.cnf())
     self.assertEqual(exp2, f2.cnf())
예제 #16
0
 def test_remove_every_generalization(self):
     g1 = Generalization(
         "and", [self.f1, Formula("Z"), self.f2,
                 Formula("Z")])
     g2 = Generalization(
         "and",
         [self.f1,
          Formula("Z"), g1, self.f1,
          Formula("Z"), self.f2, g1])
     exp = Generalization(
         "and",
         [Formula("Z"), g1, Formula("Z"), self.f2, g1])
     g2.remove_every(self.f1)
     self.assertEqual(exp, g2)
예제 #17
0
 def test_is_beta_none(self):
     # atomic
     self.assertIsNone(self.a1.is_beta())
     # literal
     self.assertIsNone(self.l1.is_beta())
     # secondary connectives
     s1 = Formula("<=>", self.a1, self.a2)
     s2 = Formula("!=", self.a1, self.a2)
     self.assertIsNone(s1.is_beta())
     self.assertIsNone(s2.is_beta())
     # not not
     f1 = self.l1.negate().negate()
     self.assertIsNone(f1.is_beta())
     f2 = self.fand1.negate().negate()
     self.assertIsNone(f2.is_beta())
     f3 = self.for1.negate().negate()
     self.assertIsNone(f3.is_beta())
예제 #18
0
 def test_init_binary(self):
     formula = Formula("&", self.a1, self.a2)
     self.assertIsInstance(formula, Formula)
     self.assertEqual("and", formula.connective)
     self.assertEqual(self.a1, formula.subformula1)
     self.assertEqual(self.a2, formula.subformula2)
예제 #19
0
 def test_negate(self):
     self.assertIsInstance(self.fand1.negate(), Formula)
     self.assertEqual(Formula("!", self.fand1), self.fand1.negate())
예제 #20
0
 def setUp(self):
     self.a1 = Formula("X")
     self.a2 = Formula("Y")
     self.l1 = Formula("!", Formula("X"))
     self.fand1 = Formula("&", Formula("X"), Formula("Y"))
     self.for1 = Formula("|", Formula("X"), Formula("Y"))
예제 #21
0
 def test_eq_ne(self):
     formula1 = Formula("&", Formula("X"), Formula("Y"))
     formula2 = Formula("&", Formula("X"), Formula("Y"))
     formula3 = Formula("|", Formula("X"), Formula("Y"))
     formula4 = Formula("&", Formula("X"), Formula("X"))
     self.assertEqual(formula1, formula2)
     self.assertNotEqual(formula1, formula3)
     self.assertNotEqual(formula1, formula4)
예제 #22
0
 def test_cnf_not_bottom(self):
     formula = Formula("!", Formula("F"))
     exp = Generalization("and", [Generalization("or", [Formula("T")])])
     self.assertEqual(exp, formula.cnf())
예제 #23
0
 def test_eq_ne_atomic(self):
     formula1 = Formula("X")
     formula2 = Formula("X")
     formula3 = Formula("Y")
     self.assertEqual(formula1, formula2)
     self.assertNotEqual(formula1, formula3)
class TestFormula(unittest.TestCase):
    def setUp(self):
        self.a1 = Formula("X")
        self.a2 = Formula("Y")
        self.l1 = Formula("!", Formula("X"))
        self.fand1 = Formula("&", Formula("X"), Formula("Y"))
        self.for1 = Formula("|", Formula("X"), Formula("Y"))


    def test_init_atomic(self):
        formula = Formula("X")
        self.assertIsInstance(formula, Formula)
        self.assertEqual(None, formula.connective)
        self.assertEqual("X", formula.subformula1)

    def test_init_atomic_wrong(self):
        self.assertRaises(Exception, Formula, "!")

    def test_init_unary(self):
        formula = Formula("!", self.a1)
        self.assertIsInstance(formula, Formula)
        self.assertEqual("not", formula.connective)
        self.assertEqual(self.a1, formula.subformula1)

    def test_init_unary_wrong(self):
        self.assertRaises(Exception, Formula, "&", Formula("X"))
        self.assertRaises(Exception, Formula, "!", "X")

    def test_init_binary(self):
        formula = Formula("&", self.a1, self.a2)
        self.assertIsInstance(formula, Formula)
        self.assertEqual("and", formula.connective)
        self.assertEqual(self.a1, formula.subformula1)
        self.assertEqual(self.a2, formula.subformula2)

    def test_init_binary_wrong(self):
        self.assertRaises(Exception, Formula, "&", "X", "Y")
        self.assertRaises(Exception, Formula, "?", Formula("X"), Formula("Y"))
        self.assertRaises(Exception, Formula, "!", Formula("X"), Formula("Y"))


    def test_str_atomic(self):
        self.assertEqual("X", str(self.a1))

    def test_str_unary(self):
        self.assertEqual("!X", str(self.l1))

    def test_str_binary(self):
        self.assertEqual("(X & Y)", str(self.fand1))

    def test_str_complex(self):
        formula = Formula("&", self.for1, self.l1)
        self.assertEqual("((X | Y) & !X)", str(formula))


    def test_eq_ne_atomic(self):
        formula1 = Formula("X")
        formula2 = Formula("X")
        formula3 = Formula("Y")
        self.assertEqual(formula1, formula2)
        self.assertNotEqual(formula1, formula3)

    def test_eq_ne(self):
        formula1 = Formula("&", Formula("X"), Formula("Y"))
        formula2 = Formula("&", Formula("X"), Formula("Y"))
        formula3 = Formula("|", Formula("X"), Formula("Y"))
        formula4 = Formula("&", Formula("X"), Formula("X"))
        self.assertEqual(formula1, formula2)
        self.assertNotEqual(formula1, formula3)
        self.assertNotEqual(formula1, formula4)


    def test_is_alpha_none(self):
        # atomic
        self.assertIsNone(self.a1.is_alpha())
        # literal
        self.assertIsNone(self.l1.is_alpha())
        # secondary connectives
        s1 = Formula("<=>", self.a1, self.a2)
        s2 = Formula("!=", self.a1, self.a2)
        self.assertIsNone(s1.is_alpha())
        self.assertIsNone(s2.is_alpha())
        # not not
        f1 = self.l1.negate().negate()
        self.assertIsNone(f1.is_alpha())
        f2 = self.fand1.negate().negate()
        self.assertIsNone(f2.is_alpha())
        f3 = self.for1.negate().negate()
        self.assertIsNone(f3.is_alpha())

    def test_is_alpha_primary(self):
        self.assertTrue(self.fand1.is_alpha())
        self.assertFalse(self.for1.is_alpha())
        f1 = Formula("!", self.fand1)
        self.assertFalse(f1.is_alpha())
        f2 = Formula("!", self.for1)
        self.assertTrue(f2.is_alpha())

    def test_is_beta_none(self):
        # atomic
        self.assertIsNone(self.a1.is_beta())
        # literal
        self.assertIsNone(self.l1.is_beta())
        # secondary connectives
        s1 = Formula("<=>", self.a1, self.a2)
        s2 = Formula("!=", self.a1, self.a2)
        self.assertIsNone(s1.is_beta())
        self.assertIsNone(s2.is_beta())
        # not not
        f1 = self.l1.negate().negate()
        self.assertIsNone(f1.is_beta())
        f2 = self.fand1.negate().negate()
        self.assertIsNone(f2.is_beta())
        f3 = self.for1.negate().negate()
        self.assertIsNone(f3.is_beta())

    def test_is_beta_primary(self):
        self.assertFalse(self.fand1.is_beta())
        self.assertTrue(self.for1.is_beta())
        f1 = Formula("!", self.fand1)
        self.assertTrue(f1.is_beta())
        f2 = Formula("!", self.for1)
        self.assertFalse(f2.is_beta())


    def test_is_literal_atomic(self):
        self.assertTrue(self.a1.is_literal())

    def test_is_literal_unary(self):
        self.assertTrue(self.l1.is_literal())
        formula2 = Formula("!", self.fand1)
        self.assertFalse(formula2.is_literal())

    def test_is_literal_binary(self):
        self.assertFalse(self.fand1.is_literal())
        self.assertFalse(self.for1.is_literal())

    def test_is_literal_top_bottom(self):
        self.assertTrue(Formula("T").is_literal())
        self.assertTrue(Formula("F").is_literal())
        self.assertFalse(Formula("!", Formula("T")).is_literal())
        self.assertFalse(Formula("!", Formula("F")).is_literal())


    def test_negate(self):
        self.assertIsInstance(self.fand1.negate(), Formula)
        self.assertEqual(Formula("!", self.fand1), self.fand1.negate())


    def test_complement(self):
        # binary
        self.assertIsInstance(self.fand1.complement(), Formula)
        self.assertEqual(self.fand1.negate(), self.fand1.complement())
        # literal
        self.assertIsInstance(self.l1.complement(), Formula)
        self.assertEqual(self.a1, self.l1.complement())
        # atomic
        self.assertIsInstance(self.a1.complement(), Formula)
        self.assertEqual(self.l1, self.a1.complement())


    def test_components_atomic(self):
        (comp1, comp2) = self.a1.components()
        self.assertIsNot(comp1, self.a1)
        self.assertEqual(comp1, self.a1)
        self.assertEqual(None, comp2)

    def test_components_unary(self):
        (comp1, comp2) = self.l1.components()
        self.assertIsNot(comp1, self.l1)
        self.assertEqual(comp1, self.l1)
        self.assertEqual(None, comp2)

    def test_components_binary(self):
        formula = Formula("&", self.a1, self.a2)
        (comp1, comp2) = formula.components()
        self.assertIsNot(comp1, self.a1)
        self.assertIsNot(comp2, self.a2)
        self.assertEqual(comp1, self.a1)
        self.assertEqual(comp2, self.a2)


    def test_nnf_atomic(self):
        self.assertIsNot(self.a1, self.a1.nnf())
        self.assertEqual(self.a1, self.a1.nnf())

    def test_nnf_unary_literal(self):
        self.assertIsNot(self.l1, self.l1.nnf())
        self.assertEqual(self.l1, self.l1.nnf())

    def test_nnf_unary(self):
        sub = Formula("&", self.a1, self.a2)
        formula = Formula("!", sub)
        exp = Formula("|",
                      Formula("!", Formula("X")),
                      Formula("!", Formula("Y")))
        self.assertEqual(exp, formula.nnf())
        self.assertIsNot(self.a1, formula.nnf().subformula1.subformula1)
        self.assertIsNot(self.a2, formula.nnf().subformula2.subformula1)

    def test_nnf_not_not(self):
        formula = Formula("!", Formula("!", self.a1))
        self.assertIsNot(self.a1, formula.nnf())
        self.assertEqual(self.a1, formula.nnf())

    def test_nnf_binary(self):
        sub1 = Formula("!", Formula("!", self.a1))
        sub2 = Formula("!", Formula("!", self.a2))
        formula = Formula("&", sub1, sub2)
        exp = Formula("&", self.a1, self.a2)
        self.assertEqual(exp, formula.nnf())
        self.assertIsNot(self.a1, formula.nnf().subformula1)
        self.assertIsNot(self.a2, formula.nnf().subformula2)


    def test_cnf_atomic(self):
        exp = Generalization("and", [Generalization("or", [self.a1])])
        self.assertEqual(exp, self.a1.cnf())

    def test_cnf_literal(self):
        exp = Generalization("and", [Generalization("or", [self.l1])])
        self.assertEqual(exp, self.l1.cnf())

    def test_cnf_alpha(self):
        exp = Generalization("and", [
                Generalization("or", [self.a1]),
                Generalization("or", [self.a2])])
        self.assertEqual(exp, self.fand1.cnf())

    def test_cnf_beta(self):
        exp = Generalization("and", [Generalization("or", [self.a1, self.a2])])
        self.assertEqual(exp, self.for1.cnf())

    def test_cnf_not_not(self):
        exp = Generalization("and", [Generalization("or", [self.a1])])
        self.assertEqual(exp, Formula("!", self.l1).cnf())

    def test_cnf_not_top(self):
        formula = Formula("!", Formula("T"))
        exp = Generalization("and", [Generalization("or", [Formula("F")])])
        self.assertEqual(exp, formula.cnf())

    def test_cnf_not_bottom(self):
        formula = Formula("!", Formula("F"))
        exp = Generalization("and", [Generalization("or", [Formula("T")])])
        self.assertEqual(exp, formula.cnf())

    def test_cnf_particular(self):
        # components
        p = Formula("P")
        np = Formula("!", p)
        q = Formula("Q")
        nq = Formula("!", q)
        r = Formula("R")
        # formulas
        f1s = "-(P-> (Q-> R)) -> ((P->Q) -> (P -> R))"
        f1 = parser.parse(f1s)
        f2s = "-(P&Q) -> R"
        f2 = parser.parse(f2s)
        # expected
        exp1 = Generalization("and", [
                Generalization("or", [np, nq, r, p, np, r]),
                Generalization("or", [np, nq, r, nq, np, r])])
        exp2 = Generalization("and", [
                Generalization("or", [p, r]),
                Generalization("or", [q, r])])
        # assertions
        self.assertEqual(exp1, f1.cnf())
        self.assertEqual(exp2, f2.cnf())
예제 #25
0
 def test_init_unary(self):
     formula = Formula("!", self.a1)
     self.assertIsInstance(formula, Formula)
     self.assertEqual("not", formula.connective)
     self.assertEqual(self.a1, formula.subformula1)
예제 #26
0
 def test_is_literal_top_bottom(self):
     self.assertTrue(Formula("T").is_literal())
     self.assertTrue(Formula("F").is_literal())
     self.assertFalse(Formula("!", Formula("T")).is_literal())
     self.assertFalse(Formula("!", Formula("F")).is_literal())
예제 #27
0
 def setUp(self):
     self.f1 = Formula("&", Formula("X"), Formula("Y"))
     self.f2 = Formula("|", Formula("X"), Formula("Y"))
 def test_is_literal_unary(self):
     self.assertTrue(self.l1.is_literal())
     formula2 = Formula("!", self.fand1)
     self.assertFalse(formula2.is_literal())
예제 #29
0
 def test_init_binary_wrong(self):
     self.assertRaises(Exception, Formula, "&", "X", "Y")
     self.assertRaises(Exception, Formula, "?", Formula("X"), Formula("Y"))
     self.assertRaises(Exception, Formula, "!", Formula("X"), Formula("Y"))
예제 #30
0
 def test_get_non_literal_position(self):
     g1 = Generalization("and", [self.f1, Formula("Z"), self.f2])
     self.assertIs(0, g1.get_non_literal_position())
     g2 = Generalization("and", [Formula("Z"), self.f2])
     self.assertIs(1, g2.get_non_literal_position())
예제 #31
0
 def test_nnf_not_not(self):
     formula = Formula("!", Formula("!", self.a1))
     self.assertIsNot(self.a1, formula.nnf())
     self.assertEqual(self.a1, formula.nnf())
예제 #32
0
 def test_init_atomic(self):
     formula = Formula("X")
     self.assertIsInstance(formula, Formula)
     self.assertEqual(None, formula.connective)
     self.assertEqual("X", formula.subformula1)
 def test_nnf_not_not(self):
     formula = Formula("!", Formula("!", self.a1))
     self.assertIsNot(self.a1, formula.nnf())
     self.assertEqual(self.a1, formula.nnf())
예제 #34
0
 def test_cnf_action_not_bottom(self):
     g1 = Generalization("or", [Formula("!", Formula("F"))])
     exp = [Generalization("or", [Formula("T")])]
     self.assertEqual(exp, g1.cnf_action())
 def test_cnf_not_bottom(self):
     formula = Formula("!", Formula("F"))
     exp = Generalization("and", [Generalization("or", [Formula("T")])])
     self.assertEqual(exp, formula.cnf())
예제 #36
0
class TestFormula(unittest.TestCase):
    def setUp(self):
        self.a1 = Formula("X")
        self.a2 = Formula("Y")
        self.l1 = Formula("!", Formula("X"))
        self.fand1 = Formula("&", Formula("X"), Formula("Y"))
        self.for1 = Formula("|", Formula("X"), Formula("Y"))

    def test_init_atomic(self):
        formula = Formula("X")
        self.assertIsInstance(formula, Formula)
        self.assertEqual(None, formula.connective)
        self.assertEqual("X", formula.subformula1)

    def test_init_atomic_wrong(self):
        self.assertRaises(Exception, Formula, "!")

    def test_init_unary(self):
        formula = Formula("!", self.a1)
        self.assertIsInstance(formula, Formula)
        self.assertEqual("not", formula.connective)
        self.assertEqual(self.a1, formula.subformula1)

    def test_init_unary_wrong(self):
        self.assertRaises(Exception, Formula, "&", Formula("X"))
        self.assertRaises(Exception, Formula, "!", "X")

    def test_init_binary(self):
        formula = Formula("&", self.a1, self.a2)
        self.assertIsInstance(formula, Formula)
        self.assertEqual("and", formula.connective)
        self.assertEqual(self.a1, formula.subformula1)
        self.assertEqual(self.a2, formula.subformula2)

    def test_init_binary_wrong(self):
        self.assertRaises(Exception, Formula, "&", "X", "Y")
        self.assertRaises(Exception, Formula, "?", Formula("X"), Formula("Y"))
        self.assertRaises(Exception, Formula, "!", Formula("X"), Formula("Y"))

    def test_str_atomic(self):
        self.assertEqual("X", str(self.a1))

    def test_str_unary(self):
        self.assertEqual("!X", str(self.l1))

    def test_str_binary(self):
        self.assertEqual("(X & Y)", str(self.fand1))

    def test_str_complex(self):
        formula = Formula("&", self.for1, self.l1)
        self.assertEqual("((X | Y) & !X)", str(formula))

    def test_eq_ne_atomic(self):
        formula1 = Formula("X")
        formula2 = Formula("X")
        formula3 = Formula("Y")
        self.assertEqual(formula1, formula2)
        self.assertNotEqual(formula1, formula3)

    def test_eq_ne(self):
        formula1 = Formula("&", Formula("X"), Formula("Y"))
        formula2 = Formula("&", Formula("X"), Formula("Y"))
        formula3 = Formula("|", Formula("X"), Formula("Y"))
        formula4 = Formula("&", Formula("X"), Formula("X"))
        self.assertEqual(formula1, formula2)
        self.assertNotEqual(formula1, formula3)
        self.assertNotEqual(formula1, formula4)

    def test_is_alpha_none(self):
        # atomic
        self.assertIsNone(self.a1.is_alpha())
        # literal
        self.assertIsNone(self.l1.is_alpha())
        # secondary connectives
        s1 = Formula("<=>", self.a1, self.a2)
        s2 = Formula("!=", self.a1, self.a2)
        self.assertIsNone(s1.is_alpha())
        self.assertIsNone(s2.is_alpha())
        # not not
        f1 = self.l1.negate().negate()
        self.assertIsNone(f1.is_alpha())
        f2 = self.fand1.negate().negate()
        self.assertIsNone(f2.is_alpha())
        f3 = self.for1.negate().negate()
        self.assertIsNone(f3.is_alpha())

    def test_is_alpha_primary(self):
        self.assertTrue(self.fand1.is_alpha())
        self.assertFalse(self.for1.is_alpha())
        f1 = Formula("!", self.fand1)
        self.assertFalse(f1.is_alpha())
        f2 = Formula("!", self.for1)
        self.assertTrue(f2.is_alpha())

    def test_is_beta_none(self):
        # atomic
        self.assertIsNone(self.a1.is_beta())
        # literal
        self.assertIsNone(self.l1.is_beta())
        # secondary connectives
        s1 = Formula("<=>", self.a1, self.a2)
        s2 = Formula("!=", self.a1, self.a2)
        self.assertIsNone(s1.is_beta())
        self.assertIsNone(s2.is_beta())
        # not not
        f1 = self.l1.negate().negate()
        self.assertIsNone(f1.is_beta())
        f2 = self.fand1.negate().negate()
        self.assertIsNone(f2.is_beta())
        f3 = self.for1.negate().negate()
        self.assertIsNone(f3.is_beta())

    def test_is_beta_primary(self):
        self.assertFalse(self.fand1.is_beta())
        self.assertTrue(self.for1.is_beta())
        f1 = Formula("!", self.fand1)
        self.assertTrue(f1.is_beta())
        f2 = Formula("!", self.for1)
        self.assertFalse(f2.is_beta())

    def test_is_literal_atomic(self):
        self.assertTrue(self.a1.is_literal())

    def test_is_literal_unary(self):
        self.assertTrue(self.l1.is_literal())
        formula2 = Formula("!", self.fand1)
        self.assertFalse(formula2.is_literal())

    def test_is_literal_binary(self):
        self.assertFalse(self.fand1.is_literal())
        self.assertFalse(self.for1.is_literal())

    def test_is_literal_top_bottom(self):
        self.assertTrue(Formula("T").is_literal())
        self.assertTrue(Formula("F").is_literal())
        self.assertFalse(Formula("!", Formula("T")).is_literal())
        self.assertFalse(Formula("!", Formula("F")).is_literal())

    def test_negate(self):
        self.assertIsInstance(self.fand1.negate(), Formula)
        self.assertEqual(Formula("!", self.fand1), self.fand1.negate())

    def test_complement(self):
        # binary
        self.assertIsInstance(self.fand1.complement(), Formula)
        self.assertEqual(self.fand1.negate(), self.fand1.complement())
        # literal
        self.assertIsInstance(self.l1.complement(), Formula)
        self.assertEqual(self.a1, self.l1.complement())
        # atomic
        self.assertIsInstance(self.a1.complement(), Formula)
        self.assertEqual(self.l1, self.a1.complement())

    def test_components_atomic(self):
        (comp1, comp2) = self.a1.components()
        self.assertIsNot(comp1, self.a1)
        self.assertEqual(comp1, self.a1)
        self.assertEqual(None, comp2)

    def test_components_unary(self):
        (comp1, comp2) = self.l1.components()
        self.assertIsNot(comp1, self.l1)
        self.assertEqual(comp1, self.l1)
        self.assertEqual(None, comp2)

    def test_components_binary(self):
        formula = Formula("&", self.a1, self.a2)
        (comp1, comp2) = formula.components()
        self.assertIsNot(comp1, self.a1)
        self.assertIsNot(comp2, self.a2)
        self.assertEqual(comp1, self.a1)
        self.assertEqual(comp2, self.a2)

    def test_nnf_atomic(self):
        self.assertIsNot(self.a1, self.a1.nnf())
        self.assertEqual(self.a1, self.a1.nnf())

    def test_nnf_unary_literal(self):
        self.assertIsNot(self.l1, self.l1.nnf())
        self.assertEqual(self.l1, self.l1.nnf())

    def test_nnf_unary(self):
        sub = Formula("&", self.a1, self.a2)
        formula = Formula("!", sub)
        exp = Formula("|", Formula("!", Formula("X")),
                      Formula("!", Formula("Y")))
        self.assertEqual(exp, formula.nnf())
        self.assertIsNot(self.a1, formula.nnf().subformula1.subformula1)
        self.assertIsNot(self.a2, formula.nnf().subformula2.subformula1)

    def test_nnf_not_not(self):
        formula = Formula("!", Formula("!", self.a1))
        self.assertIsNot(self.a1, formula.nnf())
        self.assertEqual(self.a1, formula.nnf())

    def test_nnf_binary(self):
        sub1 = Formula("!", Formula("!", self.a1))
        sub2 = Formula("!", Formula("!", self.a2))
        formula = Formula("&", sub1, sub2)
        exp = Formula("&", self.a1, self.a2)
        self.assertEqual(exp, formula.nnf())
        self.assertIsNot(self.a1, formula.nnf().subformula1)
        self.assertIsNot(self.a2, formula.nnf().subformula2)

    def test_cnf_atomic(self):
        exp = Generalization("and", [Generalization("or", [self.a1])])
        self.assertEqual(exp, self.a1.cnf())

    def test_cnf_literal(self):
        exp = Generalization("and", [Generalization("or", [self.l1])])
        self.assertEqual(exp, self.l1.cnf())

    def test_cnf_alpha(self):
        exp = Generalization(
            "and",
            [Generalization("or", [self.a1]),
             Generalization("or", [self.a2])])
        self.assertEqual(exp, self.fand1.cnf())

    def test_cnf_beta(self):
        exp = Generalization("and", [Generalization("or", [self.a1, self.a2])])
        self.assertEqual(exp, self.for1.cnf())

    def test_cnf_not_not(self):
        exp = Generalization("and", [Generalization("or", [self.a1])])
        self.assertEqual(exp, Formula("!", self.l1).cnf())

    def test_cnf_not_top(self):
        formula = Formula("!", Formula("T"))
        exp = Generalization("and", [Generalization("or", [Formula("F")])])
        self.assertEqual(exp, formula.cnf())

    def test_cnf_not_bottom(self):
        formula = Formula("!", Formula("F"))
        exp = Generalization("and", [Generalization("or", [Formula("T")])])
        self.assertEqual(exp, formula.cnf())

    def test_cnf_particular(self):
        # components
        p = Formula("P")
        np = Formula("!", p)
        q = Formula("Q")
        nq = Formula("!", q)
        r = Formula("R")
        # formulas
        f1s = "-(P-> (Q-> R)) -> ((P->Q) -> (P -> R))"
        f1 = parser.parse(f1s)
        f2s = "-(P&Q) -> R"
        f2 = parser.parse(f2s)
        # expected
        exp1 = Generalization("and", [
            Generalization("or", [np, nq, r, p, np, r]),
            Generalization("or", [np, nq, r, nq, np, r])
        ])
        exp2 = Generalization(
            "and",
            [Generalization("or", [p, r]),
             Generalization("or", [q, r])])
        # assertions
        self.assertEqual(exp1, f1.cnf())
        self.assertEqual(exp2, f2.cnf())
 def setUp(self):
     self.a1 = Formula("X")
     self.a2 = Formula("Y")
     self.l1 = Formula("!", Formula("X"))
     self.fand1 = Formula("&", Formula("X"), Formula("Y"))
     self.for1 = Formula("|", Formula("X"), Formula("Y"))
예제 #38
0
 def test_str_complex(self):
     formula = Formula("&", self.for1, self.l1)
     self.assertEqual("((X | Y) & !X)", str(formula))
예제 #39
0
 def test_cnf_action_basis(self):
     g1 = Generalization(
         "or", [Formula("X"),
                Formula("Y"),
                Formula("!", Formula("X"))])
     self.assertEqual([g1], g1.cnf_action())
예제 #40
0
 def test_cnf_not_not(self):
     exp = Generalization("and", [Generalization("or", [self.a1])])
     self.assertEqual(exp, Formula("!", self.l1).cnf())