annotate service/reasoning/reasoning.py @ 838:36dbbb01d689

redo the http PUT request part of the reasoner Ignore-this: c55d7014857040345ca7e87ce7734e43 darcs-hash:20120507071740-312f9-24147bd6144032488bce042274e042753726d913.gz
author drewp <drewp@bigasterisk.com>
date Mon, 07 May 2012 00:17:40 -0700
parents fc753b24f69a
children 5b0f970e3d52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 from rdflib import Namespace, URIRef, Literal
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:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 addTrig(g, source)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 except:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 log.error("adding source %s", source)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 raise
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 return g
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 def graphWithoutMetadata(g, ignorePredicates=[]):
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 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
50 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
51 predicates
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 """
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 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
54
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55 out = ConjunctiveGraph()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 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
57 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
58 out.addN([stmt])
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 return out
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 def graphEqual(a, b, ignorePredicates=[]):
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 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
64 (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
65 given predicates
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 """
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67 stmtsA = graphWithoutMetadata(a, ignorePredicates)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 stmtsB = graphWithoutMetadata(b, ignorePredicates)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 return set(stmtsA) == set(stmtsB)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 class Reasoning(object):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 def __init__(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 self.prevGraph = None
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
74 self.lastPollTime = 0
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 self.lastError = ""
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 self.deviceGraph = Graph()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 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
79
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80 self.rulesN3 = "(not read yet)"
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
81 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
82
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 def readRules(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 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
85 self.ruleStore = N3RuleStore()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 self.ruleGraph = Graph(self.ruleStore)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 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
88
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 def poll(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 try:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 self._poll()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 self.lastPollTime = time.time()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 except Exception, e:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 log.error(traceback.format_exc())
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 self.lastError = str(e)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 def _poll(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98 g = gatherGraph()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 if (self.prevGraph is None or
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100 not graphEqual(g, self.prevGraph,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
101 ignorePredicates=[ROOM.signalStrength])):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
102 self.graphChanged(g)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
103
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
104 self.prevGraph = 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 def graphChanged(self, g):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
107 # 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
108 try:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
109 t1 = time.time()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
110 self.readRules()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
111 ruleParseTime = time.time() - t1
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112 except ValueError, e:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
113 # 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
114 # you'll see the error too
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
115 self.inferred = Graph()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
116 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
117 Literal(traceback.format_exc())))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
118 raise
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
119
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
120 t1 = time.time()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121 self.inferred = infer(g, self.ruleStore)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 inferenceTime = time.time() - t1
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 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
125 Literal(ruleParseTime)))
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['inferenceTime'],
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
127 Literal(inferenceTime)))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
128
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
129 self.putResults(self.inferred)
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
130
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
131 try:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
132 inputGraphNt = g.serialize(format="nt")
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
133 inferredNt = self.inferred.serialize(format="nt")
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
134 body = json.dumps({"input": inputGraphNt,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
135 "inferred": inferredNt})
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
136 restkit.Resource("http://bang:8014/").post(
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
137 "reasoningChange", payload=body,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
138 headers={"content-type" : "application/json"})
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
139 except Exception, e:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
140 traceback.print_exc()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
141 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
142 log.error(e)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
143
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
144
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
145 def putResults(self, inferred):
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 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
148 getting made
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
149
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
150 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
151 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
152 (?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
153 PUT ?url <- ?val
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
154
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
155 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
156 :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
157 """
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
158
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
159 for dev, pred in [
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
160 # 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
161 # 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
162 # be the source of this list
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
163 (DEV.theaterDoorLock, ROOM.state),
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
164 (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
165 ]:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
166 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
167
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
168 if dev == DEV.theaterDoorLock: # ew
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
169 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
170
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
171 inferredObjects = list(inferred.objects(dev, pred))
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
172 if len(inferredObjects) == 0:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
173 self.putZero(dev, pred, url)
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
174 elif len(inferredObjects) == 1:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
175 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
176 elif len(inferredObjects) > 1:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
177 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
178 (dev, pred, inferredObjects))
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
179 # write about it to the inferred graph?
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
180
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
181 self.frontDoorPuts(inferred)
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
182
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
183 def putZero(self, dev, pred, putUrl):
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
184 # 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
185 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
186 if value is not None:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
187 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
188 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
189 # 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
190 # for feedback
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
191
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
192 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
193 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
194 if value is not None:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
195 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
196 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
197 else:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
198 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
199 (dev, pred, obj))
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
200
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
201 def frontDoorPuts(self, inferred):
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
202 # 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
203 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
204 url = self.deviceGraph.value(DEV.frontDoorLcdBrightness,
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
205 ROOM.putUrl)
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
206 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
207 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
208
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
209 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
210 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text))
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
211 # 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
212 # 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
213 #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
214
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
215
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
216 class Index(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
217 def get(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
218 # 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
219 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
220 if ago > 2:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
221 self.set_status(500)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
222 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
223 (ago, self.settings.reasoning.lastError))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
224 return
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
225 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
226 self.write(open('index.html').read())
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
227
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
228 # for reuse
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
229 class GraphResource(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
230 def get(self, which):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
231 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
232 r = self.settings.reasoning
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
233 g = {'lastInput': r.prevGraph,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
234 'lastOutput': r.inferred,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
235 }[which]
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
236 self.write(self.jsonRdf(g))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
237
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
238 def jsonRdf(self, g):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
239 return json.dumps(sorted(list(g)))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
240
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
241 class NtGraphs(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
242 """same as what gets posted above"""
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
243 def get(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
244 r = self.settings.reasoning
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
245 inputGraphNt = r.prevGraph.serialize(format="nt")
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
246 inferredNt = r.inferred.serialize(format="nt")
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
247 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
248 self.write(json.dumps({"input": inputGraphNt,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
249 "inferred": inferredNt}))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
250
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
251 class Rules(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
252 def get(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
253 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
254 self.write(self.settings.reasoning.rulesN3)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
255
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
256 class Static(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
257 def get(self, p):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
258 self.write(open(p).read())
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
259
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
260 class Application(cyclone.web.Application):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
261 def __init__(self, reasoning):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
262 handlers = [
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
263 (r"/", Index),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
264 (r'/(jquery.min.js)', Static),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
265 (r'/(lastInput|lastOutput)Graph', GraphResource),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
266 (r'/ntGraphs', NtGraphs),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
267 (r'/rules', Rules),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
268 ]
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
269 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
270
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
271 if __name__ == '__main__':
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
272 r = Reasoning()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
273 #import twisted.python.log
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
274 #twisted.python.log.startLogging(sys.stdout)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
275
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
276 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
277 reactor.listenTCP(9071, Application(r))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
278 reactor.run()