Mercurial > code > home > repos > homeauto
annotate service/reasoning/inputgraph.py @ 1562:c2ed44ed1e3c dependabot/pip/service/collector/twisted-19.7.0
Bump twisted from 19.2.0 to 19.7.0 in /service/collector
Bumps [twisted](https://github.com/twisted/twisted) from 19.2.0 to 19.7.0.
- [Release notes](https://github.com/twisted/twisted/releases)
- [Changelog](https://github.com/twisted/twisted/blob/trunk/NEWS.rst)
- [Commits](https://github.com/twisted/twisted/compare/twisted-19.2.0...twisted-19.7.0)
Signed-off-by: dependabot[bot] <support@github.com>
author | dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> |
---|---|
date | Fri, 14 Feb 2020 10:01:26 +0000 |
parents | d36d3b9ae516 |
children | c8562ace4917 |
rev | line source |
---|---|
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
1 import logging, time |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
2 import weakref |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
3 from typing import Callable |
1080 | 4 |
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
5 from greplin import scales |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
6 from rdflib import Graph, ConjunctiveGraph |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
7 from rdflib import Namespace, URIRef, RDFS |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
8 from rdflib.parser import StringInputSource |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
9 from rx.subjects import BehaviorSubject |
1080 | 10 from twisted.python.filepath import FilePath |
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
11 from twisted.internet import reactor |
1080 | 12 |
1374
ac127fdace62
build/import update for reasoning service
drewp <drewp@bigasterisk.com>
parents:
1266
diff
changeset
|
13 from patchablegraph.patchsource import ReconnectingPatchSource |
1156
ee168d55524a
reasoning & collector move into docker images
drewp <drewp@bigasterisk.com>
parents:
1119
diff
changeset
|
14 from rdfdb.rdflibpatch import patchQuads |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
15 from rdfdb.patch import Patch |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
16 |
1080 | 17 log = logging.getLogger('fetch') |
18 | |
19 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
20 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
21 | |
22 | |
1197
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
23 STATS = scales.collection('/web', |
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
24 scales.PmfStat('combineGraph'), |
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
25 ) |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
26 |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
27 |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
28 def parseRdf(text: str, contentType: str): |
1086 | 29 g = Graph() |
30 g.parse(StringInputSource(text), format={ | |
31 'text/n3': 'n3', | |
32 }[contentType]) | |
33 return g | |
34 | |
35 | |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
36 class RemoteData(object): |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
37 def __init__(self, onChange: Callable[[], None]): |
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
38 """we won't fire onChange during init""" |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
39 self.onChange = onChange |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
40 self.graph = ConjunctiveGraph() |
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
41 reactor.callLater(0, self._finishInit) |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
42 |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
43 def _finishInit(self): |
1197
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
44 self.patchSource = ReconnectingPatchSource( |
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
45 URIRef('http://bang:9072/graph/home'), |
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
46 #URIRef('http://frontdoor:10012/graph/events'), |
1266
ee5698ef64dd
add separate :matchPredicate support. some build and log edits.
drewp <drewp@bigasterisk.com>
parents:
1197
diff
changeset
|
47 self.onPatch, reconnectSecs=10, agent='reasoning') |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
48 |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
49 def onPatch(self, p: Patch, fullGraph: bool): |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
50 if fullGraph: |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
51 self.graph = ConjunctiveGraph() |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
52 patchQuads(self.graph, |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
53 deleteQuads=p.delQuads, |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
54 addQuads=p.addQuads, |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
55 perfect=True) |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
56 |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
57 ignorePredicates = [ |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
58 ROOM['signalStrength'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
59 # perhaps anything with a number-datatype for its |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
60 # object should be filtered out, and you have to make |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
61 # an upstream quantization (e.g. 'temp high'/'temp |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
62 # low') if you want to do reasoning on the difference |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
63 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"), |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
64 URIRef("http://bigasterisk.com/map#lastSeenAgo"), |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
65 ROOM['usingPower'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
66 ROOM['idleTimeMinutes'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
67 ROOM['idleTimeMs'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
68 ROOM['graphLoadMs'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
69 ROOM['localTimeToSecond'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
70 ROOM['history'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
71 ROOM['connectedAgo'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
72 RDFS['comment'], |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
73 ] |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
74 ignoreContexts = [ |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
75 URIRef('http://bigasterisk.com/sse_collector/'), |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
76 ] |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
77 for affected in p.addQuads + p.delQuads: |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
78 if (affected[1] not in ignorePredicates and |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
79 affected[3] not in ignoreContexts): |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
80 log.debug(" remote graph changed") |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
81 self.onChange() |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
82 break |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
83 else: |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
84 log.debug(" remote graph has no changes to trigger rules") |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
85 |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1555
diff
changeset
|
86 |
1080 | 87 class InputGraph(object): |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
88 def __init__(self, inputDirs, onChange): |
1080 | 89 """ |
90 this has one Graph that's made of: | |
91 - all .n3 files from inputDirs (read at startup) | |
92 - all the remote graphs, specified in the file graphs | |
93 | |
94 call updateFileData or updateRemoteData to reread those | |
95 graphs. getGraph to access the combined graph. | |
96 | |
97 onChange(self) is called if the contents of the full graph | |
98 change (in an interesting way) during updateFileData or | |
99 updateRemoteData. Interesting means statements other than the | |
100 ones with the predicates on the boring list. onChange(self, | |
101 oneShot=True) means: don't store the result of this change | |
102 anywhere; it needs to be processed only once | |
103 """ | |
104 self.inputDirs = inputDirs | |
1117
d5687ba23279
fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents:
1108
diff
changeset
|
105 self._onChange = onChange |
1080 | 106 self._fileGraph = Graph() |
1117
d5687ba23279
fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents:
1108
diff
changeset
|
107 self._remoteData = RemoteData(lambda: self.onChangeLocal()) |
1080 | 108 self._combinedGraph = None |
109 self._oneShotAdditionGraph = None | |
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
110 self._rxValues = weakref.WeakKeyDictionary() |
1080 | 111 |
1117
d5687ba23279
fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents:
1108
diff
changeset
|
112 def onChangeLocal(self, oneShot=False, oneShotGraph=None): |
d5687ba23279
fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents:
1108
diff
changeset
|
113 self._combinedGraph = None |
d5687ba23279
fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents:
1108
diff
changeset
|
114 self._onChange(self, oneShot=oneShot, oneShotGraph=oneShotGraph) |
1555
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
115 for rxv, (subj, pred, default) in self._rxValues.items(): |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
116 self._rxUpdate(subj, pred, default, rxv) |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
117 |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
118 def _rxUpdate(self, subj, pred, default, rxv): |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
119 rxv.on_next(self.getGraph().value(subj, pred, default=default)) |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
120 |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
121 def rxValue(self, subj, pred, default):# -> BehaviorSubject: |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
122 value = BehaviorSubject(default) |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
123 self._rxValues[value] = (subj, pred, default) |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
124 self._rxUpdate(subj, pred, default, value) |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
125 return value |
606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
drewp <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
126 |
1080 | 127 def updateFileData(self): |
128 """ | |
129 make sure we contain the correct data from the files in inputDirs | |
130 """ | |
131 # this sample one is actually only needed for the output, but I don't | |
132 # think I want to have a separate graph for the output | |
133 # handling | |
134 log.debug("read file graphs") | |
135 for fp in FilePath("input").walk(): | |
136 if fp.isdir(): | |
137 continue | |
138 if fp.splitext()[1] != '.n3': | |
139 continue | |
140 log.debug("read %s", fp) | |
141 # todo: if this fails, leave the report in the graph | |
142 self._fileGraph.parse(fp.open(), format="n3") | |
143 self._combinedGraph = None | |
144 | |
1117
d5687ba23279
fix input graph web display by dirtying combinedGraph better.
drewp <drewp@bigasterisk.com>
parents:
1108
diff
changeset
|
145 self.onChangeLocal() |
1080 | 146 |
147 def addOneShot(self, g): | |
148 """ | |
149 add this graph to the total, call onChange, and then revert | |
150 the addition of this graph | |
151 """ | |
152 self._oneShotAdditionGraph = g | |
153 self._combinedGraph = None | |
154 try: | |
1119 | 155 self.onChangeLocal(oneShot=True, oneShotGraph=g) |
1080 | 156 finally: |
157 self._oneShotAdditionGraph = None | |
158 self._combinedGraph = None | |
159 | |
1086 | 160 def addOneShotFromString(self, body, contentType): |
161 g = parseRdf(body, contentType) | |
162 if not len(g): | |
163 log.warn("incoming oneshot graph had no statements: %r", body) | |
164 return 0 | |
165 t1 = time.time() | |
166 self.addOneShot(g) | |
167 return time.time() - t1 | |
1197
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
168 |
d8acab2b01f5
mqtt has two devices now. various older cleanups.
drewp <drewp@bigasterisk.com>
parents:
1156
diff
changeset
|
169 @STATS.combineGraph.time() |
1080 | 170 def getGraph(self): |
171 """rdflib Graph with the file+remote contents of the input graph""" | |
172 # this could be much faster with the combined readonly graph | |
173 # view from rdflib | |
174 if self._combinedGraph is None: | |
175 self._combinedGraph = Graph() | |
176 if self._fileGraph: | |
177 for s in self._fileGraph: | |
178 self._combinedGraph.add(s) | |
1108
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
179 for s in self._remoteData.graph: |
8caf62030955
reasoning uses sse_collector
drewp <drewp@bigasterisk.com>
parents:
1086
diff
changeset
|
180 self._combinedGraph.add(s) |
1080 | 181 if self._oneShotAdditionGraph: |
182 for s in self._oneShotAdditionGraph: | |
183 self._combinedGraph.add(s) | |
184 | |
185 return self._combinedGraph |