annotate service/reasoning/inputgraph.py @ 281:9728288c7f2f

refactor Ignore-this: 61a7c93a71fba5feea956ef99c84a30e
author drewp@bigasterisk.com
date Fri, 06 May 2016 17:34:26 -0700
parents d3733587e749
children 66fe7a93753d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
1 import logging, time
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
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
4 from rdflib import Namespace, URIRef, Literal, RDF
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
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
12
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
13 log = logging.getLogger('fetch')
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
14
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
15 ROOM = Namespace("http://projects.bigasterisk.com/room/")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
16 DEV = Namespace("http://projects.bigasterisk.com/device/")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
17
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
18
281
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
19 def parseRdf(text, contentType):
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
20 g = Graph()
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
21 g.parse(StringInputSource(text), format={
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
22 'text/n3': 'n3',
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
23 }[contentType])
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
24 return g
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
25
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
26
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
27 class InputGraph(object):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
28 def __init__(self, inputDirs, onChange, sourceSubstr=None):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
29 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
30 this has one Graph that's made of:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
31 - all .n3 files from inputDirs (read at startup)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
32 - all the remote graphs, specified in the file graphs
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
33
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
34 call updateFileData or updateRemoteData to reread those
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
35 graphs. getGraph to access the combined graph.
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
36
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
37 onChange(self) is called if the contents of the full graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
38 change (in an interesting way) during updateFileData or
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
39 updateRemoteData. Interesting means statements other than the
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
40 ones with the predicates on the boring list. onChange(self,
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
41 oneShot=True) means: don't store the result of this change
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
42 anywhere; it needs to be processed only once
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
43
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
44 sourceSubstr filters to only pull from sources containing the
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
45 string (for debugging).
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
46 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
47 self.inputDirs = inputDirs
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
48 self.onChange = onChange
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
49 self.sourceSubstr = sourceSubstr
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
50 self._fileGraph = Graph()
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
51 self._remoteGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
52 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
53 self._oneShotAdditionGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
54 self._lastErrLog = {} # source: error
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
55
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
56 def updateFileData(self):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
57 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
58 make sure we contain the correct data from the files in inputDirs
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
59 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
60 # this sample one is actually only needed for the output, but I don't
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
61 # think I want to have a separate graph for the output
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
62 # handling
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
63 log.debug("read file graphs")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
64 for fp in FilePath("input").walk():
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
65 if fp.isdir():
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
66 continue
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
67 if fp.splitext()[1] != '.n3':
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
68 continue
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
69 log.debug("read %s", fp)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
70 # todo: if this fails, leave the report in the graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
71 self._fileGraph.parse(fp.open(), format="n3")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
72 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
73
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
74 self.onChange(self)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
75
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
76 @inlineCallbacks
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
77 def updateRemoteData(self):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
78 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
79 read all remote graphs (which are themselves enumerated within
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
80 the file data)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
81 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
82 t1 = time.time()
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
83 log.debug("read remote graphs")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
84 g = ConjunctiveGraph()
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
85
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
86 @inlineCallbacks
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
87 def fetchOne(source):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
88 try:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
89 fetchTime = yield addTrig(g, source, timeout=5)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
90 except Exception, e:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
91 e = str(e)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
92 if self._lastErrLog.get(source) != e:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
93 log.error(" can't add source %s: %s", source, e)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
94 self._lastErrLog[source] = e
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
95 g.add((URIRef(source), ROOM['graphLoadError'], Literal(e)))
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
96 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad']))
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
97 else:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
98 if self._lastErrLog.get(source):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
99 log.warning(" source %s is back", source)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
100 self._lastErrLog[source] = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
101 g.add((URIRef(source), ROOM['graphLoadMs'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
102 Literal(round(fetchTime * 1000, 1))))
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
103
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
104 fetchDone = []
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
105 filtered = 0
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
106 for source in self._fileGraph.objects(ROOM['reasoning'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
107 ROOM['source']):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
108 if self.sourceSubstr and self.sourceSubstr not in source:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
109 filtered += 1
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
110 continue
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
111 fetchDone.append(fetchOne(source))
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
112 yield gatherResults(fetchDone, consumeErrors=True)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
113 log.debug("loaded %s (skipping %s) in %.1f ms", len(fetchDone),
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
114 filtered, 1000 * (time.time() - t1))
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
115
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
116 prevGraph = self._remoteGraph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
117 self._remoteGraph = g
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
118 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
119 if (prevGraph is None or
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
120 not graphEqual(g, prevGraph, ignorePredicates=[
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
121 ROOM['signalStrength'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
122 # perhaps anything with a number-datatype for its
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
123 # object should be filtered out, and you have to make
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
124 # an upstream quantization (e.g. 'temp high'/'temp
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
125 # low') if you want to do reasoning on the difference
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
126 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"),
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
127 URIRef("http://bigasterisk.com/map#lastSeenAgo"),
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
128 ROOM['usingPower'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
129 ROOM['idleTimeMinutes'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
130 ROOM['idleTimeMs'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
131 ROOM['graphLoadMs'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
132 ROOM['localTimeToSecond'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
133 ROOM['history'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
134 ROOM['temperatureF'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
135 ROOM['connectedAgo'],
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
136 ])):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
137 log.debug(" remote graph changed")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
138 self.onChange(self)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
139 else:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
140 log.debug(" remote graph has no changes to trigger rules")
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
141
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
142 def addOneShot(self, g):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
143 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
144 add this graph to the total, call onChange, and then revert
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
145 the addition of this graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
146 """
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
147 self._oneShotAdditionGraph = g
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
148 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
149 try:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
150 self.onChange(self, oneShot=True, oneShotGraph=g)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
151 finally:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
152 self._oneShotAdditionGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
153 self._combinedGraph = None
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
154
281
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
155 def addOneShotFromString(self, body, contentType):
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
156 g = parseRdf(body, contentType)
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
157 if not len(g):
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
158 log.warn("incoming oneshot graph had no statements: %r", body)
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
159 return 0
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
160 t1 = time.time()
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
161 self.addOneShot(g)
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
162 return time.time() - t1
9728288c7f2f refactor
drewp@bigasterisk.com
parents: 275
diff changeset
163
275
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
164 def getGraph(self):
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
165 """rdflib Graph with the file+remote contents of the input graph"""
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
166 # this could be much faster with the combined readonly graph
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
167 # view from rdflib
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
168 if self._combinedGraph is None:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
169 self._combinedGraph = Graph()
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
170 if self._fileGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
171 for s in self._fileGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
172 self._combinedGraph.add(s)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
173 if self._remoteGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
174 for s in self._remoteGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
175 self._combinedGraph.add(s)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
176 if self._oneShotAdditionGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
177 for s in self._oneShotAdditionGraph:
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
178 self._combinedGraph.add(s)
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
179
d3733587e749 refactor inputgraph
drewp@bigasterisk.com
parents:
diff changeset
180 return self._combinedGraph