def buildFacts(self, indexedFormula): facts = [] for f in self.dumpLists(indexedFormula): facts.append( terms.Fact(convertBNodeToFact(f.s), f.p, convertBNodeToFact(f.o))) # for alphaNode in self.interp.rete.alphaNodeStore: # print alphaNode # i = alphaNode.pattern.noneBasedPattern() # pattern = self.convFromRete(i[0]), self.convFromRete(i[1]), self.convFromRete(i[2]) # print "pattern:", pattern # for quad in indexedFormula.statementsMatching( # subj=pattern[0], # pred=pattern[1], # obj =pattern[2]): ## print "quad:", quad # if isinstance(subj, formula.Formula) or isinstance(obj, formula.Formula): # print "The RETE engine cannot process nested formulas currently" # continue # # s, p, o = [self.convType(x, indexedFormula, None) for x in quad.spo()] # # alphaNode.add(terms.Fact(s,p,o)) for fact in indexedFormula.statements: subj, predi, obj = fact.spo() # ignore formulas for now if isinstance(subj, formula.Formula) or \ isinstance(obj, formula.Formula): print "The RETE cannot process nested formulas at the time - use it for ntriples only" # raise NotImplementedError continue # only get top level facts head = [] tail = [] s, p, o = [ self.convType(x, indexedFormula, None) for x in fact.spo() ] #to get variables. #Not good enough for Lists, but they're taken care of earlier facts.append(terms.Fact(s, p, o)) self.interp.addFacts(Set(facts), initialSet=True) return facts
from set_importer import Set #diag.chatty_flag=0 # Standard python import sys, getopt from sys import exit, stderr from time import clock import re from codecs import utf_8_encode BNF = Namespace("http://www.w3.org/2000/10/swap/grammar/bnf#") RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") branchTable = {} tokenSet = Set() class tokenHolder(object): def __init__(self): self.tok = None def __call__(self, val=123): if val != 123: self.tok = val return self return self.tok def __repr__(self): return repr(self.tok)
def checkGMP(r, f, checker, policy, level=0): r"""check a generalized modus ponens step. >>> soc = Namespace("http://example/socrates#") >>> pf = _s2f(_TestGMPStep, "http://example/socrates") >>> f = checkGMP(soc.step1, None, Checker(pf), AllPremises()) >>> f.n3String().strip() u'@prefix : <http://example/socrates#> .\n \n :socrates a :Mortal .' """ # """ emacs python mode needs help proof = checker._pf evidence = proof.the(subj=r, pred=reason.evidence) existentials = Set() bindings = {} for b in proof.each(subj=r, pred=reason.binding): var_rei = proof.the(subj=b, pred=reason.variable) var = getSymbol(proof, var_rei) val_rei = proof.the(subj=b, pred=reason.boundTo) # @@@ Check that they really are variables in the rule! val = getTerm(proof, val_rei) bindings[var] = val if proof.contains(subj=val_rei, pred=proof.store.type, obj=reason.Existential): ## if val_rei in proof.existentials(): existentials.add(val) rule = proof.the(subj=r, pred=reason.rule) proofFormula = checker.result(rule, policy, level) for s in proofFormula.statements: #@@@@@@ why look here? if s[PRED] is log.implies: ruleStatement = s break else: raise InvalidProof("Rule has %s instead of log:implies as predicate.", level) for v in proofFormula.variables(): var = proof.newSymbol(v.uriref()) if var in bindings: val = bindings[var] del bindings[var] bindings[v] = val # Check the evidence is itself proved evidenceFormula = proof.newFormula() for e in evidence: f2 = checker.result(e, policy, level) f2.store.copyFormula(f2, evidenceFormula) evidenceFormula = evidenceFormula.close() # Check: Every antecedent statement must be included as evidence antecedent = proof.newFormula() for k in bindings.values(): if k in existentials: #k in evidenceFormula.existentials() or antecedent.declareExistential(k) antecedent.loadFormulaWithSubstitution(ruleStatement[SUBJ], bindings) antecedent = antecedent.close() #antecedent = ruleStatement[SUBJ].substitution(bindings) fyi( lambda: "Bindings: %s\nAntecedent after subst: %s" % (bindings, antecedent.debugString()), level, 195) fyi("about to test if n3Entails(%s, %s)" % (evidenceFormula, antecedent), level, 1) fyi( lambda: "about to test if n3Entails(%s, %s)" % (evidenceFormula.n3String(), antecedent.n3String()), level, 80) if not n3Entails( evidenceFormula, antecedent, skipIncludes=1, level=level + 1): raise LogicalFallacy( "Can't find %s in evidence for\n" "Antecedent of rule: %s\n" "Evidence:%s\n" "Bindings:%s" % ((s[SUBJ], s[PRED], s[OBJ]), antecedent.n3String(), evidenceFormula.n3String(), bindings), level=level) fyi("Rule %s conditions met" % ruleStatement, level=level) retVal = proof.newFormula() for k in ruleStatement[OBJ].occurringIn(Set(bindings)): v = bindings[k] if v in existentials: #k in evidenceFormula.existentials() or retVal.declareExistential(v) retVal.loadFormulaWithSubstitution(ruleStatement[OBJ], bindings) retVal = retVal.close() return retVal
import swap.llyn # Chosen engine registers itself import sys # to get exception info for diagnostics reason = Namespace("http://www.w3.org/2000/10/swap/reason#") log = Namespace("http://www.w3.org/2000/10/swap/log#") rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") rei = Namespace("http://www.w3.org/2004/06/rei#") chatty = 0 debugLevelForInference = 000 debugLevelForParsing = 0 nameBlankNodes = 0 proofSteps = 0 knownReasons = Set([ reason.Premise, reason.Parsing, reason.Inference, reason.Conjunction, reason.Fact, reason.Extraction, reason.CommandLine, reason.Conclusion ]) class Policy(object): """a proof-checking policy""" def documentOK(self, u): raise RuntimeError("subclass must implement") def assumes(self, f): """Check whether a formula is an axiom. Hmm... move checkBuiltin here?""" raise RuntimeError("subclass must implement")