def setUp(self): self.owlGraph = QueryCountingGraph().parse(StringIO(EX_ONT), format='n3') rule_store, rule_graph, self.network = SetupRuleStore(makeNetwork=True) self.program = self.network.setupDescriptionLogicProgramming( self.owlGraph, addPDSemantics=False, constructNetwork=False) self.program.update(AdditionalRules(self.owlGraph))
def MagicOWLProof(self, goals, rules, factGraph, conclusionFile): progLen = len(rules) magicRuleNo = 0 dPreds = [] for rule in AdditionalRules(factGraph): rules.append(rule) if not GROUND_QUERY: goalDict = dict([((Variable('SUBJECT'), goalP, goalO), goalS) for goalS, goalP, goalO in goals]) goals = goalDict.keys() assert goals topDownStore = TopDownSPARQLEntailingStore( factGraph.store, factGraph, idb=rules, DEBUG=DEBUG, identifyHybridPredicates=True, nsBindings=nsMap) targetGraph = Graph(topDownStore) for pref, nsUri in nsMap.items(): targetGraph.bind(pref, nsUri) start = time.time() for goal in goals: queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)], factGraph, None if GROUND_QUERY else [goal[0]]) query = queryLiteral.asSPARQL() print "Goal to solve ", query rt = targetGraph.query(query, initNs=nsMap) if GROUND_QUERY: self.failUnless(rt.askAnswer[0], "Failed top-down problem") else: if (goalDict[goal]) not in rt or DEBUG: for network, _goal in topDownStore.queryNetworks: print network, _goal network.reportConflictSet(True) for query in topDownStore.edbQueries: print query.asSPARQL() print "Missing", goalDict[goal] self.failUnless((goalDict[goal]) in rt, "Failed top-down problem") sTime = time.time() - start if sTime > 1: sTimeStr = "%s seconds" % sTime else: sTime = sTime * 1000 sTimeStr = "%s milli seconds" % sTime return sTimeStr
def setupDescriptionLogicProgramming(self, owlN3Graph, expanded=[], addPDSemantics=True, classifyTBox=False, constructNetwork=True, derivedPreds=[], ignoreNegativeStratus=False, safety=DATALOG_SAFETY_NONE): rt = [ rule for rule in MapDLPtoNetwork( self, owlN3Graph, complementExpansions=expanded, constructNetwork=constructNetwork, derivedPreds=derivedPreds, ignoreNegativeStratus=ignoreNegativeStratus, safety=safety) ] if ignoreNegativeStratus: rules, negRules = rt rules = set(rules) self.negRules = set(negRules) else: rules = set(rt) if constructNetwork: self.rules.update(rules) additionalRules = set(AdditionalRules(owlN3Graph)) if addPDSemantics: from FuXi.Horn.HornRules import HornFromN3 additionalRules.update(HornFromN3(StringIO(non_DHL_OWL_Semantics))) if constructNetwork: for rule in additionalRules: self.buildNetwork(iter(rule.formula.body), iter(rule.formula.head), rule) self.rules.add(rule) else: rules.update(additionalRules) if constructNetwork: rules = self.rules # noRules = len(rules) if classifyTBox: self.feedFactsToAdd(generateTokenSet(owlN3Graph)) # print("##### DLP rules fired against OWL/RDF TBOX", self) return rules
def MagicSetTransformation(factGraph, rules, GOALS, derivedPreds=None, strictCheck=DDL_STRICTNESS_FALLBACK_DERIVED, noMagic=None, defaultPredicates=None): """ Takes a goal and a ruleset and returns an iterator over the rulest that corresponds to the magic set transformation: """ noMagic = noMagic and noMagic or [] magicPredicates = set() replacement = {} adornedProgram = SetupDDLAndAdornProgram( factGraph, rules, GOALS, derivedPreds=derivedPreds, strictCheck=strictCheck, defaultPredicates=defaultPredicates) newRules = [] for rule in adornedProgram: if rule.isSecondOrder(): import warnings warnings.warn("Second order rule no supported by GMS: %s" % rule, RuntimeWarning) magicPositions = {} #Generate magic rules for idx, pred in enumerate(iterCondition(rule.formula.body)): magicBody = [] if isinstance(pred, AdornedUniTerm): # and pred not in magicPredicates: # For each rule r in Pad, and for each occurrence of an adorned # predicate p a in its body, we generate a magic rule defining magic_p a prevPreds = [ item for _idx, item in enumerate(rule.formula.body) if _idx < idx ] if 'b' not in pred.adornment: import warnings warnings.warn( "adorned predicate w/out any bound arguments(%s in %s) !" % (pred, rule.formula), RuntimeWarning) if GetOp(pred) not in noMagic: magicPred = pred.makeMagicPred() magicPositions[idx] = (magicPred, pred) inArcs = [(N, x) for ( N, x) in IncomingSIPArcs(rule.sip, getOccurrenceId(pred)) if not set(x).difference(GetArgs(pred))] if len(inArcs) > 1: # If there are several arcs entering qi, we define the magic rule defining magic_qi # in two steps. First, for each arc Nj --> qi with label cj , we define a rule with head label_qi_j(cj ). The body # of the rule is the same as the body of the magic rule in the case where there is a single arc # entering qi (described above). Then the magic rule is defined as follows. The head is # magic_q(0). The body contains label_qi_j(cj) for all j (that is, for all arcs entering qi ). #We combine all incoming arcs into a single list of (body) conditions for the magic set PrettyPrintRule(rule) SIPRepresentation(rule.sip) print pred, magicPred _body = [] additionalRules = [] for idxSip, (N, x) in enumerate(inArcs): newPred = pred.clone() SetOp(newPred, URIRef('%s_label_%s' % (newPred.op, idxSip))) ruleBody = And( buildMagicBody(N, prevPreds, rule.formula.head, derivedPreds)) additionalRules.append( Rule(Clause(ruleBody, newPred))) _body.extend(newPred) # _body.extend(ruleBody) additionalRules.append( Rule(Clause(And(_body), magicPred))) newRules.extend(additionalRules) for i in additionalRules: print i raise NotImplementedError() else: for idxSip, (N, x) in enumerate(inArcs): ruleBody = And( buildMagicBody(N, prevPreds, rule.formula.head, derivedPreds, noMagic)) newRule = Rule(Clause(ruleBody, magicPred)) newRules.append(newRule) magicPredicates.add(magicPred) #Modify rules #we modify the original rule by inserting #occurrences of the magic predicates corresponding #to the derived predicates of the body and to the head #If there are no bound arguments in the head, we don't modify the rule idxIncrement = 0 newRule = copy.deepcopy(rule) for idx, (magicPred, origPred) in magicPositions.items(): newRule.formula.body.formulae.insert(idx + idxIncrement, magicPred) idxIncrement += 1 if 'b' in rule.formula.head.adornment and GetOp( rule.formula.head) not in noMagic: headMagicPred = rule.formula.head.makeMagicPred() if isinstance(newRule.formula.body, Uniterm): newRule.formula.body = And( [headMagicPred, newRule.formula.body]) else: newRule.formula.body.formulae.insert(0, headMagicPred) newRules.append(newRule) if not newRules: newRules.extend(AdditionalRules(factGraph)) for rule in newRules: if rule.formula.body: yield rule
def MagicOWLProof(self, goals, rules, factGraph, conclusionFile): progLen = len(rules) magicRuleNo = 0 dPreds = [] for rule in AdditionalRules(factGraph): rules.append(rule) if not GROUND_QUERY and REASONING_STRATEGY != 'gms': goalDict = dict([((Variable('SUBJECT'), goalP, goalO), goalS) for goalS, goalP, goalO in goals]) goals = goalDict.keys() assert goals if REASONING_STRATEGY == 'gms': for rule in MagicSetTransformation(factGraph, rules, goals, dPreds): magicRuleNo += 1 self.network.buildNetworkFromClause(rule) self.network.rules.add(rule) if DEBUG: log.debug("\t", rule) log.debug("rate of reduction in the size of the program: ", (100 - (float(magicRuleNo) / float(progLen)) * 100)) if REASONING_STRATEGY in ['bfp', 'sld']: # and not GROUND_QUERY: reasoningAlg = TOP_DOWN_METHOD if REASONING_STRATEGY == 'sld' \ else BFP_METHOD topDownStore = TopDownSPARQLEntailingStore( factGraph.store, factGraph, idb=rules, DEBUG=DEBUG, nsBindings=nsMap, decisionProcedure=reasoningAlg, identifyHybridPredicates=REASONING_STRATEGY == 'bfp') targetGraph = Graph(topDownStore) for pref, nsUri in nsMap.items(): targetGraph.bind(pref, nsUri) start = time.time() for goal in goals: queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)], factGraph, None if GROUND_QUERY else [goal[0]]) query = queryLiteral.asSPARQL() log.debug("Goal to solve ", query) rt = targetGraph.query(query, initNs=nsMap) if GROUND_QUERY: self.failUnless(rt.askAnswer[0], "Failed top-down problem") else: if (goalDict[goal]) not in rt or DEBUG: for network, _goal in topDownStore.queryNetworks: log.debug(network, _goal) network.reportConflictSet(True) for query in topDownStore.edbQueries: log.debug(query.asSPARQL()) self.failUnless((goalDict[goal]) in rt, "Failed top-down problem") sTime = time.time() - start if sTime > 1: sTimeStr = "%s seconds" % sTime else: sTime = sTime * 1000 sTimeStr = "%s ms" % sTime return sTimeStr elif REASONING_STRATEGY == 'gms': for goal in goals: adornedGoalSeed = AdornLiteral(goal).makeMagicPred() goal = adornedGoalSeed.toRDFTuple() if DEBUG: log.debug("Magic seed fact ", adornedGoalSeed) factGraph.add(goal) timing = self.calculateEntailments(factGraph) for goal in goals: # self.failUnless(goal in self.network.inferredFacts or goal in factGraph, # "Failed GMS query") if goal not in self.network.inferredFacts and goal not in factGraph: log.debug("missing triple %s" % (pformat(goal))) # print(list(factGraph.adornedProgram)) # from FuXi.Rete.Util import renderNetwork # dot = renderNetwork( # self.network,self.network.nsMap).write_jpeg('test-fail.jpeg') self.network.reportConflictSet(True) log.debug("=== Failed: %s ====" % pformat(goal)) else: log.debug("=== Passed! ===") return timing