Coverage for target/native-coverage/profiles/ed7d3b207b2343278478d0be29776579/project/.venv/lib/python3.13/site-packages/owlrl_turbo/__init__.py: 100%
44 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-12-01 11:12 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-12-01 11:12 +0000
1"""RDFS and OWL-RL inference using rdflib-turbo's native runtime."""
3from importlib.metadata import version
4from typing import overload
6from rdflib import BNode
7from rdflib.graph import Dataset as RDFLibDataset
8from rdflib.graph import Graph
9from rdflib_turbo import Dataset, StoreDataset
10from rdflib_turbo import Graph as NativeGraph
12from ._closure import (
13 DeductiveClosure,
14 OWLRL_Semantics,
15 RDFS_OWLRL_Semantics,
16 RDFS_Semantics,
17 _bind_diagnostics,
18 _expand,
19 _publish_changes,
20 return_closure_class,
21)
22from ._imports import AUTO, JSON, RDFA, RDFXML, TURTLE, interpret_owl_imports
24__version__ = version("owlrl-turbo")
25__all__ = [
26 "AUTO",
27 "JSON",
28 "RDFA",
29 "RDFXML",
30 "TURTLE",
31 "DeductiveClosure",
32 "OWLRL_Semantics",
33 "RDFS_OWLRL_Semantics",
34 "RDFS_Semantics",
35 "expand",
36 "interpret_owl_imports",
37 "return_closure_class",
38]
41@overload
42def expand(
43 graph: NativeGraph,
44 *,
45 inference: str = "owlrl",
46 inplace: bool = False,
47 destination: str | BNode | Graph | None = None,
48 axiomatic_triples: bool = False,
49 datatype_axioms: bool = False,
50) -> NativeGraph: ...
53@overload
54def expand(
55 graph: StoreDataset,
56 *,
57 inference: str = "owlrl",
58 inplace: bool = False,
59 destination: str | BNode | Graph | None = None,
60 axiomatic_triples: bool = False,
61 datatype_axioms: bool = False,
62) -> StoreDataset: ...
65@overload
66def expand(
67 graph: RDFLibDataset,
68 *,
69 inference: str = "owlrl",
70 inplace: bool = False,
71 destination: str | None = None,
72 axiomatic_triples: bool = False,
73 datatype_axioms: bool = False,
74) -> RDFLibDataset: ...
77@overload
78def expand(
79 graph: Graph,
80 *,
81 inference: str = "owlrl",
82 inplace: bool = False,
83 destination: str | None = None,
84 axiomatic_triples: bool = False,
85 datatype_axioms: bool = False,
86) -> Graph: ...
89@overload
90def expand(
91 graph: Dataset,
92 *,
93 inference: str = "owlrl",
94 inplace: bool = False,
95 destination: str | None = None,
96 axiomatic_triples: bool = False,
97 datatype_axioms: bool = False,
98) -> Dataset: ...
101def expand(
102 graph: Dataset | Graph,
103 *,
104 inference: str = "owlrl",
105 inplace: bool = False,
106 destination: str | BNode | Graph | None = None,
107 axiomatic_triples: bool = False,
108 datatype_axioms: bool = False,
109) -> Dataset | Graph:
110 """Compute a native closure, returning the input kind.
112 Modes are rdfs, owlrl and both. The default leaves the input unchanged.
113 destination, when specified, is an IRI naming the inference output graph;
114 native Graph inputs also accept exact blank identifiers and same-store Graph
115 destinations. Ordinary RDFLib Graph inputs cannot select a destination. Native writes commit
116 only after the complete inference operation succeeds.
117 """
118 options = {"axiomatic_triples": axiomatic_triples, "datatype_axioms": datatype_axioms}
119 if inference not in ("rdfs", "owlrl", "both"):
120 raise ValueError(f"Unknown inference mode: {inference}")
121 if isinstance(graph, (NativeGraph, StoreDataset)):
122 if isinstance(graph, StoreDataset) and destination is None:
123 destination = "urn:owlrl-turbo:inference"
124 graph._inference_scope(destination)
125 result = graph if inplace else graph.copy()
126 target = destination.identifier if isinstance(destination, Graph) else destination
127 _expand(result, inference, target, axiomatic_triples, datatype_axioms)
128 return result
129 if getattr(graph, "_turbo_native_graph", False):
130 raise TypeError("Expected a native Graph or StoreDataset instance")
131 if not isinstance(graph, Graph):
132 raise TypeError("Expected rdflib_turbo.Dataset or an RDFLib Graph/Dataset")
133 multigraph = isinstance(graph, RDFLibDataset)
134 if graph.context_aware and not multigraph:
135 raise NotImplementedError("Convert context-aware graphs to rdflib.Dataset first")
136 if destination is not None and not multigraph:
137 raise ValueError("A named inference destination requires a Dataset")
138 if multigraph and destination is None:
139 destination = "urn:owlrl-turbo:inference"
140 native_source = Dataset.from_rdflib(graph)
141 if inplace:
142 changes = native_source._capture_changes(
143 inference=inference, destination=destination, **options
144 )
145 try:
146 _publish_changes(graph, changes)
147 _bind_diagnostics(graph, changes.outcome.error_messages)
148 return graph
149 finally:
150 changes.close()
151 expanded = native_source.infer(
152 inference=inference, destination=destination, **options
153 ).to_rdflib()
154 return expanded if multigraph else expanded.default_graph