Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/lhs_evaluation.py @ 1658:7ec2483d61b5
refactor inference_functions
author | drewp@bigasterisk.com |
---|---|
date | Sun, 19 Sep 2021 13:33:10 -0700 |
parents | 20474ad4968e |
children | 31f7dab6a60b |
rev | line source |
---|---|
1605 | 1 import logging |
2 from decimal import Decimal | |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
3 from typing import (Dict, Iterator, List, Optional, Set, Tuple, Type, Union, cast) |
1605 | 4 |
5 from prometheus_client import Summary | |
1636 | 6 from rdflib import RDF, Literal, Namespace, URIRef |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
7 from rdflib.term import Node, Variable |
1605 | 8 |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
9 from candidate_binding import CandidateBinding |
1637 | 10 from inference_types import BindableTerm, Triple |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
11 from stmt_chunk import Chunk, ChunkedGraph |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
1605
diff
changeset
|
12 |
1605 | 13 log = logging.getLogger('infer') |
14 | |
15 INDENT = ' ' | |
16 | |
17 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
18 LOG = Namespace('http://www.w3.org/2000/10/swap/log#') | |
19 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
20 | |
21 | |
1658 | 22 def _numericNode(n: Node): |
1605 | 23 if not isinstance(n, Literal): |
24 raise TypeError(f'expected Literal, got {n=}') | |
25 val = n.toPython() | |
26 if not isinstance(val, (int, float, Decimal)): | |
27 raise TypeError(f'expected number, got {val=}') | |
28 return val | |
29 | |
30 | |
1658 | 31 def _parseList(graph: ChunkedGraph, subj: Node) -> Tuple[List[Node], Set[Triple]]: |
1605 | 32 """"Do like Collection(g, subj) but also return all the |
33 triples that are involved in the list""" | |
34 out = [] | |
35 used = set() | |
36 cur = subj | |
37 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
|
38 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
|
39 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
|
40 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
|
41 out.append(elem) |
1605 | 42 used.add((cur, RDF.first, out[-1])) |
43 | |
44 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
|
45 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
|
46 raise ValueError('bad list') |
1605 | 47 used.add((cur, RDF.rest, next)) |
48 | |
49 cur = next | |
50 return out, used | |
1637 | 51 |
52 | |
1658 | 53 _registeredFunctionTypes: List[Type['Function']] = [] |
1637 | 54 |
55 | |
56 def register(cls: Type['Function']): | |
1658 | 57 _registeredFunctionTypes.append(cls) |
1637 | 58 return cls |
59 | |
60 | |
61 class Function: | |
62 """any rule stmt that runs a function (not just a statement match)""" | |
1640 | 63 pred: URIRef |
1637 | 64 |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
65 def __init__(self, chunk: Chunk, ruleGraph: ChunkedGraph): |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
66 self.chunk = chunk |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
67 if chunk.predicate != self.pred: |
1637 | 68 raise TypeError |
69 self.ruleGraph = ruleGraph | |
70 | |
71 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
72 raise NotImplementedError | |
73 | |
74 def getNumericOperands(self, existingBinding: CandidateBinding) -> List[Union[int, float, Decimal]]: | |
75 out = [] | |
76 for op in self.getOperandNodes(existingBinding): | |
1658 | 77 out.append(_numericNode(op)) |
1637 | 78 |
79 return out | |
80 | |
81 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
82 """either any new bindings this function makes (could be 0), or None if it doesn't match""" | |
83 raise NotImplementedError | |
84 | |
85 def valueInObjectTerm(self, value: Node) -> Optional[CandidateBinding]: | |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
86 objVar = self.chunk.primary[2] |
1637 | 87 if not isinstance(objVar, Variable): |
88 raise TypeError(f'expected Variable, got {objVar!r}') | |
89 return CandidateBinding({cast(BindableTerm, objVar): value}) | |
90 | |
1648 | 91 def usedStatements(self) -> Set[Triple]: |
92 '''stmts in self.graph (not including self.stmt, oddly) that are part of | |
93 this function setup and aren't to be matched literally''' | |
94 return set() | |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
95 |
1637 | 96 |
97 class SubjectFunction(Function): | |
98 """function that depends only on the subject term""" | |
99 | |
100 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
101 return [existingBinding.applyTerm(self.chunk.primary[0])] |
1637 | 102 |
103 | |
104 class SubjectObjectFunction(Function): | |
105 """a filter function that depends on the subject and object terms""" | |
106 | |
107 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
108 return [existingBinding.applyTerm(self.chunk.primary[0]), existingBinding.applyTerm(self.chunk.primary[2])] |
1637 | 109 |
110 | |
111 class ListFunction(Function): | |
112 """function that takes an rdf list as input""" | |
113 | |
114 def usedStatements(self) -> Set[Triple]: | |
1658 | 115 _, used = _parseList(self.ruleGraph, self.chunk.primary[0]) |
1637 | 116 return used |
117 | |
118 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
1658 | 119 operands, _ = _parseList(self.ruleGraph, self.chunk.primary[0]) |
1637 | 120 return [existingBinding.applyTerm(x) for x in operands] |
121 | |
1658 | 122 import inference_functions # calls register() on some classes |
1637 | 123 |
1658 | 124 _byPred: Dict[URIRef, Type[Function]] = dict((cls.pred, cls) for cls in _registeredFunctionTypes) |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
125 |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
126 |
1640 | 127 def functionsFor(pred: URIRef) -> Iterator[Type[Function]]: |
128 try: | |
129 yield _byPred[pred] | |
130 except KeyError: | |
131 return | |
132 | |
1637 | 133 |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
134 # def lhsStmtsUsedByFuncs(graph: ChunkedGraph) -> Set[Chunk]: |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
135 # usedByFuncs: Set[Triple] = set() # don't worry about matching these |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
136 # for s in graph: |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
137 # for cls in functionsFor(pred=s[1]): |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
138 # usedByFuncs.update(cls(s, graph).usedStatements()) |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
139 # return usedByFuncs |
1640 | 140 |
141 | |
142 def rulePredicates() -> Set[URIRef]: | |
1658 | 143 return set(c.pred for c in _registeredFunctionTypes) |