annotate service/reasoning/reasoning.py @ 280:c192d37b2bc8

lots of logging updates (patch may be imprecise) Ignore-this: cfe81299fe9a1e2b6b1140e9ec46aaf7
author drewp@bigasterisk.com
date Fri, 06 May 2016 17:32:41 -0700
parents d3733587e749
children 9728288c7f2f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 FuXi.Rete.RuleStore import N3RuleStore
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
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
26 from twisted.internet import reactor, task
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
235
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
30 from inference import infer
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
31 from actions import Actions
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents: 265
diff changeset
32 from inputgraph import InputGraph
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
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
38 sys.path.append('../../../ffg/ffg')
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
39 import evtiming
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
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
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
44
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
45 class Reasoning(object):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
46 def __init__(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
47 self.prevGraph = None
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
48 self.lastPollTime = 0
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
49 self.lastError = ""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
50
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
51 self.actions = Actions(sendToLiveClients)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
52
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
53 self.rulesN3 = "(not read yet)"
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
54 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
55
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
56 self.inputGraph = InputGraph([], self.graphChanged)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
57 self.inputGraph.updateFileData()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
58
252
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
59 @evtiming.serviceLevel.timed('readRules')
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
60 def readRules(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
61 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
62 self.ruleStore = N3RuleStore()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
63 self.ruleGraph = Graph(self.ruleStore)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
64 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
65
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
66 @inlineCallbacks
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
67 def poll(self):
252
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
68 t1 = time.time()
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
69 try:
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
70 yield self.inputGraph.updateRemoteData()
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
71 self.lastPollTime = time.time()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
72 except Exception, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
73 log.error(traceback.format_exc())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
74 self.lastError = str(e)
252
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
75 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
76
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
77 def updateRules(self):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
78 try:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
79 t1 = time.time()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
80 self.readRules()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
81 ruleParseTime = time.time() - t1
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
82 except ValueError:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
83 # 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
84 # you'll see the error too
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
85 self.inferred = Graph()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
86 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
87 Literal(traceback.format_exc())))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
88 raise
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
89 return [(ROOM['reasoner'], ROOM['ruleParseTime'],
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
90 Literal(ruleParseTime))]
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
91
252
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
92 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
93 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None):
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
94 log.info("----------------------")
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
95 log.info("graphChanged oneShot=%s", oneShot)
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
96 if oneShot:
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
97 for s in oneShotGraph:
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
98 log.debug("oneshot stmt %r", s)
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
99 t1 = time.time()
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
100 oldInferred = self.inferred
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
101 try:
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
102 ruleStmts = self.updateRules()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
103
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
104 g = inputGraph.getGraph()
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
105 self.inferred = self._makeInferred(g)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
106 [self.inferred.add(s) for s in ruleStmts]
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
107
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
108 if oneShot:
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
109 # 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
110 # 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
111 # ones.
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
112 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
113 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
114
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
115 t2 = time.time()
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
116 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
117 putResultsTime = time.time() - t2
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
118 finally:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
119 if oneShot:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
120 self.inferred = oldInferred
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
121 log.info("graphChanged %.1f ms (putResults %.1f ms)" %
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
122 ((time.time() - t1) * 1000,
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
123 putResultsTime * 1000))
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
124
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
125 def _makeInferred(self, inputGraph):
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
126 t1 = time.time()
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
127 out = infer(inputGraph, self.ruleStore)
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
128 inferenceTime = time.time() - t1
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
129
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
130 out.add((ROOM['reasoner'], ROOM['inferenceTime'],
111
e4a4ee70a514 switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents: 77
diff changeset
131 Literal(inferenceTime)))
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
132 return out
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
133
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
134
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
135
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
136 class Index(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
137 def get(self):
252
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
138 print evtiming.serviceLevel.serviceJsonReport()
57f16890ab3a evtiming in reasoning service
drewp@bigasterisk.com
parents: 251
diff changeset
139
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
140 # 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
141 ago = time.time() - self.settings.reasoning.lastPollTime
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
142 if ago > 2:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
143 self.set_status(500)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
144 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
145 (ago, self.settings.reasoning.lastError))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
146 return
180
47682350e6f2 reasoning index page to html
drewp@bigasterisk.com
parents: 179
diff changeset
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):
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
151 @inlineCallbacks
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
152 def put(self):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
153 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
154 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
155 do in the background anyway. No payload.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
156
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
157 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
158 everything.
54
42411726a7ca screen out more values that change on every update
drewp@bigasterisk.com
parents: 49
diff changeset
159
42411726a7ca screen out more values that change on every update
drewp@bigasterisk.com
parents: 49
diff changeset
160 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
161 in very quickly
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
162 """
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
163 log.info("immediateUpdate from %s %s",
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
164 self.request.headers.get('User-Agent', '?'),
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
165 self.request.headers['Host'])
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
166 yield r.poll()
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
167 self.set_status(202)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
168
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
169 def parseRdf(text, contentType):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
170 g = Graph()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
171 g.parse(StringInputSource(text), format={
250
c1287ab87add fix oneshot. more time reportin
drewp@bigasterisk.com
parents: 249
diff changeset
172 'text/n3': 'n3',
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
173 }[contentType])
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
174 return g
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
175
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
176 class OneShot(cyclone.web.RequestHandler):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
177 def post(self):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
178 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
179 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
180 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
181
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
182 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
183 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
184 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
185 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
186 everything appears to be a 'change'.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
187 """
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
188 try:
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
189 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
190 for s in g:
251
254df9f881a6 start sending oneshot events from some devices
drewp@bigasterisk.com
parents: 250
diff changeset
191 log.debug("oneshot stmt %r", s)
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
192 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
193 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
194 return
250
c1287ab87add fix oneshot. more time reportin
drewp@bigasterisk.com
parents: 249
diff changeset
195 t1 = time.time()
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
196 self.settings.reasoning.inputGraph.addOneShot(g)
250
c1287ab87add fix oneshot. more time reportin
drewp@bigasterisk.com
parents: 249
diff changeset
197 self.set_header('x-graph-ms', str(1000 * (time.time() - t1)))
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
198 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
199 log.error(e)
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
200 raise
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
201
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
202 # for reuse
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
203 class GraphResource(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
204 def get(self, which):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
205 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
206 r = self.settings.reasoning
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
207 g = {'lastInput': r.inputGraph.getGraph(),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
208 'lastOutput': r.inferred,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
209 }[which]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
210 self.write(self.jsonRdf(g))
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 def jsonRdf(self, g):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
213 return json.dumps(sorted(list(g)))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
214
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
215 class NtGraphs(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
216 """same as what gets posted above"""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
217 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
218 r = self.settings.reasoning
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
219 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
220 inferredNt = r.inferred.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
221 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
222 self.write(json.dumps({"input": inputGraphNt,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
223 "inferred": inferredNt}))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
224
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
225 class Rules(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
226 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
227 self.set_header("Content-Type", "text/plain")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
228 self.write(self.settings.reasoning.rulesN3)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
229
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
230 class Status(cyclone.web.RequestHandler):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
231 def get(self):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
232 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
233 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
234 msg = ""
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
235 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
236 msg += "GET %s failed (%s). " % (
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
237 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
238 if not msg:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
239 self.finish("all inputs ok")
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
240 return
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
241 self.set_status(500)
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
242 self.finish(msg)
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
243
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
244 class Static(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
245 def get(self, p):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
246 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
247
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
248 liveClients = set()
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
249 def sendToLiveClients(d=None, asJson=None):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
250 j = asJson or json.dumps(d)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
251 for c in liveClients:
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
252 c.sendMessage(j)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
253
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
254 class Events(cyclone.websocket.WebSocketHandler):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
255
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
256 def connectionMade(self, *args, **kwargs):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
257 log.info("websocket opened")
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
258 liveClients.add(self)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
259
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
260 def connectionLost(self, reason):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
261 log.info("websocket closed")
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
262 liveClients.remove(self)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
263
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
264 def messageReceived(self, message):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
265 log.info("got message %s" % message)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
266
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
267 class Application(cyclone.web.Application):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
268 def __init__(self, reasoning):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
269 handlers = [
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
270 (r"/", Index),
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
271 (r"/immediateUpdate", ImmediateUpdate),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
272 (r"/oneShot", OneShot),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
273 (r'/(jquery.min.js)', Static),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
274 (r'/(lastInput|lastOutput)Graph', GraphResource),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
275 (r'/ntGraphs', NtGraphs),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
276 (r'/rules', Rules),
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
277 (r'/status', Status),
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
278 (r'/events', Events),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
279 ]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
280 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
281
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
282 def configLogging(arg):
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
283 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
284
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
285 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
286 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
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
287 datefmt=None,
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
288 reset=True,
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
289 log_colors={
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
290 'DEBUG': 'cyan',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
291 'INFO': 'green',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
292 'WARNING': 'yellow',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
293 'ERROR': 'red',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
294 'CRITICAL': 'red,bg_white',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
295 },
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
296 secondary_log_colors={},
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
297 style='%'
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
298 ))
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
299
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
300 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
301 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
302 twisted.python.log.startLogging(sys.stdout)
280
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 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
305 log.setLevel(DEBUG if arg['-r'] else WARN)
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
306 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
307
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
308
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
309 if __name__ == '__main__':
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
310 arg = docopt("""
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
311 Usage: reasoning.py [options]
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
312
280
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
313 -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
314 -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
315 -o Verbose log on the actions/output phase
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
316 --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
317 """)
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
318
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
319 r = Reasoning()
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
320 configLogging(arg)
c192d37b2bc8 lots of logging updates (patch may be imprecise)
drewp@bigasterisk.com
parents: 275
diff changeset
321 task.LoopingCall(r.poll).start(1.0 if not arg['-i'] else 10)
235
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
322 reactor.listenTCP(9071, Application(r), interface='::')
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
323 reactor.run()