Mercurial > code > home > repos > homeauto
changeset 114:4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
Ignore-this: 81dc30256f5d2658e53fce60abea66db
author | drewp@bigasterisk.com |
---|---|
date | Tue, 10 Sep 2013 00:38:52 -0700 |
parents | 66f8897b74ea |
children | c860b8c10de9 |
files | service/reasoning/inference.py service/reasoning/rdflibtrig.py service/reasoning/reasoning.py |
diffstat | 3 files changed, 47 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/service/reasoning/inference.py Tue Sep 10 00:36:52 2013 -0700 +++ b/service/reasoning/inference.py Tue Sep 10 00:38:52 2013 -0700 @@ -4,8 +4,13 @@ import sys, re import restkit -from rdflib import StringInputSource, URIRef -from rdflib.Graph import Graph +from rdflib import URIRef +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") from FuXi.Rete.Util import generateTokenSet @@ -13,20 +18,7 @@ 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,) +from rdflibtrig import parseTrig, addTrig def infer(graph, rules): """ @@ -56,10 +48,3 @@ func.__name__, 1000 * (time.time() - t1))) return ret return inner - -def addTrig(graph, url): - t1 = time.time() - trig = restkit.request(url).body_string() - fetchTime = time.time() - t1 - graph.addN(parseTrig(trig)) - return fetchTime
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/service/reasoning/rdflibtrig.py Tue Sep 10 00:38:52 2013 -0700 @@ -0,0 +1,32 @@ +import re, time +import restkit +from rdflib import URIRef +try: + from rdflib import StringInputSource + from rdflib.Graph import Graph +except ImportError: + from rdflib.parser import StringInputSource + from rdflib import Graph + +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 addTrig(graph, url): + t1 = time.time() + trig = restkit.request(url).body_string() + fetchTime = time.time() - t1 + graph.addN(parseTrig(trig)) + return fetchTime
--- a/service/reasoning/reasoning.py Tue Sep 10 00:36:52 2013 -0700 +++ b/service/reasoning/reasoning.py Tue Sep 10 00:38:52 2013 -0700 @@ -30,7 +30,7 @@ sys.path.append("../../lib") from logsetup import log -log.setLevel(logging.DEBUG) +log.setLevel(logging.INFO) outlog = logging.getLogger('output') outlog.setLevel(logging.DEBUG) @@ -238,14 +238,14 @@ fetch("http://bang:8014/reasoningChange", method="POST", timeout=2, - payload=body, + postdata=body, headers={"content-type" : ["application/json"]}).addErrback(err) def _put(self, url, payload): def err(e): - outlog.warn("put %s falied", url) + outlog.warn("put %s failed", url) outlog.info("PUT %s payload=%r", url, payload) - fetch(url, method="PUT", payload=payload, timeout=2).addErrback(err) + fetch(url, method="PUT", postdata=payload, timeout=2).addErrback(err) def putResults(self, inferred): """ @@ -317,7 +317,7 @@ value = deviceGraph.value(dev, ROOM.zeroValue) if value is not None: outlog.info("put zero (%r) to %s", value, putUrl) - self._put(putUrl, payload=value) + self._put(putUrl, payload=str(value)) # this should be written back into the inferred graph # for feedback @@ -325,7 +325,7 @@ value = deviceGraph.value(obj, ROOM.putValue) if value is not None: outlog.info("put %s to %s", value, putUrl) - self._put(putUrl, payload=value) + self._put(putUrl, payload=str(value)) else: outlog.warn("%s %s %s has no :putValue" % (dev, pred, obj)) @@ -337,7 +337,7 @@ return url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl) outlog.info("put lcd %s brightness %s", url, brt) - self._put(str(url) + "?brightness=%s" % str(brt)) + self._put(str(url) + "?brightness=%s" % str(brt), payload='') msg = "open %s motion %s" % ( inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text),