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