annotate service/reasoning/reasoning.py @ 33:c3c203100e06

redo the http PUT request part of the reasoner Ignore-this: c55d7014857040345ca7e87ce7734e43
author drewp@bigasterisk.com
date Mon, 07 May 2012 00:17:40 -0700
parents 3f0dd03112b5
children 5b0f970e3d52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
1 #!bin/python
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
2 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
3 gather subgraphs from various services, run them through a rules
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
4 engine, and make http requests with the conclusions.
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
5
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
6 E.g. 'when drew's phone is near the house, and someone is awake,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
7 unlock the door when the door's motion sensor is activated'
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
8
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
9 When do we gather? The services should be able to trigger us, perhaps
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
10 with PSHB, that their graph has changed.
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
11 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
12
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
13
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
14 from twisted.internet import reactor, task
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
15 from twisted.web.client import getPage
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
16 import time, traceback, sys, json
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
17 from rdflib.Graph import Graph, ConjunctiveGraph
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
18 from rdflib import Namespace, URIRef, Literal
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
19 import restkit
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
20 from FuXi.Rete.RuleStore import N3RuleStore
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
21 import cyclone.web
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
22 from inference import addTrig, infer
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
23
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
24 sys.path.append("../../lib")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
25 from logsetup import log
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
26
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
27 ROOM = Namespace("http://projects.bigasterisk.com/room/")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
28 DEV = Namespace("http://projects.bigasterisk.com/device/")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
29
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
30 def gatherGraph():
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
31 g = ConjunctiveGraph()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
32 for source in ["http://bang:9069/graph", # arduino watchpins
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
33 "http://bang:9070/graph", # wifi usage
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
34 "http://bang:9075/graph", # env
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
35 "http://slash:9050/graph", # garageArduino for front motion
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
36 "http://dash:9095/graph", # dash monitor
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
37 "http://bang:9095/graph", # bang monitor
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
38 ]:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
39 try:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
40 addTrig(g, source)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
41 except:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
42 log.error("adding source %s", source)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
43 raise
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
44
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
45 return g
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
46
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
47 def graphWithoutMetadata(g, ignorePredicates=[]):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
48 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
49 graph filter that removes any statements whose subjects are
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
50 contexts in the graph and also any statements with the given
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
51 predicates
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
52 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
53 ctxs = map(URIRef, set(g.contexts())) # weird they turned to strings
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
54
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
55 out = ConjunctiveGraph()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
56 for stmt in g.quads((None, None, None)):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
57 if stmt[0] not in ctxs and stmt[1] not in ignorePredicates:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
58 out.addN([stmt])
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
59 return out
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
60
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
61 def graphEqual(a, b, ignorePredicates=[]):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
62 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
63 compare graphs, omitting any metadata statements about contexts
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
64 (especially modification times) and also any statements using the
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
65 given predicates
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
66 """
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
67 stmtsA = graphWithoutMetadata(a, ignorePredicates)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
68 stmtsB = graphWithoutMetadata(b, ignorePredicates)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
69 return set(stmtsA) == set(stmtsB)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
70
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
71 class Reasoning(object):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
72 def __init__(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
73 self.prevGraph = None
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
74 self.lastPollTime = 0
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
75 self.lastError = ""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
76
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
77 self.deviceGraph = Graph()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
78 self.deviceGraph.parse("/my/proj/room/devices.n3", format="n3")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
79
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
80 self.rulesN3 = "(not read yet)"
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
81 self.inferred = Graph() # gets replaced in each graphChanged call
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
82
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
83 def readRules(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
84 self.rulesN3 = open('rules.n3').read() # for web display
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
85 self.ruleStore = N3RuleStore()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
86 self.ruleGraph = Graph(self.ruleStore)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
87 self.ruleGraph.parse('rules.n3', format='n3') # for inference
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
88
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
89 def poll(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
90 try:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
91 self._poll()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
92 self.lastPollTime = time.time()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
93 except Exception, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
94 log.error(traceback.format_exc())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
95 self.lastError = str(e)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
96
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
97 def _poll(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
98 g = gatherGraph()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
99 if (self.prevGraph is None or
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
100 not graphEqual(g, self.prevGraph,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
101 ignorePredicates=[ROOM.signalStrength])):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
102 self.graphChanged(g)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
103
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
104 self.prevGraph = g
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
105
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
106 def graphChanged(self, g):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
107 # i guess these are getting consumed each inference
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
108 try:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
109 t1 = time.time()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
110 self.readRules()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
111 ruleParseTime = time.time() - t1
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
112 except ValueError, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
113 # this is so if you're just watching the inferred output,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
114 # you'll see the error too
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
115 self.inferred = Graph()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
116 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'],
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
117 Literal(traceback.format_exc())))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
118 raise
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
119
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
120 t1 = time.time()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
121 self.inferred = infer(g, self.ruleStore)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
122 inferenceTime = time.time() - t1
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
123
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
124 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseTime'],
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
125 Literal(ruleParseTime)))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
126 self.inferred.add((ROOM['reasoner'], ROOM['inferenceTime'],
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
127 Literal(inferenceTime)))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
128
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
129 self.putResults(self.inferred)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
130
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
131 try:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
132 inputGraphNt = g.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
133 inferredNt = self.inferred.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
134 body = json.dumps({"input": inputGraphNt,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
135 "inferred": inferredNt})
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
136 restkit.Resource("http://bang:8014/").post(
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
137 "reasoningChange", payload=body,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
138 headers={"content-type" : "application/json"})
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
139 except Exception, e:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
140 traceback.print_exc()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
141 log.error("while sending changes to magma:")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
142 log.error(e)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
143
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
144
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
145 def putResults(self, inferred):
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
146 """
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
147 some conclusions in the inferred graph lead to PUT requests
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
148 getting made
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
149
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
150 if the graph contains (?d ?p ?o) and ?d and ?p are a device
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
151 and predicate we support PUTs for, then we look up
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
152 (?d :putUrl ?url) and (?o :putValue ?val) and call
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
153 PUT ?url <- ?val
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
154
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
155 If the graph doesn't contain any matches, we use (?d
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
156 :zeroValue ?val) for the value and PUT that.
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
157 """
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
158
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
159 for dev, pred in [
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
160 # the config of each putUrl should actually be in the
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
161 # context of a dev and predicate pair, and then that would
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
162 # be the source of this list
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
163 (DEV.theaterDoorLock, ROOM.state),
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
164 (URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState),
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
165 ]:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
166 url = self.deviceGraph.value(dev, ROOM.putUrl)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
167
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
168 if dev == DEV.theaterDoorLock: # ew
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
169 restkit.request(url=url+"/mode", method="PUT", body="output")
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
170
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
171 inferredObjects = list(inferred.objects(dev, pred))
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
172 if len(inferredObjects) == 0:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
173 self.putZero(dev, pred, url)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
174 elif len(inferredObjects) == 1:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
175 self.putInferred(dev, pred, url, inferredObjects[0])
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
176 elif len(inferredObjects) > 1:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
177 log.info("conflict, ignoring: %s has %s of %s" %
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
178 (dev, pred, inferredObjects))
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
179 # write about it to the inferred graph?
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
180
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
181 self.frontDoorPuts(inferred)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
182
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
183 def putZero(self, dev, pred, putUrl):
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
184 # zerovalue should be a function of pred as well.
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
185 value = self.deviceGraph.value(dev, ROOM.zeroValue)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
186 if value is not None:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
187 log.info("put zero (%r) to %s", value, putUrl)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
188 restkit.request(url=putUrl, method="PUT", body=value)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
189 # this should be written back into the inferred graph
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
190 # for feedback
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
191
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
192 def putInferred(self, dev, pred, putUrl, obj):
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
193 value = self.deviceGraph.value(obj, ROOM.putValue)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
194 if value is not None:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
195 log.info("put %s to %s", value, putUrl)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
196 restkit.request(url=putUrl, method="PUT", body=value)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
197 else:
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
198 log.warn("%s %s %s has no :putValue" %
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
199 (dev, pred, obj))
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
200
33
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
201 def frontDoorPuts(self, inferred):
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
202 # todo: shouldn't have to be a special case
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
203 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
204 url = self.deviceGraph.value(DEV.frontDoorLcdBrightness,
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
205 ROOM.putUrl)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
206 log.info("put lcd %s brightness %s", url, brt)
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
207 getPage(str(url) + "?brightness=%s" % str(brt), method="PUT")
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
208
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
209 msg = "open %s motion %s" % (inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text),
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
210 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text))
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
211 # this was meant to be 2 chars in the bottom row, but the
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
212 # easier test was to replace the whole top msg
c3c203100e06 redo the http PUT request part of the reasoner
drewp@bigasterisk.com
parents: 20
diff changeset
213 #restkit.Resource("http://slash:9080/").put("lcd", message=msg)
20
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
214
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
215
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
216 class Index(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
217 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
218 # make sure GET / fails if our poll loop died
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
219 ago = time.time() - self.settings.reasoning.lastPollTime
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
220 if ago > 2:
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
221 self.set_status(500)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
222 self.finish("last poll was %s sec ago. last error: %s" %
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
223 (ago, self.settings.reasoning.lastError))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
224 return
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
225 self.set_header("Content-Type", "application/xhtml+xml")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
226 self.write(open('index.html').read())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
227
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
228 # for reuse
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
229 class GraphResource(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
230 def get(self, which):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
231 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
232 r = self.settings.reasoning
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
233 g = {'lastInput': r.prevGraph,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
234 'lastOutput': r.inferred,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
235 }[which]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
236 self.write(self.jsonRdf(g))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
237
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
238 def jsonRdf(self, g):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
239 return json.dumps(sorted(list(g)))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
240
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
241 class NtGraphs(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
242 """same as what gets posted above"""
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
243 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
244 r = self.settings.reasoning
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
245 inputGraphNt = r.prevGraph.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
246 inferredNt = r.inferred.serialize(format="nt")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
247 self.set_header("Content-Type", "application/json")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
248 self.write(json.dumps({"input": inputGraphNt,
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
249 "inferred": inferredNt}))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
250
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
251 class Rules(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
252 def get(self):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
253 self.set_header("Content-Type", "text/plain")
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
254 self.write(self.settings.reasoning.rulesN3)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
255
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
256 class Static(cyclone.web.RequestHandler):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
257 def get(self, p):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
258 self.write(open(p).read())
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
259
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
260 class Application(cyclone.web.Application):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
261 def __init__(self, reasoning):
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
262 handlers = [
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
263 (r"/", Index),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
264 (r'/(jquery.min.js)', Static),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
265 (r'/(lastInput|lastOutput)Graph', GraphResource),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
266 (r'/ntGraphs', NtGraphs),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
267 (r'/rules', Rules),
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
268 ]
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
269 cyclone.web.Application.__init__(self, handlers, reasoning=reasoning)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
270
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
271 if __name__ == '__main__':
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
272 r = Reasoning()
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
273 #import twisted.python.log
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
274 #twisted.python.log.startLogging(sys.stdout)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
275
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
276 task.LoopingCall(r.poll).start(1.0)
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
277 reactor.listenTCP(9071, Application(r))
3f0dd03112b5 move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff changeset
278 reactor.run()