def test_aima_example(self): self._test_transformer( "B11 <=> (P12 OR P21)", AndTerm( OrTerm(NotTerm(SymbolTerm("B11")), OrTerm(SymbolTerm("P12"), SymbolTerm("P21"))), AndTerm(OrTerm(NotTerm(SymbolTerm("P12")), SymbolTerm("B11")), OrTerm(NotTerm(SymbolTerm("P21")), SymbolTerm("B11")))))
def test_aima_example(self): parser = PLParser() sentence = parser.parse("B11 <=> (P12 OR P21)") transformer = CNFTransformer() cnf = transformer.transform(sentence) self._test_gatherer(cnf, [ OrTerm(NotTerm(SymbolTerm("B11")), OrTerm(SymbolTerm("P12"), SymbolTerm("P21"))), OrTerm(NotTerm(SymbolTerm("P12")), SymbolTerm("B11")), OrTerm(NotTerm(SymbolTerm("P21")), SymbolTerm("B11")) ])
def _transform_implication(self, term): first_child = NotTerm(term.children[0]) second_child = term.children[1] # Implication transformation; A => B == (NOT A) OR B return self.transform(OrTerm(self.transform(first_child), second_child))
def _transform_not_expression(self, term): not_child = term.children[0] # Check if we can remove double NOT if not_child.type == TokenTypes.NOT: return self.transform(not_child.children[0]) # De Morgan rule NOT (A OR B) == (NOT A) AND (NOT B) elif not_child.type == TokenTypes.OR: return AndTerm(self.transform(NotTerm(not_child.children[0])), self.transform(NotTerm(not_child.children[1]))) # De Morgan rule NOT (A AND B) == (NOT A) OR (NOT B) elif not_child.type == TokenTypes.AND: return self.transform( OrTerm(self.transform(NotTerm(not_child.children[0])), self.transform(NotTerm(not_child.children[1])))) # No rules, just return NOT expression return term
def test_dpll1(self): kb = KnowledgeBase() kb.tell_str("(B12 <=> (P11 OR (P13 OR (P22 OR P02))))") kb.tell_str("(B21 <=> (P20 OR (P22 OR (P31 OR P11))))") kb.tell_str("(B01 <=> (P00 OR (P02 OR P11)))") kb.tell_str("(B10 <=> (P11 OR (P20 OR P00)))") kb.tell_str("(NOT B21)") kb.tell_str("(NOT B12)") kb.tell_str("(B10)") kb.tell_str("(B01)") kb.ask_with_dpll(SymbolTerm("P00")) kb.ask_with_dpll(NotTerm(SymbolTerm("P00")))
def _pl_resolve(self, ci, cj): or_gatherer = CNFOrGatherer() (ci_symbols, ci_not_symbols) = or_gatherer.collect(ci) (cj_symbols, cj_not_symbols) = or_gatherer.collect(cj) symbols = cj_symbols | ci_symbols not_symbols = cj_not_symbols | ci_not_symbols unique_symbols = symbols - not_symbols unique_not_symbols = not_symbols - symbols not_symbols_set = { NotTerm(not_symbol) for not_symbol in unique_not_symbols } return create_symbols_connection( OrTerm, list(unique_symbols | not_symbols_set), EmptyClause())
def test_as_sentence(self): kb = KnowledgeBase() kb.tell_str("A") kb.tell_str("A OR B") kb.tell_str("C <=> D") kb.tell_str("E => NOT F") sentence = kb.as_sentence() expected_sentence = AndTerm( SymbolTerm("A"), AndTerm( OrTerm(SymbolTerm("A"), SymbolTerm("B")), AndTerm( BiconditionalTerm(SymbolTerm("C"), SymbolTerm("D")), ImplicationTerm(SymbolTerm("E"), NotTerm(SymbolTerm("F")))))) self.assertEqual(expected_sentence, sentence)
def test_triple_negatiation_transformation(self): self._test_transformer("NOT (NOT (NOT A))", NotTerm(SymbolTerm("A")))
def _create_resolution_sentence(self, knowledge_base, alpha): kb_sentence = knowledge_base.as_sentence() return AndTerm(kb_sentence, NotTerm(alpha))
def test_not(self): self._test_parser("NOT Java", NotTerm(SymbolTerm("Java")))
def test_not_sentence(self): self._test_gatherer_str("NOT A", [NotTerm(SymbolTerm("A"))])
def test_complex_not(self): self._test_parser( "(Java AND NOT C) => Scala", ImplicationTerm( AndTerm(SymbolTerm("Java"), NotTerm(SymbolTerm("C"))), SymbolTerm("Scala")))
def test_root_and_term(self): self._test_transformer( "((A => B) AND C)", AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")), SymbolTerm("C")))
def test_de_morgan2(self): self._test_transformer( "NOT (A OR B)", AndTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
def test_parsing(self): self._test_parser( "(B11 => (NOT P11)) AND B11", AndTerm( ImplicationTerm(SymbolTerm("B11"), NotTerm(SymbolTerm("P11"))), SymbolTerm("B11")))
def test_biconditional_transformation(self): self._test_transformer( "A <=> B", AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")), OrTerm(NotTerm(SymbolTerm("B")), SymbolTerm("A"))))
def test_implication_transfromation(self): self._test_transformer( "A => B", OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")))
def test_de_morgan1(self): self._test_transformer( "NOT (A AND B)", OrTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
def test_not_parenth(self): self._test_parser("(NOT A)", NotTerm(SymbolTerm("A")))