comparison service/mqtt_to_rdf/inference_functions.py @ 1662:7a61113fd17d

new childResource function for making new URIs
author drewp@bigasterisk.com
date Sun, 19 Sep 2021 20:31:39 -0700
parents 7ec2483d61b5
children
comparison
equal deleted inserted replaced
1661:00a5624d1d14 1662:7a61113fd17d
1 """
2 Some of these are from https://www.w3.org/2000/10/swap/doc/CwmBuiltins
3 """
4 import urllib.parse
1 from decimal import Decimal 5 from decimal import Decimal
2 from typing import Optional, cast 6 from typing import Optional, cast
3 7
4 from rdflib import Literal, Namespace 8 from rdflib import Literal, Namespace, URIRef
5 9
6 from candidate_binding import CandidateBinding 10 from candidate_binding import CandidateBinding
7 from lhs_evaluation import (ListFunction, SubjectFunction, SubjectObjectFunction, register) 11 from lhs_evaluation import (ListFunction, SubjectFunction, SubjectObjectFunction, register)
8 12
9 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') 13 MATH = Namespace('http://www.w3.org/2000/10/swap/math#')
35 pred = MATH['sum'] 39 pred = MATH['sum']
36 40
37 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: 41 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
38 f = Literal(sum(self.getNumericOperands(existingBinding))) 42 f = Literal(sum(self.getNumericOperands(existingBinding)))
39 return self.valueInObjectTerm(f) 43 return self.valueInObjectTerm(f)
44
45
46 @register
47 class ChildResource(ListFunction):
48 pred = ROOM['childResource']
49
50 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
51 ops = self.getOperandNodes(existingBinding)
52 if len(ops) != 2 or not isinstance(ops[0], URIRef) or not isinstance(ops[1], Literal):
53 raise ValueError(f'expected (?baseUri ?nextSegmentString) as subject to {self}')
54 newUri = URIRef(ops[0].rstrip('/') + '/' + urllib.parse.quote(ops[1].toPython(), safe=''))
55 return self.valueInObjectTerm(newUri)