def __init__(self, mockOutput=False): self.prevGraph = None self.rulesN3 = "(not read yet)" self.inferred = Graph() # gets replaced in each graphChanged call self.outputGraph = PatchableGraph() # copy of inferred, for now self.inputGraph = InputGraph([], self.graphChanged) self.actions = Actions(self.inputGraph, sendToLiveClients, mockOutput=mockOutput) self.inputGraph.updateFileData()
def __init__(self): self.prevGraph = None self.actions = Actions(sendToLiveClients) self.rulesN3 = "(not read yet)" self.inferred = Graph() # gets replaced in each graphChanged call self.inputGraph = InputGraph([], self.graphChanged) self.inputGraph.updateFileData()
def graphChanged(self, inputGraph: InputGraph, oneShot=False, oneShotGraph: Graph = None): """ If we're getting called for a oneShot event, the oneShotGraph statements are already in inputGraph.getGraph(). """ log.info("----------------------") log.info("graphChanged (oneShot=%s %s stmts):", oneShot, len(oneShotGraph) if oneShotGraph is not None else 0) if oneShotGraph: for stmt in oneShotGraph: log.info(" oneshot -> %s", ntStatement(stmt)) t1 = time.time() oldInferred = self.inferred try: ruleStatStmts, ruleParseSec = self.updateRules() self.inferred, inferSec = self._makeInferred(inputGraph.getGraph()) self.inferred += unquoteOutputStatements(self.inferred) self.inferred += ruleStatStmts if oneShot: # It's possible a oneShotGraph statement didn't # trigger a rule to do something, but was itself the # output statement. Probably we could just mix in the # whole inputGraph here and not special-case the # oneShotGraph. self.inferred += oneShotGraph t3 = time.time() self.actions.putResults(self.inferred) putResultsTime = time.time() - t3 finally: if oneShot: self.inferred = oldInferred log.info( "graphChanged took %.1f ms (rule parse %.1f ms, infer %.1f ms, putResults %.1f ms)" % ((time.time() - t1) * 1000, ruleParseSec * 1000, inferSec * 1000, putResultsTime * 1000)) if not oneShot: self.copyOutput()
class Reasoning(object): ruleStore: ConjunctiveGraph def __init__(self, mockOutput=False): self.prevGraph = None self.rulesN3 = "(not read yet)" self.inferred = Graph() # gets replaced in each graphChanged call self.outputGraph = PatchableGraph() # copy of inferred, for now self.inputGraph = InputGraph([], self.graphChanged) self.actions = Actions(self.inputGraph, sendToLiveClients, mockOutput=mockOutput) self.inputGraph.updateFileData() @UPDATE_RULES_CALLS.time() def updateRules(self): rulesPath = 'rules.n3' try: t1 = time.time() self.rulesN3, self.ruleStore = readRules( rulesPath, outputPatterns=[ # Incomplete. See escapeoutputstatements.py for # explanation. (None, ROOM['brightness'], None), (None, ROOM['playState'], None), (None, ROOM['powerState'], None), (None, ROOM['state'], None), ]) ruleParseTime = time.time() - t1 except ValueError: # this is so if you're just watching the inferred output, # you'll see the error too self.inferred = Graph() self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], Literal(traceback.format_exc()))) self.copyOutput() raise return [(ROOM['reasoner'], ROOM['ruleParseTime'], Literal(ruleParseTime))], ruleParseTime @GRAPH_CHANGED_CALLS.time() def graphChanged(self, inputGraph: InputGraph, oneShot=False, oneShotGraph: Graph = None): """ If we're getting called for a oneShot event, the oneShotGraph statements are already in inputGraph.getGraph(). """ log.info("----------------------") log.info("graphChanged (oneShot=%s %s stmts):", oneShot, len(oneShotGraph) if oneShotGraph is not None else 0) if oneShotGraph: for stmt in oneShotGraph: log.info(" oneshot -> %s", ntStatement(stmt)) t1 = time.time() oldInferred = self.inferred try: ruleStatStmts, ruleParseSec = self.updateRules() self.inferred, inferSec = self._makeInferred(inputGraph.getGraph()) self.inferred += unquoteOutputStatements(self.inferred) self.inferred += ruleStatStmts if oneShot: # It's possible a oneShotGraph statement didn't # trigger a rule to do something, but was itself the # output statement. Probably we could just mix in the # whole inputGraph here and not special-case the # oneShotGraph. self.inferred += oneShotGraph t3 = time.time() self.actions.putResults(self.inferred) putResultsTime = time.time() - t3 finally: if oneShot: self.inferred = oldInferred log.info( "graphChanged took %.1f ms (rule parse %.1f ms, infer %.1f ms, putResults %.1f ms)" % ((time.time() - t1) * 1000, ruleParseSec * 1000, inferSec * 1000, putResultsTime * 1000)) if not oneShot: self.copyOutput() def copyOutput(self): self.outputGraph.setToGraph( (s, p, o, ROOM['inferred']) for s, p, o in self.inferred) def _makeInferred(self, inputGraph: ConjunctiveGraph): t1 = time.time() out = infer(inputGraph, self.ruleStore) for p, n in NS.items(): out.bind(p, n, override=True) inferenceTime = time.time() - t1 out.add( (ROOM['reasoner'], ROOM['inferenceTime'], Literal(inferenceTime))) return out, inferenceTime
class Reasoning(object): def __init__(self): self.prevGraph = None self.actions = Actions(sendToLiveClients) self.rulesN3 = "(not read yet)" self.inferred = Graph() # gets replaced in each graphChanged call self.inputGraph = InputGraph([], self.graphChanged) self.inputGraph.updateFileData() def updateRules(self): rulesPath = 'rules.n3' try: t1 = time.time() self.rulesN3, self.ruleStore = readRules( rulesPath, outputPatterns=[ # Incomplete. See escapeoutputstatements.py for # explanation. (None, ROOM['brightness'], None), (None, ROOM['playState'], None), (None, ROOM['powerState'], None), (None, ROOM['state'], None), ]) ruleParseTime = time.time() - t1 except ValueError: # this is so if you're just watching the inferred output, # you'll see the error too self.inferred = Graph() self.inferred.add((ROOM['reasoner'], ROOM['ruleParseError'], Literal(traceback.format_exc()))) raise return [(ROOM['reasoner'], ROOM['ruleParseTime'], Literal(ruleParseTime))], ruleParseTime @STATS.graphChanged.time() def graphChanged(self, inputGraph, oneShot=False, oneShotGraph=None): """ If we're getting called for a oneShot event, the oneShotGraph statements are already in inputGraph.getGraph(). """ log.info("----------------------") log.info("graphChanged (oneShot=%s):", oneShot) t1 = time.time() oldInferred = self.inferred try: ruleStatStmts, ruleParseSec = self.updateRules() self.inferred, inferSec = self._makeInferred(inputGraph.getGraph()) self.inferred += unquoteOutputStatements(self.inferred) self.inferred += ruleStatStmts if oneShot: # It's possible a oneShotGraph statement didn't # trigger a rule to do something, but was itself the # output statement. Probably we could just mix in the # whole inputGraph here and not special-case the # oneShotGraph. self.inferred += oneShotGraph t3 = time.time() self.actions.putResults(self.inputGraph.getGraph(), self.inferred) putResultsTime = time.time() - t3 finally: if oneShot: self.inferred = oldInferred log.info("graphChanged took %.1f ms (rule parse %.1f ms, infer %.1f ms, putResults %.1f ms)" % ((time.time() - t1) * 1000, ruleParseSec * 1000, inferSec * 1000, putResultsTime * 1000)) def _makeInferred(self, inputGraph): t1 = time.time() out = infer(inputGraph, self.ruleStore) for p, n in NS.iteritems(): out.bind(p, n, override=True) inferenceTime = time.time() - t1 out.add((ROOM['reasoner'], ROOM['inferenceTime'], Literal(inferenceTime))) return out, inferenceTime