annotate service/reasoning/reasoning.py @ 250:c1287ab87add

fix oneshot. more time reportin Ignore-this: c99ce7d8d31a8ed45bdcb8d0ad08a3aa
author drewp@bigasterisk.com
date Tue, 09 Feb 2016 22:10:38 -0800
parents e5c27d2f11ab
children 254df9f881a6
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
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
19 from twisted.internet import reactor, task
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
20 from twisted.internet.defer import inlineCallbacks, gatherResults
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
21 from twisted.python.filepath import FilePath
129
745eff67ad40 reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp@bigasterisk.com
parents: 118
diff changeset
22 import time, traceback, sys, json, logging, urllib
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
23 from rdflib import Graph, ConjunctiveGraph
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
24 from rdflib import Namespace, URIRef, Literal, RDF
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
25 from rdflib.parser import StringInputSource
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
26
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
27 import cyclone.web, cyclone.websocket
235
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
28 from inference import infer
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
29 from rdflibtrig import addTrig
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents: 47
diff changeset
30 from graphop import graphEqual
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
31 from docopt import docopt
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
32 from actions import Actions
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
33 from FuXi.Rete.RuleStore import N3RuleStore
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
34
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
35 sys.path.append("../../lib")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
36 from logsetup import log
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
37 log.setLevel(logging.WARN)
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
38 outlog = logging.getLogger('output')
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
39 outlog.setLevel(logging.WARN)
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
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
44
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
45 class InputGraph(object):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
46 def __init__(self, inputDirs, onChange):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
47 """
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
48 this has one Graph that's made of:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
49 - all .n3 files from inputDirs (read at startup)
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
50 - all the remote graphs, specified in the file graphs
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
51
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
52 call updateFileData or updateRemoteData to reread those
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
53 graphs. getGraph to access the combined graph.
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
54
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
55 onChange(self) is called if the contents of the full graph
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
56 change (in an interesting way) during updateFileData or
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
57 updateRemoteData. Interesting means statements other than the
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
58 ones with the predicates on the boring list. onChange(self,
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
59 oneShot=True) means: don't store the result of this change
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
60 anywhere; it needs to be processed only once
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
61 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
62 self.inputDirs = inputDirs
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
63 self.onChange = onChange
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
64 self._fileGraph = Graph()
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
65 self._remoteGraph = None
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
66 self._combinedGraph = None
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
67 self._oneShotAdditionGraph = None
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
68
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
69 def updateFileData(self):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
70 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
71 make sure we contain the correct data from the files in inputDirs
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
72 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
73 # this sample one is actually only needed for the output, but I don't
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
74 # think I want to have a separate graph for the output
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
75 # handling
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
76 log.debug("read file graphs")
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
77 for fp in FilePath("input").walk():
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
78 if fp.isdir():
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
79 continue
111
e4a4ee70a514 switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents: 77
diff changeset
80 if fp.splitext()[1] != '.n3':
e4a4ee70a514 switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents: 77
diff changeset
81 continue
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
82 log.debug("read %s", fp)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
83 # todo: if this fails, leave the report in the graph
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
84 self._fileGraph.parse(fp.open(), format="n3")
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
85 self._combinedGraph = None
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
86
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
87 self.onChange(self)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
88
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
89 @inlineCallbacks
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
90 def updateRemoteData(self):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
91 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
92 read all remote graphs (which are themselves enumerated within
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
93 the file data)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
94 """
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
95 t1 = time.time()
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
96 log.debug("read remote graphs")
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
97 g = ConjunctiveGraph()
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
98
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
99 @inlineCallbacks
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
100 def fetchOne(source):
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
101 try:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
102 # this part could be parallelized
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
103 fetchTime = yield addTrig(g, source)
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
104 except Exception, e:
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
105 log.error(" can't add source %s: %s", source, e)
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
106 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e))))
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
107 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad']))
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
108 else:
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
109 g.add((URIRef(source), ROOM['graphLoadMs'],
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
110 Literal(round(fetchTime * 1000, 1))))
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
111
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
112 fetchDone = []
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
113 for source in self._fileGraph.objects(ROOM['reasoning'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
114 ROOM['source']):
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
115 fetchDone.append(fetchOne(source))
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
116 yield gatherResults(fetchDone, consumeErrors=True)
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
117 log.debug("loaded all in %.1f ms", 1000 * (time.time() - t1))
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
118
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
119 prevGraph = self._remoteGraph
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
120 self._remoteGraph = g
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
121 self._combinedGraph = None
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
122 if (prevGraph is None or
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
123 not graphEqual(g, prevGraph, ignorePredicates=[
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
124 ROOM['signalStrength'],
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
125 # perhaps anything with a number-datatype for its
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
126 # object should be filtered out, and you have to make
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
127 # an upstream quantization (e.g. 'temp high'/'temp
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
128 # low') if you want to do reasoning on the difference
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
129 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
130 URIRef("http://bigasterisk.com/map#lastSeenAgo"),
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
131 ROOM['usingPower'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
132 ROOM['idleTimeMinutes'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
133 ROOM['idleTimeMs'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
134 ROOM['graphLoadMs'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
135 ROOM['localTimeToSecond'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
136 ROOM['history'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
137 ROOM['temperatureF'],
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
138 ROOM['connectedAgo'],
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
139 ])):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
140 log.debug(" remote graph changed")
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
141 self.onChange(self)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
142 else:
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
143 log.debug(" remote graph has no changes to trigger rules")
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
144
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
145 def addOneShot(self, g):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
146 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
147 add this graph to the total, call onChange, and then revert
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
148 the addition of this graph
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
149 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
150 self._oneShotAdditionGraph = g
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
151 self._combinedGraph = None
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
152 try:
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
153 self.onChange(self, oneShot=True, oneShotGraph=g)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
154 finally:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
155 self._oneShotAdditionGraph = None
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
156 self._combinedGraph = None
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
157
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
158 def getGraph(self):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
159 """rdflib Graph with the file+remote contents of the input graph"""
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
160 # this could be much faster with the combined readonly graph
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
161 # view from rdflib
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
162 if self._combinedGraph is None:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
163 self._combinedGraph = Graph()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
164 if self._fileGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
165 for s in self._fileGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
166 self._combinedGraph.add(s)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
167 if self._remoteGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
168 for s in self._remoteGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
169 self._combinedGraph.add(s)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
170 if self._oneShotAdditionGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
171 for s in self._oneShotAdditionGraph:
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
172 self._combinedGraph.add(s)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
173
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
174 return self._combinedGraph
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
175
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
176
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
177 class Reasoning(object):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
178 def __init__(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
179 self.prevGraph = None
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
180 self.lastPollTime = 0
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
181 self.lastError = ""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
182
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
183 self.actions = Actions(sendToLiveClients)
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
184
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
185 self.rulesN3 = "(not read yet)"
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
186 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
187
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
188 self.inputGraph = InputGraph([], self.graphChanged)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
189 self.inputGraph.updateFileData()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
190
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
191
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
192 def readRules(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
193 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
194 self.ruleStore = N3RuleStore()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
195 self.ruleGraph = Graph(self.ruleStore)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
196 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
197
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
198 @inlineCallbacks
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
199 def poll(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
200 try:
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
201 yield self.inputGraph.updateRemoteData()
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
202 self.lastPollTime = time.time()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
203 except Exception, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
204 log.error(traceback.format_exc())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
205 self.lastError = str(e)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
206
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
207 def updateRules(self):
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
208 try:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
209 t1 = time.time()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
210 self.readRules()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
211 ruleParseTime = time.time() - t1
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
212 except ValueError:
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
213 # 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
214 # you'll see the error too
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
215 self.inferred = Graph()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
216 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
217 Literal(traceback.format_exc())))
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
218 raise
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
219 return [(ROOM['reasoner'], ROOM['ruleParseTime'],
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
220 Literal(ruleParseTime))]
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
221
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
222 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None):
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
223 t1 = time.time()
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
224 oldInferred = self.inferred
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
225 try:
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
226 ruleStmts = self.updateRules()
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
227
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
228 g = inputGraph.getGraph()
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
229 self.inferred = self._makeInferred(g)
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
230 [self.inferred.add(s) for s in ruleStmts]
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
231
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
232 if oneShot:
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
233 # 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
234 # 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
235 # ones.
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
236 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
237 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
238
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
239 t2 = time.time()
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
240 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
241 putResultsTime = time.time() - t2
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
242 finally:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
243 if oneShot:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
244 self.inferred = oldInferred
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
245 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
246 ((time.time() - t1) * 1000,
240
0c306e76d8c5 ipv6 fetch support. refactor Actions to new class and file
drewp@bigasterisk.com
parents: 235
diff changeset
247 putResultsTime * 1000))
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
248
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
249 def _makeInferred(self, inputGraph):
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
250 t1 = time.time()
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
251 out = infer(inputGraph, self.ruleStore)
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
252 inferenceTime = time.time() - t1
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
253
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
254 out.add((ROOM['reasoner'], ROOM['inferenceTime'],
111
e4a4ee70a514 switch reasoning from restkit to async cyclone http client
drewp@bigasterisk.com
parents: 77
diff changeset
255 Literal(inferenceTime)))
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
256 return out
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
257
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
258
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
259
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
260 class Index(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
261 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
262 # 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
263 ago = time.time() - self.settings.reasoning.lastPollTime
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
264 if ago > 2:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
265 self.set_status(500)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
266 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
267 (ago, self.settings.reasoning.lastError))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
268 return
180
47682350e6f2 reasoning index page to html
drewp@bigasterisk.com
parents: 179
diff changeset
269 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
270 self.write(open('index.html').read())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
271
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
272 class ImmediateUpdate(cyclone.web.RequestHandler):
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
273 @inlineCallbacks
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
274 def put(self):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
275 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
276 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
277 do in the background anyway. No payload.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
278
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
279 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
280 everything.
54
42411726a7ca screen out more values that change on every update
drewp@bigasterisk.com
parents: 49
diff changeset
281
42411726a7ca screen out more values that change on every update
drewp@bigasterisk.com
parents: 49
diff changeset
282 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
283 in very quickly
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
284 """
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
285 print self.request.headers
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
286 log.info("immediateUpdate from %s",
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
287 self.request.headers.get('User-Agent', '?'))
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
288 yield r.poll()
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
289 self.set_status(202)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
290
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
291 def parseRdf(text, contentType):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
292 g = Graph()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
293 g.parse(StringInputSource(text), format={
250
c1287ab87add fix oneshot. more time reportin
drewp@bigasterisk.com
parents: 249
diff changeset
294 'text/n3': 'n3',
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
295 }[contentType])
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
296 return g
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
297
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
298 class OneShot(cyclone.web.RequestHandler):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
299 def post(self):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
300 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
301 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
302 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
303
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
304 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
305 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
306 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
307 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
308 everything appears to be a 'change'.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
309 """
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
310 try:
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
311 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
312 for s in g:
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
313 print "stmt", s
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
314 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
315 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
316 return
250
c1287ab87add fix oneshot. more time reportin
drewp@bigasterisk.com
parents: 249
diff changeset
317 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
318 self.settings.reasoning.inputGraph.addOneShot(g)
250
c1287ab87add fix oneshot. more time reportin
drewp@bigasterisk.com
parents: 249
diff changeset
319 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
320 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
321 log.error(e)
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
322 raise
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
323
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
324 # for reuse
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
325 class GraphResource(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
326 def get(self, which):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
327 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
328 r = self.settings.reasoning
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
329 g = {'lastInput': r.inputGraph.getGraph(),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
330 'lastOutput': r.inferred,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
331 }[which]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
332 self.write(self.jsonRdf(g))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
333
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
334 def jsonRdf(self, g):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
335 return json.dumps(sorted(list(g)))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
336
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
337 class NtGraphs(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
338 """same as what gets posted above"""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
339 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
340 r = self.settings.reasoning
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
341 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
342 inferredNt = r.inferred.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
343 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
344 self.write(json.dumps({"input": inputGraphNt,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
345 "inferred": inferredNt}))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
346
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
347 class Rules(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
348 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
349 self.set_header("Content-Type", "text/plain")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
350 self.write(self.settings.reasoning.rulesN3)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
351
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
352 class Status(cyclone.web.RequestHandler):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
353 def get(self):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
354 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
355 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
356 msg = ""
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
357 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
358 msg += "GET %s failed (%s). " % (
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
359 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
360 if not msg:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
361 self.finish("all inputs ok")
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
362 return
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
363 self.set_status(500)
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
364 self.finish(msg)
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
365
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
366 class Static(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
367 def get(self, p):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
368 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
369
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
370 liveClients = set()
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
371 def sendToLiveClients(d=None, asJson=None):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
372 j = asJson or json.dumps(d)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
373 for c in liveClients:
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
374 c.sendMessage(j)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
375
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
376 class Events(cyclone.websocket.WebSocketHandler):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
377
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
378 def connectionMade(self, *args, **kwargs):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
379 log.info("websocket opened")
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
380 liveClients.add(self)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
381
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
382 def connectionLost(self, reason):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
383 log.info("websocket closed")
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
384 liveClients.remove(self)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
385
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
386 def messageReceived(self, message):
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
387 log.info("got message %s" % message)
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
388
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
389 class Application(cyclone.web.Application):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
390 def __init__(self, reasoning):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
391 handlers = [
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
392 (r"/", Index),
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
393 (r"/immediateUpdate", ImmediateUpdate),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
394 (r"/oneShot", OneShot),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
395 (r'/(jquery.min.js)', Static),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
396 (r'/(lastInput|lastOutput)Graph', GraphResource),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
397 (r'/ntGraphs', NtGraphs),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
398 (r'/rules', Rules),
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
399 (r'/status', Status),
64
e573af8c2428 reasoning has a websocket server that broadcasts some events
drewp@bigasterisk.com
parents: 54
diff changeset
400 (r'/events', Events),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
401 ]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
402 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
403
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
404 if __name__ == '__main__':
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
405
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
406 arg = docopt("""
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
407 Usage: reasoning.py [options]
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
408
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
409 -v Verbose (and slow updates)
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
410 """)
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
411
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
412 r = Reasoning()
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
413 if arg['-v']:
235
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
414 from colorlog import ColoredFormatter
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
415 log.handlers[0].setFormatter(ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s %(white)s%(message)s",
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
416 datefmt=None,
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
417 reset=True,
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
418 log_colors={
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
419 'DEBUG': 'cyan',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
420 'INFO': 'green',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
421 'WARNING': 'yellow',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
422 'ERROR': 'red',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
423 'CRITICAL': 'red,bg_white',
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
424 },
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
425 secondary_log_colors={},
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
426 style='%'
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
427 ))
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
428
179
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
429 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
430 twisted.python.log.startLogging(sys.stdout)
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
431 log.setLevel(logging.DEBUG)
0f2ac014d9ea reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents: 129
diff changeset
432 outlog.setLevel(logging.DEBUG)
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
433
249
e5c27d2f11ab fetch all source graphs in parallel
drewp@bigasterisk.com
parents: 240
diff changeset
434 task.LoopingCall(r.poll).start(1.0 if not arg['-v'] else 10)
235
5ad229334a88 rdflib can parse trig now
drewp@bigasterisk.com
parents: 180
diff changeset
435 reactor.listenTCP(9071, Application(r), interface='::')
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
436 reactor.run()