Mercurial > code > home > repos > homeauto
comparison service/reasoning/reasoning.py @ 1555:606bb60a5e5c
something with rx on inputgraph, i forget. also cleanup imports and logging of oneshot
Ignore-this: 263923a8d12db2173017bc9dbfc638ba
darcs-hash:cedac5a49c9cbd0b8c34c80efa9282853fda2cc5
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Thu, 13 Feb 2020 23:00:06 -0800 |
parents | 0da337780f22 |
children | f3f667769aef |
comparison
equal
deleted
inserted
replaced
1554:bb765a6bf09a | 1555:606bb60a5e5c |
---|---|
20 import json, time, traceback, sys | 20 import json, time, traceback, sys |
21 from logging import getLogger, DEBUG, WARN | 21 from logging import getLogger, DEBUG, WARN |
22 | 22 |
23 from colorlog import ColoredFormatter | 23 from colorlog import ColoredFormatter |
24 from docopt import docopt | 24 from docopt import docopt |
25 from rdflib import Namespace, Literal, RDF, Graph | 25 from rdflib import Namespace, Literal, RDF, Graph, URIRef |
26 from twisted.internet import reactor, task, defer | 26 from twisted.internet import reactor, defer |
27 from twisted.internet.defer import inlineCallbacks | |
28 import cyclone.web, cyclone.websocket | 27 import cyclone.web, cyclone.websocket |
29 | 28 |
30 from greplin import scales | 29 from greplin import scales |
31 from greplin.scales.cyclonehandler import StatsHandler | 30 from greplin.scales.cyclonehandler import StatsHandler |
32 | 31 |
47 STATS = scales.collection('/web', | 46 STATS = scales.collection('/web', |
48 scales.PmfStat('graphChanged'), | 47 scales.PmfStat('graphChanged'), |
49 scales.PmfStat('updateRules'), | 48 scales.PmfStat('updateRules'), |
50 ) | 49 ) |
51 | 50 |
51 def ntStatement(stmt): | |
52 def compact(u): | |
53 if isinstance(u, URIRef) and u.startswith(ROOM): | |
54 return 'room:' + u[len(ROOM):] | |
55 return u.n3() | |
56 return '%s %s %s .' % (compact(stmt[0]), compact(stmt[1]), compact(stmt[2])) | |
57 | |
52 class Reasoning(object): | 58 class Reasoning(object): |
53 def __init__(self, mockOutput=False): | 59 def __init__(self, mockOutput=False): |
54 self.prevGraph = None | 60 self.prevGraph = None |
55 | 61 |
56 self.actions = Actions(sendToLiveClients, mockOutput=mockOutput) | |
57 | |
58 self.rulesN3 = "(not read yet)" | 62 self.rulesN3 = "(not read yet)" |
59 self.inferred = Graph() # gets replaced in each graphChanged call | 63 self.inferred = Graph() # gets replaced in each graphChanged call |
60 self.outputGraph = PatchableGraph() # copy of inferred, for now | 64 self.outputGraph = PatchableGraph() # copy of inferred, for now |
61 | 65 |
62 self.inputGraph = InputGraph([], self.graphChanged) | 66 self.inputGraph = InputGraph([], self.graphChanged) |
67 self.actions = Actions(self.inputGraph, sendToLiveClients, mockOutput=mockOutput) | |
63 self.inputGraph.updateFileData() | 68 self.inputGraph.updateFileData() |
64 | 69 |
65 @STATS.updateRules.time() | 70 @STATS.updateRules.time() |
66 def updateRules(self): | 71 def updateRules(self): |
67 rulesPath = 'rules.n3' | 72 rulesPath = 'rules.n3' |
97 log.info("----------------------") | 102 log.info("----------------------") |
98 log.info("graphChanged (oneShot=%s %s stmts):", | 103 log.info("graphChanged (oneShot=%s %s stmts):", |
99 oneShot, len(oneShotGraph) if oneShotGraph is not None else 0) | 104 oneShot, len(oneShotGraph) if oneShotGraph is not None else 0) |
100 if oneShotGraph: | 105 if oneShotGraph: |
101 for stmt in oneShotGraph: | 106 for stmt in oneShotGraph: |
102 log.info(" OS-> %r", stmt) | 107 log.info(" oneshot -> %s", ntStatement(stmt)) |
103 t1 = time.time() | 108 t1 = time.time() |
104 oldInferred = self.inferred | 109 oldInferred = self.inferred |
105 try: | 110 try: |
106 ruleStatStmts, ruleParseSec = self.updateRules() | 111 ruleStatStmts, ruleParseSec = self.updateRules() |
107 | 112 |
118 # whole inputGraph here and not special-case the | 123 # whole inputGraph here and not special-case the |
119 # oneShotGraph. | 124 # oneShotGraph. |
120 self.inferred += oneShotGraph | 125 self.inferred += oneShotGraph |
121 | 126 |
122 t3 = time.time() | 127 t3 = time.time() |
123 self.actions.putResults(self.inputGraph.getGraph(), self.inferred) | 128 self.actions.putResults(self.inferred) |
124 putResultsTime = time.time() - t3 | 129 putResultsTime = time.time() - t3 |
125 finally: | 130 finally: |
126 if oneShot: | 131 if oneShot: |
127 self.inferred = oldInferred | 132 self.inferred = oldInferred |
128 log.info("graphChanged took %.1f ms (rule parse %.1f ms, infer %.1f ms, putResults %.1f ms)" % | 133 log.info("graphChanged took %.1f ms (rule parse %.1f ms, infer %.1f ms, putResults %.1f ms)" % |