Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 285:c7476e2387dc
minor cleanups
Ignore-this: 5f932640b1dcc45bbc50d9bca53451e6
author | drewp@bigasterisk.com |
---|---|
date | Sun, 08 May 2016 03:01:24 -0700 |
parents | 95f72a22965d |
children | da0b3a1394a3 |
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 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
19 import json, time, traceback, sys |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
20 from logging import getLogger, DEBUG, WARN |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
21 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
22 from colorlog import ColoredFormatter |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
23 from docopt import docopt |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
24 from rdflib import Namespace, Literal, RDF, Graph |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
25 from twisted.internet import reactor, task |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
26 from twisted.internet.defer import inlineCallbacks |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
27 import cyclone.web, cyclone.websocket |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
28 |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
29 from inference import infer, readRules |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
30 from actions import Actions |
275 | 31 from inputgraph import InputGraph |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
32 from escapeoutputstatements import unquoteOutputStatements |
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 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
36 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
37 |
252 | 38 sys.path.append('../../../ffg/ffg') |
39 import evtiming | |
40 | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
41 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
42 DEV = Namespace("http://projects.bigasterisk.com/device/") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
43 |
281 | 44 NS = {'': ROOM, 'dev': DEV} |
283
0b0fb67b0b3a
fix input/output statement ambiguity problem by allowing quoted output statements
drewp@bigasterisk.com
parents:
282
diff
changeset
|
45 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
46 class Reasoning(object): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
47 def __init__(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
48 self.prevGraph = None |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
49 self.lastPollTime = 0 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
50 self.lastError = "" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
51 |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
52 self.actions = Actions(sendToLiveClients) |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
53 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
54 self.rulesN3 = "(not read yet)" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
55 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
|
56 |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
57 self.inputGraph = InputGraph([], self.graphChanged) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
58 self.inputGraph.updateFileData() |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
59 |
249 | 60 @inlineCallbacks |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
61 def poll(self): |
252 | 62 t1 = time.time() |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
63 try: |
249 | 64 yield self.inputGraph.updateRemoteData() |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
65 self.lastPollTime = time.time() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
66 except Exception, e: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
67 log.error(traceback.format_exc()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
68 self.lastError = str(e) |
252 | 69 evtiming.serviceLevel.addData('poll', time.time() - t1) |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
70 |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
71 def updateRules(self): |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
72 rulesPath = 'rules.n3' |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
73 try: |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
74 t1 = time.time() |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
75 self.rulesN3, self.ruleGraph = readRules( |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
76 rulesPath, outputPatterns=[ |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
77 # incomplete |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
78 (None, ROOM['brightness'], None)]) |
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
79 self._readRules(rulesPath) |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
80 ruleParseTime = time.time() - t1 |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
81 except ValueError: |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
82 # this is so if you're just watching the inferred output, |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
83 # you'll see the error too |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
84 self.inferred = Graph() |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
85 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
86 Literal(traceback.format_exc()))) |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
87 raise |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
88 return [(ROOM['reasoner'], ROOM['ruleParseTime'], |
281 | 89 Literal(ruleParseTime))], ruleParseTime |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
90 |
252 | 91 evtiming.serviceLevel.timed('graphChanged') |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
92 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): |
285 | 93 """ |
94 If we're getting called for a oneShot event, the oneShotGraph | |
95 statements are already in inputGraph.getGraph(). | |
96 """ | |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
97 log.info("----------------------") |
285 | 98 log.info("graphChanged:") |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
99 if oneShot: |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
100 for s in oneShotGraph: |
285 | 101 log.debug(" oneshot stmt %r", s) |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
102 t1 = time.time() |
49 | 103 oldInferred = self.inferred |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
104 try: |
281 | 105 ruleStatStmts, ruleParseSec = self.updateRules() |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
106 |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
107 self.inferred = self._makeInferred(inputGraph.getGraph()) |
283
0b0fb67b0b3a
fix input/output statement ambiguity problem by allowing quoted output statements
drewp@bigasterisk.com
parents:
282
diff
changeset
|
108 |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
109 self.inferred += unquoteOutputStatements(self.inferred) |
283
0b0fb67b0b3a
fix input/output statement ambiguity problem by allowing quoted output statements
drewp@bigasterisk.com
parents:
282
diff
changeset
|
110 |
285 | 111 self.inferred += ruleStatStmts |
49 | 112 |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
113 if oneShot: |
285 | 114 # It's possible a oneShotGraph statement didn't |
115 # trigger a rule to do something, but was itself the | |
116 # output statement. Probably we could just mix in the | |
117 # whole inputGraph here and not special-case the | |
118 # oneShotGraph. | |
119 self.inferred += oneShotGraph | |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
120 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
121 t2 = time.time() |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
122 self.actions.putResults(self.inputGraph.getGraph(), self.inferred) |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
123 putResultsTime = time.time() - t2 |
49 | 124 finally: |
125 if oneShot: | |
126 self.inferred = oldInferred | |
281 | 127 log.info("graphChanged took %.1f ms (rule parse %.1f ms, putResults %.1f ms)" % |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
128 ((time.time() - t1) * 1000, |
281 | 129 ruleParseSec * 1000, |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
130 putResultsTime * 1000)) |
49 | 131 |
132 def _makeInferred(self, inputGraph): | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
133 t1 = time.time() |
49 | 134 out = infer(inputGraph, self.ruleStore) |
281 | 135 for p, n in NS.iteritems(): |
136 out.bind(p, n, override=True) | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
137 inferenceTime = time.time() - t1 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
138 |
49 | 139 out.add((ROOM['reasoner'], ROOM['inferenceTime'], |
111
e4a4ee70a514
switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents:
77
diff
changeset
|
140 Literal(inferenceTime))) |
49 | 141 return out |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
142 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
143 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
144 class Index(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
145 def get(self): |
252 | 146 print evtiming.serviceLevel.serviceJsonReport() |
147 | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
148 # 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
|
149 ago = time.time() - self.settings.reasoning.lastPollTime |
282
0498634eba17
don't break GET=/ page when we're in slow-updates debug mode
drewp@bigasterisk.com
parents:
281
diff
changeset
|
150 if ago > 15: |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
151 self.set_status(500) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
152 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
|
153 (ago, self.settings.reasoning.lastError)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
154 return |
180 | 155 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
|
156 self.write(open('index.html').read()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
157 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
158 class ImmediateUpdate(cyclone.web.RequestHandler): |
249 | 159 @inlineCallbacks |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
160 def put(self): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
161 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
162 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
|
163 do in the background anyway. No payload. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
164 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
165 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
|
166 everything. |
54
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
167 |
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
168 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
|
169 in very quickly |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
170 """ |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
171 log.info("immediateUpdate from %s %s", |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
172 self.request.headers.get('User-Agent', '?'), |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
173 self.request.headers['Host']) |
249 | 174 yield r.poll() |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
175 self.set_status(202) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
176 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
177 class OneShot(cyclone.web.RequestHandler): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
178 def post(self): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
179 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
180 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
|
181 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
|
182 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
183 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
|
184 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
|
185 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
|
186 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
|
187 everything appears to be a 'change'. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
188 """ |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
189 try: |
281 | 190 dt = self.settings.reasoning.inputGraph.addOneShotFromString( |
191 self.request.body, self.request.headers['content-type']) | |
192 self.set_header('x-graph-ms', str(1000 * dt)) | |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
193 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
|
194 log.error(e) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
195 raise |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
196 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
197 # for reuse |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
198 class GraphResource(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
199 def get(self, which): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
200 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
201 r = self.settings.reasoning |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
202 g = {'lastInput': r.inputGraph.getGraph(), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
203 'lastOutput': r.inferred, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
204 }[which] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
205 self.write(self.jsonRdf(g)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
206 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
207 def jsonRdf(self, g): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
208 return json.dumps(sorted(list(g))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
209 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
210 class NtGraphs(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
211 """same as what gets posted above""" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
212 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
213 r = self.settings.reasoning |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
214 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
|
215 inferredNt = r.inferred.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
216 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
217 self.write(json.dumps({"input": inputGraphNt, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
218 "inferred": inferredNt})) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
219 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
220 class Rules(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
221 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
222 self.set_header("Content-Type", "text/plain") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
223 self.write(self.settings.reasoning.rulesN3) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
224 |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
225 class Status(cyclone.web.RequestHandler): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
226 def get(self): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
227 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
|
228 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
|
229 msg = "" |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
230 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
|
231 msg += "GET %s failed (%s). " % ( |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
232 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
|
233 if not msg: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
234 self.finish("all inputs ok") |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
235 return |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
236 self.set_status(500) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
237 self.finish(msg) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
238 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
239 class Static(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
240 def get(self, p): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
241 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
|
242 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
243 liveClients = set() |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
244 def sendToLiveClients(d=None, asJson=None): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
245 j = asJson or json.dumps(d) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
246 for c in liveClients: |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
247 c.sendMessage(j) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
248 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
249 class Events(cyclone.websocket.WebSocketHandler): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
250 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
251 def connectionMade(self, *args, **kwargs): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
252 log.info("websocket opened") |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
253 liveClients.add(self) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
254 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
255 def connectionLost(self, reason): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
256 log.info("websocket closed") |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
257 liveClients.remove(self) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
258 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
259 def messageReceived(self, message): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
260 log.info("got message %s" % message) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
261 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
262 class Application(cyclone.web.Application): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
263 def __init__(self, reasoning): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
264 handlers = [ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
265 (r"/", Index), |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
266 (r"/immediateUpdate", ImmediateUpdate), |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
267 (r"/oneShot", OneShot), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
268 (r'/(jquery.min.js)', Static), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
269 (r'/(lastInput|lastOutput)Graph', GraphResource), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
270 (r'/ntGraphs', NtGraphs), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
271 (r'/rules', Rules), |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
272 (r'/status', Status), |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
273 (r'/events', Events), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
274 ] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
275 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
|
276 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
277 def configLogging(arg): |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
278 log.setLevel(WARN) |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
279 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
280 if arg['-i'] or arg['-r'] or arg['-o']: |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
281 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s %(name)-6s %(filename)-12s:%(lineno)-3s %(funcName)-20s%(reset)s %(white)s%(message)s", |
235 | 282 datefmt=None, |
283 reset=True, | |
284 log_colors={ | |
285 'DEBUG': 'cyan', | |
286 'INFO': 'green', | |
287 'WARNING': 'yellow', | |
288 'ERROR': 'red', | |
289 'CRITICAL': 'red,bg_white', | |
290 }, | |
291 secondary_log_colors={}, | |
292 style='%' | |
293 )) | |
294 | |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
295 if arg['-i']: |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
296 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
|
297 twisted.python.log.startLogging(sys.stdout) |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
298 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
299 getLogger('fetch').setLevel(DEBUG if arg['-i'] else WARN) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
300 log.setLevel(DEBUG if arg['-r'] else WARN) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
301 getLogger('output').setLevel(DEBUG if arg['-o'] else WARN) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
302 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
303 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
304 if __name__ == '__main__': |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
305 arg = docopt(""" |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
306 Usage: reasoning.py [options] |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
307 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
308 -i Verbose log on the input phase (and slow down the polling) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
309 -r Verbose log on the reasoning phase and web stuff |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
310 -o Verbose log on the actions/output phase |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
311 --source=<substr> Limit sources to those with this string. |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
312 """) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
313 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
314 r = Reasoning() |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
315 configLogging(arg) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
316 task.LoopingCall(r.poll).start(1.0 if not arg['-i'] else 10) |
235 | 317 reactor.listenTCP(9071, Application(r), interface='::') |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
318 reactor.run() |