changeset 1662:7a61113fd17d

new childResource function for making new URIs
author drewp@bigasterisk.com
date Sun, 19 Sep 2021 20:31:39 -0700
parents 00a5624d1d14
children a0bf320c70fe
files service/mqtt_to_rdf/inference_functions.py service/mqtt_to_rdf/inference_test.py
diffstat 2 files changed, 26 insertions(+), 1 deletions(-) [+]
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)
--- a/service/mqtt_to_rdf/inference_test.py	Sun Sep 19 16:51:51 2021 -0700
+++ b/service/mqtt_to_rdf/inference_test.py	Sun Sep 19 20:31:39 2021 -0700
@@ -220,6 +220,15 @@
         inf = makeInferenceWithRules("{ :a :b ?x . ?x room:asFarenheit ?f } => { :new :stmt ?f } .")
         self.assertGraphEqual(inf.infer(N3(":a :b 12 .")), N3(":new :stmt 53.6 ."))
 
+    def testChildResource(self):
+        inf = makeInferenceWithRules("{ :a :b ?x . (:c ?x) room:childResource ?y .} => { :new :stmt ?y  } .")
+        self.assertGraphEqual(inf.infer(N3(':a :b "foo" .')), N3(":new :stmt <http://projects.bigasterisk.com/room/c/foo> ."))
+
+    def testChildResourceSegmentQuoting(self):
+        inf = makeInferenceWithRules("{ :a :b ?x . (:c ?x) room:childResource ?y .} => { :new :stmt ?y  } .")
+        self.assertGraphEqual(inf.infer(N3(':a :b "b / w -> #." .')),
+                              N3(":new :stmt <http://projects.bigasterisk.com/room/c/b%20%2F%20w%20-%3E%20%23.> ."))
+
 
 class TestUseCases(WithGraphEqual):