本文整理汇总了Python中rdflib.graph.Graph.sipOrder方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.sipOrder方法的具体用法?Python Graph.sipOrder怎么用?Python Graph.sipOrder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.Graph
的用法示例。
在下文中一共展示了Graph.sipOrder方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BuildNaturalSIP
# 需要导入模块: from rdflib.graph import Graph [as 别名]
# 或者: from rdflib.graph.Graph import sipOrder [as 别名]
def BuildNaturalSIP(clause,
derivedPreds,
adornedHead,
hybridPreds2Replace=None,
ignoreUnboundDPreds=False):
"""
Natural SIP:
Informally, for a rule of a program, a sip represents a
decision about the order in which the predicates of the rule will be evaluated, and how values
for variables are passed from predicates to other predicates during evaluation
>>> from functools import reduce
>>> from io import StringIO
>>> from FuXi.Rete.RuleStore import SetupRuleStore
>>> from FuXi.Rete import PROGRAM2
>>> ruleStore, ruleGraph = SetupRuleStore(StringIO(PROGRAM2))
>>> ruleStore._finalize()
>>> fg = Graph().parse(data=PROGRAM2, format='n3')
>>> from FuXi.Horn.HornRules import Ruleset
>>> rs = Ruleset(n3Rules=ruleGraph.store.rules, nsMapping=ruleGraph.store.nsMgr)
>>> for rule in rs: print(rule)
Forall ?Y ?X ( ex:sg(?X ?Y) :- ex:flat(?X ?Y) )
Forall ?Y ?Z4 ?X ?Z1 ?Z2 ?Z3 ( ex:sg(?X ?Y) :- And( ex:up(?X ?Z1) ex:sg(?Z1 ?Z2) ex:flat(?Z2 ?Z3) ex:sg(?Z3 ?Z4) ex:down(?Z4 ?Y) ) )
>>> sip = BuildNaturalSIP(list(rs)[-1], [], None) #doctest: +SKIP
>>> for N, x in IncomingSIPArcs(sip, MAGIC.sg): print(N.n3(), x.n3()) #doctest: +SKIP
( <http://doi.acm.org/10.1145/28659.28689#up> <http://doi.acm.org/10.1145/28659.28689#sg> <http://doi.acm.org/10.1145/28659.28689#flat> ) ( ?Z3 )
( <http://doi.acm.org/10.1145/28659.28689#up> <http://doi.acm.org/10.1145/28659.28689#sg> ) ( ?Z1 )
>>> sip = BuildNaturalSIP(list(rs)[-1], [MAGIC.sg], None) #doctest: +SKIP
>>> list(sip.query('SELECT ?q { ?prop a magic:SipArc . [] ?prop ?q . }', initNs={%(u)s'magic':MAGIC})) #doctest: +SKIP
[rdflib.term.URIRef(%(u)s'http://doi.acm.org/10.1145/28659.28689#sg'), rdflib.term.URIRef(%(u)s'http://doi.acm.org/10.1145/28659.28689#sg')]
"""
from FuXi.Rete.Magic import AdornedUniTerm
occurLookup = {}
boundHead = isinstance(
adornedHead, AdornedUniTerm) and 'b' in adornedHead.adornment
phBoundVars = list(adornedHead.getDistinguishedVariables(varsOnly=True))
# assert isinstance(clause.head, Uniterm), "Only one literal in the head."
def collectSip(left, right):
if isinstance(left, list):
vars = CollectSIPArcVars(left, right, phBoundVars)
if not vars and ignoreUnboundDPreds:
raise InvalidSIPException("No bound variables for %s" % right)
leftList = Collection(sipGraph, None)
left = list(set(left))
[leftList.append(i) for i in [GetOp(ii) for ii in left]]
left.append(right)
# arc = SIPGraphArc(leftList.uri, getOccurrenceId(right, occurLookup), vars, sipGraph)
return left
else:
left.isHead = True
vars = CollectSIPArcVars(left, right, phBoundVars)
if not vars and ignoreUnboundDPreds:
raise InvalidSIPException("No bound variables for %s" % right)
ph = GetOp(left)
# q = getOccurrenceId(right, occurLookup)
if boundHead:
# arc = SIPGraphArc(ph, q, vars, sipGraph, headPassing=boundHead)
sipGraph.add((ph, RDF.type, MAGIC.BoundHeadPredicate))
rt = [left, right]
else:
rt = [right]
return rt
sipGraph = Graph()
if isinstance(clause.body, And):
if ignoreUnboundDPreds:
foundSip = False
sips = findFullSip(([clause.head], None), clause.body)
while not foundSip:
sip = next(sips) if py3compat.PY3 else sips.next()
try:
reduce(collectSip,
iterCondition(And(sip)))
foundSip = True
bodyOrder = sip
except InvalidSIPException:
foundSip = False
else:
if first(filter(lambda i: isinstance(i, Uniterm) and i.naf or False,
clause.body)):
# There are negative literals in body, ensure
# the given sip order puts negated literals at the end
bodyOrder = first(
filter(ProperSipOrderWithNegation,
findFullSip(([clause.head], None),
clause.body)))
else:
bodyOrder = first(
findFullSip(([clause.head], None), clause.body))
assert bodyOrder, "Couldn't find a valid SIP for %s" % clause
reduce(collectSip,
iterCondition(And(bodyOrder)))
sipGraph.sipOrder = And(bodyOrder[1:])
# assert validSip(sipGraph), sipGraph.serialize(format='n3')
else:
if boundHead:
reduce(collectSip, itertools.chain(iterCondition(clause.head),
iterCondition(clause.body)))
#.........这里部分代码省略.........