annotate service/reasoning/reasoning.py @ 49:0aeb8d6ea124

cleanup Ignore-this: 5daf1af90686860062cb65a6dfc6526a
author drewp@bigasterisk.com
date Mon, 31 Dec 2012 14:13:22 -0800
parents 571e773c77c3
children 42411726a7ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
1 #!bin/python
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
2 """
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
3 Graph consists of:
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
4 input/* (read at startup)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
5 webinput/* (new files are noticed in here)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
6 any number of remote graphs, specified in the other graph as objects of (:reasoning, :source, *), reread constantly
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
7
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
8 gather subgraphs from various services, run them through a rules
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
9 engine, and make http requests with the conclusions.
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
10
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
11 E.g. 'when drew's phone is near the house, and someone is awake,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
12 unlock the door when the door's motion sensor is activated'
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
13
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
14 When do we gather? The services should be able to trigger us, perhaps
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
15 with PSHB, that their graph has changed.
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
16 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
17
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
18
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
19 from twisted.internet import reactor, task
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
20 from twisted.web.client import getPage
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
21 from twisted.python.filepath import FilePath
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
22 import time, traceback, sys, json, logging
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
23 from rdflib.Graph import Graph, ConjunctiveGraph
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
24 from rdflib import Namespace, URIRef, Literal, RDF, StringInputSource
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
25 import restkit
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
26 from FuXi.Rete.RuleStore import N3RuleStore
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
27 import cyclone.web
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
28 from inference import addTrig, infer
48
571e773c77c3 refactor graph ops
drewp@bigasterisk.com
parents: 47
diff changeset
29 from graphop import graphEqual
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
30
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
31 sys.path.append("../../lib")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
32 from logsetup import log
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
33 log.setLevel(logging.INFO)
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
34 outlog = logging.getLogger('output')
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
35
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
36 ROOM = Namespace("http://projects.bigasterisk.com/room/")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
37 DEV = Namespace("http://projects.bigasterisk.com/device/")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
38
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
39
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
40 class InputGraph(object):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
41 def __init__(self, inputDirs, onChange):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
42 """
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
43 this has one Graph that's made of:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
44 - all .n3 files from inputDirs (read at startup)
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
45 - all the remote graphs, specified in the file graphs
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
46
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
47 call updateFileData or updateRemoteData to reread those
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
48 graphs. getGraph to access the combined graph.
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
49
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
50 onChange(self) is called if the contents of the full graph
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
51 change (in an interesting way) during updateFileData or
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
52 updateRemoteData. Interesting means statements other than the
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
53 ones with the predicates on the boring list. onChange(self,
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
54 oneShot=True) means: don't store the result of this change
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
55 anywhere; it needs to be processed only once
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
56 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
57 self.inputDirs = inputDirs
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
58 self.onChange = onChange
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
59 self._fileGraph = Graph()
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
60 self._remoteGraph = None
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
61 self._combinedGraph = None
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
62 self._oneShotAdditionGraph = None
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
63
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
64 def updateFileData(self):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
65 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
66 make sure we contain the correct data from the files in inputDirs
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
67 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
68 # this sample one is actually only needed for the output, but I don't
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
69 # think I want to have a separate graph for the output
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
70 # handling
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
71 log.debug("read file graphs")
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
72 for fp in FilePath("input").walk():
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
73 if fp.isdir():
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
74 continue
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
75 log.debug("read %s", fp)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
76 # todo: if this fails, leave the report in the graph
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
77 self._fileGraph.parse(fp.open(), format="n3")
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
78 self._combinedGraph = None
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
79
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
80 self.onChange(self)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
81
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
82 def updateRemoteData(self):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
83 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
84 read all remote graphs (which are themselves enumerated within
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
85 the file data)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
86 """
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
87 log.debug("read remote graphs")
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
88 g = ConjunctiveGraph()
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
89 for source in self._fileGraph.objects(ROOM['reasoning'],
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
90 ROOM['source']):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
91 try:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
92 # this part could be parallelized
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
93 fetchTime = addTrig(g, source)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
94 except Exception, e:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
95 log.error(" adding source %s: %s", source, e)
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
96 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e))))
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
97 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad']))
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
98 else:
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
99 g.add((URIRef(source), ROOM['graphLoadSecs'],
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
100 Literal(fetchTime)))
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
101 prevGraph = self._remoteGraph
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
102 self._remoteGraph = g
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
103 self._combinedGraph = None
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
104 if (prevGraph is None or
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
105 not graphEqual(g, prevGraph, ignorePredicates=[
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
106 ROOM.signalStrength,
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
107 ROOM.graphLoadSecs,
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
108 # perhaps anything with a number-datatype for its
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
109 # object should be filtered out, and you have to make
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
110 # an upstream quantization (e.g. 'temp high'/'temp
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
111 # low') if you want to do reasoning on the difference
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
112 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
113 URIRef("http://bigasterisk.com/map#lastSeenAgo"),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
114 URIRef("http://projects.bigasterisk.com/room/usingPower"),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
115 ])):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
116 log.debug(" remote graph changed")
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
117 self.onChange(self)
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
118 else:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
119 log.debug(" remote graph is unchanged")
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
120
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
121 def addOneShot(self, g):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
122 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
123 add this graph to the total, call onChange, and then revert
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
124 the addition of this graph
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
125 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
126 self._oneShotAdditionGraph = g
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
127 self._combinedGraph = None
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
128 try:
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
129 self.onChange(self, oneShot=True)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
130 finally:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
131 self._oneShotAdditionGraph = None
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
132 self._combinedGraph = None
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
133
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
134 def getGraph(self):
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
135 """rdflib Graph with the file+remote contents of the input graph"""
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
136 # this could be much faster with the combined readonly graph
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
137 # view from rdflib
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
138 if self._combinedGraph is None:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
139 self._combinedGraph = Graph()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
140 if self._fileGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
141 for s in self._fileGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
142 self._combinedGraph.add(s)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
143 if self._remoteGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
144 for s in self._remoteGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
145 self._combinedGraph.add(s)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
146 if self._oneShotAdditionGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
147 for s in self._oneShotAdditionGraph:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
148 self._combinedGraph.add(s)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
149
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
150 return self._combinedGraph
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
151
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
152 class Reasoning(object):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
153 def __init__(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
154 self.prevGraph = None
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
155 self.lastPollTime = 0
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
156 self.lastError = ""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
157
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
158 self.rulesN3 = "(not read yet)"
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
159 self.inferred = Graph() # gets replaced in each graphChanged call
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
160
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
161 self.inputGraph = InputGraph([], self.graphChanged)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
162 self.inputGraph.updateFileData()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
163
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
164
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
165 def readRules(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
166 self.rulesN3 = open('rules.n3').read() # for web display
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
167 self.ruleStore = N3RuleStore()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
168 self.ruleGraph = Graph(self.ruleStore)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
169 self.ruleGraph.parse('rules.n3', format='n3') # for inference
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
170
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
171 def poll(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
172 try:
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
173 self.inputGraph.updateRemoteData()
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
174 self.lastPollTime = time.time()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
175 except Exception, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
176 log.error(traceback.format_exc())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
177 self.lastError = str(e)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
178
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
179 def graphChanged(self, inputGraph, oneShot=False):
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
180 oldInferred = self.inferred
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
181 try:
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
182 try:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
183 t1 = time.time()
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
184 self.readRules()
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
185 ruleParseTime = time.time() - t1
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
186 except ValueError, e:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
187 # this is so if you're just watching the inferred output,
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
188 # you'll see the error too
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
189 self.inferred = Graph()
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
190 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'],
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
191 Literal(traceback.format_exc())))
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
192 raise
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
193
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
194 g = inputGraph.getGraph()
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
195 self.inferred = self._makeInferred(g)
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
196 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseTime'],
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
197 Literal(ruleParseTime)))
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
198
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
199 self.putResults(self.inferred)
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
200 self._postToMagma(g)
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
201 finally:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
202 if oneShot:
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
203 self.inferred = oldInferred
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
204
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
205 def _makeInferred(self, inputGraph):
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
206 t1 = time.time()
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
207 out = infer(inputGraph, self.ruleStore)
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
208 inferenceTime = time.time() - t1
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
209
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
210 out.add((ROOM['reasoner'], ROOM['inferenceTime'],
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
211 Literal(inferenceTime)))
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
212 return out
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
213
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
214 def _postToMagma(self, inputGraph):
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
215 try:
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
216 inputGraphNt = inputGraph.serialize(format="nt")
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
217 inferredNt = self.inferred.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
218 body = json.dumps({"input": inputGraphNt,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
219 "inferred": inferredNt})
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
220 restkit.Resource("http://bang:8014/").post(
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
221 "reasoningChange", payload=body,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
222 headers={"content-type" : "application/json"})
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
223 except Exception, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
224 traceback.print_exc()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
225 log.error("while sending changes to magma:")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
226 log.error(e)
46
f5623d9b07fd rewriting reasoning to use graphs for config
drewp@bigasterisk.com
parents: 45
diff changeset
227
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
228 def putResults(self, inferred):
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
229 """
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
230 some conclusions in the inferred graph lead to PUT requests
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
231 getting made
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
232
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
233 if the graph contains (?d ?p ?o) and ?d and ?p are a device
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
234 and predicate we support PUTs for, then we look up
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
235 (?d :putUrl ?url) and (?o :putValue ?val) and call
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
236 PUT ?url <- ?val
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
237
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
238 If the graph doesn't contain any matches, we use (?d
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
239 :zeroValue ?val) for the value and PUT that.
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
240 """
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
241 deviceGraph = self.inputGraph.getGraph()
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
242 for dev, pred in [
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
243 # the config of each putUrl should actually be in the
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
244 # context of a dev and predicate pair, and then that would
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
245 # be the source of this list
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
246 (DEV.theaterDoorLock, ROOM.state),
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
247 (URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState),
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
248 (URIRef('http://bigasterisk.com/host/dash/monitor'), ROOM.powerState),
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
249 ]:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
250 url = deviceGraph.value(dev, ROOM.putUrl)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
251
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
252 if dev == DEV.theaterDoorLock: # ew
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
253 restkit.request(url=url+"/mode", method="PUT", body="output")
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
254
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
255 inferredObjects = list(inferred.objects(dev, pred))
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
256 if len(inferredObjects) == 0:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
257 self.putZero(deviceGraph, dev, pred, url)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
258 elif len(inferredObjects) == 1:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
259 self.putInferred(deviceGraph, dev, pred, url, inferredObjects[0])
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
260 elif len(inferredObjects) > 1:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
261 log.info("conflict, ignoring: %s has %s of %s" %
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
262 (dev, pred, inferredObjects))
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
263 # write about it to the inferred graph?
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
264
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
265 self.frontDoorPuts(deviceGraph, inferred)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
266
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
267 def putZero(self, deviceGraph, dev, pred, putUrl):
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
268 # zerovalue should be a function of pred as well.
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
269 value = deviceGraph.value(dev, ROOM.zeroValue)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
270 if value is not None:
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
271 outlog.info("put zero (%r) to %s", value, putUrl)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
272 restkit.request(url=putUrl, method="PUT", body=value)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
273 # this should be written back into the inferred graph
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
274 # for feedback
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
275
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
276 def putInferred(self, deviceGraph, dev, pred, putUrl, obj):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
277 value = deviceGraph.value(obj, ROOM.putValue)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
278 if value is not None:
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
279 outlog.info("put %s to %s", value, putUrl)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
280 restkit.request(url=putUrl, method="PUT", body=value)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
281 else:
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
282 outlog.warn("%s %s %s has no :putValue" %
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
283 (dev, pred, obj))
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
284
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
285 def frontDoorPuts(self, deviceGraph, inferred):
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
286 # todo: shouldn't have to be a special case
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
287 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
288 if brt is None:
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
289 return
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
290 url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl)
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
291 outlog.info("put lcd %s brightness %s", url, brt)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
292 def failed(err):
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
293 outlog.error("lcd brightness: %s", err)
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
294 getPage(str(url) + "?brightness=%s" % str(brt),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
295 method="PUT").addErrback(failed)
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
296
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
297 msg = "open %s motion %s" % (
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
298 inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
299 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text))
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
300 # this was meant to be 2 chars in the bottom row, but the
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
301 # easier test was to replace the whole top msg
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
302 #restkit.Resource("http://slash:9080/").put("lcd", message=msg)
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
303
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
304
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
305
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
306 class Index(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
307 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
308 # make sure GET / fails if our poll loop died
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
309 ago = time.time() - self.settings.reasoning.lastPollTime
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
310 if ago > 2:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
311 self.set_status(500)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
312 self.finish("last poll was %s sec ago. last error: %s" %
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
313 (ago, self.settings.reasoning.lastError))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
314 return
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
315 self.set_header("Content-Type", "application/xhtml+xml")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
316 self.write(open('index.html').read())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
317
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
318 class ImmediateUpdate(cyclone.web.RequestHandler):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
319 def put(self):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
320 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
321 request an immediate load of the remote graphs; the thing we
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
322 do in the background anyway. No payload.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
323
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
324 Using PUT because this is idempotent and retryable and
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
325 everything.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
326 """
49
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
327 print self.request.headers
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
328 log.info("immediateUpdate from %s",
0aeb8d6ea124 cleanup
drewp@bigasterisk.com
parents: 48
diff changeset
329 self.request.headers.get('User-Agent', '?'))
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
330 r.poll()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
331 self.set_status(202)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
332
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
333 def parseRdf(text, contentType):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
334 g = Graph()
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
335 g.parse(StringInputSource(text), format={
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
336 'text/n3': 'n3',
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
337 }[contentType])
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
338 return g
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
339
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
340 class OneShot(cyclone.web.RequestHandler):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
341 def post(self):
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
342 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
343 payload is an rdf graph. The statements are momentarily added
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
344 to the input graph for exactly one update.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
345
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
346 todo: how do we go from a transition like doorclosed-to-open
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
347 to a oneshot event? the upstream shouldn't have to do it. Do
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
348 we make those oneshot events here? for every object change?
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
349 there are probably special cases regarding startup time when
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
350 everything appears to be a 'change'.
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
351 """
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
352 g = parseRdf(self.request.body, self.request.headers['content-type'])
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
353 self.settings.reasoning.inputGraph.addOneShot(g)
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
354
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
355 # for reuse
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
356 class GraphResource(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
357 def get(self, which):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
358 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
359 r = self.settings.reasoning
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
360 g = {'lastInput': r.inputGraph.getGraph(),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
361 'lastOutput': r.inferred,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
362 }[which]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
363 self.write(self.jsonRdf(g))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
364
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
365 def jsonRdf(self, g):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
366 return json.dumps(sorted(list(g)))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
367
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
368 class NtGraphs(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
369 """same as what gets posted above"""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
370 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
371 r = self.settings.reasoning
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
372 inputGraphNt = r.inputGraph.getGraph().serialize(format="nt")
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
373 inferredNt = r.inferred.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
374 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
375 self.write(json.dumps({"input": inputGraphNt,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
376 "inferred": inferredNt}))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
377
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
378 class Rules(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
379 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
380 self.set_header("Content-Type", "text/plain")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
381 self.write(self.settings.reasoning.rulesN3)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
382
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
383 class Status(cyclone.web.RequestHandler):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
384 def get(self):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
385 self.set_header("Content-Type", "text/plain")
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
386 g = self.settings.reasoning.inputGraph.getGraph()
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
387 msg = ""
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
388 for badSource in g.subjects(RDF.type, ROOM['FailedGraphLoad']):
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
389 msg += "GET %s failed (%s). " % (
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
390 badSource, g.value(badSource, ROOM['graphLoadError']))
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
391 if not msg:
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
392 self.finish("all inputs ok")
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
393 return
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
394 self.set_status(500)
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
395 self.finish(msg)
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
396
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
397 class Static(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
398 def get(self, p):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
399 self.write(open(p).read())
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
400
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
401 class Application(cyclone.web.Application):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
402 def __init__(self, reasoning):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
403 handlers = [
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
404 (r"/", Index),
47
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
405 (r"/immediateUpdate", ImmediateUpdate),
0448fbd96a31 scan more input files. oneshot and immediate update features.
drewp@bigasterisk.com
parents: 46
diff changeset
406 (r"/oneShot", OneShot),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
407 (r'/(jquery.min.js)', Static),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
408 (r'/(lastInput|lastOutput)Graph', GraphResource),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
409 (r'/ntGraphs', NtGraphs),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
410 (r'/rules', Rules),
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
411 (r'/status', Status),
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
412 ]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
413 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
414
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
415 if __name__ == '__main__':
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
416 r = Reasoning()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
417 #import twisted.python.log
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
418 #twisted.python.log.startLogging(sys.stdout)
45
5b0f970e3d52 /status page, errors on failed graphs, time reports of successful fetches
drewp@bigasterisk.com
parents: 33
diff changeset
419
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
420 task.LoopingCall(r.poll).start(1.0)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
421 reactor.listenTCP(9071, Application(r))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
422 reactor.run()