1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# Exposed module metadata
:
"""Read-only view of ontology metadata."""
...
...
...
...
...
...
...
...
"""Ontology environment for managing ontologies and graphs."""
"""Create or open an ontology environment.
When ``graph_store`` is supplied the environment delegates all graph
storage to the provided object (which must implement ``add_graph``,
``get_graph``, ``remove_graph``, and ``graph_ids``).
Set ``init_from_store=True`` together with ``graph_store`` to
reconstruct the environment's metadata from graphs that are already
present in the store, instead of starting from an empty state.
Query indexes (PSO/POS/SPO/OSP posting lists and a transitive-closure
table for property paths) are built in memory on first use from the
persistent ``.r5tu`` store; nothing is written to disk and no
configuration is required.
"""
...
...
"""Refresh the environment from its search directories.
Pass ``all=True`` to force re-ingestion of every known ontology
regardless of whether its source has changed.
"""
...
"""Re-read all graphs from the attached graph store and rebuild the
environment's ontology metadata and import dependency graph.
Call this whenever the graph store has been mutated externally and
the in-memory view needs to catch up. Only meaningful when the
environment was created with ``graph_store``.
"""
...
# ------------------------------------------------------------------ #
# Adding ontologies #
# ------------------------------------------------------------------ #
"""Add an ontology to the environment and return its IRI.
*location* may be a file path, a URL string, or an in-memory
``rdflib.Graph``. Set ``fetch_imports=False`` to skip recursive
import resolution; ``force=True`` forces re-fetching even when a
cached copy is fresh.
*rename* optionally overrides the ontology's declared IRI. When
supplied, every occurrence of the original IRI in the stored graph
(subject and object positions, except ``owl:versionIRI`` values) is
rewritten to *rename* before the graph is stored. The environment
registers the ontology under the new IRI; the original IRI is no
longer directly addressable.
Example — load a third-party ontology under a local canonical IRI::
env.add("https://example.org/upstream.ttl",
rename="https://my-org.com/local/upstream")
"""
...
"""Add an ontology without following its ``owl:imports`` declarations.
Accepts the same *rename* parameter as :py:meth:`add`.
"""
...
"""Rename the IRI of an ontology already in the environment.
Reads the stored graph for *uri*, rewrites all occurrences of the
current IRI to *new_iri* (subject and object positions, excluding
``owl:versionIRI`` values), stores the result under the new name,
removes the old named graph, and rebuilds the import dependency graph.
Returns the new IRI string.
Rewrite rules:
* ``<old> rdf:type owl:Ontology`` → ``<new> rdf:type owl:Ontology``
* ``<old> owl:imports <X>`` → ``<new> owl:imports <X>``
* ``<old> sh:prefixes <old>`` → ``<new> sh:prefixes <new>``
(self-referential link rewritten on both sides)
* ``<X> sh:prefixes <old>`` → ``<X> sh:prefixes <new>``
(object-only rewrite when subject differs)
* ``<old> owl:versionIRI <old>`` → ``<new> owl:versionIRI <old>``
(subject rewritten; version value **preserved**)
"""
...
# ------------------------------------------------------------------ #
# Aliases #
# ------------------------------------------------------------------ #
"""Add an alias for a canonical ontology IRI.
The alias will route to the same graph as the canonical IRI.
Aliases only point to canonical IRIs (not other aliases) to avoid chains.
Example:
env.add_alias("http://example.com/ont-alias", "http://example.com/ont")
env["http://example.com/ont-alias"] # Returns same graph as env["http://example.com/ont"]
"""
...
"""Remove an alias."""
...
"""Get the canonical IRI for an alias.
Returns None if the IRI is not an alias.
"""
...
"""List all aliases that point to a given canonical IRI."""
...
"""Check if an IRI is a canonical ontology (not an alias)."""
...
# ------------------------------------------------------------------ #
# Querying ontologies #
# ------------------------------------------------------------------ #
"""Return a read-only store-backed view of the named graph for *uri*.
Mutating the returned graph raises ``ValueError``; use
:py:meth:`copy_graph` for a mutable in-memory copy.
"""
...
"""Copy the named graph for *uri* into a mutable ``rdflib.Graph``.
If *graph* is provided, triples are added to it and the same graph is
returned. Otherwise a new in-memory graph is returned.
"""
...
"""Return a read-only merged view over the imports closure of *uri*.
Behaves like a merged ``rdflib.Graph`` but routes triple-pattern
lookups across the underlying named graphs without materializing
a copy. Returns ``(view, closure_names)``; ``view`` is a
:py:class:`ontoenv.ClosureGraphView`. Use :py:meth:`copy_closure` for
a mutable in-memory merge.
"""
...
"""Deprecated alias for :py:meth:`get_closure`.
Emits ``DeprecationWarning``.
"""
...
"""Stream ``(s, p, o)`` triples for one named graph as rdflib terms.
Skips the rdflib ``Graph`` wrapper; use when you only need to scan.
"""
...
"""Stream ``(s, p, o)`` triples across the imports closure of *uri*.
Triples are not de-duplicated across named graphs; wrap in
``set()`` if you need set semantics.
"""
...
"""Return the metadata object for the ontology identified by *uri*."""
...
"""Return the IRIs of all ontologies currently in the environment."""
...
"""Return IRIs of ontologies that directly import *uri*."""
...
"""Return the IRIs of all ontologies in the transitive import closure.
*uri* may be:
* a **string IRI** of an ontology already in the environment — the
closure is computed entirely from the stored dependency graph.
* an **``rdflib.Graph``** not yet in the environment — its
``owl:imports`` triples are extracted and the closure is resolved
from the environment without modifying it. The graph's own
ontology IRI (if present) appears first in the result.
*recursion_depth* limits traversal depth; ``-1`` means unlimited.
"""
...
"""Copy the import closure of *uri* into a mutable graph.
Returns a ``(graph, closure_iris)`` tuple. If *graph* is provided the
triples are added to it and the same graph is returned; otherwise a
new ``rdflib.Graph`` is returned.
"""
...
"""Merge the closure of *uri* into *destination_graph* in place."""
...
"""Resolve the ``owl:imports`` of *graph* and merge them into it.
Returns the list of ontology IRIs that were merged.
Set ``fetch_missing=True`` to fetch imports that are not yet in the
environment.
"""
...
"""Return the merged dependency graph and the closure IRI list.
Similar to ``import_dependencies`` but returns a *new* graph
containing only the dependencies (the input graph is not modified).
*graph_name* overrides the ontology IRI used for sh:prefixes
rewriting.
"""
...
"""Return IRIs of ``owl:imports`` that cannot be resolved.
*uri* may be:
* ``None`` — scan every ontology in the environment and return the
union of all unresolvable imports (de-duplicated).
* a **string IRI** of an ontology already in the environment — walk
its full transitive closure and return every unresolvable import.
* an **``rdflib.Graph``** not yet in the environment — extract its
direct ``owl:imports``; imports absent from the environment are
reported immediately; imports that are present are checked
transitively for their own missing dependencies.
"""
...
"""Return a prefix→namespace mapping.
If *ontology* is ``None`` all namespaces across the environment are
merged. Set ``include_closure=True`` to include namespaces from the
transitive import closure of *ontology*.
"""
...
"""Return a read-only store-backed ``rdflib.Dataset`` view.
Mutation raises ``ValueError``. The returned dataset is cached and
refreshed automatically after environment mutations.
"""
...
"""Copy the environment into a mutable ``rdflib.Dataset``.
If *dataset* is provided, quads are added to it and the same dataset is
returned. Otherwise a new in-memory dataset is returned.
"""
...
"""Deprecated alias for :meth:`get_dataset` / :meth:`copy_dataset`.
Emits ``DeprecationWarning``. ``backend="copy"`` without *store*
returns :meth:`copy_dataset`; otherwise this returns a read-only
Dataset view using the requested backend.
"""
...
"""Deprecated alias for :meth:`get_dataset` / :meth:`copy_dataset`.
Emits ``DeprecationWarning``. ``backend="copy"`` without *store*
returns :meth:`copy_dataset`; otherwise this returns a read-only
Dataset view using the requested backend.
"""
...
"""Re-snapshot ``self`` into an existing ``OntoEnvStore``-backed Dataset.
Raises ``TypeError`` if ``dataset.store`` is not an
:class:`ontoenv.OntoEnvStore`.
"""
...
"""Deprecated alias for :meth:`get_dataset` / :meth:`copy_dataset`.
Emits ``DeprecationWarning``. ``mode="copy"`` returns
:meth:`copy_dataset`; otherwise this returns a read-only Dataset view
using the requested mode.
"""
...
"""Print a Turtle serialisation of the environment to stdout."""
...
# ------------------------------------------------------------------ #
# Configuration accessors #
# ------------------------------------------------------------------ #
...
...
...
...
...
...
...
...
...
...
...
# ------------------------------------------------------------------ #
# Pythonic sugar #
# ------------------------------------------------------------------ #
"""Always ``True`` — use ``env is None`` to detect absence."""
...
"""Number of ontologies in the environment."""
...
"""``uri in env`` — True if *uri* resolves to a known ontology."""
...
"""``env[uri]`` — shorthand for :py:meth:`get_graph`."""
...
"""Iterate over the URIs of every ontology in the environment."""
...
...
...
"""rdflib Store implementation backed by OntoEnv's native query engine."""
...
...
...
...
...
...
...
...
...
...
...
...