cognite-neat 0.85.12__py3-none-any.whl → 0.87.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cognite-neat might be problematic. Click here for more details.
- cognite/neat/_version.py +1 -1
- cognite/neat/app/api/routers/core.py +4 -4
- cognite/neat/constants.py +11 -9
- cognite/neat/graph/extractors/_mock_graph_generator.py +8 -9
- cognite/neat/graph/loaders/__init__.py +5 -2
- cognite/neat/graph/loaders/_base.py +13 -5
- cognite/neat/graph/loaders/_rdf2asset.py +94 -20
- cognite/neat/graph/loaders/_rdf2dms.py +3 -16
- cognite/neat/graph/queries/_base.py +58 -5
- cognite/neat/graph/queries/_construct.py +17 -15
- cognite/neat/graph/queries/_shared.py +20 -6
- cognite/neat/graph/stores/_base.py +19 -10
- cognite/neat/graph/transformers/_rdfpath.py +7 -0
- cognite/neat/legacy/graph/extractors/_dexpi.py +0 -5
- cognite/neat/legacy/graph/stores/_base.py +24 -8
- cognite/neat/legacy/graph/stores/_graphdb_store.py +3 -2
- cognite/neat/legacy/graph/stores/_memory_store.py +3 -3
- cognite/neat/legacy/graph/stores/_oxigraph_store.py +8 -4
- cognite/neat/legacy/graph/stores/_rdf_to_graph.py +5 -3
- cognite/neat/legacy/graph/transformations/query_generator/sparql.py +48 -15
- cognite/neat/legacy/rules/importers/_graph2rules.py +34 -7
- cognite/neat/legacy/rules/models/raw_rules.py +18 -6
- cognite/neat/legacy/rules/models/rules.py +32 -12
- cognite/neat/rules/_shared.py +6 -1
- cognite/neat/rules/analysis/__init__.py +4 -4
- cognite/neat/rules/analysis/_asset.py +128 -0
- cognite/neat/rules/analysis/_base.py +385 -6
- cognite/neat/rules/analysis/_information.py +155 -0
- cognite/neat/rules/exporters/_base.py +4 -4
- cognite/neat/rules/exporters/_rules2dms.py +1 -1
- cognite/neat/rules/exporters/_rules2ontology.py +5 -5
- cognite/neat/rules/importers/_base.py +4 -4
- cognite/neat/rules/importers/_dtdl2rules/dtdl_converter.py +2 -8
- cognite/neat/rules/importers/_inference2rules.py +2 -2
- cognite/neat/rules/importers/_owl2rules/_owl2metadata.py +1 -1
- cognite/neat/rules/importers/_spreadsheet2rules.py +5 -5
- cognite/neat/rules/models/__init__.py +3 -3
- cognite/neat/rules/models/_base.py +10 -10
- cognite/neat/rules/models/asset/_rules.py +9 -10
- cognite/neat/rules/models/dms/_converter.py +4 -5
- cognite/neat/rules/models/dms/_rules.py +6 -3
- cognite/neat/rules/models/domain.py +5 -2
- cognite/neat/rules/models/entities.py +2 -9
- cognite/neat/rules/models/information/_converter.py +3 -3
- cognite/neat/rules/models/information/_rules.py +13 -11
- cognite/neat/rules/models/information/_rules_input.py +1 -2
- cognite/neat/rules/models/information/_validation.py +1 -1
- cognite/neat/utils/utils.py +54 -18
- cognite/neat/workflows/steps/lib/current/graph_store.py +28 -8
- cognite/neat/workflows/steps/lib/legacy/graph_extractor.py +129 -27
- cognite/neat/workflows/steps/lib/legacy/graph_store.py +4 -4
- {cognite_neat-0.85.12.dist-info → cognite_neat-0.87.0.dist-info}/METADATA +1 -1
- {cognite_neat-0.85.12.dist-info → cognite_neat-0.87.0.dist-info}/RECORD +56 -54
- cognite/neat/rules/analysis/_information_rules.py +0 -469
- {cognite_neat-0.85.12.dist-info → cognite_neat-0.87.0.dist-info}/LICENSE +0 -0
- {cognite_neat-0.85.12.dist-info → cognite_neat-0.87.0.dist-info}/WHEEL +0 -0
- {cognite_neat-0.85.12.dist-info → cognite_neat-0.87.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,469 +0,0 @@
|
|
|
1
|
-
import itertools
|
|
2
|
-
import logging
|
|
3
|
-
import warnings
|
|
4
|
-
from collections import defaultdict
|
|
5
|
-
from typing import Any, Generic, TypeVar
|
|
6
|
-
|
|
7
|
-
import pandas as pd
|
|
8
|
-
from pydantic import ValidationError
|
|
9
|
-
|
|
10
|
-
from cognite.neat.rules.models import SchemaCompleteness
|
|
11
|
-
from cognite.neat.rules.models._rdfpath import RDFPath
|
|
12
|
-
from cognite.neat.rules.models.asset import AssetClass, AssetProperty, AssetRules
|
|
13
|
-
from cognite.neat.rules.models.entities import (
|
|
14
|
-
AssetEntity,
|
|
15
|
-
ClassEntity,
|
|
16
|
-
EntityTypes,
|
|
17
|
-
ParentClassEntity,
|
|
18
|
-
ReferenceEntity,
|
|
19
|
-
RelationshipEntity,
|
|
20
|
-
)
|
|
21
|
-
from cognite.neat.rules.models.information import (
|
|
22
|
-
InformationClass,
|
|
23
|
-
InformationProperty,
|
|
24
|
-
InformationRules,
|
|
25
|
-
)
|
|
26
|
-
from cognite.neat.utils.utils import get_inheritance_path
|
|
27
|
-
|
|
28
|
-
T_Rules = TypeVar("T_Rules", InformationRules, AssetRules)
|
|
29
|
-
T_Property = TypeVar("T_Property", InformationProperty, AssetProperty)
|
|
30
|
-
T_Class = TypeVar("T_Class", InformationClass, AssetClass)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class _SharedAnalysis(Generic[T_Rules, T_Property, T_Class]):
|
|
34
|
-
def __init__(self, rules: T_Rules):
|
|
35
|
-
self.rules: T_Rules = rules
|
|
36
|
-
|
|
37
|
-
@property
|
|
38
|
-
def directly_referred_classes(self) -> set[ClassEntity]:
|
|
39
|
-
return {
|
|
40
|
-
class_.reference.as_class_entity()
|
|
41
|
-
for class_ in self.rules.classes
|
|
42
|
-
if self.rules.reference
|
|
43
|
-
and class_.reference
|
|
44
|
-
and isinstance(class_.reference, ReferenceEntity)
|
|
45
|
-
and class_.reference.prefix == self.rules.reference.metadata.prefix
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
@property
|
|
49
|
-
def inherited_referred_classes(self) -> set[ClassEntity]:
|
|
50
|
-
dir_referred_classes = self.directly_referred_classes
|
|
51
|
-
inherited_referred_classes = []
|
|
52
|
-
for class_ in dir_referred_classes:
|
|
53
|
-
inherited_referred_classes.extend(self.class_inheritance_path(class_))
|
|
54
|
-
return set(inherited_referred_classes)
|
|
55
|
-
|
|
56
|
-
def class_parent_pairs(self) -> dict[ClassEntity, list[ParentClassEntity]]:
|
|
57
|
-
"""This only returns class - parent pairs only if parent is in the same data model"""
|
|
58
|
-
class_subclass_pairs: dict[ClassEntity, list[ParentClassEntity]] = {}
|
|
59
|
-
|
|
60
|
-
if not self.rules:
|
|
61
|
-
return class_subclass_pairs
|
|
62
|
-
|
|
63
|
-
for definition in self.rules.classes:
|
|
64
|
-
class_subclass_pairs[definition.class_] = []
|
|
65
|
-
|
|
66
|
-
if definition.parent is None:
|
|
67
|
-
continue
|
|
68
|
-
|
|
69
|
-
for parent in definition.parent:
|
|
70
|
-
if parent.prefix == definition.class_.prefix:
|
|
71
|
-
class_subclass_pairs[definition.class_].append(parent)
|
|
72
|
-
else:
|
|
73
|
-
warnings.warn(
|
|
74
|
-
f"Parent class {parent} of class {definition} is not in the same namespace, skipping !",
|
|
75
|
-
stacklevel=2,
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
return class_subclass_pairs
|
|
79
|
-
|
|
80
|
-
def classes_with_properties(self, consider_inheritance: bool = False) -> dict[ClassEntity, list[T_Property]]:
|
|
81
|
-
"""Returns classes that have been defined in the data model.
|
|
82
|
-
|
|
83
|
-
Args:
|
|
84
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
85
|
-
|
|
86
|
-
Returns:
|
|
87
|
-
Dictionary of classes with a list of properties defined for them
|
|
88
|
-
|
|
89
|
-
!!! note "consider_inheritance"
|
|
90
|
-
If consider_inheritance is True, properties from parent classes will also be considered.
|
|
91
|
-
This means if a class has a parent class, and the parent class has properties defined for it,
|
|
92
|
-
while we do not have any properties defined for the child class, we will still consider the
|
|
93
|
-
properties from the parent class. If consider_inheritance is False, we will only consider
|
|
94
|
-
properties defined for the child class, thus if no properties are defined for the child class,
|
|
95
|
-
it will not be included in the returned dictionary.
|
|
96
|
-
"""
|
|
97
|
-
|
|
98
|
-
class_property_pairs: dict[ClassEntity, list[T_Property]] = defaultdict(list)
|
|
99
|
-
|
|
100
|
-
for property_ in self.rules.properties:
|
|
101
|
-
class_property_pairs[property_.class_].append(property_) # type: ignore
|
|
102
|
-
|
|
103
|
-
if consider_inheritance:
|
|
104
|
-
class_parent_pairs = self.class_parent_pairs()
|
|
105
|
-
for class_ in class_parent_pairs:
|
|
106
|
-
self._add_inherited_properties(class_, class_property_pairs, class_parent_pairs)
|
|
107
|
-
|
|
108
|
-
return class_property_pairs
|
|
109
|
-
|
|
110
|
-
def class_inheritance_path(self, class_: ClassEntity | str) -> list[ClassEntity]:
|
|
111
|
-
class_ = class_ if isinstance(class_, ClassEntity) else ClassEntity.load(class_)
|
|
112
|
-
class_parent_pairs = self.class_parent_pairs()
|
|
113
|
-
return get_inheritance_path(class_, class_parent_pairs)
|
|
114
|
-
|
|
115
|
-
@classmethod
|
|
116
|
-
def _add_inherited_properties(
|
|
117
|
-
cls,
|
|
118
|
-
class_: ClassEntity,
|
|
119
|
-
class_property_pairs: dict[ClassEntity, list[T_Property]],
|
|
120
|
-
class_parent_pairs: dict[ClassEntity, list[ParentClassEntity]],
|
|
121
|
-
):
|
|
122
|
-
inheritance_path = get_inheritance_path(class_, class_parent_pairs)
|
|
123
|
-
for parent in inheritance_path:
|
|
124
|
-
# ParentClassEntity -> ClassEntity to match the type of class_property_pairs
|
|
125
|
-
if parent.as_class_entity() in class_property_pairs:
|
|
126
|
-
for property_ in class_property_pairs[parent.as_class_entity()]:
|
|
127
|
-
property_ = property_.model_copy()
|
|
128
|
-
|
|
129
|
-
# This corresponds to importing properties from parent class
|
|
130
|
-
# making sure that the property is attached to desired child class
|
|
131
|
-
property_.class_ = class_
|
|
132
|
-
property_.inherited = True
|
|
133
|
-
|
|
134
|
-
# need same if we have RDF path to make sure that the starting class is the
|
|
135
|
-
|
|
136
|
-
if class_ in class_property_pairs:
|
|
137
|
-
class_property_pairs[class_].append(property_)
|
|
138
|
-
else:
|
|
139
|
-
class_property_pairs[class_] = [property_]
|
|
140
|
-
|
|
141
|
-
def class_property_pairs(
|
|
142
|
-
self, only_rdfpath: bool = False, consider_inheritance: bool = False
|
|
143
|
-
) -> dict[ClassEntity, dict[str, T_Property]]:
|
|
144
|
-
"""Returns a dictionary of classes with a dictionary of properties associated with them.
|
|
145
|
-
|
|
146
|
-
Args:
|
|
147
|
-
only_rdfpath : To consider only properties which have rule `rdfpath` set. Defaults False
|
|
148
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
149
|
-
|
|
150
|
-
Returns:
|
|
151
|
-
Dictionary of classes with a dictionary of properties associated with them.
|
|
152
|
-
|
|
153
|
-
!!! note "difference to get_classes_with_properties"
|
|
154
|
-
This method returns a dictionary of classes with a dictionary of properties associated with them.
|
|
155
|
-
While get_classes_with_properties returns a dictionary of classes with a list of
|
|
156
|
-
properties defined for them,
|
|
157
|
-
here we filter the properties based on the `only_rdfpath` parameter and only consider
|
|
158
|
-
the first definition of a property if it is defined more than once.
|
|
159
|
-
|
|
160
|
-
!!! note "only_rdfpath"
|
|
161
|
-
If only_rdfpath is True, only properties with RuleType.rdfpath will be returned as
|
|
162
|
-
a part of the dictionary of properties related to a class. Otherwise, all properties
|
|
163
|
-
will be returned.
|
|
164
|
-
|
|
165
|
-
!!! note "consider_inheritance"
|
|
166
|
-
If consider_inheritance is True, properties from parent classes will also be considered.
|
|
167
|
-
This means if a class has a parent class, and the parent class has properties defined for it,
|
|
168
|
-
while we do not have any properties defined for the child class, we will still consider the
|
|
169
|
-
properties from the parent class. If consider_inheritance is False, we will only consider
|
|
170
|
-
properties defined for the child class, thus if no properties are defined for the child class,
|
|
171
|
-
it will not be included in the returned dictionary.
|
|
172
|
-
"""
|
|
173
|
-
# TODO: https://cognitedata.atlassian.net/jira/software/projects/NEAT/boards/893?selectedIssue=NEAT-78
|
|
174
|
-
|
|
175
|
-
class_property_pairs = {}
|
|
176
|
-
|
|
177
|
-
for class_, properties in self.classes_with_properties(consider_inheritance).items():
|
|
178
|
-
processed_properties = {}
|
|
179
|
-
for property_ in properties:
|
|
180
|
-
if property_.property_ in processed_properties:
|
|
181
|
-
# TODO: use appropriate Warning class from _exceptions.py
|
|
182
|
-
# if missing make one !
|
|
183
|
-
warnings.warn(
|
|
184
|
-
f"Property {property_.property_} for {class_} has been defined more than once!"
|
|
185
|
-
" Only the first definition will be considered, skipping the rest..",
|
|
186
|
-
stacklevel=2,
|
|
187
|
-
)
|
|
188
|
-
continue
|
|
189
|
-
|
|
190
|
-
if (only_rdfpath and isinstance(property_.transformation, RDFPath)) or not only_rdfpath:
|
|
191
|
-
processed_properties[property_.property_] = property_
|
|
192
|
-
class_property_pairs[class_] = processed_properties
|
|
193
|
-
|
|
194
|
-
return class_property_pairs
|
|
195
|
-
|
|
196
|
-
def class_linkage(self, consider_inheritance: bool = False) -> pd.DataFrame:
|
|
197
|
-
"""Returns a dataframe with the class linkage of the data model.
|
|
198
|
-
|
|
199
|
-
Args:
|
|
200
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
201
|
-
|
|
202
|
-
Returns:
|
|
203
|
-
Dataframe with the class linkage of the data model
|
|
204
|
-
"""
|
|
205
|
-
|
|
206
|
-
class_linkage = pd.DataFrame(
|
|
207
|
-
columns=[
|
|
208
|
-
"source_class",
|
|
209
|
-
"target_class",
|
|
210
|
-
"connecting_property",
|
|
211
|
-
"max_occurrence",
|
|
212
|
-
]
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
class_property_pairs = self.classes_with_properties(consider_inheritance)
|
|
216
|
-
properties = list(itertools.chain.from_iterable(class_property_pairs.values()))
|
|
217
|
-
|
|
218
|
-
for property_ in properties:
|
|
219
|
-
if property_.type_ == EntityTypes.object_property:
|
|
220
|
-
new_row = pd.Series(
|
|
221
|
-
{
|
|
222
|
-
"source_class": property_.class_,
|
|
223
|
-
"connecting_property": property_.property_,
|
|
224
|
-
"target_class": property_.value_type,
|
|
225
|
-
"max_occurrence": property_.max_count,
|
|
226
|
-
}
|
|
227
|
-
)
|
|
228
|
-
class_linkage = pd.concat([class_linkage, new_row.to_frame().T], ignore_index=True)
|
|
229
|
-
|
|
230
|
-
class_linkage.drop_duplicates(inplace=True)
|
|
231
|
-
class_linkage = class_linkage[["source_class", "connecting_property", "target_class", "max_occurrence"]]
|
|
232
|
-
|
|
233
|
-
return class_linkage
|
|
234
|
-
|
|
235
|
-
def connected_classes(self, consider_inheritance: bool = False) -> set[ClassEntity]:
|
|
236
|
-
"""Return a set of classes that are connected to other classes.
|
|
237
|
-
|
|
238
|
-
Args:
|
|
239
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
240
|
-
|
|
241
|
-
Returns:
|
|
242
|
-
Set of classes that are connected to other classes
|
|
243
|
-
"""
|
|
244
|
-
class_linkage = self.class_linkage(consider_inheritance)
|
|
245
|
-
return set(class_linkage.source_class.values).union(set(class_linkage.target_class.values))
|
|
246
|
-
|
|
247
|
-
def defined_classes(self, consider_inheritance: bool = False) -> set[ClassEntity]:
|
|
248
|
-
"""Returns classes that have properties defined for them in the data model.
|
|
249
|
-
|
|
250
|
-
Args:
|
|
251
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
252
|
-
|
|
253
|
-
Returns:
|
|
254
|
-
Set of classes that have been defined in the data model
|
|
255
|
-
"""
|
|
256
|
-
class_property_pairs = self.classes_with_properties(consider_inheritance)
|
|
257
|
-
properties = list(itertools.chain.from_iterable(class_property_pairs.values()))
|
|
258
|
-
|
|
259
|
-
return {property.class_ for property in properties}
|
|
260
|
-
|
|
261
|
-
def disconnected_classes(self, consider_inheritance: bool = False) -> set[ClassEntity]:
|
|
262
|
-
"""Return a set of classes that are disconnected (i.e. isolated) from other classes.
|
|
263
|
-
|
|
264
|
-
Args:
|
|
265
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
266
|
-
|
|
267
|
-
Returns:
|
|
268
|
-
Set of classes that are disconnected from other classes
|
|
269
|
-
"""
|
|
270
|
-
return self.defined_classes(consider_inheritance) - self.connected_classes(consider_inheritance)
|
|
271
|
-
|
|
272
|
-
def symmetrically_connected_classes(
|
|
273
|
-
self, consider_inheritance: bool = False
|
|
274
|
-
) -> set[tuple[ClassEntity, ClassEntity]]:
|
|
275
|
-
"""Returns a set of pairs of symmetrically linked classes.
|
|
276
|
-
|
|
277
|
-
Args:
|
|
278
|
-
consider_inheritance: Whether to consider inheritance or not. Defaults False
|
|
279
|
-
|
|
280
|
-
Returns:
|
|
281
|
-
Set of pairs of symmetrically linked classes
|
|
282
|
-
|
|
283
|
-
!!! note "Symmetrically Connected Classes"
|
|
284
|
-
Symmetrically connected classes are classes that are connected to each other
|
|
285
|
-
in both directions. For example, if class A is connected to class B, and class B
|
|
286
|
-
is connected to class A, then classes A and B are symmetrically connected.
|
|
287
|
-
"""
|
|
288
|
-
|
|
289
|
-
# TODO: Find better name for this method
|
|
290
|
-
sym_pairs: set[tuple[ClassEntity, ClassEntity]] = set()
|
|
291
|
-
|
|
292
|
-
class_linkage = self.class_linkage(consider_inheritance)
|
|
293
|
-
if class_linkage.empty:
|
|
294
|
-
return sym_pairs
|
|
295
|
-
|
|
296
|
-
for _, row in class_linkage.iterrows():
|
|
297
|
-
source = row.source_class
|
|
298
|
-
target = row.target_class
|
|
299
|
-
target_targets = class_linkage[class_linkage.source_class == target].target_class.values
|
|
300
|
-
if source in target_targets and (source, target) not in sym_pairs:
|
|
301
|
-
sym_pairs.add((source, target))
|
|
302
|
-
return sym_pairs
|
|
303
|
-
|
|
304
|
-
def as_property_dict(
|
|
305
|
-
self,
|
|
306
|
-
) -> dict[str, list[T_Property]]:
|
|
307
|
-
"""This is used to capture all definitions of a property in the data model."""
|
|
308
|
-
property_dict: dict[str, list[T_Property]] = defaultdict(list)
|
|
309
|
-
for definition in self.rules.properties:
|
|
310
|
-
property_dict[definition.property_].append(definition) # type: ignore
|
|
311
|
-
return property_dict
|
|
312
|
-
|
|
313
|
-
def as_class_dict(self) -> dict[str, T_Class]:
|
|
314
|
-
"""This is to simplify access to classes through dict."""
|
|
315
|
-
class_dict: dict[str, T_Class] = {}
|
|
316
|
-
for definition in self.rules.classes:
|
|
317
|
-
class_dict[str(definition.class_.suffix)] = definition # type: ignore
|
|
318
|
-
return class_dict
|
|
319
|
-
|
|
320
|
-
def subset_rules(self, desired_classes: set[ClassEntity]) -> T_Rules:
|
|
321
|
-
"""
|
|
322
|
-
Subset rules to only include desired classes and their properties.
|
|
323
|
-
|
|
324
|
-
Args:
|
|
325
|
-
desired_classes: Desired classes to include in the reduced data model
|
|
326
|
-
|
|
327
|
-
Returns:
|
|
328
|
-
Instance of InformationRules
|
|
329
|
-
|
|
330
|
-
!!! note "Inheritance"
|
|
331
|
-
If desired classes contain a class that is a subclass of another class(es), the parent class(es)
|
|
332
|
-
will be included in the reduced data model as well even though the parent class(es) are
|
|
333
|
-
not in the desired classes set. This is to ensure that the reduced data model is
|
|
334
|
-
consistent and complete.
|
|
335
|
-
|
|
336
|
-
!!! note "Partial Reduction"
|
|
337
|
-
This method does not perform checks if classes that are value types of desired classes
|
|
338
|
-
properties are part of desired classes. If a class is not part of desired classes, but it
|
|
339
|
-
is a value type of a property of a class that is part of desired classes, derived reduced
|
|
340
|
-
rules will be marked as partial.
|
|
341
|
-
|
|
342
|
-
!!! note "Validation"
|
|
343
|
-
This method will attempt to validate the reduced rules with custom validations.
|
|
344
|
-
If it fails, it will return a partial rules with a warning message, validated
|
|
345
|
-
only with base Pydantic validators.
|
|
346
|
-
"""
|
|
347
|
-
|
|
348
|
-
if self.rules.metadata.schema_ is not SchemaCompleteness.complete:
|
|
349
|
-
raise ValueError("Rules are not complete cannot perform reduction!")
|
|
350
|
-
class_as_dict = self.as_class_dict()
|
|
351
|
-
class_parents_pairs = self.class_parent_pairs()
|
|
352
|
-
defined_classes = self.defined_classes(consider_inheritance=True)
|
|
353
|
-
|
|
354
|
-
possible_classes = defined_classes.intersection(desired_classes)
|
|
355
|
-
impossible_classes = desired_classes - possible_classes
|
|
356
|
-
|
|
357
|
-
# need to add all the parent classes of the desired classes to the possible classes
|
|
358
|
-
parents: set[ClassEntity] = set()
|
|
359
|
-
for class_ in possible_classes:
|
|
360
|
-
parents = parents.union(
|
|
361
|
-
{parent.as_class_entity() for parent in get_inheritance_path(class_, class_parents_pairs)}
|
|
362
|
-
)
|
|
363
|
-
possible_classes = possible_classes.union(parents)
|
|
364
|
-
|
|
365
|
-
if not possible_classes:
|
|
366
|
-
logging.error("None of the desired classes are defined in the data model!")
|
|
367
|
-
raise ValueError("None of the desired classes are defined in the data model!")
|
|
368
|
-
|
|
369
|
-
if impossible_classes:
|
|
370
|
-
logging.warning(f"Could not find the following classes defined in the data model: {impossible_classes}")
|
|
371
|
-
warnings.warn(
|
|
372
|
-
f"Could not find the following classes defined in the data model: {impossible_classes}",
|
|
373
|
-
stacklevel=2,
|
|
374
|
-
)
|
|
375
|
-
|
|
376
|
-
reduced_data_model: dict[str, Any] = {
|
|
377
|
-
"metadata": self.rules.metadata.model_copy(),
|
|
378
|
-
"prefixes": (self.rules.prefixes or {}).copy(),
|
|
379
|
-
"classes": [],
|
|
380
|
-
"properties": [],
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
logging.info(f"Reducing data model to only include the following classes: {possible_classes}")
|
|
384
|
-
for class_ in possible_classes:
|
|
385
|
-
reduced_data_model["classes"].append(class_as_dict[str(class_.suffix)])
|
|
386
|
-
|
|
387
|
-
class_property_pairs = self.classes_with_properties(consider_inheritance=False)
|
|
388
|
-
|
|
389
|
-
for class_, properties in class_property_pairs.items():
|
|
390
|
-
if class_ in possible_classes:
|
|
391
|
-
reduced_data_model["properties"].extend(properties)
|
|
392
|
-
|
|
393
|
-
try:
|
|
394
|
-
return type(self.rules)(**reduced_data_model)
|
|
395
|
-
except ValidationError as e:
|
|
396
|
-
warnings.warn(f"Reduced data model is not complete: {e}", stacklevel=2)
|
|
397
|
-
reduced_data_model["metadata"].schema_ = SchemaCompleteness.partial
|
|
398
|
-
return type(self.rules).model_construct(**reduced_data_model)
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
class InformationArchitectRulesAnalysis(_SharedAnalysis[InformationRules, InformationProperty, InformationClass]):
|
|
402
|
-
"""Assumes analysis over only the complete schema"""
|
|
403
|
-
|
|
404
|
-
...
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
class AssetArchitectRulesAnalysis(_SharedAnalysis[AssetRules, AssetProperty, AssetClass]):
|
|
408
|
-
"""Assumes analysis over only the complete schema"""
|
|
409
|
-
|
|
410
|
-
def class_property_pairs(
|
|
411
|
-
self,
|
|
412
|
-
only_rdfpath: bool = False,
|
|
413
|
-
consider_inheritance: bool = False,
|
|
414
|
-
implementation_type: EntityTypes = EntityTypes.asset,
|
|
415
|
-
) -> dict[ClassEntity, dict[str, AssetProperty]]:
|
|
416
|
-
class_property_pairs = {}
|
|
417
|
-
|
|
418
|
-
T_implementation = AssetEntity if implementation_type == EntityTypes.asset else RelationshipEntity
|
|
419
|
-
|
|
420
|
-
for class_, properties in self.classes_with_properties(consider_inheritance).items():
|
|
421
|
-
processed_properties = {}
|
|
422
|
-
for property_ in properties:
|
|
423
|
-
if property_.property_ in processed_properties:
|
|
424
|
-
# TODO: use appropriate Warning class from _exceptions.py
|
|
425
|
-
# if missing make one !
|
|
426
|
-
warnings.warn(
|
|
427
|
-
f"Property {property_.property_} for {class_} has been defined more than once!"
|
|
428
|
-
" Only the first definition will be considered, skipping the rest..",
|
|
429
|
-
stacklevel=2,
|
|
430
|
-
)
|
|
431
|
-
continue
|
|
432
|
-
|
|
433
|
-
if (
|
|
434
|
-
property_.implementation
|
|
435
|
-
and any(isinstance(implementation, T_implementation) for implementation in property_.implementation)
|
|
436
|
-
and (not only_rdfpath or (only_rdfpath and isinstance(property_.transformation, RDFPath)))
|
|
437
|
-
):
|
|
438
|
-
implementation = [
|
|
439
|
-
implementation
|
|
440
|
-
for implementation in property_.implementation
|
|
441
|
-
if isinstance(implementation, T_implementation)
|
|
442
|
-
]
|
|
443
|
-
|
|
444
|
-
processed_properties[property_.property_] = property_.model_copy(
|
|
445
|
-
deep=True, update={"implementation": implementation}
|
|
446
|
-
)
|
|
447
|
-
|
|
448
|
-
if processed_properties:
|
|
449
|
-
class_property_pairs[class_] = processed_properties
|
|
450
|
-
|
|
451
|
-
return class_property_pairs
|
|
452
|
-
|
|
453
|
-
def asset_definition(
|
|
454
|
-
self, only_rdfpath: bool = False, consider_inheritance: bool = False
|
|
455
|
-
) -> dict[ClassEntity, dict[str, AssetProperty]]:
|
|
456
|
-
return self.class_property_pairs(
|
|
457
|
-
consider_inheritance=consider_inheritance,
|
|
458
|
-
only_rdfpath=only_rdfpath,
|
|
459
|
-
implementation_type=EntityTypes.asset,
|
|
460
|
-
)
|
|
461
|
-
|
|
462
|
-
def relationship_definition(
|
|
463
|
-
self, only_rdfpath: bool = False, consider_inheritance: bool = False
|
|
464
|
-
) -> dict[ClassEntity, dict[str, AssetProperty]]:
|
|
465
|
-
return self.class_property_pairs(
|
|
466
|
-
consider_inheritance=consider_inheritance,
|
|
467
|
-
only_rdfpath=only_rdfpath,
|
|
468
|
-
implementation_type=EntityTypes.relationship,
|
|
469
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|