Mercurial > code > home > repos > homeauto
diff service/reasoning/inputgraph.py @ 281:9728288c7f2f
refactor
Ignore-this: 61a7c93a71fba5feea956ef99c84a30e
author | drewp@bigasterisk.com |
---|---|
date | Fri, 06 May 2016 17:34:26 -0700 |
parents | d3733587e749 |
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