Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 851:0d86b3955bcd
rewriting reasoning to use graphs for config
Ignore-this: 2bcd9ea1c9fffe2dce123596587ac70a
darcs-hash:20121231042638-312f9-d9cc79c24c73e7cdbac3660d6bcf4e347b5707a9
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 30 Dec 2012 20:26:38 -0800 |
parents | 887d47682d94 |
children | 0448fbd96a31 |
rev | line source |
---|---|
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 #!bin/python |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
2 """ |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
3 Graph consists of: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
4 input/* (read at startup) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
5 webinput/* (new files are noticed in here) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
6 any number of remote graphs, specified in the other graph as objects of (:reasoning, :source, *), reread constantly |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
7 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 gather subgraphs from various services, run them through a rules |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 engine, and make http requests with the conclusions. |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
10 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
11 E.g. 'when drew's phone is near the house, and someone is awake, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 unlock the door when the door's motion sensor is activated' |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 When do we gather? The services should be able to trigger us, perhaps |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 with PSHB, that their graph has changed. |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
18 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 from twisted.internet import reactor, task |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 from twisted.web.client import getPage |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
21 import time, traceback, sys, json, logging |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 from rdflib.Graph import Graph, ConjunctiveGraph |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
23 from rdflib import Namespace, URIRef, Literal, RDF |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 import restkit |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 from FuXi.Rete.RuleStore import N3RuleStore |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 import cyclone.web |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 from inference import addTrig, infer |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 sys.path.append("../../lib") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 from logsetup import log |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
31 log.setLevel(logging.DEBUG) |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 DEV = Namespace("http://projects.bigasterisk.com/device/") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 def graphWithoutMetadata(g, ignorePredicates=[]): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 graph filter that removes any statements whose subjects are |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 contexts in the graph and also any statements with the given |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 predicates |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
43 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 out = ConjunctiveGraph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 for stmt in g.quads((None, None, None)): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 out.addN([stmt]) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 return out |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 def graphEqual(a, b, ignorePredicates=[]): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 compare graphs, omitting any metadata statements about contexts |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 (especially modification times) and also any statements using the |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 given predicates |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
56 stmtsA = graphWithoutMetadata(a, ignorePredicates) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
57 stmtsB = graphWithoutMetadata(b, ignorePredicates) |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
58 return set(stmtsA) == set(stmtsB) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
59 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
60 class InputGraph(object): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
61 def __init__(self, inputDirs, onChange): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
62 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
63 all .n3 files from inputDirs will be read. |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
64 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
65 onChange(self) is called if the contents of the full graph change |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
66 (in an interesting way) during updateFileData or |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
67 updateRemoteData. Interesting means statements other than the |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
68 ones with the predicates on the boring list. |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
69 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
70 self.inputDirs = inputDirs |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
71 self.onChange = onChange |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
72 self._fileGraph = Graph() |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
73 self._remoteGraph = None |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
74 self.updateFileData() |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
75 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
76 def updateFileData(self): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
77 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
78 make sure we contain the correct data from the files in inputDirs |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
79 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
80 # this sample one is actually only needed for the output, but I don't |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
81 # think I want to have a separate graph for the output |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
82 # handling |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
83 log.debug("read file graphs") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
84 self._fileGraph.parse("/home/drewp/oldplus/home/drewp/projects/room/devices.n3", format="n3") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
85 self._fileGraph.parse("/home/drewp/projects/homeauto/service/reasoning/input/startup.n3", format="n3") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
86 self.onChange(self) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
87 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
88 def updateRemoteData(self): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
89 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
90 read all remote graphs (which are themselves enumerated within |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
91 the file data) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
92 """ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
93 log.debug("read remote graphs") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
94 g = ConjunctiveGraph() |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
95 for source in self._fileGraph.objects(ROOM['reasoning'], |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
96 ROOM['source']): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
97 try: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
98 fetchTime = addTrig(g, source) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
99 except Exception, e: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
100 log.error("adding source %s: %s", source, e) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
101 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e)))) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
102 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad'])) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
103 else: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
104 g.add((URIRef(source), ROOM['graphLoadSecs'], |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
105 Literal(fetchTime))) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
106 prevGraph = self._remoteGraph |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
107 self._remoteGraph = g |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
108 if prevGraph is not None: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
109 log.debug("prev %s now %s", len(prevGraph), len(g)) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
110 if (prevGraph is None or |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
111 not graphEqual(g, prevGraph, ignorePredicates=[ |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
112 ROOM.signalStrength, |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
113 ROOM.graphLoadSecs])): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
114 log.debug("remote graph changed") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
115 self.onChange(self) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
116 else: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
117 log.debug("remote graph is unchanged") |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
118 |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
119 def getGraph(self): |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
120 """rdflib Graph with the file+remote contents of the input graph""" |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
121 # use the combined readonly graph view for this? |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
122 g = Graph() |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
123 if self._fileGraph: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
124 for s in self._fileGraph: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
125 g.add(s) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
126 if self._remoteGraph: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
127 for s in self._remoteGraph: |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
128 g.add(s) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
129 return g |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
130 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
131 class Reasoning(object): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
132 def __init__(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
133 self.prevGraph = None |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
134 self.lastPollTime = 0 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
135 self.lastError = "" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
136 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
137 self.rulesN3 = "(not read yet)" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
138 self.inferred = Graph() # gets replaced in each graphChanged call |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
139 |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
140 self.inputGraph = InputGraph([], self.graphChanged) |
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
141 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
142 def readRules(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
143 self.rulesN3 = open('rules.n3').read() # for web display |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
144 self.ruleStore = N3RuleStore() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
145 self.ruleGraph = Graph(self.ruleStore) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
146 self.ruleGraph.parse('rules.n3', format='n3') # for inference |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
147 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
148 def poll(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
149 try: |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
150 self.inputGraph.updateRemoteData() |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
151 self.lastPollTime = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
152 except Exception, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
153 log.error(traceback.format_exc()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
154 self.lastError = str(e) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
155 |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
156 def graphChanged(self, inputGraph): |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
157 # i guess these are getting consumed each inference |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
158 try: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
159 t1 = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
160 self.readRules() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
161 ruleParseTime = time.time() - t1 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
162 except ValueError, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
163 # this is so if you're just watching the inferred output, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
164 # you'll see the error too |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
165 self.inferred = Graph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
166 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
167 Literal(traceback.format_exc()))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
168 raise |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
169 |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
170 g = inputGraph.getGraph() |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
171 t1 = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
172 self.inferred = infer(g, self.ruleStore) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
173 inferenceTime = time.time() - t1 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
174 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
175 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseTime'], |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
176 Literal(ruleParseTime))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
177 self.inferred.add((ROOM['reasoner'], ROOM['inferenceTime'], |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
178 Literal(inferenceTime))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
179 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
180 self.putResults(self.inferred) |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
181 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
182 try: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
183 inputGraphNt = g.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
184 inferredNt = self.inferred.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
185 body = json.dumps({"input": inputGraphNt, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
186 "inferred": inferredNt}) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
187 restkit.Resource("http://bang:8014/").post( |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
188 "reasoningChange", payload=body, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
189 headers={"content-type" : "application/json"}) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
190 except Exception, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
191 traceback.print_exc() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
192 log.error("while sending changes to magma:") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
193 log.error(e) |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
194 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
195 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
196 def putResults(self, inferred): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
197 """ |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
198 some conclusions in the inferred graph lead to PUT requests |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
199 getting made |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
200 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
201 if the graph contains (?d ?p ?o) and ?d and ?p are a device |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
202 and predicate we support PUTs for, then we look up |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
203 (?d :putUrl ?url) and (?o :putValue ?val) and call |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
204 PUT ?url <- ?val |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
205 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
206 If the graph doesn't contain any matches, we use (?d |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
207 :zeroValue ?val) for the value and PUT that. |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
208 """ |
851
0d86b3955bcd
rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents:
850
diff
changeset
|
209 return |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
210 for dev, pred in [ |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
211 # the config of each putUrl should actually be in the |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
212 # context of a dev and predicate pair, and then that would |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
213 # be the source of this list |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
214 (DEV.theaterDoorLock, ROOM.state), |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
215 (URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState), |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
216 ]: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
217 url = self.deviceGraph.value(dev, ROOM.putUrl) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
218 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
219 if dev == DEV.theaterDoorLock: # ew |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
220 restkit.request(url=url+"/mode", method="PUT", body="output") |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
221 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
222 inferredObjects = list(inferred.objects(dev, pred)) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
223 if len(inferredObjects) == 0: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
224 self.putZero(dev, pred, url) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
225 elif len(inferredObjects) == 1: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
226 self.putInferred(dev, pred, url, inferredObjects[0]) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
227 elif len(inferredObjects) > 1: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
228 log.info("conflict, ignoring: %s has %s of %s" % |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
229 (dev, pred, inferredObjects)) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
230 # write about it to the inferred graph? |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
231 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
232 self.frontDoorPuts(inferred) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
233 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
234 def putZero(self, dev, pred, putUrl): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
235 # zerovalue should be a function of pred as well. |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
236 value = self.deviceGraph.value(dev, ROOM.zeroValue) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
237 if value is not None: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
238 log.info("put zero (%r) to %s", value, putUrl) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
239 restkit.request(url=putUrl, method="PUT", body=value) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
240 # this should be written back into the inferred graph |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
241 # for feedback |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
242 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
243 def putInferred(self, dev, pred, putUrl, obj): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
244 value = self.deviceGraph.value(obj, ROOM.putValue) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
245 if value is not None: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
246 log.info("put %s to %s", value, putUrl) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
247 restkit.request(url=putUrl, method="PUT", body=value) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
248 else: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
249 log.warn("%s %s %s has no :putValue" % |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
250 (dev, pred, obj)) |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
251 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
252 def frontDoorPuts(self, inferred): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
253 # todo: shouldn't have to be a special case |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
254 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
255 url = self.deviceGraph.value(DEV.frontDoorLcdBrightness, |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
256 ROOM.putUrl) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
257 log.info("put lcd %s brightness %s", url, brt) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
258 getPage(str(url) + "?brightness=%s" % str(brt), method="PUT") |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
259 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
260 msg = "open %s motion %s" % (inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text), |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
261 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text)) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
262 # this was meant to be 2 chars in the bottom row, but the |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
263 # easier test was to replace the whole top msg |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
264 #restkit.Resource("http://slash:9080/").put("lcd", message=msg) |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
265 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
266 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
267 class Index(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
268 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
269 # make sure GET / fails if our poll loop died |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
270 ago = time.time() - self.settings.reasoning.lastPollTime |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
271 if ago > 2: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
272 self.set_status(500) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
273 self.finish("last poll was %s sec ago. last error: %s" % |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
274 (ago, self.settings.reasoning.lastError)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
275 return |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
276 self.set_header("Content-Type", "application/xhtml+xml") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
277 self.write(open('index.html').read()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
278 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
279 # for reuse |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
280 class GraphResource(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
281 def get(self, which): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
282 self.set_header("Content-Type", "application/json") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
283 r = self.settings.reasoning |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
284 g = {'lastInput': r.prevGraph, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
285 'lastOutput': r.inferred, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
286 }[which] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
287 self.write(self.jsonRdf(g)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
288 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
289 def jsonRdf(self, g): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
290 return json.dumps(sorted(list(g))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
291 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
292 class NtGraphs(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
293 """same as what gets posted above""" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
294 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
295 r = self.settings.reasoning |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
296 inputGraphNt = r.prevGraph.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
297 inferredNt = r.inferred.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
298 self.set_header("Content-Type", "application/json") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
299 self.write(json.dumps({"input": inputGraphNt, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
300 "inferred": inferredNt})) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
301 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
302 class Rules(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
303 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
304 self.set_header("Content-Type", "text/plain") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
305 self.write(self.settings.reasoning.rulesN3) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
306 |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
307 class Status(cyclone.web.RequestHandler): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
308 def get(self): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
309 self.set_header("Content-Type", "text/plain") |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
310 g = self.settings.reasoning.prevGraph |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
311 msg = "" |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
312 for badSource in g.subjects(RDF.type, ROOM['FailedGraphLoad']): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
313 msg += "GET %s failed (%s). " % ( |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
314 badSource, g.value(badSource, ROOM['graphLoadError'])) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
315 if not msg: |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
316 self.write("all inputs ok") |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
317 self.set_status(500) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
318 self.finish(msg) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
319 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
320 class Static(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
321 def get(self, p): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
322 self.write(open(p).read()) |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
323 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
324 class Application(cyclone.web.Application): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
325 def __init__(self, reasoning): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
326 handlers = [ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
327 (r"/", Index), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
328 (r'/(jquery.min.js)', Static), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
329 (r'/(lastInput|lastOutput)Graph', GraphResource), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
330 (r'/ntGraphs', NtGraphs), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
331 (r'/rules', Rules), |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
332 (r'/status', Status), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
333 ] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
334 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
335 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
336 if __name__ == '__main__': |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
337 r = Reasoning() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
338 #import twisted.python.log |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
339 #twisted.python.log.startLogging(sys.stdout) |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
340 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
341 task.LoopingCall(r.poll).start(1.0) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
342 reactor.listenTCP(9071, Application(r)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
343 reactor.run() |