diff 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
line wrap: on
line diff
--- a/service/mqtt_to_rdf/inference_functions.py	Sun Sep 19 16:51:51 2021 -0700
+++ b/service/mqtt_to_rdf/inference_functions.py	Sun Sep 19 20:31:39 2021 -0700
@@ -1,7 +1,11 @@
+"""
+Some of these are from https://www.w3.org/2000/10/swap/doc/CwmBuiltins
+"""
+import urllib.parse
 from decimal import Decimal
 from typing import Optional, cast
 
-from rdflib import Literal, Namespace
+from rdflib import Literal, Namespace, URIRef
 
 from candidate_binding import CandidateBinding
 from lhs_evaluation import (ListFunction, SubjectFunction, SubjectObjectFunction, register)
@@ -37,3 +41,15 @@
     def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
         f = Literal(sum(self.getNumericOperands(existingBinding)))
         return self.valueInObjectTerm(f)
+
+
+@register
+class ChildResource(ListFunction):
+    pred = ROOM['childResource']
+
+    def bind(self, existingBinding: CandidateBinding) -> Optional[CandidateBinding]:
+        ops = self.getOperandNodes(existingBinding)
+        if len(ops) != 2 or not isinstance(ops[0], URIRef) or not isinstance(ops[1], Literal):
+            raise ValueError(f'expected (?baseUri ?nextSegmentString) as subject to {self}')
+        newUri = URIRef(ops[0].rstrip('/') + '/' + urllib.parse.quote(ops[1].toPython(), safe=''))
+        return self.valueInObjectTerm(newUri)