|
34 | 34 | A knowledge base ontology currently uses a prefix 'kb:', expanding to 'http://example.org/kb/'. This knowledge base has a node kb:File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9. What is the IRI of its FileFacet?
|
35 | 35 |
|
36 | 36 | >>> from case_utils.namespace import NS_UCO_OBSERVABLE
|
37 |
| ->>> file_iri: str = "http://example.org/kb/File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9" |
38 |
| ->>> n_file = URIRef(file_iri) |
39 |
| ->>> n_file_facet = get_facet_uriref(n_file, NS_UCO_OBSERVABLE.FileFacet) |
| 37 | +>>> ns_kb = Namespace("http://example.org/kb/") |
| 38 | +>>> n_file = ns_kb["File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9"] |
| 39 | +>>> n_file_facet = get_facet_uriref(n_file, NS_UCO_OBSERVABLE.FileFacet, namespace=ns_kb) |
40 | 40 | >>> n_file_facet
|
41 | 41 | rdflib.term.URIRef('http://example.org/kb/FileFacet-01d292e3-0f38-5974-868d-006ef07f5186')
|
42 | 42 |
|
43 | 43 | A documentation policy change has been enacted, and now all knowledge base individuals need to use the URN example form. What is the FileFacet IRI now?
|
44 | 44 |
|
| 45 | +>>> ns_kb_2 = Namespace("urn:example:kb:") |
45 | 46 | >>> file_iri_2: str = "urn:example:kb:File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9"
|
46 | 47 | >>> n_file_2 = URIRef(file_iri_2)
|
47 |
| ->>> n_file_facet_2 = get_facet_uriref(n_file_2, NS_UCO_OBSERVABLE.FileFacet) |
| 48 | +>>> n_file_facet_2 = get_facet_uriref(n_file_2, NS_UCO_OBSERVABLE.FileFacet, namespace=ns_kb_2) |
48 | 49 | >>> n_file_facet_2
|
49 | 50 | rdflib.term.URIRef('urn:example:kb:FileFacet-01d292e3-0f38-5974-868d-006ef07f5186')
|
50 | 51 |
|
|
53 | 54 | >>> assert str(n_file_facet)[-36:] == str(n_file_facet_2)[-36:]
|
54 | 55 | """
|
55 | 56 |
|
56 |
| -__version__ = "0.0.2" |
| 57 | +__version__ = "0.0.3" |
57 | 58 |
|
58 | 59 | import binascii
|
59 | 60 | import re
|
@@ -148,85 +149,37 @@ def facet_inherence_uuid(
|
148 | 149 | return uuid.uuid5(uco_object_inherence_uuid, str(n_facet_class))
|
149 | 150 |
|
150 | 151 |
|
151 |
| -def guess_namespace(n_node: URIRef, *args: Any, **kwargs: Any) -> Namespace: |
152 |
| - """ |
153 |
| - This function attempts simple heuristics to extract from a URIRef its namespace IRI. The heuristics, being simple, are not necessarily going to provide desirable answers, and might be semantically incorrect. |
154 |
| -
|
155 |
| - :rtype rdflib.Namespace: |
156 |
| -
|
157 |
| - Examples |
158 |
| - ======== |
159 |
| -
|
160 |
| - >>> guess_namespace(URIRef("http://example.org/kb/Foo")) |
161 |
| - Namespace('http://example.org/kb/') |
162 |
| - >>> guess_namespace(URIRef("http://example.org/kb#Foo")) |
163 |
| - Namespace('http://example.org/kb#') |
164 |
| - >>> guess_namespace(URIRef("urn:example:kb#Foo")) |
165 |
| - Namespace('urn:example:kb#') |
166 |
| -
|
167 |
| - Note this function might not always give desirable answers. |
168 |
| -
|
169 |
| - >>> guess_namespace(URIRef("urn:example:kb#Foo/Bar")) |
170 |
| - Namespace('urn:example:kb#') |
171 |
| - >>> guess_namespace(URIRef("urn:example:kb/Foo#Bar")) |
172 |
| - Namespace('urn:example:kb/Foo#') |
173 |
| -
|
174 |
| - Some patterns, such as Simple Storage Service (S3) URLs being treated as RDF IRIs, should avoid using this function. This object that houses a PCAP blob can function as an IRI, but the guessed namespace value does not serve as an RDF namespace. |
175 |
| -
|
176 |
| - >>> guess_namespace(URIRef("s3://digitalcorpora/corpora/scenarios/2008-nitroba/nitroba.pcap")) |
177 |
| - Namespace('s3://digitalcorpora/corpora/scenarios/2008-nitroba/') |
178 |
| - """ |
179 |
| - node_iri = str(n_node) |
180 |
| - if "#" in node_iri: |
181 |
| - namespace_iri = node_iri[: 1 + node_iri.rindex("#")] |
182 |
| - elif "/" in node_iri: |
183 |
| - namespace_iri = node_iri[: 1 + node_iri.rindex("/")] |
184 |
| - else: |
185 |
| - namespace_iri = node_iri[: 1 + node_iri.rindex(":")] |
186 |
| - return Namespace(namespace_iri) |
187 |
| - |
188 |
| - |
189 | 152 | def get_facet_uriref(
|
190 | 153 | n_uco_object: URIRef,
|
191 | 154 | n_facet_class: URIRef,
|
192 | 155 | *args: Any,
|
193 |
| - namespace: Optional[Namespace] = None, |
| 156 | + namespace: Namespace, |
194 | 157 | **kwargs: Any
|
195 | 158 | ) -> URIRef:
|
196 | 159 | """
|
197 |
| - :param namespace: An optional RDFLib Namespace object to use for prefixing the Facet IRI with a knowledge base prefix IRI. If not provided, will be guessed from a right-truncation of n_uco_object under some namespace forms (hash-, then slash-, then colon-terminated). This is a potentially fragile guessing mechanism, so users should feel encouraged to provide this optional parameter. |
198 |
| - :type namespace rdflib.Namespace or None: |
| 160 | + :param namespace: An RDFLib Namespace object to use for prefixing the Facet IRI with a knowledge base prefix IRI. |
| 161 | + :type namespace rdflib.Namespace: |
199 | 162 |
|
200 | 163 | Examples
|
201 | 164 | ========
|
202 | 165 |
|
203 |
| - What is the URLFacet pertaining to the Nitroba University Scenario's PCAP file, when being interpreted as a Simple Storage Service (S3) object? Note that this example will show that in some cases a (RDFLib) Namespace will be desired. |
| 166 | + What is the URLFacet pertaining to the Nitroba University Scenario's PCAP file, when being interpreted as a Simple Storage Service (S3) object? |
204 | 167 |
|
205 | 168 | >>> from case_utils.namespace import NS_UCO_OBSERVABLE
|
206 | 169 | >>> pcap_url: str = "s3://digitalcorpora/corpora/scenarios/2008-nitroba/nitroba.pcap"
|
207 | 170 | >>> n_pcap = URIRef(pcap_url)
|
208 |
| - >>> n_pcap_url_facet_try1 = get_facet_uriref(n_pcap, NS_UCO_OBSERVABLE.URLFacet) |
209 |
| - >>> n_pcap_url_facet_try1 |
210 |
| - rdflib.term.URIRef('s3://digitalcorpora/corpora/scenarios/2008-nitroba/URLFacet-4b6023da-dbc4-5e1e-9a2f-aca2a6f6405c') |
211 |
| - >>> # Looks like a (RDFLib) Namespace object should be provided. |
212 | 171 | >>> ns_kb = Namespace("http://example.org/kb/")
|
213 |
| - >>> n_pcap_url_facet_try2 = get_facet_uriref(n_pcap, NS_UCO_OBSERVABLE.URLFacet, namespace=ns_kb) |
214 |
| - >>> n_pcap_url_facet_try2 |
| 172 | + >>> n_pcap_url_facet = get_facet_uriref(n_pcap, NS_UCO_OBSERVABLE.URLFacet, namespace=ns_kb) |
| 173 | + >>> n_pcap_url_facet |
215 | 174 | rdflib.term.URIRef('http://example.org/kb/URLFacet-4b6023da-dbc4-5e1e-9a2f-aca2a6f6405c')
|
216 | 175 | """
|
217 | 176 | uco_object_uuid_namespace: uuid.UUID = inherence_uuid(n_uco_object)
|
218 | 177 | facet_uuid = facet_inherence_uuid(uco_object_uuid_namespace, n_facet_class)
|
219 | 178 |
|
220 |
| - _namespace: Namespace |
221 |
| - if namespace is None: |
222 |
| - _namespace = guess_namespace(n_uco_object) |
223 |
| - else: |
224 |
| - _namespace = namespace |
225 |
| - |
226 | 179 | # NOTE: This encodes an assumption that Facets (including extension Facets) use the "Slash" IRI style.
|
227 | 180 | facet_class_local_name = str(n_facet_class).rsplit("/")[-1]
|
228 | 181 |
|
229 |
| - return _namespace[facet_class_local_name + "-" + str(facet_uuid)] |
| 182 | + return namespace[facet_class_local_name + "-" + str(facet_uuid)] |
230 | 183 |
|
231 | 184 |
|
232 | 185 | def hash_method_value_uuid(l_hash_method: Literal, l_hash_value: Literal) -> uuid.UUID:
|
|
0 commit comments