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