Mercurial > code > home > repos > homeauto
diff service/reasoning/inference.py @ 284:95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
Ignore-this: e8788fe6e9c8738671bc1f8910a906c
refactor all rule/inference code to one module; all escaping/unescaping to another.
author | drewp@bigasterisk.com |
---|---|
date | Sun, 08 May 2016 02:58:25 -0700 |
parents | 9728288c7f2f |
children | 3b61c0dfaaef |
line wrap: on
line diff
--- a/service/reasoning/inference.py Fri May 06 18:38:18 2016 -0700 +++ b/service/reasoning/inference.py Sun May 08 02:58:25 2016 -0700 @@ -2,18 +2,62 @@ see ./reasoning for usage """ -import sys +import sys, os try: from rdflib.Graph import Graph except ImportError: from rdflib import Graph + +from rdflib.parser import StringInputSource 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 FuXi.Rete.RuleStore import N3RuleStore + +from rdflib import plugin, Namespace from rdflib.store import Store +sys.path.append('../../../ffg/ffg') +import evtiming + +from escapeoutputstatements import escapeOutputStatements +ROOM = Namespace("http://projects.bigasterisk.com/room/") + +_rulesCache = (None, None, None, None) +@evtiming.serviceLevel.timed('readRules') +def readRules(rulesPath, outputPatterns): + """ + returns (rulesN3, ruleGraph) + + This includes escaping certain statements in the output + (implied) subgraaphs so they're not confused with input + statements. + """ + global _rulesCache + mtime = os.path.getmtime(rulesPath) + key = (rulesPath, mtime) + if _rulesCache[:2] == key: + _, _, rulesN3, expandedN3 = _rulesCache + else: + rulesN3 = open(rulesPath).read() # for web display + + plainGraph = Graph() + plainGraph.parse(StringInputSource(rulesN3), + format='n3') # for inference + escapeOutputStatements(plainGraph, outputPatterns=outputPatterns) + expandedN3 = plainGraph.serialize(format='n3') + _rulesCache = key + (rulesN3, expandedN3) + + # the rest needs to happen each time since inference is + # consuming the ruleGraph somehow + ruleStore = N3RuleStore() + ruleGraph = Graph(ruleStore) + + ruleGraph.parse(StringInputSource(expandedN3), format='n3') + log.debug('%s rules' % len(ruleStore.rules)) + return rulesN3, ruleGraph + def infer(graph, rules): """ returns new graph of inferred statements