changeset 1086:6ab5238fc049

refactor Ignore-this: 61a7c93a71fba5feea956ef99c84a30e darcs-hash:e82a05debdd45d9127b50ae7cf8efe26e9537989
author drewp <drewp@bigasterisk.com>
date Fri, 06 May 2016 17:34:26 -0700
parents dda3f1524a52
children 806d2f633775
files service/reasoning/inference.py service/reasoning/inputgraph.py service/reasoning/reasoning.py
diffstat 3 files changed, 29 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/service/reasoning/inference.py	Fri May 06 17:32:41 2016 -0700
+++ b/service/reasoning/inference.py	Fri May 06 17:34:26 2016 -0700
@@ -4,10 +4,8 @@
 
 import sys
 try:
-    from rdflib import StringInputSource
     from rdflib.Graph import Graph
 except ImportError:
-    from rdflib.parser import StringInputSource
     from rdflib import Graph
 
 sys.path.append("/my/proj/room/fuxi/build/lib.linux-x86_64-2.6")
--- a/service/reasoning/inputgraph.py	Fri May 06 17:32:41 2016 -0700
+++ b/service/reasoning/inputgraph.py	Fri May 06 17:34:26 2016 -0700
@@ -2,6 +2,7 @@
 
 from rdflib import Graph, ConjunctiveGraph
 from rdflib import Namespace, URIRef, Literal, RDF
+from rdflib.parser import StringInputSource
 
 from twisted.python.filepath import FilePath
 from twisted.internet.defer import inlineCallbacks, gatherResults
@@ -15,6 +16,14 @@
 DEV = Namespace("http://projects.bigasterisk.com/device/")
 
 
+def parseRdf(text, contentType):
+    g = Graph()
+    g.parse(StringInputSource(text), format={
+        'text/n3': 'n3',
+        }[contentType])
+    return g
+
+
 class InputGraph(object):
     def __init__(self, inputDirs, onChange, sourceSubstr=None):
         """
@@ -143,6 +152,15 @@
             self._oneShotAdditionGraph = None
             self._combinedGraph = None
 
+    def addOneShotFromString(self, body, contentType):
+        g = parseRdf(body, contentType)
+        if not len(g):
+            log.warn("incoming oneshot graph had no statements: %r", body)
+            return 0
+        t1 = time.time()
+        self.addOneShot(g)
+        return time.time() - t1
+            
     def getGraph(self):
         """rdflib Graph with the file+remote contents of the input graph"""
         # this could be much faster with the combined readonly graph
--- a/service/reasoning/reasoning.py	Fri May 06 17:32:41 2016 -0700
+++ b/service/reasoning/reasoning.py	Fri May 06 17:34:26 2016 -0700
@@ -41,6 +41,7 @@
 ROOM = Namespace("http://projects.bigasterisk.com/room/")
 DEV = Namespace("http://projects.bigasterisk.com/device/")
 
+NS = {'': ROOM, 'dev': DEV}
         
 class Reasoning(object):
     def __init__(self):
@@ -87,7 +88,7 @@
                                Literal(traceback.format_exc())))
             raise
         return [(ROOM['reasoner'], ROOM['ruleParseTime'],
-                               Literal(ruleParseTime))]
+                 Literal(ruleParseTime))], ruleParseTime
 
     evtiming.serviceLevel.timed('graphChanged')
     def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None):
@@ -99,11 +100,11 @@
         t1 = time.time()
         oldInferred = self.inferred
         try:
-            ruleStmts = self.updateRules()
+            ruleStatStmts, ruleParseSec = self.updateRules()
             
             g = inputGraph.getGraph()
             self.inferred = self._makeInferred(g)
-            [self.inferred.add(s) for s in ruleStmts]
+            [self.inferred.add(s) for s in ruleStatStmts]
 
             if oneShot:
                 # unclear where this should go, but the oneshot'd
@@ -118,13 +119,16 @@
         finally:
             if oneShot:
                 self.inferred = oldInferred
-        log.info("graphChanged %.1f ms (putResults %.1f ms)" %
+        log.info("graphChanged took %.1f ms (rule parse %.1f ms, putResults %.1f ms)" %
                  ((time.time() - t1) * 1000,
+                  ruleParseSec * 1000,
                   putResultsTime * 1000))
 
     def _makeInferred(self, inputGraph):
         t1 = time.time()
         out = infer(inputGraph, self.ruleStore)
+        for p, n in NS.iteritems():
+            out.bind(p, n, override=True)
         inferenceTime = time.time() - t1
 
         out.add((ROOM['reasoner'], ROOM['inferenceTime'],
@@ -166,13 +170,6 @@
         yield r.poll()
         self.set_status(202)
 
-def parseRdf(text, contentType):
-    g = Graph()
-    g.parse(StringInputSource(text), format={
-        'text/n3': 'n3',
-        }[contentType])
-    return g
-
 class OneShot(cyclone.web.RequestHandler):
     def post(self):
         """
@@ -186,15 +183,9 @@
         everything appears to be a 'change'.
         """
         try:
-            g = parseRdf(self.request.body, self.request.headers['content-type'])
-            for s in g:
-                log.debug("oneshot stmt %r", s)
-            if not len(g):
-                log.warn("incoming oneshot graph had no statements: %r", self.request.body)
-                return
-            t1 = time.time()
-            self.settings.reasoning.inputGraph.addOneShot(g)
-            self.set_header('x-graph-ms', str(1000 * (time.time() - t1)))
+            dt = self.settings.reasoning.inputGraph.addOneShotFromString(
+                self.request.body, self.request.headers['content-type'])
+            self.set_header('x-graph-ms', str(1000 * dt))
         except Exception as e:
             log.error(e)
             raise