annotate service/reasoning/reasoning.py @ 984:5da9200418db

reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands Ignore-this: 7646fba7b5496eb1f33a87452af6c6de darcs-hash:20150514083000-312f9-ef4db22cf33250e690b6021b6b1968cc788d14cb
author drewp <drewp@bigasterisk.com>
date Thu, 14 May 2015 01:30:00 -0700
parents 3bb18b7d21df
children 47682350e6f2
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 """
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
3 Graph consists of:
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
4 input/* (read at startup)
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
5 webinput/* (new files are noticed in here)
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
6 any number of remote graphs, specified in the other graph as objects of (:reasoning, :source, *), reread constantly
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
7
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 gather subgraphs from various services, run them through a rules
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 engine, and make http requests with the conclusions.
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 E.g. 'when drew's phone is near the house, and someone is awake,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 unlock the door when the door's motion sensor is activated'
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 When do we gather? The services should be able to trigger us, perhaps
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 with PSHB, that their graph has changed.
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 """
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 from twisted.internet import reactor, task
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
20 from twisted.python.filepath import FilePath
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
21 import time, traceback, sys, json, logging, urllib
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
22 from rdflib import Graph, ConjunctiveGraph
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
23 from rdflib import Namespace, URIRef, Literal, RDF
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
24 from rdflib.parser import StringInputSource
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
25 from cyclone.httpclient import fetch
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
26 import cyclone.web, cyclone.websocket
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 from inference import addTrig, infer
853
39857f82922e refactor graph ops
drewp <drewp@bigasterisk.com>
parents: 852
diff changeset
28 from graphop import graphEqual
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
29 from docopt import docopt
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
30
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
31 from FuXi.Rete.RuleStore import N3RuleStore
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 sys.path.append("../../lib")
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 from logsetup import log
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
35 log.setLevel(logging.WARN)
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
36 outlog = logging.getLogger('output')
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
37 outlog.setLevel(logging.WARN)
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 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
40 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
41
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
42
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
43 class InputGraph(object):
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
44 def __init__(self, inputDirs, onChange):
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
45 """
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
46 this has one Graph that's made of:
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
47 - all .n3 files from inputDirs (read at startup)
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
48 - all the remote graphs, specified in the file graphs
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
49
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
50 call updateFileData or updateRemoteData to reread those
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
51 graphs. getGraph to access the combined graph.
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
52
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
53 onChange(self) is called if the contents of the full graph
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
54 change (in an interesting way) during updateFileData or
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
55 updateRemoteData. Interesting means statements other than the
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
56 ones with the predicates on the boring list. onChange(self,
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
57 oneShot=True) means: don't store the result of this change
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
58 anywhere; it needs to be processed only once
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
59 """
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
60 self.inputDirs = inputDirs
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
61 self.onChange = onChange
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
62 self._fileGraph = Graph()
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
63 self._remoteGraph = None
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
64 self._combinedGraph = None
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
65 self._oneShotAdditionGraph = None
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
66
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
67 def updateFileData(self):
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
68 """
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
69 make sure we contain the correct data from the files in inputDirs
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
70 """
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
71 # this sample one is actually only needed for the output, but I don't
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
72 # think I want to have a separate graph for the output
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
73 # handling
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
74 log.debug("read file graphs")
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
75 for fp in FilePath("input").walk():
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
76 if fp.isdir():
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
77 continue
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
78 if fp.splitext()[1] != '.n3':
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
79 continue
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
80 log.debug("read %s", fp)
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
81 # todo: if this fails, leave the report in the graph
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
82 self._fileGraph.parse(fp.open(), format="n3")
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
83 self._combinedGraph = None
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
84
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
85 self.onChange(self)
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
86
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
87 def updateRemoteData(self):
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
88 """
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
89 read all remote graphs (which are themselves enumerated within
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
90 the file data)
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
91 """
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
92 log.debug("read remote graphs")
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
93 g = ConjunctiveGraph()
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
94 for source in self._fileGraph.objects(ROOM['reasoning'],
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
95 ROOM['source']):
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
96 try:
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
97 # this part could be parallelized
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
98 fetchTime = addTrig(g, source)
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
99 except Exception, e:
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
100 log.error(" adding source %s: %s", source, e)
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
101 g.add((URIRef(source), ROOM['graphLoadError'], Literal(str(e))))
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
102 g.add((URIRef(source), RDF.type, ROOM['FailedGraphLoad']))
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
103 else:
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
104 g.add((URIRef(source), ROOM['graphLoadSecs'],
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
105 Literal(fetchTime)))
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
106 prevGraph = self._remoteGraph
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
107 self._remoteGraph = g
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
108 self._combinedGraph = None
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
109 if (prevGraph is None or
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
110 not graphEqual(g, prevGraph, ignorePredicates=[
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
111 ROOM.signalStrength,
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
112 ROOM.graphLoadSecs,
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
113 # perhaps anything with a number-datatype for its
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
114 # object should be filtered out, and you have to make
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
115 # an upstream quantization (e.g. 'temp high'/'temp
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
116 # low') if you want to do reasoning on the difference
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
117 URIRef("http://bigasterisk.com/map#lastSeenAgoSec"),
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
118 URIRef("http://bigasterisk.com/map#lastSeenAgo"),
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
119 URIRef("http://projects.bigasterisk.com/room/usingPower"),
859
379cefec542e screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
120 URIRef("http://projects.bigasterisk.com/room/idleTimeMinutes"),
379cefec542e screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
121 URIRef("http://projects.bigasterisk.com/room/idleTimeMs"),
923
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 919
diff changeset
122 ROOM.history,
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
123 ])):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
124 log.debug(" remote graph changed")
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
125 self.onChange(self)
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
126 else:
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
127 log.debug(" remote graph is unchanged")
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
128
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
129 def addOneShot(self, g):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
130 """
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
131 add this graph to the total, call onChange, and then revert
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
132 the addition of this graph
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
133 """
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
134 self._oneShotAdditionGraph = g
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
135 self._combinedGraph = None
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
136 try:
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
137 self.onChange(self, oneShot=True, oneShotGraph=g)
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
138 finally:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
139 self._oneShotAdditionGraph = None
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
140 self._combinedGraph = None
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
141
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
142 def getGraph(self):
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
143 """rdflib Graph with the file+remote contents of the input graph"""
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
144 # this could be much faster with the combined readonly graph
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
145 # view from rdflib
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
146 if self._combinedGraph is None:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
147 self._combinedGraph = Graph()
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
148 if self._fileGraph:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
149 for s in self._fileGraph:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
150 self._combinedGraph.add(s)
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
151 if self._remoteGraph:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
152 for s in self._remoteGraph:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
153 self._combinedGraph.add(s)
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
154 if self._oneShotAdditionGraph:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
155 for s in self._oneShotAdditionGraph:
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
156 self._combinedGraph.add(s)
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
157
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
158 return self._combinedGraph
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
159
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
160 class Reasoning(object):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
161 def __init__(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
162 self.prevGraph = None
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
163 self.lastPollTime = 0
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
164 self.lastError = ""
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
165
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
166 self.rulesN3 = "(not read yet)"
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
167 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
168
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
169 self.inputGraph = InputGraph([], self.graphChanged)
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
170 self.inputGraph.updateFileData()
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
171
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
172
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
173 def readRules(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
174 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
175 self.ruleStore = N3RuleStore()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
176 self.ruleGraph = Graph(self.ruleStore)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
177 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
178
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
179 def poll(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
180 try:
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
181 self.inputGraph.updateRemoteData()
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
182 self.lastPollTime = time.time()
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
183 except Exception, e:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
184 log.error(traceback.format_exc())
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
185 self.lastError = str(e)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
186
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
187 def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None):
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
188 t1 = time.time()
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
189 oldInferred = self.inferred
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
190 try:
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
191 try:
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
192 t1 = time.time()
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
193 self.readRules()
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
194 ruleParseTime = time.time() - t1
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
195 except ValueError, e:
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
196 # this is so if you're just watching the inferred output,
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
197 # you'll see the error too
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
198 self.inferred = Graph()
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
199 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'],
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
200 Literal(traceback.format_exc())))
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
201 raise
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
202
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
203 g = inputGraph.getGraph()
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
204 self.inferred = self._makeInferred(g)
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
205 self.inferred.add((ROOM['reasoner'], ROOM['ruleParseTime'],
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
206 Literal(ruleParseTime)))
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
207
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
208 if oneShot:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
209 # unclear where this should go, but the oneshot'd
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
210 # statements should be just as usable as inferred
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
211 # ones.
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
212 for s in oneShotGraph:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
213 self.inferred.add(s)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
214
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
215 t2 = time.time()
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
216 self.putResults(self.inferred)
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
217 putResultsTime = time.time() - t2
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
218 t3 = time.time()
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
219 self._postToMagma(g)
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
220 postMagmaTime = time.time() - t3
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
221 finally:
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
222 if oneShot:
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
223 self.inferred = oldInferred
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
224 log.info("graphChanged %.1f ms (putResults %.1f ms; postToMagma %.1f ms)" %
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
225 ((time.time() - t1) * 1000,
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
226 putResultsTime * 1000,
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
227 postMagmaTime * 1000))
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
228
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
229 def _makeInferred(self, inputGraph):
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
230 t1 = time.time()
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
231 out = infer(inputGraph, self.ruleStore)
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
232 inferenceTime = time.time() - t1
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
233
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
234 out.add((ROOM['reasoner'], ROOM['inferenceTime'],
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
235 Literal(inferenceTime)))
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
236 return out
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
237
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
238 def _postToMagma(self, inputGraph):
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
239 return # not up right now
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
240 inputGraphNt = inputGraph.serialize(format="nt")
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
241 inferredNt = self.inferred.serialize(format="nt")
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
242 body = json.dumps({"input": inputGraphNt,
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
243 "inferred": inferredNt})
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
244 def err(e):
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
245 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
246 log.error(e)
851
0d86b3955bcd rewriting reasoning to use graphs for config
drewp <drewp@bigasterisk.com>
parents: 850
diff changeset
247
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
248 fetch("http://bang:8014/reasoningChange",
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
249 method="POST",
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
250 timeout=2,
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents: 917
diff changeset
251 postdata=body,
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
252 headers={"content-type" : ["application/json"]}).addErrback(err)
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
253
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
254 def _put(self, url, payload):
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
255 def err(e):
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
256 outlog.warn(" put %s failed", url)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
257 outlog.info(" PUT %s payload=%r", url, payload)
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents: 917
diff changeset
258 fetch(url, method="PUT", postdata=payload, timeout=2).addErrback(err)
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
259
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
260 def putResults(self, inferred):
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
261 """
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
262 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
263 getting made
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
264
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
265 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
266 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
267 (?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
268 PUT ?url <- ?val
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
269
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
270 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
271 :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
272 """
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
273 deviceGraph = self.inputGraph.getGraph()
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
274 self.oneShotPostActions(deviceGraph, inferred)
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
275 for dev, pred in [
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
276 # 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
277 # 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
278 # be the source of this list
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
279 #(DEV.theaterDoorLock, ROOM.state),
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
280 #(URIRef('http://bigasterisk.com/host/bang/monitor'), ROOM.powerState),
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
281 (URIRef('http://bigasterisk.com/host/dash/monitor'), ROOM.powerState),
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
282 (URIRef('http://projects.bigasterisk.com/room/storageCeilingLedLong'), ROOM.brightness),
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
283 (URIRef('http://projects.bigasterisk.com/room/storageCeilingLedCross'), ROOM.brightness),
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
284 ]:
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
285 url = deviceGraph.value(dev, ROOM.putUrl)
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
286
916
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
287 if url and dev == DEV.theaterDoorLock: # ew
e8cce78a79d6 switch reasoning from restkit to async cyclone http client
drewp <drewp@bigasterisk.com>
parents: 882
diff changeset
288 self._put(url+"/mode", payload="output")
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
289
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
290 inferredObjects = list(inferred.objects(dev, pred))
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
291 if len(inferredObjects) == 0:
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
292 self.putZero(deviceGraph, dev, pred, url)
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
293 elif len(inferredObjects) == 1:
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
294 self.putInferred(deviceGraph, url, inferredObjects[0])
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
295 elif len(inferredObjects) > 1:
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
296 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
297 (dev, pred, inferredObjects))
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
298 # 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
299
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
300 #self.frontDoorPuts(deviceGraph, inferred)
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
301
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
302
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
303 def oneShotPostActions(self, deviceGraph, inferred):
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
304 """
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
305 Inferred graph may contain some one-shot statements. We'll send
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
306 statement objects to anyone on web sockets, and also generate
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
307 POST requests as described in the graph.
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
308
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
309 one-shot statement ?s ?p ?o
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
310 with this in the graph:
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
311 ?osp a :OneShotPost
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
312 ?osp :subject ?s
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
313 ?osp :predicate ?p
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
314 this will cause a post to ?o
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
315 """
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
316 # nothing in this actually makes them one-shot yet. they'll
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
317 # just fire as often as we get in here, which is not desirable
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
318 log.info("oneShotPostActions")
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
319 def err(e):
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
320 outlog.warn("post %s failed", postTarget)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
321 for osp in deviceGraph.subjects(RDF.type, ROOM['OneShotPost']):
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
322 s = deviceGraph.value(osp, ROOM['subject'])
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
323 p = deviceGraph.value(osp, ROOM['predicate'])
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
324 if s is None or p is None:
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
325 continue
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
326 for postTarget in inferred.objects(s, p):
882
1e2a3f85c61a keep hacking reasoning to handle my output actions
drewp <drewp@bigasterisk.com>
parents: 869
diff changeset
327 log.info("post target %r", postTarget)
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
328 # this packet ought to have 'oneShot' in it somewhere
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
329 sendToLiveClients({"s":s, "p":p, "o":postTarget})
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
330
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
331 outlog.info(" POST %s", postTarget)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
332 fetch(postTarget, method="POST", timeout=2).addErrback(err)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
333 self.postMpdCommands(inferred)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
334
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
335 def postMpdCommands(self, inferred):
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
336 """special case to be eliminated. mpd play urls are made of an
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
337 mpd service and a song/album/playlist uri to be played.
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
338 Ideally the graph rules would assemble these like
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
339 http://{mpd}/addAndPlay?uri={toPlay} or maybe toPlay as the payload
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
340 which would be fairly general but still allow toPlay uris to
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
341 be matched with any player."""
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
342 def post(postTarget):
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
343 outlog.info("special mpd POST %s", postTarget)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
344 def err(e):
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
345 outlog.warn("post %s failed", postTarget)
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
346 fetch(postTarget, method="POST", timeout=2).addErrback(err)
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
347
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
348 rootSkippingAuth = "http://brace:9009/"
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
349 for mpd in [URIRef("http://bigasterisk.com/host/brace/mpd")]:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
350
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
351
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
352 for song in inferred.objects(mpd, ROOM['startMusic']):
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
353 outlog.info("mpd statement: %r" % song)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
354 assert song.startswith('http://bigasterisk.com/music/')
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
355 post(rootSkippingAuth + "addAndPlay" + urllib.quote(song[len("http://bigasterisk.com/music"):]))
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
356
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
357 for state in inferred.objects(mpd, ROOM['playState']):
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
358 log.info('hello playstate %s', state)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
359 if state == ROOM['pause']:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
360 log.info("mpd %s %s", mpd, state)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
361 post(rootSkippingAuth + "mpd/pause")
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
362 for vol in inferred.objects(mpd, ROOM['audioState']):
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
363 if vol == ROOM['volumeStepUp']:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
364 post(rootSkippingAuth + "volumeAdjust?amount=6&max=70")
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
365 if vol == ROOM['volumeStepDown']:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
366 post(rootSkippingAuth + "volumeAdjust?amount=-6&min=10")
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
367
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
368 def putZero(self, deviceGraph, dev, pred, putUrl):
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
369 # zerovalue should be a function of pred as well.
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
370 value = deviceGraph.value(dev, ROOM.zeroValue)
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
371 if value is not None:
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
372 outlog.info("put zero (%r) to %s", value, putUrl)
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents: 917
diff changeset
373 self._put(putUrl, payload=str(value))
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
374 # 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
375 # for feedback
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
376
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
377 def putInferred(self, deviceGraph, putUrl, obj):
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
378 """
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
379 HTTP PUT to putUrl, with a payload that's either obj's :putValue
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
380 or obj itself.
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
381 """
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
382 value = deviceGraph.value(obj, ROOM.putValue)
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
383 if value is not None:
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
384 outlog.info("put %s to %s", value, putUrl)
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents: 917
diff changeset
385 self._put(putUrl, payload=str(value))
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
386 elif isinstance(obj, Literal):
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
387 outlog.info("put %s to %s", obj, putUrl)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
388 self._put(putUrl, payload=str(obj))
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
389 else:
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
390 outlog.warn("don't know what payload to put for %s. obj=%r",
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
391 putUrl, obj)
850
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
392
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
393 def frontDoorPuts(self, deviceGraph, inferred):
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
394 # 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
395 brt = inferred.value(DEV.frontDoorLcd, ROOM.brightness)
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
396 if brt is None:
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
397 return
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
398 url = deviceGraph.value(DEV.frontDoorLcdBrightness, ROOM.putUrl)
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
399 outlog.info("put lcd %s brightness %s", url, brt)
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents: 917
diff changeset
400 self._put(str(url) + "?brightness=%s" % str(brt), payload='')
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
401
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
402 msg = "open %s motion %s" % (
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
403 inferred.value(DEV['frontDoorOpenIndicator'], ROOM.text),
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
404 inferred.value(DEV['frontDoorMotionIndicator'], ROOM.text))
838
36dbbb01d689 redo the http PUT request part of the reasoner
drewp <drewp@bigasterisk.com>
parents: 825
diff changeset
405 # 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
406 # 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
407 #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
408
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
409
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
410
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
411 class Index(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
412 def get(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
413 # 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
414 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
415 if ago > 2:
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
416 self.set_status(500)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
417 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
418 (ago, self.settings.reasoning.lastError))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
419 return
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
420 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
421 self.write(open('index.html').read())
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
422
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
423 class ImmediateUpdate(cyclone.web.RequestHandler):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
424 def put(self):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
425 """
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
426 request an immediate load of the remote graphs; the thing we
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
427 do in the background anyway. No payload.
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
428
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
429 Using PUT because this is idempotent and retryable and
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
430 everything.
859
379cefec542e screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
431
379cefec542e screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
432 todo: this should do the right thing when many requests come
379cefec542e screen out more values that change on every update
drewp <drewp@bigasterisk.com>
parents: 854
diff changeset
433 in very quickly
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
434 """
854
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
435 print self.request.headers
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
436 log.info("immediateUpdate from %s",
1815d2343a3f cleanup
drewp <drewp@bigasterisk.com>
parents: 853
diff changeset
437 self.request.headers.get('User-Agent', '?'))
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
438 r.poll()
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
439 self.set_status(202)
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
440
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
441 def parseRdf(text, contentType):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
442 g = Graph()
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
443 g.parse(StringInputSource(text), format={
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
444 'text/n3': 'n3',
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
445 }[contentType])
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
446 return g
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
447
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
448 class OneShot(cyclone.web.RequestHandler):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
449 def post(self):
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
450 """
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
451 payload is an rdf graph. The statements are momentarily added
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
452 to the input graph for exactly one update.
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
453
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
454 todo: how do we go from a transition like doorclosed-to-open
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
455 to a oneshot event? the upstream shouldn't have to do it. Do
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
456 we make those oneshot events here? for every object change?
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
457 there are probably special cases regarding startup time when
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
458 everything appears to be a 'change'.
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
459 """
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
460 try:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
461 g = parseRdf(self.request.body, self.request.headers['content-type'])
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
462 for s in g:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
463 print "stmt", s
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
464 if not len(g):
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
465 log.warn("incoming oneshot graph had no statements: %r", self.request.body)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
466 return
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
467 self.settings.reasoning.inputGraph.addOneShot(g)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
468 except Exception as e:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
469 log.error(e)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
470 raise
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
471
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
472 # for reuse
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
473 class GraphResource(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
474 def get(self, which):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
475 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
476 r = self.settings.reasoning
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
477 g = {'lastInput': r.inputGraph.getGraph(),
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
478 'lastOutput': r.inferred,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
479 }[which]
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
480 self.write(self.jsonRdf(g))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
481
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
482 def jsonRdf(self, g):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
483 return json.dumps(sorted(list(g)))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
484
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
485 class NtGraphs(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
486 """same as what gets posted above"""
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
487 def get(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
488 r = self.settings.reasoning
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
489 inputGraphNt = r.inputGraph.getGraph().serialize(format="nt")
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
490 inferredNt = r.inferred.serialize(format="nt")
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
491 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
492 self.write(json.dumps({"input": inputGraphNt,
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
493 "inferred": inferredNt}))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
494
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
495 class Rules(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
496 def get(self):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
497 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
498 self.write(self.settings.reasoning.rulesN3)
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
499
850
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
500 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
501 def get(self):
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
502 self.set_header("Content-Type", "text/plain")
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
503 g = self.settings.reasoning.inputGraph.getGraph()
850
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
504 msg = ""
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
505 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
506 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
507 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
508 if not msg:
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
509 self.finish("all inputs ok")
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
510 return
850
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
511 self.set_status(500)
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
512 self.finish(msg)
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
513
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
514 class Static(cyclone.web.RequestHandler):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
515 def get(self, p):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
516 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
517
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
518 liveClients = set()
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
519 def sendToLiveClients(d=None, asJson=None):
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
520 j = asJson or json.dumps(d)
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
521 for c in liveClients:
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
522 c.sendMessage(j)
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
523
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
524 class Events(cyclone.websocket.WebSocketHandler):
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
525
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
526 def connectionMade(self, *args, **kwargs):
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
527 log.info("websocket opened")
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
528 liveClients.add(self)
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
529
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
530 def connectionLost(self, reason):
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
531 log.info("websocket closed")
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
532 liveClients.remove(self)
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
533
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
534 def messageReceived(self, message):
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
535 log.info("got message %s" % message)
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
536
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
537 class Application(cyclone.web.Application):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
538 def __init__(self, reasoning):
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
539 handlers = [
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
540 (r"/", Index),
852
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
541 (r"/immediateUpdate", ImmediateUpdate),
6ccd930834d1 scan more input files. oneshot and immediate update features.
drewp <drewp@bigasterisk.com>
parents: 851
diff changeset
542 (r"/oneShot", OneShot),
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
543 (r'/(jquery.min.js)', Static),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
544 (r'/(lastInput|lastOutput)Graph', GraphResource),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
545 (r'/ntGraphs', NtGraphs),
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
546 (r'/rules', Rules),
850
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
547 (r'/status', Status),
869
15af9d8c7186 reasoning has a websocket server that broadcasts some events
drewp <drewp@bigasterisk.com>
parents: 859
diff changeset
548 (r'/events', Events),
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
549 ]
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
550 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
551
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
552 if __name__ == '__main__':
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
553
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
554 arg = docopt("""
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
555 Usage: reasoning.py [options]
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
556
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
557 -v Verbose
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
558 """)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
559
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
560 r = Reasoning()
984
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
561 if arg['-v']:
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
562 import twisted.python.log
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
563 twisted.python.log.startLogging(sys.stdout)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
564 log.setLevel(logging.DEBUG)
5da9200418db reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp <drewp@bigasterisk.com>
parents: 934
diff changeset
565 outlog.setLevel(logging.DEBUG)
850
887d47682d94 /status page, errors on failed graphs, time reports of successful fetches
drewp <drewp@bigasterisk.com>
parents: 838
diff changeset
566
825
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
567 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
568 reactor.listenTCP(9071, Application(r))
fc753b24f69a move reasoning from /my/proj/room, new integration with magma
drewp <drewp@bigasterisk.com>
parents:
diff changeset
569 reactor.run()