diff service/reasoning/inputgraph.py @ 1086:6ab5238fc049

refactor Ignore-this: 61a7c93a71fba5feea956ef99c84a30e darcs-hash:e82a05debdd45d9127b50ae7cf8efe26e9537989
author drewp <drewp@bigasterisk.com>
date Fri, 06 May 2016 17:34:26 -0700
parents 4d16fa39d54a
children 66fe7a93753d
line wrap: on
line diff
--- 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