Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 235:5ad229334a88
rdflib can parse trig now
Ignore-this: ad1feb1d88249f509d2906b6cdbe62c6
author | drewp@bigasterisk.com |
---|---|
date | Mon, 01 Feb 2016 02:15:23 -0800 |
parents | 47682350e6f2 |
children | 0c306e76d8c5 |
rev | line source |
---|---|
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
1 #!bin/python |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
2 """ |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
3 Graph consists of: |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
4 input/* (read at startup) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
5 webinput/* (new files are noticed in here) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
6 any number of remote graphs, specified in the other graph as objects of (:reasoning, :source, *), reread constantly |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
7 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
8 gather subgraphs from various services, run them through a rules |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
9 engine, and make http requests with the conclusions. |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
11 E.g. 'when drew's phone is near the house, and someone is awake, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
12 unlock the door when the door's motion sensor is activated' |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
13 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
14 When do we gather? The services should be able to trigger us, perhaps |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
15 with PSHB, that their graph has changed. |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
16 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
17 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
18 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
19 from twisted.internet import reactor, task |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
20 from twisted.python.filepath import FilePath |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
21 import time, traceback, sys, json, logging, urllib |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
22 from rdflib import Graph, ConjunctiveGraph |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
23 from rdflib import Namespace, URIRef, Literal, RDF |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
24 from rdflib.parser import StringInputSource |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
25 from cyclone.httpclient import fetch |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
26 import cyclone.web, cyclone.websocket |
235 | 27 from inference import infer |
28 from rdflibtrig import addTrig | |
48 | 29 from graphop import graphEqual |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
30 from docopt import docopt |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
31 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
32 from FuXi.Rete.RuleStore import N3RuleStore |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
33 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
34 sys.path.append("../../lib") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
35 from logsetup import log |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
36 log.setLevel(logging.WARN) |
49 | 37 outlog = logging.getLogger('output') |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
38 outlog.setLevel(logging.WARN) |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
39 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
40 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
41 DEV = Namespace("http://projects.bigasterisk.com/device/") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
42 |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
43 |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
44 class InputGraph(object): |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
45 def __init__(self, inputDirs, onChange): |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
46 """ |
49 | 47 this has one Graph that's made of: |
48 - all .n3 files from inputDirs (read at startup) | |
49 - all the remote graphs, specified in the file graphs | |
50 | |
51 call updateFileData or updateRemoteData to reread those | |
52 graphs. getGraph to access the combined graph. | |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
53 |
49 | 54 onChange(self) is called if the contents of the full graph |
55 change (in an interesting way) during updateFileData or | |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
56 updateRemoteData. Interesting means statements other than the |
49 | 57 ones with the predicates on the boring list. onChange(self, |
58 oneShot=True) means: don't store the result of this change | |
59 anywhere; it needs to be processed only once | |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
60 """ |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
61 self.inputDirs = inputDirs |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
62 self.onChange = onChange |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
63 self._fileGraph = Graph() |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
64 self._remoteGraph = None |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
65 self._combinedGraph = None |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
66 self._oneShotAdditionGraph = None |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
67 |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
68 def updateFileData(self): |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
69 """ |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
70 make sure we contain the correct data from the files in inputDirs |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
71 """ |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
72 # this sample one is actually only needed for the output, but I don't |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
73 # think I want to have a separate graph for the output |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
74 # handling |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
75 log.debug("read file graphs") |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
76 for fp in FilePath("input").walk(): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
77 if fp.isdir(): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
78 continue |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
79 if fp.splitext()[1] != '.n3': |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
80 continue |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
81 log.debug("read %s", fp) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
82 # todo: if this fails, leave the report in the graph |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
83 self._fileGraph.parse(fp.open(), format="n3") |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
84 self._combinedGraph = None |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
85 |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
86 self.onChange(self) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
87 |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
88 def updateRemoteData(self): |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
89 """ |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
90 read all remote graphs (which are themselves enumerated within |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
91 the file data) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
92 """ |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
93 log.debug("read remote graphs") |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
94 g = ConjunctiveGraph() |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
95 for source in self._fileGraph.objects(ROOM['reasoning'], |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
96 ROOM['source']): |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
97 try: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
98 # this part could be parallelized |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
99 fetchTime = addTrig(g, source) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
100 except Exception, e: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
101 log.error(" adding source %s: %s", source, e) |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
102 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e)))) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
103 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad'])) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
104 else: |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
105 g.add((URIRef(source), ROOM['graphLoadSecs'], |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
106 Literal(fetchTime))) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
107 prevGraph = self._remoteGraph |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
108 self._remoteGraph = g |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
109 self._combinedGraph = None |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
110 if (prevGraph is None or |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
111 not graphEqual(g, prevGraph, ignorePredicates=[ |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
112 ROOM.signalStrength, |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
113 ROOM.graphLoadSecs, |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
114 # perhaps anything with a number-datatype for its |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
115 # object should be filtered out, and you have to make |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
116 # an upstream quantization (e.g. 'temp high'/'temp |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
117 # low') if you want to do reasoning on the difference |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
118 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"), |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
119 URIRef("http://bigasterisk.com/map#lastSeenAgo"), |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
120 URIRef("http://projects.bigasterisk.com/room/usingPower"), |
54
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
121 URIRef("http://projects.bigasterisk.com/room/idleTimeMinutes"), |
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
122 URIRef("http://projects.bigasterisk.com/room/idleTimeMs"), |
118 | 123 ROOM.history, |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
124 ])): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
125 log.debug(" remote graph changed") |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
126 self.onChange(self) |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
127 else: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
128 log.debug(" remote graph is unchanged") |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
129 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
130 def addOneShot(self, g): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
131 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
132 add this graph to the total, call onChange, and then revert |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
133 the addition of this graph |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
134 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
135 self._oneShotAdditionGraph = g |
49 | 136 self._combinedGraph = None |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
137 try: |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
138 self.onChange(self, oneShot=True, oneShotGraph=g) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
139 finally: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
140 self._oneShotAdditionGraph = None |
49 | 141 self._combinedGraph = None |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
142 |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
143 def getGraph(self): |
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
144 """rdflib Graph with the file+remote contents of the input graph""" |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
145 # this could be much faster with the combined readonly graph |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
146 # view from rdflib |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
147 if self._combinedGraph is None: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
148 self._combinedGraph = Graph() |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
149 if self._fileGraph: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
150 for s in self._fileGraph: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
151 self._combinedGraph.add(s) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
152 if self._remoteGraph: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
153 for s in self._remoteGraph: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
154 self._combinedGraph.add(s) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
155 if self._oneShotAdditionGraph: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
156 for s in self._oneShotAdditionGraph: |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
157 self._combinedGraph.add(s) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
158 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
159 return self._combinedGraph |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
160 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
161 class Reasoning(object): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
162 def __init__(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
163 self.prevGraph = None |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
164 self.lastPollTime = 0 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
165 self.lastError = "" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
166 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
167 self.rulesN3 = "(not read yet)" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
168 self.inferred = Graph() # gets replaced in each graphChanged call |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
169 |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
170 self.inputGraph = InputGraph([], self.graphChanged) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
171 self.inputGraph.updateFileData() |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
172 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
173 def readRules(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
174 self.rulesN3 = open('rules.n3').read() # for web display |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
175 self.ruleStore = N3RuleStore() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
176 self.ruleGraph = Graph(self.ruleStore) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
177 self.ruleGraph.parse('rules.n3', format='n3') # for inference |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
178 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
179 def poll(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
180 try: |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
181 self.inputGraph.updateRemoteData() |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
182 self.lastPollTime = time.time() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
183 except Exception, e: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
184 log.error(traceback.format_exc()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
185 self.lastError = str(e) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
186 |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
187 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
188 t1 = time.time() |
49 | 189 oldInferred = self.inferred |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
190 try: |
49 | 191 try: |
192 t1 = time.time() | |
193 self.readRules() | |
194 ruleParseTime = time.time() - t1 | |
195 except ValueError, e: | |
196 # this is so if you're just watching the inferred output, | |
197 # you'll see the error too | |
198 self.inferred = Graph() | |
199 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], | |
200 Literal(traceback.format_exc()))) | |
201 raise | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
202 |
49 | 203 g = inputGraph.getGraph() |
204 self.inferred = self._makeInferred(g) | |
205 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseTime'], | |
206 Literal(ruleParseTime))) | |
207 | |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
208 if oneShot: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
209 # unclear where this should go, but the oneshot'd |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
210 # statements should be just as usable as inferred |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
211 # ones. |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
212 for s in oneShotGraph: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
213 self.inferred.add(s) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
214 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
215 t2 = time.time() |
49 | 216 self.putResults(self.inferred) |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
217 putResultsTime = time.time() - t2 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
218 t3 = time.time() |
49 | 219 self._postToMagma(g) |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
220 postMagmaTime = time.time() - t3 |
49 | 221 finally: |
222 if oneShot: | |
223 self.inferred = oldInferred | |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
224 log.info("graphChanged %.1f ms (putResults %.1f ms; postToMagma %.1f ms)" % |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
225 ((time.time() - t1) * 1000, |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
226 putResultsTime * 1000, |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
227 postMagmaTime * 1000)) |
49 | 228 |
229 def _makeInferred(self, inputGraph): | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
230 t1 = time.time() |
49 | 231 out = infer(inputGraph, self.ruleStore) |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
232 inferenceTime = time.time() - t1 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
233 |
49 | 234 out.add((ROOM['reasoner'], ROOM['inferenceTime'], |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
235 Literal(inferenceTime))) |
49 | 236 return out |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
237 |
49 | 238 def _postToMagma(self, inputGraph): |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
239 return # not up right now |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
240 inputGraphNt = inputGraph.serialize(format="nt") |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
241 inferredNt = self.inferred.serialize(format="nt") |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
242 body = json.dumps({"input": inputGraphNt, |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
243 "inferred": inferredNt}) |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
244 def err(e): |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
245 log.error("while sending changes to magma:") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
246 log.error(e) |
46
f5623d9b07fd
rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents:
45
diff
changeset
|
247 |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
248 fetch("http://bang:8014/reasoningChange", |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
249 method="POST", |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
250 timeout=2, |
114
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
112
diff
changeset
|
251 postdata=body, |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
252 headers={"content-type" : ["application/json"]}).addErrback(err) |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
253 |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
254 def _put(self, url, payload): |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
255 def err(e): |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
256 outlog.warn(" put %s failed", url) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
257 outlog.info(" PUT %s payload=%r", url, payload) |
114
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
112
diff
changeset
|
258 fetch(url, method="PUT", postdata=payload, timeout=2).addErrback(err) |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
259 |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
260 def putResults(self, inferred): |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
261 """ |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
262 some conclusions in the inferred graph lead to PUT requests |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
263 getting made |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
264 |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
265 if the graph contains (?d ?p ?o) and ?d and ?p are a device |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
266 and predicate we support PUTs for, then we look up |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
267 (?d :putUrl ?url) and (?o :putValue ?val) and call |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
268 PUT ?url <- ?val |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
269 |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
270 If the graph doesn't contain any matches, we use (?d |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
271 :zeroValue ?val) for the value and PUT that. |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
272 """ |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
273 deviceGraph = self.inputGraph.getGraph() |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
274 self.oneShotPostActions(deviceGraph, inferred) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
275 for dev, pred in [ |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
276 # the config of each putUrl should actually be in the |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
277 # context of a dev and predicate pair, and then that would |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
278 # be the source of this list |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
279 #(DEV.theaterDoorLock, ROOM.state), |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
280 #(URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState), |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
281 (URIRef('http://bigasterisk.com/host/dash/monitor'), ROOM.powerState), |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
282 (URIRef('http://projects.bigasterisk.com/room/storageCeilingLedLong'), ROOM.brightness), |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
283 (URIRef('http://projects.bigasterisk.com/room/storageCeilingLedCross'), ROOM.brightness), |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
284 ]: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
285 url = deviceGraph.value(dev, ROOM.putUrl) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
286 |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
287 if url and dev == DEV.theaterDoorLock: # ew |
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
288 self._put(url+"/mode", payload="output") |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
289 |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
290 inferredObjects = list(inferred.objects(dev, pred)) |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
291 if len(inferredObjects) == 0: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
292 self.putZero(deviceGraph, dev, pred, url) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
293 elif len(inferredObjects) == 1: |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
294 self.putInferred(deviceGraph, url, inferredObjects[0]) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
295 elif len(inferredObjects) > 1: |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
296 log.info("conflict, ignoring: %s has %s of %s" % |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
297 (dev, pred, inferredObjects)) |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
298 # write about it to the inferred graph? |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
299 |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
300 #self.frontDoorPuts(deviceGraph, inferred) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
301 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
302 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
303 def oneShotPostActions(self, deviceGraph, inferred): |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
304 """ |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
305 Inferred graph may contain some one-shot statements. We'll send |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
306 statement objects to anyone on web sockets, and also generate |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
307 POST requests as described in the graph. |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
308 |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
309 one-shot statement ?s ?p ?o |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
310 with this in the graph: |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
311 ?osp a :OneShotPost |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
312 ?osp :subject ?s |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
313 ?osp :predicate ?p |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
314 this will cause a post to ?o |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
315 """ |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
316 # nothing in this actually makes them one-shot yet. they'll |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
317 # just fire as often as we get in here, which is not desirable |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
318 log.info("oneShotPostActions") |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
319 def err(e): |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
320 outlog.warn("post %s failed", postTarget) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
321 for osp in deviceGraph.subjects(RDF.type, ROOM['OneShotPost']): |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
322 s = deviceGraph.value(osp, ROOM['subject']) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
323 p = deviceGraph.value(osp, ROOM['predicate']) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
324 if s is None or p is None: |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
325 continue |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
326 for postTarget in inferred.objects(s, p): |
77
b19f137702d5
keep hacking reasoning to handle my output actions
drewp@bigasterisk.com
parents:
64
diff
changeset
|
327 log.info("post target %r", postTarget) |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
328 # this packet ought to have 'oneShot' in it somewhere |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
329 sendToLiveClients({"s":s, "p":p, "o":postTarget}) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
330 |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
331 outlog.info(" POST %s", postTarget) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
332 fetch(postTarget, method="POST", timeout=2).addErrback(err) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
333 self.postMpdCommands(inferred) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
334 |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
335 def postMpdCommands(self, inferred): |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
336 """special case to be eliminated. mpd play urls are made of an |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
337 mpd service and a song/album/playlist uri to be played. |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
338 Ideally the graph rules would assemble these like |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
339 http://{mpd}/addAndPlay?uri={toPlay} or maybe toPlay as the payload |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
340 which would be fairly general but still allow toPlay uris to |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
341 be matched with any player.""" |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
342 def post(postTarget): |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
343 outlog.info("special mpd POST %s", postTarget) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
344 def err(e): |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
345 outlog.warn("post %s failed", postTarget) |
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
346 fetch(postTarget, method="POST", timeout=2).addErrback(err) |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
347 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
348 rootSkippingAuth = "http://brace:9009/" |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
349 for mpd in [URIRef("http://bigasterisk.com/host/brace/mpd")]: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
350 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
351 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
352 for song in inferred.objects(mpd, ROOM['startMusic']): |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
353 outlog.info("mpd statement: %r" % song) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
354 assert song.startswith('http://bigasterisk.com/music/') |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
355 post(rootSkippingAuth + "addAndPlay" + urllib.quote(song[len("http://bigasterisk.com/music"):])) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
356 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
357 for state in inferred.objects(mpd, ROOM['playState']): |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
358 log.info('hello playstate %s', state) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
359 if state == ROOM['pause']: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
360 log.info("mpd %s %s", mpd, state) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
361 post(rootSkippingAuth + "mpd/pause") |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
362 for vol in inferred.objects(mpd, ROOM['audioState']): |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
363 if vol == ROOM['volumeStepUp']: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
364 post(rootSkippingAuth + "volumeAdjust?amount=6&max=70") |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
365 if vol == ROOM['volumeStepDown']: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
366 post(rootSkippingAuth + "volumeAdjust?amount=-6&min=10") |
129
745eff67ad40
reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents:
118
diff
changeset
|
367 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
368 def putZero(self, deviceGraph, dev, pred, putUrl): |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
369 # zerovalue should be a function of pred as well. |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
370 value = deviceGraph.value(dev, ROOM.zeroValue) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
371 if value is not None: |
49 | 372 outlog.info("put zero (%r) to %s", value, putUrl) |
114
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
112
diff
changeset
|
373 self._put(putUrl, payload=str(value)) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
374 # this should be written back into the inferred graph |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
375 # for feedback |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
376 |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
377 def putInferred(self, deviceGraph, putUrl, obj): |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
378 """ |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
379 HTTP PUT to putUrl, with a payload that's either obj's :putValue |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
380 or obj itself. |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
381 """ |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
382 value = deviceGraph.value(obj, ROOM.putValue) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
383 if value is not None: |
49 | 384 outlog.info("put %s to %s", value, putUrl) |
114
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
112
diff
changeset
|
385 self._put(putUrl, payload=str(value)) |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
386 elif isinstance(obj, Literal): |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
387 outlog.info("put %s to %s", obj, putUrl) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
388 self._put(putUrl, payload=str(obj)) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
389 else: |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
390 outlog.warn("don't know what payload to put for %s. obj=%r", |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
391 putUrl, obj) |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
392 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
393 def frontDoorPuts(self, deviceGraph, inferred): |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
394 # todo: shouldn't have to be a special case |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
395 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
396 if brt is None: |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
397 return |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
398 url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl) |
49 | 399 outlog.info("put lcd %s brightness %s", url, brt) |
114
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
112
diff
changeset
|
400 self._put(str(url) + "?brightness=%s" % str(brt), payload='') |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
401 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
402 msg = "open %s motion %s" % ( |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
403 inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text), |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
404 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text)) |
33
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
405 # this was meant to be 2 chars in the bottom row, but the |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
406 # easier test was to replace the whole top msg |
c3c203100e06
redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents:
20
diff
changeset
|
407 #restkit.Resource("http://slash:9080/").put("lcd", message=msg) |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
408 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
409 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
410 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
411 class Index(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
412 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
413 # make sure GET / fails if our poll loop died |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
414 ago = time.time() - self.settings.reasoning.lastPollTime |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
415 if ago > 2: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
416 self.set_status(500) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
417 self.finish("last poll was %s sec ago. last error: %s" % |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
418 (ago, self.settings.reasoning.lastError)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
419 return |
180 | 420 self.set_header("Content-Type", "text/html") |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
421 self.write(open('index.html').read()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
422 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
423 class ImmediateUpdate(cyclone.web.RequestHandler): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
424 def put(self): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
425 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
426 request an immediate load of the remote graphs; the thing we |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
427 do in the background anyway. No payload. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
428 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
429 Using PUT because this is idempotent and retryable and |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
430 everything. |
54
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
431 |
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
432 todo: this should do the right thing when many requests come |
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
433 in very quickly |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
434 """ |
49 | 435 print self.request.headers |
436 log.info("immediateUpdate from %s", | |
437 self.request.headers.get('User-Agent', '?')) | |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
438 r.poll() |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
439 self.set_status(202) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
440 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
441 def parseRdf(text, contentType): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
442 g = Graph() |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
443 g.parse(StringInputSource(text), format={ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
444 'text/n3': 'n3', |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
445 }[contentType]) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
446 return g |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
447 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
448 class OneShot(cyclone.web.RequestHandler): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
449 def post(self): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
450 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
451 payload is an rdf graph. The statements are momentarily added |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
452 to the input graph for exactly one update. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
453 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
454 todo: how do we go from a transition like doorclosed-to-open |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
455 to a oneshot event? the upstream shouldn't have to do it. Do |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
456 we make those oneshot events here? for every object change? |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
457 there are probably special cases regarding startup time when |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
458 everything appears to be a 'change'. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
459 """ |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
460 try: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
461 g = parseRdf(self.request.body, self.request.headers['content-type']) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
462 for s in g: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
463 print "stmt", s |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
464 if not len(g): |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
465 log.warn("incoming oneshot graph had no statements: %r", self.request.body) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
466 return |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
467 self.settings.reasoning.inputGraph.addOneShot(g) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
468 except Exception as e: |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
469 log.error(e) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
470 raise |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
471 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
472 # for reuse |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
473 class GraphResource(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
474 def get(self, which): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
475 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
476 r = self.settings.reasoning |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
477 g = {'lastInput': r.inputGraph.getGraph(), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
478 'lastOutput': r.inferred, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
479 }[which] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
480 self.write(self.jsonRdf(g)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
481 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
482 def jsonRdf(self, g): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
483 return json.dumps(sorted(list(g))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
484 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
485 class NtGraphs(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
486 """same as what gets posted above""" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
487 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
488 r = self.settings.reasoning |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
489 inputGraphNt = r.inputGraph.getGraph().serialize(format="nt") |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
490 inferredNt = r.inferred.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
491 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
492 self.write(json.dumps({"input": inputGraphNt, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
493 "inferred": inferredNt})) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
494 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
495 class Rules(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
496 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
497 self.set_header("Content-Type", "text/plain") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
498 self.write(self.settings.reasoning.rulesN3) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
499 |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
500 class Status(cyclone.web.RequestHandler): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
501 def get(self): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
502 self.set_header("Content-Type", "text/plain") |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
503 g = self.settings.reasoning.inputGraph.getGraph() |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
504 msg = "" |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
505 for badSource in g.subjects(RDF.type, ROOM['FailedGraphLoad']): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
506 msg += "GET %s failed (%s). " % ( |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
507 badSource, g.value(badSource, ROOM['graphLoadError'])) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
508 if not msg: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
509 self.finish("all inputs ok") |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
510 return |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
511 self.set_status(500) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
512 self.finish(msg) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
513 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
514 class Static(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
515 def get(self, p): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
516 self.write(open(p).read()) |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
517 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
518 liveClients = set() |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
519 def sendToLiveClients(d=None, asJson=None): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
520 j = asJson or json.dumps(d) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
521 for c in liveClients: |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
522 c.sendMessage(j) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
523 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
524 class Events(cyclone.websocket.WebSocketHandler): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
525 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
526 def connectionMade(self, *args, **kwargs): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
527 log.info("websocket opened") |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
528 liveClients.add(self) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
529 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
530 def connectionLost(self, reason): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
531 log.info("websocket closed") |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
532 liveClients.remove(self) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
533 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
534 def messageReceived(self, message): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
535 log.info("got message %s" % message) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
536 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
537 class Application(cyclone.web.Application): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
538 def __init__(self, reasoning): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
539 handlers = [ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
540 (r"/", Index), |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
541 (r"/immediateUpdate", ImmediateUpdate), |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
542 (r"/oneShot", OneShot), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
543 (r'/(jquery.min.js)', Static), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
544 (r'/(lastInput|lastOutput)Graph', GraphResource), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
545 (r'/ntGraphs', NtGraphs), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
546 (r'/rules', Rules), |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
547 (r'/status', Status), |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
548 (r'/events', Events), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
549 ] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
550 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
551 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
552 if __name__ == '__main__': |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
553 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
554 arg = docopt(""" |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
555 Usage: reasoning.py [options] |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
556 |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
557 -v Verbose |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
558 """) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
559 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
560 r = Reasoning() |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
561 if arg['-v']: |
235 | 562 from colorlog import ColoredFormatter |
563 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s %(white)s%(message)s", | |
564 datefmt=None, | |
565 reset=True, | |
566 log_colors={ | |
567 'DEBUG': 'cyan', | |
568 'INFO': 'green', | |
569 'WARNING': 'yellow', | |
570 'ERROR': 'red', | |
571 'CRITICAL': 'red,bg_white', | |
572 }, | |
573 secondary_log_colors={}, | |
574 style='%' | |
575 )) | |
576 | |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
577 import twisted.python.log |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
578 twisted.python.log.startLogging(sys.stdout) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
579 log.setLevel(logging.DEBUG) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
580 outlog.setLevel(logging.DEBUG) |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
581 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
582 task.LoopingCall(r.poll).start(1.0) |
235 | 583 reactor.listenTCP(9071, Application(r), interface='::') |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
584 reactor.run() |