Mercurial > code > home > repos > homeauto
annotate service/reasoning/reasoning.py @ 850:887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
Ignore-this: 14742f2a6237f996d7024ff4454a8057
darcs-hash:20121230110225-312f9-eca2846716440b11d8bcfa54ce8908a3c0a8c0a8
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 30 Dec 2012 03:02:25 -0800 |
parents | 36dbbb01d689 |
children | f5623d9b07fd |
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 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
3 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
|
4 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
|
5 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
6 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
|
7 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
|
8 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 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
|
10 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
|
11 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 |
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 from twisted.internet import reactor, task |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 from twisted.web.client import getPage |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 import time, traceback, sys, json |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 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
|
18 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
|
19 import restkit |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 from FuXi.Rete.RuleStore import N3RuleStore |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 import cyclone.web |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 from inference import addTrig, infer |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 sys.path.append("../../lib") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 from logsetup import log |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 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
|
28 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
|
29 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 def gatherGraph(): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 g = ConjunctiveGraph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 for source in ["http://bang:9069/graph", # arduino watchpins |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 "http://bang:9070/graph", # wifi usage |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 "http://bang:9075/graph", # env |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 "http://slash:9050/graph", # garageArduino for front motion |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
36 "http://dash:9095/graph", # dash monitor |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
37 "http://bang:9095/graph", # bang monitor |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 ]: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 try: |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
40 fetchTime = addTrig(g, source) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
41 except Exception, e: |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
42 log.error("adding source %s: %s", source, e) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
43 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e)))) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
44 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad'])) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
45 else: |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
46 g.add((URIRef(source), ROOM['graphLoadSecs'], Literal(fetchTime))) |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 return g |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 def graphWithoutMetadata(g, ignorePredicates=[]): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 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
|
52 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
|
53 predicates |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 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
|
56 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
57 out = ConjunctiveGraph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
58 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
|
59 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
|
60 out.addN([stmt]) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
61 return out |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
62 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
63 def graphEqual(a, b, ignorePredicates=[]): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
64 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
65 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
|
66 (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
|
67 given predicates |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
68 """ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
69 stmtsA = graphWithoutMetadata(a, ignorePredicates) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
70 stmtsB = graphWithoutMetadata(b, ignorePredicates) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
71 return set(stmtsA) == set(stmtsB) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
72 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
73 class Reasoning(object): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
74 def __init__(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
75 self.prevGraph = None |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
76 self.lastPollTime = 0 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
77 self.lastError = "" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
78 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
79 self.deviceGraph = Graph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
80 self.deviceGraph.parse("/my/proj/room/devices.n3", format="n3") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
81 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
82 self.rulesN3 = "(not read yet)" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
83 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
|
84 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
85 def readRules(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
86 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
|
87 self.ruleStore = N3RuleStore() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
88 self.ruleGraph = Graph(self.ruleStore) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
89 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
|
90 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
91 def poll(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
92 try: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
93 self._poll() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
94 self.lastPollTime = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
95 except Exception, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
96 log.error(traceback.format_exc()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
97 self.lastError = str(e) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
98 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
99 def _poll(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
100 g = gatherGraph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
101 if (self.prevGraph is None or |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
102 not graphEqual(g, self.prevGraph, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
103 ignorePredicates=[ROOM.signalStrength])): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
104 self.graphChanged(g) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
105 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
106 self.prevGraph = g |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
107 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
108 def graphChanged(self, g): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
109 # 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
|
110 try: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
111 t1 = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
112 self.readRules() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
113 ruleParseTime = time.time() - t1 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
114 except ValueError, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
115 # 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
|
116 # you'll see the error too |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
117 self.inferred = Graph() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
118 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
|
119 Literal(traceback.format_exc()))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
120 raise |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
121 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
122 t1 = time.time() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
123 self.inferred = infer(g, self.ruleStore) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
124 inferenceTime = time.time() - t1 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
125 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
126 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
|
127 Literal(ruleParseTime))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
128 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
|
129 Literal(inferenceTime))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
130 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
131 self.putResults(self.inferred) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
132 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
133 try: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
134 inputGraphNt = g.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
135 inferredNt = self.inferred.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
136 body = json.dumps({"input": inputGraphNt, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
137 "inferred": inferredNt}) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
138 restkit.Resource("http://bang:8014/").post( |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
139 "reasoningChange", payload=body, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
140 headers={"content-type" : "application/json"}) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
141 except Exception, e: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
142 traceback.print_exc() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
143 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
|
144 log.error(e) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
145 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
146 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
147 def putResults(self, inferred): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
148 """ |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
149 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
|
150 getting made |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
151 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
152 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
|
153 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
|
154 (?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
|
155 PUT ?url <- ?val |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
156 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
157 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
|
158 :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
|
159 """ |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
160 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
161 for dev, pred in [ |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
162 # 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
|
163 # 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
|
164 # be the source of this list |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
165 (DEV.theaterDoorLock, ROOM.state), |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
166 (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
|
167 ]: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
168 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
|
169 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
170 if dev == DEV.theaterDoorLock: # ew |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
171 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
|
172 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
173 inferredObjects = list(inferred.objects(dev, pred)) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
174 if len(inferredObjects) == 0: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
175 self.putZero(dev, pred, url) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
176 elif len(inferredObjects) == 1: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
177 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
|
178 elif len(inferredObjects) > 1: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
179 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
|
180 (dev, pred, inferredObjects)) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
181 # 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
|
182 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
183 self.frontDoorPuts(inferred) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
184 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
185 def putZero(self, dev, pred, putUrl): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
186 # 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
|
187 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
|
188 if value is not None: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
189 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
|
190 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
|
191 # 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
|
192 # for feedback |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
193 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
194 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
|
195 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
|
196 if value is not None: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
197 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
|
198 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
|
199 else: |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
200 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
|
201 (dev, pred, obj)) |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
202 |
838
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
203 def frontDoorPuts(self, inferred): |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
204 # 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
|
205 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
|
206 url = self.deviceGraph.value(DEV.frontDoorLcdBrightness, |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
207 ROOM.putUrl) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
208 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
|
209 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
|
210 |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
211 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
|
212 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text)) |
36dbbb01d689
redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents:
825
diff
changeset
|
213 # 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
|
214 # 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
|
215 #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
|
216 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
217 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
218 class Index(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
219 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
220 # 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
|
221 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
|
222 if ago > 2: |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
223 self.set_status(500) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
224 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
|
225 (ago, self.settings.reasoning.lastError)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
226 return |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
227 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
|
228 self.write(open('index.html').read()) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
229 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
230 # for reuse |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
231 class GraphResource(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
232 def get(self, which): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
233 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
|
234 r = self.settings.reasoning |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
235 g = {'lastInput': r.prevGraph, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
236 'lastOutput': r.inferred, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
237 }[which] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
238 self.write(self.jsonRdf(g)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
239 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
240 def jsonRdf(self, g): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
241 return json.dumps(sorted(list(g))) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
242 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
243 class NtGraphs(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
244 """same as what gets posted above""" |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
245 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
246 r = self.settings.reasoning |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
247 inputGraphNt = r.prevGraph.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
248 inferredNt = r.inferred.serialize(format="nt") |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
249 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
|
250 self.write(json.dumps({"input": inputGraphNt, |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
251 "inferred": inferredNt})) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
252 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
253 class Rules(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
254 def get(self): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
255 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
|
256 self.write(self.settings.reasoning.rulesN3) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
257 |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
258 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
|
259 def get(self): |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
260 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
|
261 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
|
262 msg = "" |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
263 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
|
264 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
|
265 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
|
266 if not msg: |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
267 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
|
268 self.set_status(500) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
269 self.finish(msg) |
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
270 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
271 class Static(cyclone.web.RequestHandler): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
272 def get(self, p): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
273 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
|
274 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
275 class Application(cyclone.web.Application): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
276 def __init__(self, reasoning): |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
277 handlers = [ |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
278 (r"/", Index), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
279 (r'/(jquery.min.js)', Static), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
280 (r'/(lastInput|lastOutput)Graph', GraphResource), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
281 (r'/ntGraphs', NtGraphs), |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
282 (r'/rules', Rules), |
850
887d47682d94
/status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents:
838
diff
changeset
|
283 (r'/status', Status), |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
284 ] |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
285 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
|
286 |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
287 if __name__ == '__main__': |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
288 r = Reasoning() |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
289 #import twisted.python.log |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
290 #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
|
291 |
825
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
292 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
|
293 reactor.listenTCP(9071, Application(r)) |
fc753b24f69a
move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
294 reactor.run() |