Esempio n. 1
0
def SetupMetaInterpreter(tBoxGraph, goal, useThingRule=True):
    from pprint import pprint
    from FuXi.LP.BackwardFixpointProcedure import BackwardFixpointProcedure
    from FuXi.Rete.Magic import SetupDDLAndAdornProgram  # , PrettyPrintRule
    from FuXi.Horn.PositiveConditions import BuildUnitermFromTuple  # , Exists
    from FuXi.Rete.TopDown import PrepareSipCollection
    from FuXi.DLP import LloydToporTransformation, makeRule
    from FuXi.Rete.SidewaysInformationPassing import GetOp

    owlThingAppears = False
    if useThingRule and OWL.Thing in tBoxGraph.all_nodes():
        owlThingAppears = True
    completionRules = HornFromN3(StringIO(RULES))
    if owlThingAppears:
        completionRules.formulae.extend(
            HornFromN3(StringIO(CONDITIONAL_THING_RULE)))
    reducedCompletionRules = set()
    for rule in completionRules:
        for clause in LloydToporTransformation(rule.formula):
            rule = makeRule(clause, {})
            # _debug(rule)
            # PrettyPrintRule(rule)
            reducedCompletionRules.add(rule)

    network = SetupRuleStore(makeNetwork=True)[-1]
    SetupDDLAndAdornProgram(
        tBoxGraph,
        reducedCompletionRules,
        [goal],
        derivedPreds=derivedPredicates,
        ignoreUnboundDPreds=True,
        hybridPreds2Replace=hybridPredicates)

    lit = BuildUnitermFromTuple(goal)
    op = GetOp(lit)
    lit.setOperator(URIRef(op + '_derived'))
    goal = lit.toRDFTuple()

    sipCollection = PrepareSipCollection(reducedCompletionRules)
    tBoxGraph.templateMap = {}
    bfp = BackwardFixpointProcedure(
        tBoxGraph,
        network,
        derivedPredicates,
        goal,
        sipCollection,
        hybridPredicates=hybridPredicates,
        debug=True)
    bfp.createTopDownReteNetwork(True)
    _debug(pformat(reducedCompletionRules))
    rt = bfp.answers(debug=True)
    _debug(pformat(rt))
    _debug(bfp.metaInterpNetwork)
    bfp.metaInterpNetwork.reportConflictSet(True, sys.stderr)
    for query in bfp.edbQueries:
        _debug("Dispatched query against dataset: %s" % query.asSPARQL())
    _debug(pformat(list(bfp.goalSolutions)))
Esempio n. 2
0
def SetupMetaInterpreter(tBoxGraph, goal, useThingRule=True):
    from FuXi.LP.BackwardFixpointProcedure import BackwardFixpointProcedure
    from FuXi.Rete.Magic import SetupDDLAndAdornProgram
    from FuXi.Horn.PositiveConditions import BuildUnitermFromTuple
    from FuXi.Rete.TopDown import PrepareSipCollection
    from FuXi.DLP import LloydToporTransformation, makeRule
    from FuXi.Rete.SidewaysInformationPassing import GetOp

    owlThingAppears = False
    if useThingRule and OWL.Thing in tBoxGraph.all_nodes():
        owlThingAppears = True
    completionRules = HornFromN3(StringIO(RULES))
    if owlThingAppears:
        completionRules.formulae.extend(
            HornFromN3(StringIO(CONDITIONAL_THING_RULE)))
    reducedCompletionRules = set()
    for rule in completionRules:
        for clause in LloydToporTransformation(rule.formula):
            rule = makeRule(clause, {})
            # log.debug(rule)
            # PrettyPrintRule(rule)
            reducedCompletionRules.add(rule)

    network = SetupRuleStore(makeNetwork=True)[-1]
    SetupDDLAndAdornProgram(
        tBoxGraph,
        reducedCompletionRules,
        [goal],
        derivedPreds=derivedPredicates,
        ignoreUnboundDPreds=True,
        hybridPreds2Replace=hybridPredicates)

    lit = BuildUnitermFromTuple(goal)
    op = GetOp(lit)
    lit.setOperator(URIRef(op + u'_derived'))
    goal = lit.toRDFTuple()

    sipCollection = PrepareSipCollection(reducedCompletionRules)
    tBoxGraph.templateMap = {}
    bfp = BackwardFixpointProcedure(
        tBoxGraph,
        network,
        derivedPredicates,
        goal,
        sipCollection,
        hybridPredicates=hybridPredicates,
        debug=True)
    bfp.createTopDownReteNetwork(True)
    log.debug(reducedCompletionRules)
    rt = bfp.answers(debug=True)
    log.debug(rt)
    log.debug(bfp.metaInterpNetwork)
    bfp.metaInterpNetwork.reportConflictSet(True, sys.stderr)
    for query in bfp.edbQueries:
        log.debug("Dispatched query against dataset: ", query.asSPARQL())
    log.debug(list(bfp.goalSolutions))
Esempio n. 3
0
 def invokeDecisionProcedure(self,
                             tp,
                             factGraph,
                             bindings,
                             debug,
                             sipCollection):
     from FuXi.Rete.RuleStore import SetupRuleStore
     isNotGround = first(
                     filter(
                         lambda i: isinstance(i, Variable), tp))
     rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
     bfp = BackwardFixpointProcedure(
         factGraph,
         network,
         self.derivedPredicates,
         tp,
         sipCollection,
         hybridPredicates=self.hybridPredicates,
         debug=self.DEBUG)
     bfp.createTopDownReteNetwork(self.DEBUG)
     if self.DEBUG:
         _debug("Goal/Query: %s " % tp)
         _debug("Query was not ground" \
                 if isNotGround is not None \
                 else "Query was ground")
     rt = bfp.answers(debug=self.DEBUG)
     self.queryNetworks.append((bfp.metaInterpNetwork, tp))
     self.edbQueries.update(bfp.edbQueries)
     if isNotGround is not None:
         for item in bfp.goalSolutions:
             yield item, None
     else:
         yield rt, None
     if debug:
         _debug(bfp.metaInterpNetwork)
         bfp.metaInterpNetwork.reportConflictSet(True, sys.stderr)
         for query in self.edbQueries:
             _debug(
                 "Dispatched query against dataset: %s" % query.asSPARQL())
Esempio n. 4
0
    def invokeDecisionProcedure(self, tp, factGraph, bindings, debug,
                                sipCollection):
        isNotGround = first(
            itertools.ifilter(lambda i: isinstance(i, Variable), tp))
        rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
        network.nsMap.update(self.nsBindings)
        bfp = BackwardFixpointProcedure(factGraph,
                                        network,
                                        self.derivedPredicates,
                                        tp,
                                        sipCollection,
                                        hybridPredicates=self.hybridPredicates,
                                        debug=self.DEBUG,
                                        nsBindings=self.nsBindings,
                                        toldBNode=self.toldBNode)
        bfp.createTopDownReteNetwork(self.DEBUG)
        if self.existentialInHead:
            BNodeSkolemizationAction(network)
        if self.DEBUG:
            print >> sys.stderr, "Goal/Query: ", tp
            print >> sys.stderr, "Query was not ground" if isNotGround is not None else "Query was ground"
        rt = bfp.answers(debug=self.DEBUG)

        self.queryNetworks.append((bfp.metaInterpNetwork, tp))
        self.edbQueries.update(bfp.edbQueries)
        if debug:
            print >> sys.stderr, bfp.metaInterpNetwork
            bfp.metaInterpNetwork.reportConflictSet(True, sys.stderr)
            for query in self.edbQueries:
                print >> sys.stderr, "Dispatched query against dataset: ", query.asSPARQL(
                )

        if isNotGround is not None:
            for item in bfp.goalSolutions:
                yield item, None
        else:
            yield rt, None
Esempio n. 5
0
    def invokeDecisionProcedure(self,tp,factGraph,bindings,debug,sipCollection):
        isNotGround = first(itertools.ifilter(lambda i:isinstance(i,Variable),
                                              tp))
        rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True)
        network.nsMap.update(self.nsBindings)
        bfp = BackwardFixpointProcedure(
                    factGraph,
                    network,
                    self.derivedPredicates,
                    tp,
                    sipCollection,
                    hybridPredicates=self.hybridPredicates,
                    debug=self.DEBUG,
                    nsBindings=self.nsBindings,
                    toldBNode = self.toldBNode)
        bfp.createTopDownReteNetwork(self.DEBUG)
        if self.existentialInHead:
            BNodeSkolemizationAction(network)
        if self.DEBUG:
            print >>sys.stderr, "Goal/Query: ", tp
            print >>sys.stderr, "Query was not ground" if isNotGround is not None else "Query was ground"
        rt=bfp.answers(debug=self.DEBUG)

        self.queryNetworks.append((bfp.metaInterpNetwork,tp))
        self.edbQueries.update(bfp.edbQueries)
        if debug:
            print >>sys.stderr, bfp.metaInterpNetwork
            bfp.metaInterpNetwork.reportConflictSet(True,sys.stderr)
            for query in self.edbQueries:
                print >>sys.stderr, "Dispatched query against dataset: ", query.asSPARQL()

        if isNotGround is not None:
            for item in bfp.goalSolutions:
                yield item,None
        else:
            yield rt,None