Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
def CalculateStratifiedModel(network, ontGraph, derivedPreds, edb=None):
    posRules, ignored = MapDLPtoNetwork(network,
                                        ontGraph,
                                        constructNetwork=False,
                                        derivedPreds=derivedPreds,
                                        ignoreNegativeStratus=True)
    for rule in posRules:
        network.buildNetworkFromClause(rule)
    network.feedFactsToAdd(generateTokenSet(edb and edb or ontGraph))
    for i in ignored:
        # Evaluate the Graph pattern, and instanciate the head of the rule with
        # the solutions returned
        sel, compiler = StratifiedSPARQL(i)
        query = compiler.compile(sel)
        i.stratifiedQuery = query
        vars = sel.projection
        for rt in (edb and edb or ontGraph).query(query):
            solutions = {}
            if isinstance(rt, tuple):
                solutions.update(dict(
                    [(vars[idx], i) for idx, i in enumerate(rt)]))
            else:
                solutions[vars[0]] = rt
            i.solutions = solutions
            head = copy.deepcopy(i.formula.head)
            head.ground(solutions)
            fact = head.toRDFTuple()
            network.inferredFacts.add(fact)
            network.feedFactsToAdd(generateTokenSet([fact]))

    # Now we need to clear assertions that cross the individual,
    # concept, relation divide
    # toRemove=[]
    for s, p, o in network.inferredFacts.triples((None,
                                                  RDF.type,
                                                  None)):
        if s in (edb and edb or ontGraph).predicates() or\
           s in [_s for _s, _p, _o in
                 (edb and edb or ontGraph).triples_choices(
                     (None,
                         RDF.type,
                         [OWL_NS.Class,
                          OWL_NS.Restriction]))]:
            network.inferredFacts.remove((s, p, o))
    return posRules, ignored