2
2
from typing import TYPE_CHECKING , Dict , Optional , Union
3
3
4
4
import rdflib
5
- from rdflib .graph import DATASET_DEFAULT_GRAPH_ID
6
5
from rdflib .namespace import NamespaceManager
7
6
8
- from .clone import clone_blank_node , clone_graph , clone_node
7
+ from .clone import clone_blank_node , clone_graph , clone_node , clone_dataset
9
8
from .consts import OWL , RDF , ConjunctiveLike , GraphLike , OWL_classes , OWL_properties , RDFS_classes , RDFS_properties
10
9
11
10
if TYPE_CHECKING :
12
11
from rdflib import BNode
13
- from rdflib .term import IdentifiedNode
12
+ from rdflib .term import URIRef
14
13
15
14
from .consts import RDFNode
16
15
17
16
OWLNamedIndividual = OWL .NamedIndividual
18
17
19
18
20
- def inoculate (data_graph : rdflib .Graph , ontology : rdflib . Graph ) -> rdflib .Graph :
19
+ def inoculate (data_graph : rdflib .Graph , ontology : GraphLike ) -> rdflib .Graph :
21
20
"""
22
21
Copies all RDFS and OWL axioms (classes, relationship definitions, and properties)
23
22
from the ontology graph into the data_graph.
@@ -33,6 +32,9 @@ def inoculate(data_graph: rdflib.Graph, ontology: rdflib.Graph) -> rdflib.Graph:
33
32
ontology_ns = ontology .namespace_manager
34
33
data_graph_ns = data_graph .namespace_manager
35
34
35
+ if isinstance (ontology , (rdflib .ConjunctiveGraph , rdflib .Dataset )):
36
+ # always set default context true on the ontology DS
37
+ ontology .default_context = True
36
38
# Bind any missing ontology namespaces in the DataGraph NS manager.
37
39
if ontology_ns is not data_graph_ns :
38
40
data_graph_prefixes = {p : n for (p , n ) in data_graph_ns .namespaces ()}
@@ -108,119 +110,44 @@ def inoculate(data_graph: rdflib.Graph, ontology: rdflib.Graph) -> rdflib.Graph:
108
110
109
111
110
112
def inoculate_dataset (
111
- base_ds : ConjunctiveLike , ontology_ds : GraphLike , target_ds : Optional [Union [ConjunctiveLike , str ]] = None
113
+ base_ds : ConjunctiveLike , ontology_ds : GraphLike , target_ds : Optional [Union [ConjunctiveLike , str ]] = None ,
114
+ target_graph_identifier : Optional ['URIRef' ] = None ,
112
115
):
113
116
"""
114
117
Make a clone of base_ds (dataset) and add RDFS and OWL triples from ontology_ds
115
118
:param base_ds:
116
119
:type base_ds: rdflib.Dataset
117
120
:param ontology_ds:
118
- :type ontology_ds: rdflib.Dataset
121
+ :type ontology_ds: rdflib.Dataset|rdflib.ConjunctiveGraph|rdflib.Graph
119
122
:param target_ds:
120
123
:type target_ds: rdflib.Dataset|str|NoneType
124
+ :param target_graph_identifier:
125
+ :type target_graph_identifier: rdflib.URIRef | None
121
126
:return: The cloned Dataset with ontology triples from ontology_ds
122
127
:rtype: rdflib.Dataset
123
128
"""
124
129
125
- # TODO: Decide whether we need to clone base_ds before calling this,
126
- # or we clone base_ds as part of this function
127
- default_union : bool = base_ds .default_union
128
- base_named_graphs = [
129
- (
130
- rdflib .Graph (base_ds .store , i , namespace_manager = base_ds .namespace_manager ) # type: ignore[arg-type]
131
- if not isinstance (i , rdflib .Graph )
132
- else i
133
- )
134
- for i in base_ds .store .contexts (None )
135
- ]
136
- if isinstance (base_ds , rdflib .Dataset ) and len (base_named_graphs ) < 1 :
137
- base_named_graphs = [
138
- rdflib .Graph (base_ds .store , DATASET_DEFAULT_GRAPH_ID , namespace_manager = base_ds .namespace_manager )
139
- ]
140
- base_default_context_id = base_ds .default_context .identifier
141
130
if target_ds is None :
142
- target_ds = rdflib . Dataset ( default_union = default_union )
143
- target_ds . namespace_manager = NamespaceManager ( target_ds , 'core' )
144
- target_ds . default_context . namespace_manager = target_ds . namespace_manager
131
+ target_ds = clone_dataset ( base_ds )
132
+ elif target_ds is base_ds :
133
+ pass
145
134
elif target_ds == "inplace" or target_ds == "base" :
146
135
target_ds = base_ds
147
136
elif isinstance (target_ds , str ):
148
137
raise RuntimeError ("target_ds cannot be a string (unless it is 'inplace' or 'base')" )
138
+
149
139
if isinstance (target_ds , (rdflib .ConjunctiveGraph , rdflib .Dataset )):
150
140
if not isinstance (target_ds , rdflib .Dataset ):
151
141
raise RuntimeError ("Cannot inoculate ConjunctiveGraph, use Dataset instead." )
152
142
else :
153
143
raise RuntimeError ("Cannot inoculate datasets if target_ds passed in is not a Dataset itself." )
154
- ont_default_context_id : Union [IdentifiedNode , str , None ]
155
- if isinstance (ontology_ds , (rdflib .Dataset , rdflib .ConjunctiveGraph )):
156
- ont_graphs = [
157
- (
158
- rdflib .Graph (ontology_ds .store , i , namespace_manager = ontology_ds .namespace_manager ) # type: ignore[arg-type]
159
- if not isinstance (i , rdflib .Graph )
160
- else i
161
- )
162
- for i in ontology_ds .store .contexts (None )
163
- ]
164
- ont_default_context_id = ontology_ds .default_context .identifier
165
- else :
166
- ont_graphs = [ontology_ds ]
167
- ont_default_context_id = None
168
- if target_ds is base_ds or target_ds == "inplace" or target_ds == "base" :
169
- target_ds = base_ds
170
- for bg in base_named_graphs :
171
- if len (base_named_graphs ) > 1 and bg .identifier == base_default_context_id and len (bg ) < 1 :
172
- # skip empty default named graph in base_graph
173
- continue
174
- for og in ont_graphs :
175
- if len (ont_graphs ) > 1 and og .identifier == ont_default_context_id and len (og ) < 1 :
176
- # skip empty default named graph in ontology_graph
177
- continue
178
- inoculate (bg , og )
144
+
145
+ if target_graph_identifier :
146
+ dest_graph = target_ds .get_context (target_graph_identifier )
179
147
else :
180
- inoculated_graphs = {}
181
- for bg in base_named_graphs :
182
- if len (base_named_graphs ) > 1 and bg .identifier == base_default_context_id and len (bg ) < 1 :
183
- # skip empty default named graph in base_graph
184
- continue
185
- target_g = rdflib .Graph (store = target_ds .store , identifier = bg .identifier )
186
- clone_g = clone_graph (bg , target_graph = target_g )
187
- for og in ont_graphs :
188
- if len (ont_graphs ) > 1 and og .identifier == ont_default_context_id and len (og ) < 1 :
189
- # skip empty default named graph in ontology_graph
190
- continue
191
- inoculate (clone_g , og )
192
- inoculated_graphs [bg .identifier ] = clone_g
193
-
194
- base_graph_identifiers = [bg .identifier for bg in base_named_graphs ]
195
- base_default_context_id = base_ds .default_context .identifier
196
- target_default_context_id = target_ds .default_context .identifier
197
- if base_default_context_id != target_default_context_id :
198
- old_target_default_context = target_ds .default_context
199
- old_target_default_context_id = old_target_default_context .identifier
200
- if isinstance (target_ds , rdflib .Dataset ):
201
- new_target_default_context = target_ds .graph (base_default_context_id )
202
- else :
203
- new_target_default_context = target_ds .get_context (base_default_context_id )
204
- target_ds .store .add_graph (new_target_default_context )
205
- target_ds .default_context = new_target_default_context
206
- if old_target_default_context_id not in base_graph_identifiers :
207
- if isinstance (target_ds , rdflib .Dataset ):
208
- target_ds .remove_graph (old_target_default_context )
209
- else :
210
- target_ds .store .remove_graph (old_target_default_context )
211
- target_default_context_id = new_target_default_context .identifier
212
- else :
213
- if isinstance (target_ds , rdflib .Dataset ):
214
- _ = target_ds .graph (target_default_context_id )
215
- else :
216
- t_default = target_ds .get_context (target_default_context_id )
217
- target_ds .store .add_graph (t_default )
218
- for i , ig in inoculated_graphs .items ():
219
- if ig == target_ds .default_context or i == target_default_context_id :
220
- continue
221
- if isinstance (target_ds , rdflib .Dataset ):
222
- _ = target_ds .graph (ig ) # alias to Dataset.add_graph()
223
- else :
224
- target_ds .store .add_graph (ig )
148
+ dest_graph = target_ds .default_context
149
+
150
+ # inoculate() routine will set default_union on the ontology_ds if it is a Dataset
151
+ inoculate (dest_graph , ontology_ds )
225
152
226
153
return target_ds
0 commit comments