예제 #1
0
    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
예제 #2
0
 def clear_table(self, table):
     self.rules[table] = utility.OrderedSet()
     self.facts[table] = FactSet()
예제 #3
0
 def setUp(self):
     super(TestFactSet, self).setUp()
     self.factset = FactSet()