예제 #1
0
    def test_conversion(self):
        formula_equivalences = [
            ("!(A <=> B)", "(!A | !B) & (A | B)"),
            ("(A <=> B)", "(!A | B) & (A | !B)"),
            ("!((A <=> B) => ((A => B) & (B => A)))", "(A | B) & (!A | B) & (!B | A) & (!A | !B)"),
            ("(A <=> B) => ((A => B) & (B => A))", ""),
            ("(P & !Q) | (R & S) | (Q & R & !S)", "(P | Q | S) & (P | R) & (!Q | R)"),
            ("(A => (B & C)) => B", "A | B"),
            ("((!A | B) & (!A | C)) => B", "A | B"),
            ("!(A => (B & C))", "A & (!B | !C)"),
            ("(A => B) => C", "(A | C) & (!B | C)"),
            ("(B11 => (P12 | P21)) & ((P12 | P21) => B11)", "(!B11 | P12 | P21) & (!P12 | B11) & (!P21 | B11)"),
            ("(B11 <=> (P12 | P21))", "(!B11 | P12 | P21) & (!P12 | B11) & (!P21 | B11)"),
            ("(A | B) => C", "(!A | C) & (!B | C)")
        ]

        for formula, cnf in formula_equivalences:
            result = cnfparser.parse_propositional_sentence(formula)
            result2 = cnfparser.parse_propositional_sentence(cnf)
            expected_cnf = [{symbol.strip() for symbol in
                             conjunct.replace(")", "").replace("(", "").split("|")
                            }
                            for conjunct in cnf.split("&")
                            if len(conjunct) != 0
                           ]
            self.assertCountEqual(result, result2)
            self.assertCountEqual(result, expected_cnf)

        general_formulae = [first for first, second in formula_equivalences]
        cnfs = [second for first, second in formula_equivalences]
        from_general = proplogic.KnowledgeBase("\n".join(general_formulae))
        from_cnfs = proplogic.KnowledgeBase("\n".join(cnfs))
        self.assertEqual(from_general, from_cnfs)
        self.assertFalse(from_general != from_cnfs)
예제 #2
0
    def test_kb_parsing(self):
        formulae = [
            "(A => B | D)",
            "((A & B) => C)",
            "(C <=> !D)",
        ]
        kb = proplogic.KnowledgeBase("\n".join(formulae))
        expected = "(!A | B | D) & (!A | !B | C) & (!C | !D) & (C | D)"
        expected_cnf = cnfparser.parse_propositional_sentence(expected)
        self.assertCountEqual(kb.raw_kb, expected_cnf)

        and_concatenation = cnfparser.parse_propositional_sentence(" & ".join(formulae))
        self.assertCountEqual(kb.raw_kb, and_concatenation)