def add_rule(self, key, rule): """Add a rule to the Ruleset @rule can be a Rule or a Fact. Returns True if add_rule() changes the RuleSet. """ if isinstance(rule, Fact): # If the rule is a Fact, then add it to self.facts. if key not in self.facts: self.facts[key] = FactSet() return self.facts[key].add(rule) elif len(rule.body) == 0 and not rule.head.is_negated(): # If the rule is a Rule, with no body, then it's a Fact, so # convert the Rule to a Fact to a Fact and add to self.facts. f = Fact(key, (a.name for a in rule.head.arguments)) if key not in self.facts: self.facts[key] = FactSet() return self.facts[key].add(f) else: # else the rule is a regular rule, so add it to self.rules. if key in self.rules: return self.rules[key].add(rule) else: self.rules[key] = utility.OrderedSet([rule]) return True
def setUp(self): super(TestFactSet, self).setUp() self.factset = FactSet()
class TestFactSet(base.TestCase): def setUp(self): super(TestFactSet, self).setUp() self.factset = FactSet() def test_empty(self): self.assertFalse((1, 2, 3) in self.factset) self.assertEqual(0, len(self.factset)) def test_add_one(self): f = (1, 2, 'a') self.factset.add(f) self.assertEqual(1, len(self.factset)) self.assertEqual(set([f]), self.factset.find(((0, 1), (1, 2), (2, 'a')))) def test_add_few(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.assertEqual(3, len(self.factset)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200),))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'),))) self.assertEqual(set([f1]), self.factset.find(((0, 1), (1, 200), (2, 'a'),))) self.assertEqual(set(), self.factset.find(((0, 8),))) def test_remove(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.assertEqual(3, len(self.factset)) self.assertTrue(self.factset.remove(f1)) self.assertEqual(2, len(self.factset)) self.assertEqual(set([f2, f3]), self.factset.find(((1, 200),))) self.assertTrue(self.factset.remove(f3)) self.assertEqual(1, len(self.factset)) self.assertEqual(set([f2]), self.factset.find(((1, 200),))) self.assertFalse(self.factset.remove(f3)) self.assertTrue(self.factset.remove(f2)) self.assertEqual(0, len(self.factset)) self.assertEqual(set(), self.factset.find(((1, 200),))) def test_create_index(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.factset.create_index((1,)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200),))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'),))) self.assertEqual(set([f1, f2]), self.factset.find(((1, 200), (2, 'a')))) self.assertEqual(set([f1]), self.factset.find(((0, 1), (1, 200), (2, 'a'),))) self.assertEqual(set(), self.factset.find(((0, 8),))) self.factset.create_index((1, 2)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200),))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'),))) self.assertEqual(set([f1, f2]), self.factset.find(((1, 200), (2, 'a')))) self.assertEqual(set([f1]), self.factset.find(((0, 1), (1, 200), (2, 'a'),))) self.assertEqual(set(), self.factset.find(((0, 8),))) def test_remove_index(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.factset.create_index((1,)) self.factset.create_index((1, 2)) self.factset.remove_index((1,)) self.factset.remove_index((1, 2)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200),))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'),))) self.assertEqual(set([f1, f2]), self.factset.find(((1, 200), (2, 'a')))) self.assertEqual(set([f1]), self.factset.find(((0, 1), (1, 200), (2, 'a'),))) self.assertEqual(set(), self.factset.find(((0, 8),))) def test_indexed_find(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) # Count iterations without index. iterations = [] # measure how many iterations find() uses. self.assertEqual(set([f1]), self.factset.find(((0, 1),), iterations)) self.assertEqual(3, iterations[0]) # Count iterations with index match. self.factset.create_index((0,)) iterations = [] self.assertEqual(set([f1]), self.factset.find(((0, 1),), iterations)) self.assertEqual(1, iterations[0]) # Count iterations when there is a matching index, but not match for # this particular key. iterations = [] self.assertEqual(set(), self.factset.find(((0, 100),), iterations)) self.assertEqual(1, iterations[0]) # Count iterations after deleting index. self.factset.remove_index((0,)) iterations = [] self.assertEqual(set([f1]), self.factset.find(((0, 1),), iterations)) self.assertEqual(3, iterations[0])
def clear_table(self, table): self.rules[table] = utility.OrderedSet() self.facts[table] = FactSet()
class TestFactSet(base.TestCase): def setUp(self): super(TestFactSet, self).setUp() self.factset = FactSet() def test_empty(self): self.assertFalse((1, 2, 3) in self.factset) self.assertEqual(0, len(self.factset)) def test_add_one(self): f = (1, 2, 'a') self.factset.add(f) self.assertEqual(1, len(self.factset)) self.assertEqual(set([f]), self.factset.find( ((0, 1), (1, 2), (2, 'a')))) def test_add_few(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.assertEqual(3, len(self.factset)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200), ))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'), ))) self.assertEqual(set([f1]), self.factset.find(( (0, 1), (1, 200), (2, 'a'), ))) self.assertEqual(set(), self.factset.find(((0, 8), ))) def test_remove(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.assertEqual(3, len(self.factset)) self.assertTrue(self.factset.remove(f1)) self.assertEqual(2, len(self.factset)) self.assertEqual(set([f2, f3]), self.factset.find(((1, 200), ))) self.assertTrue(self.factset.remove(f3)) self.assertEqual(1, len(self.factset)) self.assertEqual(set([f2]), self.factset.find(((1, 200), ))) self.assertFalse(self.factset.remove(f3)) self.assertTrue(self.factset.remove(f2)) self.assertEqual(0, len(self.factset)) self.assertEqual(set(), self.factset.find(((1, 200), ))) def test_create_index(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.factset.create_index((1, )) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200), ))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'), ))) self.assertEqual(set([f1, f2]), self.factset.find( ((1, 200), (2, 'a')))) self.assertEqual(set([f1]), self.factset.find(( (0, 1), (1, 200), (2, 'a'), ))) self.assertEqual(set(), self.factset.find(((0, 8), ))) self.factset.create_index((1, 2)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200), ))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'), ))) self.assertEqual(set([f1, f2]), self.factset.find( ((1, 200), (2, 'a')))) self.assertEqual(set([f1]), self.factset.find(( (0, 1), (1, 200), (2, 'a'), ))) self.assertEqual(set(), self.factset.find(((0, 8), ))) def test_remove_index(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) self.factset.create_index((1, )) self.factset.create_index((1, 2)) self.factset.remove_index((1, )) self.factset.remove_index((1, 2)) self.assertEqual(set([f1, f2, f3]), self.factset.find(((1, 200), ))) self.assertEqual(set([f1, f2]), self.factset.find(((2, 'a'), ))) self.assertEqual(set([f1, f2]), self.factset.find( ((1, 200), (2, 'a')))) self.assertEqual(set([f1]), self.factset.find(( (0, 1), (1, 200), (2, 'a'), ))) self.assertEqual(set(), self.factset.find(((0, 8), ))) def test_indexed_find(self): f1 = (1, 200, 'a') f2 = (2, 200, 'a') f3 = (3, 200, 'c') self.factset.add(f1) self.factset.add(f2) self.factset.add(f3) # Count iterations without index. iterations = [] # measure how many iterations find() uses. self.assertEqual(set([f1]), self.factset.find(((0, 1), ), iterations)) self.assertEqual(3, iterations[0]) # Count iterations with index match. self.factset.create_index((0, )) iterations = [] self.assertEqual(set([f1]), self.factset.find(((0, 1), ), iterations)) self.assertEqual(1, iterations[0]) # Count iterations when there is a matching index, but not match for # this particular key. iterations = [] self.assertEqual(set(), self.factset.find(((0, 100), ), iterations)) self.assertEqual(1, iterations[0]) # Count iterations after deleting index. self.factset.remove_index((0, )) iterations = [] self.assertEqual(set([f1]), self.factset.find(((0, 1), ), iterations)) self.assertEqual(3, iterations[0])