annotate service/reasoning/inputgraph.py @ 392:79d041273e26

mqtt has two devices now. various older cleanups. Ignore-this: 67ca3acc5dc6aa672d0c896c9f5ae48e
author drewp@bigasterisk.com
date Sat, 19 Jan 2019 12:08:59 -0800
parents 7716b1810d6c
children 1ceb26846eca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
1 import logging, time, sys
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
2
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
3 from rdflib import Graph, ConjunctiveGraph
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
4 from rdflib import Namespace, URIRef, Literal, RDF, RDFS
281
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
5 from rdflib.parser import StringInputSource
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
6
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
7 from twisted.python.filepath import FilePath
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
8 from twisted.internet.defer import inlineCallbacks, gatherResults
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
9
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
10 from rdflibtrig import addTrig
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
11 from graphop import graphEqual
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
12 from greplin import scales
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
13
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
14 from patchsource import ReconnectingPatchSource
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
15
351
7716b1810d6c reasoning & collector move into docker images
drewp@bigasterisk.com
parents: 314
diff changeset
16 sys.path.append("/my/proj/rdfdb")
7716b1810d6c reasoning & collector move into docker images
drewp@bigasterisk.com
parents: 314
diff changeset
17 from rdfdb.rdflibpatch import patchQuads
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
18
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
19 log = logging.getLogger('fetch')
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
20
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
21 ROOM = Namespace("http://projects.bigasterisk.com/room/")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
22 DEV = Namespace("http://projects.bigasterisk.com/device/")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
23
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
24
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
25 STATS = scales.collection('/web',
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
26 scales.PmfStat('combineGraph'),
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
27 )
281
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
28 def parseRdf(text, contentType):
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
29 g = Graph()
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
30 g.parse(StringInputSource(text), format={
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
31 'text/n3': 'n3',
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
32 }[contentType])
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
33 return g
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
34
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
35
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
36 class RemoteData(object):
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
37 def __init__(self, onChange):
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
38 self.onChange = onChange
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
39 self.graph = ConjunctiveGraph()
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
40 self.patchSource = ReconnectingPatchSource(
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
41 URIRef('http://bang:9072/graph/home'),
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
42 #URIRef('http://frontdoor:10012/graph/events'),
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
43 self.onPatch, reconnectSecs=10)
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
44
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
45 def onPatch(self, p, fullGraph):
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
46 if fullGraph:
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
47 self.graph = ConjunctiveGraph()
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
48 patchQuads(self.graph,
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
49 deleteQuads=p.delQuads,
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
50 addQuads=p.addQuads,
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
51 perfect=True)
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
52
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
53 ignorePredicates = [
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
54 ROOM['signalStrength'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
55 # perhaps anything with a number-datatype for its
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
56 # object should be filtered out, and you have to make
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
57 # an upstream quantization (e.g. 'temp high'/'temp
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
58 # low') if you want to do reasoning on the difference
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
59 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"),
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
60 URIRef("http://bigasterisk.com/map#lastSeenAgo"),
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
61 ROOM['usingPower'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
62 ROOM['idleTimeMinutes'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
63 ROOM['idleTimeMs'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
64 ROOM['graphLoadMs'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
65 ROOM['localTimeToSecond'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
66 ROOM['history'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
67 ROOM['connectedAgo'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
68 RDFS['comment'],
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
69 ]
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
70 ignoreContexts = [
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
71 URIRef('http://bigasterisk.com/sse_collector/'),
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
72 ]
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
73 for affected in p.addQuads + p.delQuads:
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
74 if (affected[1] not in ignorePredicates and
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
75 affected[3] not in ignoreContexts):
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
76 log.debug(" remote graph changed")
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
77 self.onChange()
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
78 break
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
79 else:
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
80 log.debug(" remote graph has no changes to trigger rules")
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
81
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
82 class InputGraph(object):
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
83 def __init__(self, inputDirs, onChange):
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
84 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
85 this has one Graph that's made of:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
86 - all .n3 files from inputDirs (read at startup)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
87 - all the remote graphs, specified in the file graphs
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
88
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
89 call updateFileData or updateRemoteData to reread those
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
90 graphs. getGraph to access the combined graph.
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
91
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
92 onChange(self) is called if the contents of the full graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
93 change (in an interesting way) during updateFileData or
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
94 updateRemoteData. Interesting means statements other than the
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
95 ones with the predicates on the boring list. onChange(self,
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
96 oneShot=True) means: don't store the result of this change
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
97 anywhere; it needs to be processed only once
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
98 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
99 self.inputDirs = inputDirs
312
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
100 self._onChange = onChange
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
101 self._fileGraph = Graph()
312
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
102 self._remoteData = RemoteData(lambda: self.onChangeLocal())
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
103 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
104 self._oneShotAdditionGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
105
312
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
106 def onChangeLocal(self, oneShot=False, oneShotGraph=None):
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
107 self._combinedGraph = None
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
108 self._onChange(self, oneShot=oneShot, oneShotGraph=oneShotGraph)
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
109
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
110 def updateFileData(self):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
111 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
112 make sure we contain the correct data from the files in inputDirs
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
113 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
114 # this sample one is actually only needed for the output, but I don't
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
115 # think I want to have a separate graph for the output
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
116 # handling
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
117 log.debug("read file graphs")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
118 for fp in FilePath("input").walk():
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
119 if fp.isdir():
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
120 continue
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
121 if fp.splitext()[1] != '.n3':
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
122 continue
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
123 log.debug("read %s", fp)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
124 # todo: if this fails, leave the report in the graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
125 self._fileGraph.parse(fp.open(), format="n3")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
126 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
127
312
170dc9b1e789 fix input graph web display by dirtying combinedGraph better.
drewp@bigasterisk.com
parents: 303
diff changeset
128 self.onChangeLocal()
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
129
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
130 def addOneShot(self, g):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
131 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
132 add this graph to the total, call onChange, and then revert
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
133 the addition of this graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
134 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
135 self._oneShotAdditionGraph = g
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
136 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
137 try:
314
c51075f267bc fix oneshot
drewp@bigasterisk.com
parents: 312
diff changeset
138 self.onChangeLocal(oneShot=True, oneShotGraph=g)
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
139 finally:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
140 self._oneShotAdditionGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
141 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
142
281
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
143 def addOneShotFromString(self, body, contentType):
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
144 g = parseRdf(body, contentType)
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
145 if not len(g):
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
146 log.warn("incoming oneshot graph had no statements: %r", body)
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
147 return 0
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
148 t1 = time.time()
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
149 self.addOneShot(g)
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
150 return time.time() - t1
392
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
151
79d041273e26 mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents: 351
diff changeset
152 @STATS.combineGraph.time()
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
153 def getGraph(self):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
154 """rdflib Graph with the file+remote contents of the input graph"""
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
155 # this could be much faster with the combined readonly graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
156 # view from rdflib
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
157 if self._combinedGraph is None:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
158 self._combinedGraph = Graph()
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
159 if self._fileGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
160 for s in self._fileGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
161 self._combinedGraph.add(s)
303
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
162 for s in self._remoteData.graph:
66fe7a93753d reasoning uses sse_collector
drewp@bigasterisk.com
parents: 281
diff changeset
163 self._combinedGraph.add(s)
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
164 if self._oneShotAdditionGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
165 for s in self._oneShotAdditionGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
166 self._combinedGraph.add(s)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
167
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
168 return self._combinedGraph