示例#1
0
 def test_rule_notself(self):
     ctx = Backward()
     ctx.evaluate("""
         !A => A
     """)
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
示例#2
0
 def test_and(self):
     ctx = Backward()
     ans = ctx.evaluate("= a b c")
     ans = ctx.evaluate("a & b & c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a & !b & c")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("a & !(!b & !c)")
     self.assertEqual(ans, [True])
示例#3
0
 def test_rule(self):
     ctx = Backward()
     ans = ctx.evaluate("a => b")
     ans = ctx.evaluate("b => c")
     ans = ctx.evaluate("= a")
     ans = ctx.evaluate("a b c")
     self.assertEqual(ans, [True, True, True])
     ans = ctx.evaluate("a => d & e & f")
     ans = ctx.evaluate("d e f")
     self.assertEqual(ans, [True, True, True])
示例#4
0
 def test_xor(self):
     ctx = Backward()
     ans = ctx.evaluate("= a c")
     ans = ctx.evaluate("a ^ c")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("!a ^ c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a ^ !c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a ^ b")
     self.assertEqual(ans, [True])
示例#5
0
 def test_or(self):
     ctx = Backward()
     ans = ctx.evaluate("= a c")
     ans = ctx.evaluate("!a | c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("a | !c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("b | c")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("!a | !c")
     self.assertEqual(ans, [False])
示例#6
0
 def test_atom(self):
     ctx = Backward()
     ans = ctx.evaluate("a")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("= a c")
     ans = ctx.evaluate("a b c")
     self.assertEqual(ans, [True, False, True])
     ans = ctx.evaluate("=")
     ans = ctx.evaluate("a b c")
     self.assertEqual(ans, [False, False, False])
     ans = ctx.evaluate("!a")
     self.assertEqual(ans, [True])
示例#7
0
 def test_rule_and(self):
     ctx = Backward()
     ans = ctx.evaluate("""
         B => A
         D & E => B
         G & H => F
         I & J => G
         G => H
         L & M => K
         O & P => L & N
         N => M
     """)
     ans = ctx.evaluate("= D E I J O P")
     ans = ctx.evaluate("A F K P")
     self.assertEqual(ans, [True, True, True, True])
     ans = ctx.evaluate("= D E I J P")
     ans = ctx.evaluate("A F K P")
     self.assertEqual(ans, [True, True, False, True])
示例#8
0
 def test_not(self):
     ctx = Backward()
     ans = ctx.evaluate("= a")
     ans = ctx.evaluate("a")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("!a")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("!!a")
     self.assertEqual(ans, [True])
     ans = ctx.evaluate("!!(!a)")
     self.assertEqual(ans, [False])
     ans = ctx.evaluate("!b")
     self.assertEqual(ans, [True])
示例#9
0
 def test_rule_samerule(self):
     ctx = Backward()
     ctx.evaluate("""
         B => A
         C => A
     """)
     ctx.evaluate("= B")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= B C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
示例#10
0
 def test_rule_xor(self):
     ctx = Backward()
     ctx.evaluate("""
         B & C => A
         D ^ E => B
         B => C
     """)
     ctx.evaluate("=")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
     ctx.evaluate("= D")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= E")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= D E")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
示例#11
0
 def test_rule_not2(self):
     ctx = Backward()
     ctx.evaluate("!A | A => B")
     ctx.evaluate("A | !A => C")
     ans = ctx.evaluate("B C")
     self.assertEqual(ans, [True, True])
示例#12
0
 def test_rule_not1(self):
     ctx = Backward()
     ctx.evaluate("B & !C => A")
     ctx.evaluate("=")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
     ctx.evaluate("= B")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [True])
     ctx.evaluate("= C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
     ctx.evaluate("= B C")
     ans = ctx.evaluate("A")
     self.assertEqual(ans, [False])
示例#13
0
    And initial facts:

        Fritz croaks
        Fritz eats flies

    Our goal is to decide wether Fritz is green.
"""

if __name__ == "__main__":

    ctx = Backward()

    # Defining our knowledge base

    ctx.evaluate("croaks & eatsFlies => frog")
    ctx.evaluate("chirps & sings => canary")
    ctx.evaluate("frog => green")
    ctx.evaluate("canary => yellow")

    # Initial facts

    # ctx.evaluate("= croaks eatsFlies")

    # Let's bind some questions to ask the user whenever the value of the variable is unknown.

    ctx.bind_question("croaks", "Does it croak?")
    ctx.bind_question("eatsFlies", "Does it eat flies?")
    ctx.bind_question("chirps", "Does it chirp?")
    ctx.bind_question("sings", "Does it sing?")