Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/lhs_evaluation.py @ 1660:31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
author | drewp@bigasterisk.com |
---|---|
date | Sun, 19 Sep 2021 15:39:37 -0700 |
parents | 7ec2483d61b5 |
children | 00a5624d1d14 |
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 | |
1637 | 31 class Function: |
32 """any rule stmt that runs a function (not just a statement match)""" | |
1640 | 33 pred: URIRef |
1637 | 34 |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
35 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
|
36 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
|
37 if chunk.predicate != self.pred: |
1637 | 38 raise TypeError |
39 self.ruleGraph = ruleGraph | |
40 | |
41 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
42 raise NotImplementedError | |
43 | |
44 def getNumericOperands(self, existingBinding: CandidateBinding) -> List[Union[int, float, Decimal]]: | |
45 out = [] | |
46 for op in self.getOperandNodes(existingBinding): | |
1658 | 47 out.append(_numericNode(op)) |
1637 | 48 |
49 return out | |
50 | |
51 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
52 """either any new bindings this function makes (could be 0), or None if it doesn't match""" | |
53 raise NotImplementedError | |
54 | |
55 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
|
56 objVar = self.chunk.primary[2] |
1637 | 57 if not isinstance(objVar, Variable): |
58 raise TypeError(f'expected Variable, got {objVar!r}') | |
59 return CandidateBinding({cast(BindableTerm, objVar): value}) | |
60 | |
61 | |
62 class SubjectFunction(Function): | |
63 """function that depends only on the subject term""" | |
64 | |
65 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
66 if self.chunk.primary[0] is None: |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
67 raise ValueError(f'expected one operand on {self.chunk}') |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
68 return [existingBinding.applyTerm(self.chunk.primary[0])] |
1637 | 69 |
70 | |
71 class SubjectObjectFunction(Function): | |
72 """a filter function that depends on the subject and object terms""" | |
73 | |
74 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
75 if self.chunk.primary[0] is None or self.chunk.primary[2] is None: |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
76 raise ValueError(f'expected one operand on each side of {self.chunk}') |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
77 return [existingBinding.applyTerm(self.chunk.primary[0]), existingBinding.applyTerm(self.chunk.primary[2])] |
1637 | 78 |
79 | |
80 class ListFunction(Function): | |
81 """function that takes an rdf list as input""" | |
82 | |
83 def usedStatements(self) -> Set[Triple]: | |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
84 raise NotImplementedError |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
85 if self.chunk.subjist is None: |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
86 raise ValueError(f'expected subject list on {self.chunk}') |
1658 | 87 _, used = _parseList(self.ruleGraph, self.chunk.primary[0]) |
1637 | 88 return used |
89 | |
90 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]: | |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
91 if self.chunk.subjList is None: |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
92 raise ValueError(f'expected subject list on {self.chunk}') |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
93 return [existingBinding.applyTerm(x) for x in self.chunk.subjList] |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
94 |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
95 |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
96 _registeredFunctionTypes: List[Type['Function']] = [] |
1637 | 97 |
1660
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
98 |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
99 def register(cls: Type['Function']): |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
100 _registeredFunctionTypes.append(cls) |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
101 return cls |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
102 |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
103 |
31f7dab6a60b
function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
104 import inference_functions # calls register() on some classes |
1637 | 105 |
1658 | 106 _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
|
107 |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
108 |
1640 | 109 def functionsFor(pred: URIRef) -> Iterator[Type[Function]]: |
110 try: | |
111 yield _byPred[pred] | |
112 except KeyError: | |
113 return | |
114 | |
1637 | 115 |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1648
diff
changeset
|
116 # 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
|
117 # 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
|
118 # 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
|
119 # 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
|
120 # 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
|
121 # return usedByFuncs |
1640 | 122 |
123 | |
124 def rulePredicates() -> Set[URIRef]: | |
1658 | 125 return set(c.pred for c in _registeredFunctionTypes) |