Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference_functions.py @ 1696:cdf706cf5f82 master
fix a test (odometer rollover direction)
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 22:56:25 -0700 |
parents | 7a61113fd17d |
children |
rev | line source |
---|---|
1662
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
1 """ |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
2 Some of these are from https://www.w3.org/2000/10/swap/doc/CwmBuiltins |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
3 """ |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
4 import urllib.parse |
1658 | 5 from decimal import Decimal |
6 from typing import Optional, cast | |
7 | |
1662
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
8 from rdflib import Literal, Namespace, URIRef |
1658 | 9 |
10 from candidate_binding import CandidateBinding | |
11 from lhs_evaluation import (ListFunction, SubjectFunction, SubjectObjectFunction, register) | |
12 | |
13 MATH = Namespace('http://www.w3.org/2000/10/swap/math#') | |
14 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
15 | |
16 | |
17 @register | |
18 class Gt(SubjectObjectFunction): | |
19 pred = MATH['greaterThan'] | |
20 | |
21 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
22 [x, y] = self.getNumericOperands(existingBinding) | |
23 if x > y: | |
24 return CandidateBinding({}) # no new values; just allow matching to keep going | |
25 | |
26 | |
27 @register | |
28 class AsFarenheit(SubjectFunction): | |
29 pred = ROOM['asFarenheit'] | |
30 | |
31 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
32 [x] = self.getNumericOperands(existingBinding) | |
33 f = cast(Literal, Literal(Decimal(x) * 9 / 5 + 32)) | |
34 return self.valueInObjectTerm(f) | |
35 | |
36 | |
37 @register | |
38 class Sum(ListFunction): | |
39 pred = MATH['sum'] | |
40 | |
41 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: | |
42 f = Literal(sum(self.getNumericOperands(existingBinding))) | |
43 return self.valueInObjectTerm(f) | |
1662
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
44 |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
45 |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
46 @register |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
47 class ChildResource(ListFunction): |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
48 pred = ROOM['childResource'] |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
49 |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
50 def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]: |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
51 ops = self.getOperandNodes(existingBinding) |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
52 if len(ops) != 2 or not isinstance(ops[0], URIRef) or not isinstance(ops[1], Literal): |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
53 raise ValueError(f'expected (?baseUri ?nextSegmentString) as subject to {self}') |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
54 newUri = URIRef(ops[0].rstrip('/') + '/' + urllib.parse.quote(ops[1].toPython(), safe='')) |
7a61113fd17d
new childResource function for making new URIs
drewp@bigasterisk.com
parents:
1658
diff
changeset
|
55 return self.valueInObjectTerm(newUri) |