# HG changeset patch # User drewp@bigasterisk.com # Date 1632297805 25200 # Node ID 3cf7f313b2853c9bce8dced9e6da9d0b71cce81b # Parent 4a15b4cd46007dcdc4b6f560bcda98ab113ac6e6 forgot a file in the BNode subtypes commit a few steps back diff -r 4a15b4cd4600 -r 3cf7f313b285 service/mqtt_to_rdf/candidate_binding.py --- a/service/mqtt_to_rdf/candidate_binding.py Wed Sep 22 01:02:26 2021 -0700 +++ b/service/mqtt_to_rdf/candidate_binding.py Wed Sep 22 01:03:25 2021 -0700 @@ -1,12 +1,11 @@ import logging from dataclasses import dataclass -from typing import Dict, Iterator +from typing import Dict, Iterable, Iterator, Union -from prometheus_client import Summary -from rdflib import BNode, Graph +from rdflib import Graph from rdflib.term import Node, Variable -from inference_types import BindableTerm, BindingUnknown, Triple +from inference_types import BindableTerm, BindingUnknown, RuleUnboundBnode, Triple log = logging.getLogger('cbind') INDENT = ' ' @@ -20,11 +19,20 @@ class CandidateBinding: binding: Dict[BindableTerm, Node] + def __post_init__(self): + for n in self.binding.values(): + if isinstance(n, RuleUnboundBnode): + raise TypeError(repr(self)) + def __repr__(self): b = " ".join("%r=%r" % (var, value) for var, value in sorted(self.binding.items())) return f'CandidateBinding({b})' - def apply(self, g: Graph, returnBoundStatementsOnly=True) -> Iterator[Triple]: + def key(self): + """note this is only good for the current value, and self.binding is mutable""" + return tuple(sorted(self.binding.items())) + + def apply(self, g: Union[Graph, Iterable[Triple]], returnBoundStatementsOnly=True) -> Iterator[Triple]: for stmt in g: try: bound = ( @@ -42,7 +50,7 @@ yield bound def applyTerm(self, term: Node, failUnbound=True): - if isinstance(term, (Variable, BNode)): + if isinstance(term, (Variable, RuleUnboundBnode)): if term in self.binding: return self.binding[term] else: