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())
Example #2
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())
 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())
Example #4
0
 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())
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())
Example #6
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())