from terms import URI, Variable, Fact from interpreter import Interpreter from sets import Set import sys sys.path += ['/home/katz/semweb/cwm/2000/10/'] #TEMPORARY from swap.formula import Formula class CWMFormula(Formula): """Our implementation of CWM's Formula object.""" def __init__(self): # We're reusing the interpreter's representation of facts rather than # recreating an indexed store here. self.interp = Interpreter([]) #empty interpreter (no rules) def getStatements(self): """Retrieve the statements from the interpreter""" return self.interp.totalFacts() def setStatements(self, facts): """Reset the knowledge base to whatever facts (Set) are given to us :: Since we have no actual truth maintenance, this operation is dorky; there could be a case where a fact X is inferred (and is in the set 'inferredFacts', waiting to be added) but is asked to be removed by 'removeStatement'. If we remove X from our knowledge base (i.e. 'initialFacts') it will simply be added in the next round again since it is a member of 'inferredFacts'. We just remove it from the knowledge base carelessly. Horrible.""" # check inferredFacts, set appropriately self.interp.initialFacts = self.interp.inferredFacts.difference(facts) # same for totalFacts self.interp.totalFacts = self.interp.totalFacts.difference(facts) statements = property(getStatements, setStatements, None, "Statements in formula") def add(self, subj, pred, obj, why=None): """Add a triple to the formula :: We ignore 'why' always""" self.interp.addFacts(Set([Fact(subj, pred, obj)])) return 1 # signal success -- although it seems like CWM ignores this return value def removeStatement(self, stmt): """Removes a statement :: This makes no (semantic) sense, we implement it regardless. `stmt' is of CWM type StoredStatement.""" fact = Fact(stmt.subject(), stmt.predicate(), stmt.objecT()) self.statements = Set([fact]) def infer(self): """Put the current facts through the Rete""" self.interp.run()