Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 608:62171aa2bc4e
mock output mode
Ignore-this: a7b9dd184a2f3c811cd7c1d3ca4a002c
author | drewp@bigasterisk.com |
---|---|
date | Wed, 24 Jul 2019 00:34:41 -0700 |
parents | a34dee00a989 |
children | 5290df01d911 |
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 """ |
303 | 17 from crochet import no_setup |
18 no_setup() | |
19 | |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
20 import json, time, traceback, sys |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
21 from logging import getLogger, DEBUG, WARN |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
22 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
23 from colorlog import ColoredFormatter |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
24 from docopt import docopt |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
25 from rdflib import Namespace, Literal, RDF, Graph |
463
1ceb26846eca
add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents:
392
diff
changeset
|
26 from twisted.internet import reactor, task, defer |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
27 from twisted.internet.defer import inlineCallbacks |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
28 import cyclone.web, cyclone.websocket |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
29 |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
30 from greplin import scales |
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
31 from greplin.scales.cyclonehandler import StatsHandler |
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
32 |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
33 from inference import infer, readRules |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
34 from actions import Actions |
275 | 35 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
|
36 from escapeoutputstatements import unquoteOutputStatements |
600 | 37 |
38 from standardservice.logsetup import log, verboseLogging | |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
39 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
40 |
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 |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
46 STATS = scales.collection('/web', |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
47 scales.PmfStat('graphChanged'), |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
48 scales.PmfStat('updateRules'), |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
49 ) |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
50 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
51 class Reasoning(object): |
608 | 52 def __init__(self, mockOutput=False): |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
53 self.prevGraph = None |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
54 |
608 | 55 self.actions = Actions(sendToLiveClients, mockOutput=mockOutput) |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
56 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
57 self.rulesN3 = "(not read yet)" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
58 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
|
59 |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
60 self.inputGraph = InputGraph([], self.graphChanged) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
61 self.inputGraph.updateFileData() |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
62 |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
63 @STATS.updateRules.time() |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
64 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
|
65 rulesPath = 'rules.n3' |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
66 try: |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
67 t1 = time.time() |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
68 self.rulesN3, self.ruleStore = readRules( |
284
95f72a22965d
rules become simple-looking again; fix the ambiguity in memory after loading them.
drewp@bigasterisk.com
parents:
283
diff
changeset
|
69 rulesPath, outputPatterns=[ |
286 | 70 # Incomplete. See escapeoutputstatements.py for |
71 # explanation. | |
72 (None, ROOM['brightness'], None), | |
73 (None, ROOM['playState'], None), | |
74 (None, ROOM['powerState'], None), | |
75 (None, ROOM['state'], None), | |
76 ]) | |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
77 ruleParseTime = time.time() - t1 |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
78 except ValueError: |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
79 # 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
|
80 # you'll see the error too |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
81 self.inferred = Graph() |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
82 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
|
83 Literal(traceback.format_exc()))) |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
84 raise |
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
85 return [(ROOM['reasoner'], ROOM['ruleParseTime'], |
281 | 86 Literal(ruleParseTime))], ruleParseTime |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
87 |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
88 @STATS.graphChanged.time() |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
89 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): |
285 | 90 """ |
91 If we're getting called for a oneShot event, the oneShotGraph | |
92 statements are already in inputGraph.getGraph(). | |
93 """ | |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
94 log.info("----------------------") |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
95 log.info("graphChanged (oneShot=%s %s stmts):", |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
96 oneShot, len(oneShotGraph) if oneShotGraph is not None else 0) |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
97 if oneShotGraph: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
98 for stmt in oneShotGraph: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
99 log.info(" OS-> %r", stmt) |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
100 t1 = time.time() |
49 | 101 oldInferred = self.inferred |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
102 try: |
281 | 103 ruleStatStmts, ruleParseSec = self.updateRules() |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
104 |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
105 self.inferred, inferSec = self._makeInferred(inputGraph.getGraph()) |
283
0b0fb67b0b3a
fix input/output statement ambiguity problem by allowing quoted output statements
drewp@bigasterisk.com
parents:
282
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 += unquoteOutputStatements(self.inferred) |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
108 |
285 | 109 self.inferred += ruleStatStmts |
49 | 110 |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
111 if oneShot: |
285 | 112 # It's possible a oneShotGraph statement didn't |
113 # trigger a rule to do something, but was itself the | |
114 # output statement. Probably we could just mix in the | |
115 # whole inputGraph here and not special-case the | |
116 # oneShotGraph. | |
117 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
|
118 |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
119 t3 = time.time() |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
120 self.actions.putResults(self.inputGraph.getGraph(), self.inferred) |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
121 putResultsTime = time.time() - t3 |
49 | 122 finally: |
123 if oneShot: | |
124 self.inferred = oldInferred | |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
125 log.info("graphChanged took %.1f ms (rule parse %.1f ms, infer %.1f ms, putResults %.1f ms)" % |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
126 ((time.time() - t1) * 1000, |
281 | 127 ruleParseSec * 1000, |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
128 inferSec * 1000, |
240
0c306e76d8c5
ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents:
235
diff
changeset
|
129 putResultsTime * 1000)) |
49 | 130 |
131 def _makeInferred(self, inputGraph): | |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
132 t1 = time.time() |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
133 |
49 | 134 out = infer(inputGraph, self.ruleStore) |
281 | 135 for p, n in NS.iteritems(): |
136 out.bind(p, n, override=True) | |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
137 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
138 inferenceTime = time.time() - t1 |
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))) |
288
e03696277b32
logging and cleanups. Upgrade fuxi to have the cPickle optimization.
drewp@bigasterisk.com
parents:
287
diff
changeset
|
141 return out, inferenceTime |
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 |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
144 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
145 class Index(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
146 def get(self): |
180 | 147 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
|
148 self.write(open('index.html').read()) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
149 |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
150 class ImmediateUpdate(cyclone.web.RequestHandler): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
151 def put(self): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
152 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
153 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
|
154 do in the background anyway. No payload. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
155 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
156 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
|
157 everything. |
54
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
158 |
42411726a7ca
screen out more values that change on every update
drewp@bigasterisk.com
parents:
49
diff
changeset
|
159 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
|
160 in very quickly |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
161 """ |
303 | 162 log.warn("immediateUpdate from %s %s - ignored", |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
163 self.request.headers.get('User-Agent', '?'), |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
164 self.request.headers['Host']) |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
165 self.set_status(202) |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
166 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
167 class OneShot(cyclone.web.RequestHandler): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
168 def post(self): |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
169 """ |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
170 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
|
171 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
|
172 |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 everything appears to be a 'change'. |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
178 """ |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
179 try: |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
351
diff
changeset
|
180 log.info('POST to oneShot, headers=%s', self.request.headers) |
281 | 181 dt = self.settings.reasoning.inputGraph.addOneShotFromString( |
182 self.request.body, self.request.headers['content-type']) | |
183 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
|
184 except Exception as e: |
315 | 185 traceback.print_exc() |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
186 log.error(e) |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
187 raise |
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
129
diff
changeset
|
188 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
189 # for reuse |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
190 class GraphResource(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
191 def get(self, which): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
192 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
193 r = self.settings.reasoning |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
194 g = {'lastInput': r.inputGraph.getGraph(), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
195 'lastOutput': r.inferred, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
196 }[which] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
197 self.write(self.jsonRdf(g)) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
198 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
199 def jsonRdf(self, g): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
200 return json.dumps(sorted(list(g))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
201 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
202 class NtGraphs(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
203 """same as what gets posted above""" |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
204 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
205 r = self.settings.reasoning |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
206 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
|
207 inferredNt = r.inferred.serialize(format="nt") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
208 self.set_header("Content-Type", "application/json") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
209 self.write(json.dumps({"input": inputGraphNt, |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
210 "inferred": inferredNt})) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
211 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
212 class Rules(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
213 def get(self): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
214 self.set_header("Content-Type", "text/plain") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
215 self.write(self.settings.reasoning.rulesN3) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
216 |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
217 class Status(cyclone.web.RequestHandler): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
218 def get(self): |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
219 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
|
220 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
|
221 msg = "" |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
222 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
|
223 msg += "GET %s failed (%s). " % ( |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
224 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
|
225 if not msg: |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
226 self.finish("all inputs ok") |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
227 return |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
228 self.set_status(500) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
229 self.finish(msg) |
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
230 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
231 class Static(cyclone.web.RequestHandler): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
232 def get(self, p): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
233 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
|
234 |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
235 liveClients = set() |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
236 def sendToLiveClients(d=None, asJson=None): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
237 j = asJson or json.dumps(d) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
238 for c in liveClients: |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
239 c.sendMessage(j) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
240 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
241 class Events(cyclone.websocket.WebSocketHandler): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
242 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
243 def connectionMade(self, *args, **kwargs): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
244 log.info("websocket opened") |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
245 liveClients.add(self) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
246 |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
247 def connectionLost(self, reason): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
248 log.info("websocket closed") |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
249 liveClients.remove(self) |
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 messageReceived(self, message): |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
252 log.info("got message %s" % message) |
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
253 |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
254 class Application(cyclone.web.Application): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
255 def __init__(self, reasoning): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
256 handlers = [ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
257 (r"/", Index), |
47
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
258 (r"/immediateUpdate", ImmediateUpdate), |
0448fbd96a31
scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents:
46
diff
changeset
|
259 (r"/oneShot", OneShot), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
260 (r'/(jquery.min.js)', Static), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
261 (r'/(lastInput|lastOutput)Graph', GraphResource), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
262 (r'/ntGraphs', NtGraphs), |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
263 (r'/rules', Rules), |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
264 (r'/status', Status), |
64
e573af8c2428
reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents:
54
diff
changeset
|
265 (r'/events', Events), |
287
3b61c0dfaaef
switch from evtiming to greplin.scales. Optimize rules reader to reuse previous data (400ms -> 0.6ms)
drewp@bigasterisk.com
parents:
286
diff
changeset
|
266 (r'/stats/(.*)', StatsHandler, {'serverName': 'reasoning'}), |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
267 ] |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
268 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
|
269 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
270 def configLogging(arg): |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
271 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
|
272 |
600 | 273 if arg['-i'] or arg['-r'] or arg['-o'] or arg['-v']: |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
274 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 | 275 datefmt=None, |
276 reset=True, | |
277 log_colors={ | |
278 'DEBUG': 'cyan', | |
279 'INFO': 'green', | |
280 'WARNING': 'yellow', | |
281 'ERROR': 'red', | |
282 'CRITICAL': 'red,bg_white', | |
283 }, | |
284 secondary_log_colors={}, | |
285 style='%' | |
286 )) | |
463
1ceb26846eca
add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents:
392
diff
changeset
|
287 defer.setDebugging(True) |
1ceb26846eca
add separate :matchPredicate support. some build and log edits.
drewp@bigasterisk.com
parents:
392
diff
changeset
|
288 |
235 | 289 |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
290 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
|
291 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
|
292 twisted.python.log.startLogging(sys.stdout) |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
293 |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
294 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
|
295 log.setLevel(DEBUG if arg['-r'] else WARN) |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
296 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
|
297 |
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 if __name__ == '__main__': |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
300 arg = docopt(""" |
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
301 Usage: reasoning.py [options] |
45
5b0f970e3d52
/status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents:
33
diff
changeset
|
302 |
303 | 303 -i Verbose log on the input phase |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
304 -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
|
305 -o Verbose log on the actions/output phase |
600 | 306 -v Everywhere verbose |
608 | 307 --mockoutput Don't make outgoing requests |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
308 """) |
608 | 309 |
310 r = Reasoning(arg['--mockoutput']) | |
280
c192d37b2bc8
lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents:
275
diff
changeset
|
311 configLogging(arg) |
235 | 312 reactor.listenTCP(9071, Application(r), interface='::') |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
313 reactor.run() |