Mercurial > code > home > repos > homeauto
comparison service/reasoning/inference.py @ 1685:6b80a6c58907
minor changes to several services
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 23:12:43 -0700 |
parents | c8562ace4917 |
children |
comparison
equal
deleted
inserted
replaced
1684:c41af73a410f | 1685:6b80a6c58907 |
---|---|
1 """ | 1 """ |
2 see ./reasoning for usage | 2 see ./reasoning for usage |
3 """ | 3 """ |
4 | 4 |
5 import contextlib | |
6 import os | 5 import os |
6 import logging | |
7 | 7 |
8 from prometheus_client import Summary | 8 from prometheus_client import Summary |
9 from rdflib import Graph, Namespace | 9 from rdflib import Graph, Namespace |
10 from rdflib.graph import ConjunctiveGraph | 10 from rdflib.graph import ConjunctiveGraph |
11 from rdflib.parser import StringInputSource | 11 from rdflib.parser import StringInputSource |
12 | 12 |
13 from escapeoutputstatements import escapeOutputStatements | 13 from escapeoutputstatements import escapeOutputStatements |
14 | |
15 log = logging.getLogger() | |
14 | 16 |
15 READ_RULES_CALLS = Summary('read_rules_calls', 'calls') | 17 READ_RULES_CALLS = Summary('read_rules_calls', 'calls') |
16 | 18 |
17 ROOM = Namespace("http://projects.bigasterisk.com/room/") | 19 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
18 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | 20 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') |
75 workingSet = ConjunctiveGraph() | 77 workingSet = ConjunctiveGraph() |
76 workingSet.addN(graph.quads()) | 78 workingSet.addN(graph.quads()) |
77 | 79 |
78 implied = ConjunctiveGraph() | 80 implied = ConjunctiveGraph() |
79 | 81 |
82 bailout_iterations = 100 | |
80 delta = 1 | 83 delta = 1 |
81 while delta > 0: | 84 while delta > 0 and bailout_iterations > 0: |
85 bailout_iterations -= 1 | |
82 delta = -len(implied) | 86 delta = -len(implied) |
83 | 87 |
84 for r in rules: | 88 for r in rules: |
85 if r[1] == LOG['implies']: | 89 if r[1] == LOG['implies']: |
86 containsSetup = all(st in workingSet for st in r[0]) | 90 containsSetup = all(st in workingSet for st in r[0]) |
100 log.info(f' this inference round added {delta} more implied stmts') | 104 log.info(f' this inference round added {delta} more implied stmts') |
101 log.info(f'{len(implied)} stmts implied:') | 105 log.info(f'{len(implied)} stmts implied:') |
102 for st in implied: | 106 for st in implied: |
103 log.info(f' {st}') | 107 log.info(f' {st}') |
104 return implied | 108 return implied |
105 | |
106 # based on fuxi/tools/rdfpipe.py | |
107 target = Graph() | |
108 tokenSet = generateTokenSet(graph) | |
109 with _dontChangeRulesStore(rules): | |
110 network = ReteNetwork(rules, inferredTarget=target) | |
111 network.feedFactsToAdd(tokenSet) | |
112 | |
113 return target | |
114 | |
115 | |
116 @contextlib.contextmanager | |
117 def _dontChangeRulesStore(rules): | |
118 if not hasattr(rules, '_stashOriginalRules'): | |
119 rules._stashOriginalRules = rules.rules[:] | |
120 yield | |
121 for k in list(rules.formulae.keys()): | |
122 if not k.startswith('_:Formula'): | |
123 del rules.formulae[k] | |
124 rules.rules = rules._stashOriginalRules[:] | |
125 | |
126 | |
127 import logging | |
128 import time | |
129 | |
130 log = logging.getLogger() | |
131 | |
132 | |
133 def logTime(func): | |
134 | |
135 def inner(*args, **kw): | |
136 t1 = time.time() | |
137 try: | |
138 ret = func(*args, **kw) | |
139 finally: | |
140 log.info("Call to %s took %.1f ms" % (func.__name__, 1000 * (time.time() - t1))) | |
141 return ret | |
142 | |
143 return inner |