Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/lhs_evaluation.py @ 1642:78024b27f9ec
serve graph/config
author | drewp@bigasterisk.com |
---|---|
date | Fri, 17 Sep 2021 11:01:06 -0700 |
parents | 4bb6f593ebf3 |
children | 3059f31b2dfa |
rev | line source |
---|---|
1637 | 1 from dataclasses import dataclass |
1605 | 2 import logging |
3 from decimal import Decimal | |
1637 | 4 from candidate_binding import CandidateBinding |
1640 | 5 from typing import Dict, Iterator, List, Optional, Set, Tuple, Type, Union, cast |
1605 | 6 |
7 from prometheus_client import Summary | |
1636 | 8 from rdflib import RDF, Literal, Namespace, URIRef |
1637 | 9 from rdflib.graph import Graph |
10 from rdflib.term import Node, Variable | |
1605 | 11 |
1637 | 12 from inference_types import BindableTerm, Triple |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
13 |
1605 | 14 log = logging.getLogger('infer') |
15 | |
16 INDENT = ' ' | |
17 | |
18 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
19 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
20 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
21 | |
22 | |
23 def numericNode(n: Node): | |
24 if not isinstance(n, Literal): | |
25 raise TypeError(f'expected Literal, got {n=}') | |
26 val = n.toPython() | |
27 if not isinstance(val, (int, float, Decimal)): | |
28 raise TypeError(f'expected number, got {val=}') | |
29 return val | |
30 | |
31 | |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
32 def parseList(graph, subj) -> Tuple[List[Node], Set[Triple]]: |
1605 | 33 """"Do like Collection(g, subj) but also return all the |
34 triples that are involved in the list""" | |
35 out = [] | |
36 used = set() | |
37 cur = subj | |
38 while cur != RDF.nil: | |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
39 elem = graph.value(cur, RDF.first) |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
40 if elem is None: |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
41 raise ValueError('bad list') |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
42 out.append(elem) |
1605 | 43 used.add((cur, RDF.first, out[-1])) |
44 | |
45 next = graph.value(cur, RDF.rest) | |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
46 if next is None: |
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
47 raise ValueError('bad list') |
1605 | 48 used.add((cur, RDF.rest, next)) |
49 | |
50 cur = next | |
51 return out, used | |
1637 | 52 |
53 | |
54 registeredFunctionTypes: List[Type['Function']] = [] | |
55 | |
56 | |
57 def register(cls: Type['Function']): | |
58 registeredFunctionTypes.append(cls) | |
59 return cls | |
60 | |
61 | |
62 class Function: | |
63 """any rule stmt that runs a function (not just a statement match)""" | |
1640 | 64 pred: URIRef |
1637 | 65 |
66 def __init__(self, stmt: Triple, ruleGraph: Graph): | |
67 self.stmt = stmt | |
68 if stmt[1] != self.pred: | |
69 raise TypeError | |
70 self.ruleGraph = ruleGraph | |
71 | |
72 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
73 raise NotImplementedError | |
74 | |
75 def getNumericOperands(self, existingBinding: CandidateBinding) -> List[Union[int, float, Decimal]]: | |
76 out = [] | |
77 for op in self.getOperandNodes(existingBinding): | |
78 out.append(numericNode(op)) | |
79 | |
80 return out | |
81 | |
82 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
83 """either any new bindings this function makes (could be 0), or None if it doesn't match""" | |
84 raise NotImplementedError | |
85 | |
86 def valueInObjectTerm(self, value: Node) -> Optional[CandidateBinding]: | |
87 objVar = self.stmt[2] | |
88 if not isinstance(objVar, Variable): | |
89 raise TypeError(f'expected Variable, got {objVar!r}') | |
90 return CandidateBinding({cast(BindableTerm, objVar): value}) | |
91 | |
92 | |
93 class SubjectFunction(Function): | |
94 """function that depends only on the subject term""" | |
95 | |
96 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
97 return [existingBinding.applyTerm(self.stmt[0])] | |
98 | |
99 | |
100 class SubjectObjectFunction(Function): | |
101 """a filter function that depends on the subject and object terms""" | |
102 | |
103 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
104 return [existingBinding.applyTerm(self.stmt[0]), existingBinding.applyTerm(self.stmt[2])] | |
105 | |
106 | |
107 class ListFunction(Function): | |
108 """function that takes an rdf list as input""" | |
109 | |
110 def usedStatements(self) -> Set[Triple]: | |
111 _, used = parseList(self.ruleGraph, self.stmt[0]) | |
112 return used | |
113 | |
114 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
115 operands, _ = parseList(self.ruleGraph, self.stmt[0]) | |
116 return [existingBinding.applyTerm(x) for x in operands] | |
117 | |
118 | |
119 @register | |
120 class Gt(SubjectObjectFunction): | |
121 pred = MATH['greaterThan'] | |
122 | |
123 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
124 [x, y] = self.getNumericOperands(existingBinding) | |
125 if x > y: | |
126 return CandidateBinding({}) # no new values; just allow matching to keep going | |
127 | |
128 | |
129 @register | |
130 class AsFarenheit(SubjectFunction): | |
131 pred = ROOM['asFarenheit'] | |
132 | |
133 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
134 [x] = self.getNumericOperands(existingBinding) | |
135 f = cast(Literal, Literal(Decimal(x) * 9 / 5 + 32)) | |
136 return self.valueInObjectTerm(f) | |
137 | |
138 | |
139 @register | |
140 class Sum(ListFunction): | |
141 pred = MATH['sum'] | |
142 | |
143 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
144 f = Literal(sum(self.getNumericOperands(existingBinding))) | |
145 return self.valueInObjectTerm(f) | |
146 | |
1640 | 147 ### registeration is done |
1637 | 148 |
1640 | 149 _byPred: Dict[URIRef, Type[Function]] = dict((cls.pred, cls) for cls in registeredFunctionTypes) |
150 def functionsFor(pred: URIRef) -> Iterator[Type[Function]]: | |
151 try: | |
152 yield _byPred[pred] | |
153 except KeyError: | |
154 return | |
155 | |
1637 | 156 |
157 def lhsStmtsUsedByFuncs(graph: Graph) -> Set[Triple]: | |
158 usedByFuncs: Set[Triple] = set() # don't worry about matching these | |
159 for s in graph: | |
160 for cls in functionsFor(pred=s[1]): | |
161 if issubclass(cls, ListFunction): | |
162 usedByFuncs.update(cls(s, graph).usedStatements()) | |
163 return usedByFuncs | |
1640 | 164 |
165 | |
166 def rulePredicates() -> Set[URIRef]: | |
167 return set(c.pred for c in registeredFunctionTypes) |