示例#1
0
def DisjunctiveNormalForm(program, safety=DATALOG_SAFETY_NONE, network=None):
    for rule in program:
        tx_horn_clause = NormalizeClause(rule.formula)
        for tx_horn_clause in LloydToporTransformation(tx_horn_clause, True):
            if safety in [DATALOG_SAFETY_LOOSE, DATALOG_SAFETY_STRICT]:
                rule = Rule(tx_horn_clause,
                            nsMapping=network and network.nsMap or {})
                if not rule.isSafe():
                    if safety == DATALOG_SAFETY_LOOSE:
                        warnings.warn("Ignoring unsafe rule (%s)" % rule,
                                      SyntaxWarning, 3)
                        continue
                    elif safety == DATALOG_SAFETY_STRICT:
                        raise SyntaxError("Unsafe RIF Core rule: %s" % rule)
            disj = [
                i for i in breadth_first(tx_horn_clause.body)
                if isinstance(i, Or)
            ]
            if len(disj) > 0:
                NormalizeDisjunctions(disj, tx_horn_clause, program, network)
            elif isinstance(tx_horn_clause.head, (And, Uniterm)):
                # print("No Disjunction in the body")
                for hc in ExtendN3Rules(network,
                                        NormalizeClause(tx_horn_clause)):
                    yield makeRule(hc, network and network.nsMap or {})
示例#2
0
文件: __init__.py 项目: baojie/FuXi-1
def DisjunctiveNormalForm(program, safety=DATALOG_SAFETY_NONE, network=None):
    for rule in program:
        tx_horn_clause = NormalizeClause(rule.formula)
        for tx_horn_clause in LloydToporTransformation(tx_horn_clause, True):
            if safety in [DATALOG_SAFETY_LOOSE, DATALOG_SAFETY_STRICT]:
                rule = Rule(
                    tx_horn_clause, nsMapping=network and network.nsMap or {})
                if not rule.isSafe():
                    if safety == DATALOG_SAFETY_LOOSE:
                        import warnings
                        warnings.warn("Ignoring unsafe rule (%s)" % rule,
                                      SyntaxWarning,
                                      3)
                        continue
                    elif safety == DATALOG_SAFETY_STRICT:
                        raise SyntaxError("Unsafe RIF Core rule: %s" % rule)
            disj = [i for i in breadth_first(
                tx_horn_clause.body) if isinstance(i, Or)]
            if len(disj) > 0:
                NormalizeDisjunctions(disj, tx_horn_clause, program, network)
            elif isinstance(tx_horn_clause.head, (And, Uniterm)):
                    # print("No Disjunction in the body")
                    for hc in ExtendN3Rules(
                            network, NormalizeClause(tx_horn_clause)):
                        yield makeRule(hc, network and network.nsMap or {})
示例#3
0
def MapDLPtoNetwork(network,
                    factGraph,
                    complementExpansions=[],
                    constructNetwork=False,
                    derivedPreds=[],
                    ignoreNegativeStratus=False,
                    safety=DATALOG_SAFETY_NONE):
    ruleset = set()
    negativeStratus = []
    for horn_clause in T(factGraph,
                         complementExpansions=complementExpansions,
                         derivedPreds=derivedPreds):
        # print("## RIF BLD Horn Rules: Before LloydTopor: ##\n", horn_clause)
        # print("## RIF BLD Horn Rules: After LloydTopor: ##")
        fullReduce = True
        for tx_horn_clause in LloydToporTransformation(horn_clause,
                                                       fullReduce):
            tx_horn_clause = NormalizeClause(tx_horn_clause)
            disj = [
                i for i in breadth_first(tx_horn_clause.body)
                if isinstance(i, Or)
            ]
            if len(disj) > 0:
                NormalizeDisjunctions(disj, tx_horn_clause, ruleset, network,
                                      constructNetwork, negativeStratus,
                                      ignoreNegativeStratus)
            elif isinstance(tx_horn_clause.head, (And, Uniterm)):
                # print("No Disjunction in the body")
                for hc in ExtendN3Rules(network,
                                        NormalizeClause(tx_horn_clause),
                                        constructNetwork):
                    if safety in [DATALOG_SAFETY_LOOSE, DATALOG_SAFETY_STRICT]:
                        rule = Rule(hc, nsMapping=network.nsMap)
                        if not rule.isSafe():
                            if safety == DATALOG_SAFETY_LOOSE:
                                warnings.warn(
                                    "Ignoring unsafe rule (%s)" % rule,
                                    SyntaxWarning, 3)
                                continue
                            elif safety == DATALOG_SAFETY_STRICT:
                                raise SyntaxError("Unsafe RIF Core rule: %s" %
                                                  rule)
                    _rule = makeRule(hc, network.nsMap)
                    if _rule.negativeStratus:
                        negativeStratus.append(_rule)
                    if _rule is not None and (not _rule.negativeStratus
                                              or not ignoreNegativeStratus):
                        ruleset.add(_rule)
            # Extract free variables anre add rule to ruleset
        # print("#######################")
    # print("########## Finished Building decision network from DLP ##########")
    # renderNetwork(network).write_graphviz('out.dot')
    if ignoreNegativeStratus:
        return ruleset, negativeStratus
    else:
        return iter(ruleset)
示例#4
0
def MapDLPtoNetwork(network,
                    factGraph,
                    complementExpansions=[],
                    constructNetwork=False,
                    derivedPreds=[],
                    ignoreNegativeStratus=False,
                    safety = DATALOG_SAFETY_NONE):
    from FuXi.Rete.SidewaysInformationPassing import GetArgs, iterCondition, GetOp
    ruleset=set()
    negativeStratus=[]
    for horn_clause in T(factGraph,complementExpansions=complementExpansions,derivedPreds=derivedPreds):
#        print "## RIF BLD Horn Rules: Before LloydTopor: ##\n",horn_clause
#        print "## RIF BLD Horn Rules: After LloydTopor: ##"
        fullReduce=True
        for tx_horn_clause in LloydToporTransformation(horn_clause,fullReduce):
            tx_horn_clause = NormalizeClause(tx_horn_clause)
            disj = [i for i in breadth_first(tx_horn_clause.body) if isinstance(i,Or)]
            import warnings
            if len(disj)>0:
                NormalizeDisjunctions(disj,
                                      tx_horn_clause,
                                      ruleset,
                                      network,
                                      constructNetwork,
                                      negativeStratus,
                                      ignoreNegativeStratus)
            elif isinstance(tx_horn_clause.head,(And,Uniterm)):
    #                print "No Disjunction in the body"
                    for hc in ExtendN3Rules(network,NormalizeClause(tx_horn_clause),constructNetwork):
                        if safety in [DATALOG_SAFETY_LOOSE, DATALOG_SAFETY_STRICT]:
                            rule = Rule(hc,nsMapping=network.nsMap)
                            if not rule.isSafe():
                                if safety == DATALOG_SAFETY_LOOSE:
                                    import warnings
                                    warnings.warn("Ignoring unsafe rule (%s)"%rule,
                                                  SyntaxWarning,
                                                  3)
                                    continue
                                elif safety == DATALOG_SAFETY_STRICT:
                                    raise SyntaxError("Unsafe RIF Core rule: %s"%rule) 
                        _rule=makeRule(hc,network.nsMap)
                        if _rule.negativeStratus:
                            negativeStratus.append(_rule)                    
                        if _rule is not None and (not _rule.negativeStratus or not ignoreNegativeStratus):
                            ruleset.add(_rule)                    
            #Extract free variables anre add rule to ruleset
#        print "#######################"
#    print "########## Finished Building decision network from DLP ##########"
    #renderNetwork(network).write_graphviz('out.dot')
    if ignoreNegativeStratus:
        return ruleset,negativeStratus
    else:
        return iter(ruleset)