Mercurial > code > home > repos > homeauto
diff service/mqtt_to_rdf/lhs_evaluation.py @ 1636:3252bdc284bc
rm dead code from previous tries
author | drewp@bigasterisk.com |
---|---|
date | Mon, 13 Sep 2021 00:18:47 -0700 |
parents | ba59cfc3c747 |
children | ec3f98d0c1d8 |
line wrap: on
line diff
--- a/service/mqtt_to_rdf/lhs_evaluation.py Mon Sep 13 00:06:00 2021 -0700 +++ b/service/mqtt_to_rdf/lhs_evaluation.py Mon Sep 13 00:18:47 2021 -0700 @@ -1,14 +1,12 @@ import logging from decimal import Decimal -from typing import Dict, Iterable, Iterator, List, Set, Tuple +from typing import List, Set, Tuple from prometheus_client import Summary -from rdflib import RDF, Graph, Literal, Namespace, URIRef -from rdflib.term import Node, Variable +from rdflib import RDF, Literal, Namespace, URIRef +from rdflib.term import Node -from candidate_binding import CandidateBinding -from inference import CandidateBinding -from inference_types import BindableTerm, EvaluationFailed, Triple +from inference_types import Triple log = logging.getLogger('infer') @@ -18,76 +16,6 @@ LOG = Namespace('http://www.w3.org/2000/10/swap/log#') MATH = Namespace('http://www.w3.org/2000/10/swap/math#') -# Graph() makes a BNode if you don't pass -# identifier, which can be a bottleneck. -GRAPH_ID = URIRef('dont/care') - - -# alternate name LhsComponent -class Evaluation: - """some lhs statements need to be evaluated with a special function - (e.g. math) and then not considered for the rest of the rule-firing - process. It's like they already 'matched' something, so they don't need - to match a statement from the known-true working set. - - One Evaluation instance is for one function call. - """ - - @staticmethod - def findEvals(graph: Graph) -> Iterator['Evaluation']: - for stmt in graph.triples((None, MATH['sum'], None)): - operands, operandsStmts = parseList(graph, stmt[0]) - yield Evaluation(operands, stmt, operandsStmts) - - for stmt in graph.triples((None, MATH['greaterThan'], None)): - yield Evaluation([stmt[0], stmt[2]], stmt, []) - - for stmt in graph.triples((None, ROOM['asFarenheit'], None)): - yield Evaluation([stmt[0]], stmt, []) - - # internal, use findEvals - def __init__(self, operands: List[Node], mainStmt: Triple, otherStmts: Iterable[Triple]) -> None: - self.operands = operands - self.operandsStmts = Graph(identifier=GRAPH_ID) - self.operandsStmts += otherStmts # may grow - self.operandsStmts.add(mainStmt) - self.stmt = mainStmt - - def resultBindings(self, inputBindings: CandidateBinding) -> Tuple[CandidateBinding, Graph]: - """under the bindings so far, what would this evaluation tell us, and which stmts would be consumed from doing so?""" - pred = self.stmt[1] - objVar: Node = self.stmt[2] - boundOperands = [] - for op in self.operands: - if isinstance(op, Variable): - try: - op = inputBindings.binding[op] - except KeyError: - return CandidateBinding(binding={}), self.operandsStmts - - boundOperands.append(op) - - if pred == MATH['sum']: - obj = Literal(sum(map(numericNode, boundOperands))) - if not isinstance(objVar, Variable): - raise TypeError(f'expected Variable, got {objVar!r}') - res = CandidateBinding({objVar: obj}) - elif pred == ROOM['asFarenheit']: - if len(boundOperands) != 1: - raise ValueError(":asFarenheit takes 1 subject operand") - f = Literal(Decimal(numericNode(boundOperands[0])) * 9 / 5 + 32) - if not isinstance(objVar, Variable): - raise TypeError(f'expected Variable, got {objVar!r}') - res = CandidateBinding({objVar: f}) - elif pred == MATH['greaterThan']: - if not (numericNode(boundOperands[0]) > numericNode(boundOperands[1])): - raise EvaluationFailed() - res= CandidateBinding({}) - else: - raise NotImplementedError(repr(pred)) - - return res, self.operandsStmts - def numericNode(n: Node): if not isinstance(n, Literal):