annotate service/mqtt_to_rdf/lhs_evaluation.py @ 1673:80f4e741ca4f

redo RHS bnode processing
author drewp@bigasterisk.com
date Wed, 22 Sep 2021 01:00:32 -0700
parents 00a5624d1d14
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1605
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
1 import logging
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
2 from decimal import Decimal
1673
80f4e741ca4f redo RHS bnode processing
drewp@bigasterisk.com
parents: 1661
diff changeset
3 from typing import Dict, Iterator, List, Optional, Type, Union, cast
1605
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
4
1673
80f4e741ca4f redo RHS bnode processing
drewp@bigasterisk.com
parents: 1661
diff changeset
5 from rdflib import 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
6 from rdflib.term import Node, Variable
1605
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
7
1651
20474ad4968e WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents: 1648
diff changeset
8 from candidate_binding import CandidateBinding
1673
80f4e741ca4f redo RHS bnode processing
drewp@bigasterisk.com
parents: 1661
diff changeset
9 from inference_types import BindableTerm
80f4e741ca4f redo RHS bnode processing
drewp@bigasterisk.com
parents: 1661
diff changeset
10 from stmt_chunk import Chunk
1607
b21885181e35 more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents: 1605
diff changeset
11
1605
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
12 log = logging.getLogger('infer')
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
13
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
14 INDENT = ' '
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
15
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
16 ROOM = Namespace("http://projects.bigasterisk.com/room/")
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
17 LOG = Namespace('http://www.w3.org/2000/10/swap/log#')
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
18 MATH = Namespace('http://www.w3.org/2000/10/swap/math#')
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
19
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
20
1658
7ec2483d61b5 refactor inference_functions
drewp@bigasterisk.com
parents: 1651
diff changeset
21 def _numericNode(n: Node):
1605
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
22 if not isinstance(n, Literal):
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
23 raise TypeError(f'expected Literal, got {n=}')
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
24 val = n.toPython()
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
25 if not isinstance(val, (int, float, Decimal)):
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
26 raise TypeError(f'expected number, got {val=}')
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
27 return val
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
28
449746d1598f WIP move evaluation to new file
drewp@bigasterisk.com
parents:
diff changeset
29
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
30 class Function:
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
31 """any rule stmt that runs a function (not just a statement match)"""
1640
4bb6f593ebf3 speedups: abort some rules faster
drewp@bigasterisk.com
parents: 1637
diff changeset
32 pred: URIRef
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
33
1661
00a5624d1d14 cleanups and optimizations
drewp@bigasterisk.com
parents: 1660
diff changeset
34 def __init__(self, chunk: 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
35 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
36 if chunk.predicate != self.pred:
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
37 raise TypeError
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
38
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
39 def getOperandNodes(self, existingBinding: CandidateBinding) -> List[Node]:
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
40 raise NotImplementedError
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
41
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
42 def getNumericOperands(self, existingBinding: CandidateBinding) -> List[Union[int, float, Decimal]]:
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
43 out = []
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
44 for op in self.getOperandNodes(existingBinding):
1658
7ec2483d61b5 refactor inference_functions
drewp@bigasterisk.com
parents: 1651
diff changeset
45 out.append(_numericNode(op))
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
46
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
47 return out
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
48
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
49 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
50 """either any new bindings this function makes (could be 0), or None if it doesn't match"""
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
51 raise NotImplementedError
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
52
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
53 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
54 objVar = self.chunk.primary[2]
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
55 if not isinstance(objVar, Variable):
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
56 raise TypeError(f'expected Variable, got {objVar!r}')
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
57 return CandidateBinding({cast(BindableTerm, objVar): value})
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
58
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
59
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
60 class SubjectFunction(Function):
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
61 """function that depends only on the subject term"""
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
62
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
63 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
64 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
65 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
66 return [existingBinding.applyTerm(self.chunk.primary[0])]
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
67
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
68
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
69 class SubjectObjectFunction(Function):
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
70 """a filter function that depends on the subject and object terms"""
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
71
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
72 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
73 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
74 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
75 return [existingBinding.applyTerm(self.chunk.primary[0]), existingBinding.applyTerm(self.chunk.primary[2])]
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
76
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
77
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
78 class ListFunction(Function):
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
79 """function that takes an rdf list as input"""
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
80
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
81 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
82 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
83 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
84 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
85
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
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1658
diff changeset
87 _registeredFunctionTypes: List[Type['Function']] = []
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
88
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
89
31f7dab6a60b function evaluation uses Chunk lists now and runs fast. Only a few edge cases still broken
drewp@bigasterisk.com
parents: 1658
diff changeset
90 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
91 _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
92 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
93
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 import inference_functions # calls register() on some classes
1637
ec3f98d0c1d8 refactor rules eval
drewp@bigasterisk.com
parents: 1636
diff changeset
96
1658
7ec2483d61b5 refactor inference_functions
drewp@bigasterisk.com
parents: 1651
diff changeset
97 _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
98
20474ad4968e WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents: 1648
diff changeset
99
1640
4bb6f593ebf3 speedups: abort some rules faster
drewp@bigasterisk.com
parents: 1637
diff changeset
100 def functionsFor(pred: URIRef) -> Iterator[Type[Function]]:
4bb6f593ebf3 speedups: abort some rules faster
drewp@bigasterisk.com
parents: 1637
diff changeset
101 try:
4bb6f593ebf3 speedups: abort some rules faster
drewp@bigasterisk.com
parents: 1637
diff changeset
102 yield _byPred[pred]
4bb6f593ebf3 speedups: abort some rules faster
drewp@bigasterisk.com
parents: 1637
diff changeset
103 except KeyError:
4bb6f593ebf3 speedups: abort some rules faster
drewp@bigasterisk.com
parents: 1637
diff changeset
104 return