コード例 #1
0
 def test_decimal_negative(self):
     brain = Brain()
     brain.add('(age, -0.42)')
     results = list(brain.query('(age, X)'))
     assertObjectSubsetIn(self, results, {
         'X': Decimal('-0.42'),
     })
コード例 #2
0
 def test_integer_negative(self):
     brain = Brain()
     brain.add('(age, -52)')
     results = list(brain.query('(age, X)'))
     assertObjectSubsetIn(self, results, {
         'X': -52,
     })
コード例 #3
0
 def test_decimal(self):
     """
     You can use Decimals.
     """
     brain = Brain()
     brain.add('(age, 5.2)')
     results = list(brain.query('(age, X)'))
     assertObjectSubsetIn(self, results, {
         'X': Decimal('5.2'),
     })
コード例 #4
0
 def test_integers(self):
     """
     You can use integers.
     """
     brain = Brain()
     brain.add('(age, 52)')
     results = list(brain.query('(age, X)'))
     assertObjectSubsetIn(self, results, {
         'X': 52,
     })
コード例 #5
0
    def test_not(self):
        """
        You can use not to negate stuff.
        """
        brain = Brain()
        map(brain.add, [
            '(good, X) if (not, (bad, X))',
            '(bad, cats)',
        ])
        results = list(brain.query('(good, cats)'))
        self.assertEqual(len(results), 0, "Cats are not good.")

        results = list(brain.query('(good, muffins)'))
        self.assertEqual(len(results), 1, "Muffins are good: %r" % (results, ))
コード例 #6
0
 def test_matchterm_deep(self):
     """
     Variables should match whole terms at any depth.
     """
     brain = Brain()
     rules = [
         '(X, good) if (john, said, X)',
         '(X, rad) if (X, good)',
         '(john, said, hello)',
         '(john, said, (cats, drink, milk))',
     ]
     map(brain.add, rules)
     results = list(brain.query('(X, rad)'))
     assertObjectSubsetIn(self, results, {
         'X': 'hello',
     })
     assertObjectSubsetIn(self, results, {
         'X': ('cats', 'drink', 'milk'),
     })
コード例 #7
0
 def test_matchterm(self):
     """
     Variables should match whole terms.
     """
     brain = Brain()
     rules = [
         '(X, good) if (john, said, X)',
         '(john, said, hello)',
         '(john, said, (cats, eat, food))',
     ]
     map(brain.add, rules)
     results = list(brain.query('(Z, good)'))
     assertObjectSubsetIn(self, results, {
         'Z': 'hello',
     })
     assertObjectSubsetIn(self, results, {
         'Z': ('cats', 'eat', 'food'),
     })
     self.assertEqual(len(results), 2)
コード例 #8
0
 def setUp(self):
     Var.count = 0
     self.brain = Brain()
     rules = [
         # rules
         '(parent, P, C) if (mother, P, C)',
         '(parent, P, C) if (father, P, C)',
         '(daughter, P, C) if (parent, P, C) and (female, C)',
         '(son, P, C) if (parent, P, C) and (male, C)',
         '(grandparent, G, C) if (parent, G, P) and (parent, P, C)',
         '(sibling, X, Y) if (parent, P, X) and (parent, P, Y)',
         # data
         '(female, alicia)',
         '(mother, mary, alicia)',
         '(father, joseph, alicia)',
         '(mother, mary, mike)',
         '(father, joseph, mike)',
         '(mother, rita, joseph)',
     ]
     for r in rules:
         self.brain.add(r)