comparison service/mqtt_to_rdf/inference_functions.py @ 1658:7ec2483d61b5

refactor inference_functions
author drewp@bigasterisk.com
date Sun, 19 Sep 2021 13:33:10 -0700
parents
children 7a61113fd17d
comparison
equal deleted inserted replaced
1657:274bb6c04627 1658:7ec2483d61b5
1 from decimal import Decimal
2 from typing import Optional, cast
3
4 from rdflib import Literal, Namespace
5
6 from candidate_binding import CandidateBinding
7 from lhs_evaluation import (ListFunction, SubjectFunction, SubjectObjectFunction, register)
8
9 MATH = Namespace('http://www.w3.org/2000/10/swap/math#')
10 ROOM = Namespace("http://projects.bigasterisk.com/room/")
11
12
13 @register
14 class Gt(SubjectObjectFunction):
15 pred = MATH['greaterThan']
16
17 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
18 [x, y] = self.getNumericOperands(existingBinding)
19 if x > y:
20 return CandidateBinding({}) # no new values; just allow matching to keep going
21
22
23 @register
24 class AsFarenheit(SubjectFunction):
25 pred = ROOM['asFarenheit']
26
27 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
28 [x] = self.getNumericOperands(existingBinding)
29 f = cast(Literal, Literal(Decimal(x) * 9 / 5 + 32))
30 return self.valueInObjectTerm(f)
31
32
33 @register
34 class Sum(ListFunction):
35 pred = MATH['sum']
36
37 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
38 f = Literal(sum(self.getNumericOperands(existingBinding)))
39 return self.valueInObjectTerm(f)