diff service/mqtt_to_rdf/candidate_binding.py @ 1618:48bf62008c82

attempted to rewrite with CandidateTermMatches but it broke
author drewp@bigasterisk.com
date Wed, 08 Sep 2021 18:32:11 -0700
parents bcfa368e5498
children
line wrap: on
line diff
--- a/service/mqtt_to_rdf/candidate_binding.py	Mon Sep 06 23:26:07 2021 -0700
+++ b/service/mqtt_to_rdf/candidate_binding.py	Wed Sep 08 18:32:11 2021 -0700
@@ -1,5 +1,5 @@
 from dataclasses import dataclass
-from typing import Dict, Iterable, Iterator, Union
+from typing import Dict, Iterable, Iterator, Optional, Union
 
 from prometheus_client import Summary
 from rdflib import BNode, Graph
@@ -16,24 +16,28 @@
         b = " ".join("%s=%s" % (k, v) for k, v in sorted(self.binding.items()))
         return f'CandidateBinding({b})'
 
-    def apply(self, g: Union[Graph, Iterable[Triple]]) -> Iterator[Triple]:
+    def apply(self, g: Union[Graph, Iterable[Triple]], returnBoundStatementsOnly=True) -> Iterator[Triple]:
         for stmt in g:
             try:
-                bound = (self._applyTerm(stmt[0]), self._applyTerm(stmt[1]), self._applyTerm(stmt[2]))
+                bound = (
+                    self._applyTerm(stmt[0], returnBoundStatementsOnly), 
+                    self._applyTerm(stmt[1], returnBoundStatementsOnly), 
+                    self._applyTerm(stmt[2], returnBoundStatementsOnly))
             except BindingUnknown:
                 continue
             yield bound
 
-    def _applyTerm(self, term: Node):
+    def _applyTerm(self, term: Node, failUnbound=True):
         if isinstance(term, (Variable, BNode)):
             if term in self.binding:
                 return self.binding[term]
             else:
-                raise BindingUnknown()
+                if failUnbound:
+                    raise BindingUnknown()
         return term
 
     def addNewBindings(self, newBindings: 'CandidateBinding'):
         for k, v in newBindings.binding.items():
-            if k in self.binding and self.binding[k] != v:
-                raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}')
+            # if k in self.binding and self.binding[k] != v:
+            #     raise ValueError(f'conflict- thought {k} would be {self.binding[k]} but another Evaluation said it should be {v}')
             self.binding[k] = v