Mercurial > code > home > repos > homeauto
diff service/reasoning/inference.py @ 20:3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
Ignore-this: 5c5551d566324f5a6e87f6f7623f3c3
author | drewp@bigasterisk.com |
---|---|
date | Tue, 07 Feb 2012 02:45:22 -0800 |
parents | |
children | 5b0f970e3d52 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/reasoning/inference.py Tue Feb 07 02:45:22 2012 -0800 @@ -0,0 +1,62 @@ +""" +see ./reasoning for usage +""" + +import sys, re +import restkit +from rdflib import StringInputSource, URIRef +from rdflib.Graph import Graph + +sys.path.append("/my/proj/room/fuxi/build/lib.linux-x86_64-2.6") +from FuXi.Rete.Util import generateTokenSet +from FuXi.Rete import ReteNetwork +from rdflib import plugin +from rdflib.store import Store + +def parseTrig(trig): + """ + yields quads + """ + m = re.match(r"<([^>]+)> \{(.*)\}\s*$", trig, re.DOTALL) + if m is None: + raise NotImplementedError("trig format was too tricky: %r..." % trig[:200]) + + ctx = URIRef(m.group(1)) + n3 = m.group(2) + g = Graph() + g.parse(StringInputSource(n3), format="n3") + for stmt in g: + yield stmt + (ctx,) + +def infer(graph, rules): + """ + returns new graph of inferred statements + """ + # based on fuxi/tools/rdfpipe.py + store = plugin.get('IOMemory',Store)() + store.open('') + + target = Graph() + tokenSet = generateTokenSet(graph) + network = ReteNetwork(rules, inferredTarget=target) + network.feedFactsToAdd(tokenSet) + + store.rollback() + return target + +import time, logging +log = logging.getLogger() +def logTime(func): + def inner(*args, **kw): + t1 = time.time() + try: + ret = func(*args, **kw) + finally: + log.info("Call to %s took %.1f ms" % ( + func.__name__, 1000 * (time.time() - t1))) + return ret + return inner + +def addTrig(graph, url): + trig = restkit.request(url).body_string() + graph.addN(parseTrig(trig))