def testQueryMemoization(self): raise SkipTest( "SKIPFAIL testQueryMemoization, see test/testBFPQueryMemoization.py") topDownStore = TopDownSPARQLEntailingStore( self.owlGraph.store, self.owlGraph, idb=self.program, DEBUG=False, nsBindings=nsMap, decisionProcedure=BFP_METHOD, identifyHybridPredicates=True) targetGraph = Graph(topDownStore) for pref, nsUri in nsMap.items(): targetGraph.bind(pref, nsUri) goal = (Variable('SUBJECT'), RDF.type, EX.C) queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)], self.owlGraph, [Variable('SUBJECT')]) query = queryLiteral.asSPARQL() # rt=targetGraph.query(query,initNs=nsMap) # if len(topDownStore.edbQueries) == len(set(topDownStore.edbQueries)): # pprint(topDownStore.edbQueries) print("Queries dispatched against EDB") for query in self.owlGraph.queriesDispatched: print(query) self.failUnlessEqual( len(self.owlGraph.queriesDispatched), 4, "Duplicate query")
def testQueryMemoization(self): raise SkipTest( "SKIPFAIL testQueryMemoization, see test/testBFPQueryMemoization.py" ) topDownStore = TopDownSPARQLEntailingStore( self.owlGraph.store, self.owlGraph, idb=self.program, DEBUG=False, nsBindings=nsMap, decisionProcedure=BFP_METHOD, identifyHybridPredicates=True) targetGraph = Graph(topDownStore) for pref, nsUri in nsMap.items(): targetGraph.bind(pref, nsUri) goal = (Variable('SUBJECT'), RDF.type, EX.C) queryLiteral = EDBQuery([BuildUnitermFromTuple(goal)], self.owlGraph, [Variable('SUBJECT')]) query = queryLiteral.asSPARQL() # rt=targetGraph.query(query,initNs=nsMap) # if len(topDownStore.edbQueries) == len(set(topDownStore.edbQueries)): # pprint(topDownStore.edbQueries) print("Queries dispatched against EDB") for query in self.owlGraph.queriesDispatched: print(query) self.failUnlessEqual(len(self.owlGraph.queriesDispatched), 4, "Duplicate query")
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 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 conjunctiveSipStrategy(self, goalsRemaining, factGraph, bindings=None): """ Given a conjunctive set of triples, invoke sip-strategy passing on intermediate solutions to facilitate 'join' behavior """ bindings = bindings if bindings else {} try: tp = goalsRemaining.next() assert isinstance(bindings, dict) dPred = self.derivedPredicateFromTriple(tp) if dPred is None: baseEDBQuery = EDBQuery([BuildUnitermFromTuple(tp)], self.edb, bindings=bindings) if self.DEBUG: print >>sys.stderr,"Evaluating TP against EDB: ",\ baseEDBQuery.asSPARQL() query, rt = baseEDBQuery.evaluate() if isinstance(rt, bool) and rt: yield bindings elif not isinstance(rt, bool): rt = list(rt) remaining_goals = itertools.tee(goalsRemaining, len(rt)) for idx in range(len(rt)): item = {} item.update(rt[idx]) item.update(bindings) if self.DEBUG: print >> sys.stderr, "Solution from EDB query: ", item for ansDict in self.conjunctiveSipStrategy( remaining_goals[idx], factGraph, item): yield ansDict else: queryLit = BuildUnitermFromTuple(tp) currentOp = GetOp(queryLit) queryLit.setOperator(currentOp) query = EDBQuery([queryLit], self.edb, bindings=bindings) if bindings: tp = first(query.formulae).toRDFTuple() if self.DEBUG: print >> sys.stderr, "Goal/Query: ", query.asSPARQL() tp = self.hybridPredQueryPreparation(tp) SetupDDLAndAdornProgram(self.edb, self.idb, [tp], derivedPreds=self.derivedPredicates, ignoreUnboundDPreds=True, nsBindings=self.nsBindings) sipCollection = PrepareSipCollection(self.edb.adornedProgram) if self.DEBUG and sipCollection: for sip in SIPRepresentation(sipCollection): print >> sys.stderr, sip pprint(list(self.edb.adornedProgram), sys.stderr) elif self.DEBUG: print >> sys.stderr, "No SIP graph!" for nextAnswer, ns in self.invokeDecisionProcedure( tp, factGraph, bindings, self.DEBUG, sipCollection): if isinstance(nextAnswer, dict): #Recieved solutions to 'open' query, merge with given bindings #and continue for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, mergeMappings1To2(bindings, nextAnswer)): yield ansDict elif nextAnswer: #we (successfully) proved a ground query, pass on bindings assert isinstance(nextAnswer, bool) for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, bindings): yield ansDict except StopIteration: yield bindings
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
def conjunctiveSipStrategy(self,goalsRemaining,factGraph,bindings=None): """ Given a conjunctive set of triples, invoke sip-strategy passing on intermediate solutions to facilitate 'join' behavior """ bindings = bindings if bindings else {} try: tp = goalsRemaining.next() assert isinstance(bindings,dict) dPred = self.derivedPredicateFromTriple(tp) if dPred is None: baseEDBQuery = EDBQuery([BuildUnitermFromTuple(tp)], self.edb, bindings=bindings) if self.DEBUG: print >>sys.stderr,"Evaluating TP against EDB: ",\ baseEDBQuery.asSPARQL() query,rt = baseEDBQuery.evaluate() # _vars = baseEDBQuery.returnVars for item in rt: bindings.update(item) for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, bindings): yield ansDict else: queryLit = BuildUnitermFromTuple(tp) currentOp = GetOp(queryLit) queryLit.setOperator(currentOp) query=EDBQuery([queryLit], self.edb, bindings=bindings) if bindings: tp=first(query.formulae).toRDFTuple() if self.DEBUG: print >>sys.stderr,"Goal/Query: ", query.asSPARQL() SetupDDLAndAdornProgram( self.edb, self.idb, [tp], derivedPreds=self.derivedPredicates, ignoreUnboundDPreds = True, hybridPreds2Replace=self.hybridPredicates) if self.hybridPredicates: lit = BuildUnitermFromTuple(tp) op = GetOp(lit) if op in self.hybridPredicates: lit.setOperator(URIRef(op+u'_derived')) tp = lit.toRDFTuple() sipCollection=PrepareSipCollection(self.edb.adornedProgram) if self.DEBUG and sipCollection: for sip in SIPRepresentation(sipCollection): print >>sys.stderr,sip pprint(list(self.edb.adornedProgram),sys.stderr) elif self.DEBUG: print >> sys.stderr, "No SIP graph!" for nextAnswer,ns in self.invokeDecisionProcedure( tp, factGraph, bindings, self.DEBUG, sipCollection): nonGroundGoal = isinstance(nextAnswer,dict) if nonGroundGoal or nextAnswer: #Either we recieved bindings from top-down evaluation #or we (successfully) proved a ground query if not nonGroundGoal: #Attempt to prove a ground query, return the response rt = nextAnswer else: #Recieved solutions to 'open' query, merge with given bindings #and continue rt = mergeMappings1To2(bindings,nextAnswer) #either answers were provided (the goal wasn't grounded) or #the goal was ground and successfully proved for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, rt): yield ansDict except StopIteration: yield bindings
def conjunctiveSipStrategy(self, goalsRemaining, factGraph, bindings=None): """ Given a conjunctive set of triples, invoke sip-strategy passing on intermediate solutions to facilitate 'join' behavior """ bindings = bindings if bindings else {} try: tp = next(goalsRemaining) assert isinstance(bindings, dict) dPred = self.derivedPredicateFromTriple(tp) if dPred is None: baseEDBQuery = EDBQuery([BuildUnitermFromTuple(tp)], self.edb, bindings=bindings) if self.DEBUG: _debug( "Evaluating TP against EDB: %s" % baseEDBQuery.asSPARQL()) query, rt = baseEDBQuery.evaluate() if isinstance(rt, bool) and rt: yield bindings elif not isinstance(rt, bool): rt = list(rt) remaining_goals = itertools.tee(goalsRemaining, len(rt)) for idx in range(len(rt)): item = {} item.update(rt[idx]) item.update(bindings) if self.DEBUG: _debug("Solution from EDB query: %s" % item) for ansDict in self.conjunctiveSipStrategy( remaining_goals[idx], factGraph, item): yield ansDict else: queryLit = BuildUnitermFromTuple(tp) currentOp = GetOp(queryLit) queryLit.setOperator(currentOp) query = EDBQuery([queryLit], self.edb, bindings=bindings) if bindings: tp = first(query.formulae).toRDFTuple() if self.DEBUG: _debug("Goal/Query: %s" % query.asSPARQL()) tp = self.hybridPredQueryPreparation(tp) SetupDDLAndAdornProgram( self.edb, self.idb, [tp], derivedPreds=self.derivedPredicates, ignoreUnboundDPreds=True) sipCollection = PrepareSipCollection(self.edb.adornedProgram) if self.DEBUG and sipCollection: for sip in SIPRepresentation(sipCollection): _debug(sip) _debug(pformat(list(self.edb.adornedProgram))) elif self.DEBUG: _debug("No SIP graph.") for nextAnswer, ns in self.invokeDecisionProcedure( tp, factGraph, bindings, self.DEBUG, sipCollection): if isinstance(nextAnswer, dict): # Received solutions to 'open' query, merge with given # bindings and continue for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, mergeMappings1To2(bindings, nextAnswer)): yield ansDict elif nextAnswer: # we (successfully) proved a ground query, pass on # bindings assert isinstance(nextAnswer, bool) for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, bindings): yield ansDict except StopIteration: yield bindings
def conjunctiveSipStrategy(self, goalsRemaining, factGraph, bindings=None): """ Given a conjunctive set of triples, invoke sip-strategy passing on intermediate solutions to facilitate 'join' behavior """ bindings = bindings if bindings else {} try: tp = next(goalsRemaining) assert isinstance(bindings, dict) dPred = self.derivedPredicateFromTriple(tp) if dPred is None: baseEDBQuery = EDBQuery([BuildUnitermFromTuple(tp)], self.edb, bindings=bindings) if self.DEBUG: print("Evaluating TP against EDB:%s" % baseEDBQuery.asSPARQL()) query, rt = baseEDBQuery.evaluate() # _vars = baseEDBQuery.returnVars for item in rt: bindings.update(item) for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, bindings): yield ansDict else: queryLit = BuildUnitermFromTuple(tp) currentOp = GetOp(queryLit) queryLit.setOperator(currentOp) query = EDBQuery([queryLit], self.edb, bindings=bindings) if bindings: tp = first(query.formulae).toRDFTuple() if self.DEBUG: print("Goal/Query: ", query.asSPARQL()) SetupDDLAndAdornProgram( self.edb, self.idb, [tp], derivedPreds=self.derivedPredicates, ignoreUnboundDPreds=True, hybridPreds2Replace=self.hybridPredicates) if self.hybridPredicates: lit = BuildUnitermFromTuple(tp) op = GetOp(lit) if op in self.hybridPredicates: lit.setOperator(URIRef(op + u'_derived')) tp = lit.toRDFTuple() sipCollection = PrepareSipCollection(self.edb.adornedProgram) if self.DEBUG and sipCollection: for sip in SIPRepresentation(sipCollection): print(sip) pprint(list(self.edb.adornedProgram), sys.stderr) elif self.DEBUG: print("No SIP graph.") for nextAnswer, ns in self.invokeDecisionProcedure( tp, factGraph, bindings, self.DEBUG, sipCollection): nonGroundGoal = isinstance(nextAnswer, dict) if nonGroundGoal or nextAnswer: #Either we recieved bindings from top-down evaluation #or we (successfully) proved a ground query if not nonGroundGoal: #Attempt to prove a ground query, return the response rt = nextAnswer else: #Recieved solutions to 'open' query, merge with given bindings #and continue rt = mergeMappings1To2(bindings, nextAnswer) #either answers were provided (the goal wasn't grounded) or #the goal was ground and successfully proved for ansDict in self.conjunctiveSipStrategy( goalsRemaining, factGraph, rt): yield ansDict except StopIteration: yield bindings