コード例 #1
0
 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")))))
コード例 #2
0
    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"))
        ])
コード例 #3
0
ファイル: visitors.py プロジェクト: kamranalam27/aima-python
    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))
コード例 #4
0
ファイル: visitors.py プロジェクト: kamranalam27/aima-python
    def _transform_or(self, term):
        left_child = term.children[0]
        right_child = term.children[1]

        # If we have A OR (B AND C)
        if not left_child.is_function() and right_child.type == TokenTypes.AND:
            and_expression = right_child
            symbol = left_child
        # If we have (B AND C) OR A
        elif not right_child.is_function(
        ) and left_child.type == TokenTypes.AND:
            and_expression = left_child
            symbol = right_child
        else:
            # Just return OR term
            return term

        and_left_child = and_expression.children[0]
        and_right_child = and_expression.children[1]

        # OR distribution; A OR (B AND C) == (A OR B) AND (A OR C)
        return AndTerm(self.transform(OrTerm(and_left_child, symbol)),
                       self.transform(OrTerm(and_right_child, symbol)))
コード例 #5
0
ファイル: visitors.py プロジェクト: kamranalam27/aima-python
    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
コード例 #6
0
    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)
コード例 #7
0
 def test_root_and_term(self):
     self._test_transformer(
         "((A => B) AND C)",
         AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")),
                 SymbolTerm("C")))
コード例 #8
0
 def test_or_distribution2(self):
     self._test_transformer(
         "A OR (B AND C)",
         AndTerm(OrTerm(SymbolTerm("B"), SymbolTerm("A")),
                 OrTerm(SymbolTerm("C"), SymbolTerm("A"))))
コード例 #9
0
 def test_or_distribution1(self):
     self._test_transformer(
         "(A AND B) OR C",
         AndTerm(OrTerm(SymbolTerm("A"), SymbolTerm("C")),
                 OrTerm(SymbolTerm("B"), SymbolTerm("C"))))
コード例 #10
0
 def test_de_morgan1(self):
     self._test_transformer(
         "NOT (A AND B)",
         OrTerm(NotTerm(SymbolTerm("A")), NotTerm(SymbolTerm("B"))))
コード例 #11
0
 def test_biconditional_transformation(self):
     self._test_transformer(
         "A <=> B",
         AndTerm(OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")),
                 OrTerm(NotTerm(SymbolTerm("B")), SymbolTerm("A"))))
コード例 #12
0
 def test_implication_transfromation(self):
     self._test_transformer(
         "A => B", OrTerm(NotTerm(SymbolTerm("A")), SymbolTerm("B")))
コード例 #13
0
 def test_or(self):
     self._test_transformer("A OR B",
                            OrTerm(SymbolTerm("A"), SymbolTerm("B")))
コード例 #14
0
 def test_pl_resolve_with_one_literal(self):
     self._test_pl_resolve("A OR B", "(NOT B) OR C",
                           OrTerm(SymbolTerm("A"), SymbolTerm("C")))