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
- from rdflib .namespace import NamespaceManager
7
5
8
- from .clone import clone_blank_node , clone_graph , clone_node
6
+ from .clone import clone_blank_node , clone_dataset , clone_node
9
7
from .consts import OWL , RDF , ConjunctiveLike , GraphLike , OWL_classes , OWL_properties , RDFS_classes , RDFS_properties
10
8
11
9
if TYPE_CHECKING :
12
10
from rdflib import BNode
13
- from rdflib .term import IdentifiedNode
11
+ from rdflib .term import URIRef
14
12
15
13
from .consts import RDFNode
16
14
17
15
OWLNamedIndividual = OWL .NamedIndividual
18
16
19
17
20
- def inoculate (data_graph : rdflib .Graph , ontology : rdflib . Graph ) -> rdflib .Graph :
18
+ def inoculate (data_graph : rdflib .Graph , ontology : GraphLike ) -> rdflib .Graph :
21
19
"""
22
20
Copies all RDFS and OWL axioms (classes, relationship definitions, and properties)
23
21
from the ontology graph into the data_graph.
@@ -33,6 +31,9 @@ def inoculate(data_graph: rdflib.Graph, ontology: rdflib.Graph) -> rdflib.Graph:
33
31
ontology_ns = ontology .namespace_manager
34
32
data_graph_ns = data_graph .namespace_manager
35
33
34
+ if isinstance (ontology , (rdflib .ConjunctiveGraph , rdflib .Dataset )):
35
+ # always set default context true on the ontology DS
36
+ ontology .default_context = True
36
37
# Bind any missing ontology namespaces in the DataGraph NS manager.
37
38
if ontology_ns is not data_graph_ns :
38
39
data_graph_prefixes = {p : n for (p , n ) in data_graph_ns .namespaces ()}
@@ -108,119 +109,46 @@ def inoculate(data_graph: rdflib.Graph, ontology: rdflib.Graph) -> rdflib.Graph:
108
109
109
110
110
111
def inoculate_dataset (
111
- base_ds : ConjunctiveLike , ontology_ds : GraphLike , target_ds : Optional [Union [ConjunctiveLike , str ]] = None
112
+ base_ds : ConjunctiveLike ,
113
+ ontology_ds : GraphLike ,
114
+ target_ds : Optional [Union [ConjunctiveLike , str ]] = None ,
115
+ target_graph_identifier : Optional ['URIRef' ] = None ,
112
116
):
113
117
"""
114
118
Make a clone of base_ds (dataset) and add RDFS and OWL triples from ontology_ds
115
119
:param base_ds:
116
120
:type base_ds: rdflib.Dataset
117
121
:param ontology_ds:
118
- :type ontology_ds: rdflib.Dataset
122
+ :type ontology_ds: rdflib.Dataset|rdflib.ConjunctiveGraph|rdflib.Graph
119
123
:param target_ds:
120
124
:type target_ds: rdflib.Dataset|str|NoneType
125
+ :param target_graph_identifier:
126
+ :type target_graph_identifier: rdflib.URIRef | None
121
127
:return: The cloned Dataset with ontology triples from ontology_ds
122
128
:rtype: rdflib.Dataset
123
129
"""
124
130
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
131
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
132
+ target_ds = clone_dataset ( base_ds )
133
+ elif target_ds is base_ds :
134
+ pass
145
135
elif target_ds == "inplace" or target_ds == "base" :
146
136
target_ds = base_ds
147
137
elif isinstance (target_ds , str ):
148
138
raise RuntimeError ("target_ds cannot be a string (unless it is 'inplace' or 'base')" )
139
+
149
140
if isinstance (target_ds , (rdflib .ConjunctiveGraph , rdflib .Dataset )):
150
141
if not isinstance (target_ds , rdflib .Dataset ):
151
142
raise RuntimeError ("Cannot inoculate ConjunctiveGraph, use Dataset instead." )
152
143
else :
153
144
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 )
145
+
146
+ if target_graph_identifier :
147
+ dest_graph = target_ds .get_context (target_graph_identifier )
179
148
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 )
149
+ dest_graph = target_ds .default_context
150
+
151
+ # inoculate() routine will set default_union on the ontology_ds if it is a Dataset
152
+ inoculate (dest_graph , ontology_ds )
225
153
226
154
return target_ds
0 commit comments