Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/candidate_binding.py @ 1724:8d2c75a93d82
use pdm (these are new deps)
author | drewp@bigasterisk.com |
---|---|
date | Tue, 20 Jun 2023 23:13:09 -0700 |
parents | 3cf7f313b285 |
children |
rev | line source |
---|---|
1632 | 1 import logging |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
2 from dataclasses import dataclass |
1675
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
3 from typing import Dict, Iterable, Iterator, Union |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
4 |
1675
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
5 from rdflib import Graph |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
6 from rdflib.term import Node, Variable |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
7 |
1675
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
8 from inference_types import BindableTerm, BindingUnknown, RuleUnboundBnode, Triple |
1638
0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
drewp@bigasterisk.com
parents:
1635
diff
changeset
|
9 |
1634
ba59cfc3c747
hack math:sum in there. Test suite is passing except some slow performers
drewp@bigasterisk.com
parents:
1632
diff
changeset
|
10 log = logging.getLogger('cbind') |
1632 | 11 INDENT = ' ' |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
12 |
1638
0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
drewp@bigasterisk.com
parents:
1635
diff
changeset
|
13 |
1667 | 14 class BindingConflict(ValueError): # might be the same as `Inconsistent` |
1638
0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
drewp@bigasterisk.com
parents:
1635
diff
changeset
|
15 pass |
0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
drewp@bigasterisk.com
parents:
1635
diff
changeset
|
16 |
0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
drewp@bigasterisk.com
parents:
1635
diff
changeset
|
17 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
18 @dataclass |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
19 class CandidateBinding: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
20 binding: Dict[BindableTerm, Node] |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
21 |
1675
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
22 def __post_init__(self): |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
23 for n in self.binding.values(): |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
24 if isinstance(n, RuleUnboundBnode): |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
25 raise TypeError(repr(self)) |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
26 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
27 def __repr__(self): |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1651
diff
changeset
|
28 b = " ".join("%r=%r" % (var, value) for var, value in sorted(self.binding.items())) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
29 return f'CandidateBinding({b})' |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
30 |
1675
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
31 def key(self): |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
32 """note this is only good for the current value, and self.binding is mutable""" |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
33 return tuple(sorted(self.binding.items())) |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
34 |
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
35 def apply(self, g: Union[Graph, Iterable[Triple]], returnBoundStatementsOnly=True) -> Iterator[Triple]: |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
36 for stmt in g: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
37 try: |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1638
diff
changeset
|
38 bound = ( |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1638
diff
changeset
|
39 self.applyTerm(stmt[0], returnBoundStatementsOnly), # |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1638
diff
changeset
|
40 self.applyTerm(stmt[1], returnBoundStatementsOnly), # |
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1638
diff
changeset
|
41 self.applyTerm(stmt[2], returnBoundStatementsOnly)) |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
42 except BindingUnknown: |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1651
diff
changeset
|
43 if log.isEnabledFor(logging.DEBUG): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1651
diff
changeset
|
44 log.debug(f'{INDENT*7} CB.apply cant bind {stmt} using {self.binding}') |
1632 | 45 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
46 continue |
1664
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1651
diff
changeset
|
47 if log.isEnabledFor(logging.DEBUG): |
1a7c1261302c
logic fix- some bindings were being returned 2+; some 0 times
drewp@bigasterisk.com
parents:
1651
diff
changeset
|
48 log.debug(f'{INDENT*7} CB.apply took {stmt} to {bound}') |
1632 | 49 |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
50 yield bound |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
51 |
1635
22d481f0a924
refactor: use CandidateBinding throughout, not loose dicts
drewp@bigasterisk.com
parents:
1634
diff
changeset
|
52 def applyTerm(self, term: Node, failUnbound=True): |
1675
3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
drewp@bigasterisk.com
parents:
1667
diff
changeset
|
53 if isinstance(term, (Variable, RuleUnboundBnode)): |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
54 if term in self.binding: |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
55 return self.binding[term] |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
56 else: |
1619
e24058ae4806
support CB.apply(returnBoundStatementsOnly)
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
57 if failUnbound: |
e24058ae4806
support CB.apply(returnBoundStatementsOnly)
drewp@bigasterisk.com
parents:
1615
diff
changeset
|
58 raise BindingUnknown() |
1607
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
59 return term |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
60 |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
61 def addNewBindings(self, newBindings: 'CandidateBinding'): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
62 for k, v in newBindings.binding.items(): |
b21885181e35
more modules, types. Maybe less repeated computation on BoundLhs
drewp@bigasterisk.com
parents:
diff
changeset
|
63 if k in self.binding and self.binding[k] != v: |
1638
0ba1625037ae
don't crash, just skip the rule if there's a BindingConflict (no clear test case yet)
drewp@bigasterisk.com
parents:
1635
diff
changeset
|
64 raise BindingConflict(f'thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}') |
1615
bcfa368e5498
change a Graph.__sub__ to Set.difference in verify() for a big speedup
drewp@bigasterisk.com
parents:
1607
diff
changeset
|
65 self.binding[k] = v |
1635
22d481f0a924
refactor: use CandidateBinding throughout, not loose dicts
drewp@bigasterisk.com
parents:
1634
diff
changeset
|
66 |
22d481f0a924
refactor: use CandidateBinding throughout, not loose dicts
drewp@bigasterisk.com
parents:
1634
diff
changeset
|
67 def copy(self): |
22d481f0a924
refactor: use CandidateBinding throughout, not loose dicts
drewp@bigasterisk.com
parents:
1634
diff
changeset
|
68 return CandidateBinding(self.binding.copy()) |
22d481f0a924
refactor: use CandidateBinding throughout, not loose dicts
drewp@bigasterisk.com
parents:
1634
diff
changeset
|
69 |
22d481f0a924
refactor: use CandidateBinding throughout, not loose dicts
drewp@bigasterisk.com
parents:
1634
diff
changeset
|
70 def contains(self, term: BindableTerm): |
1651
20474ad4968e
WIP - functions are broken as i move most layers to work in Chunks not Triples
drewp@bigasterisk.com
parents:
1638
diff
changeset
|
71 return term in self.binding |