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