cognite-neat 0.98.0__py3-none-any.whl → 0.99.1__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/_client/__init__.py +4 -0
- cognite/neat/_client/_api/data_modeling_loaders.py +585 -0
- cognite/neat/_client/_api/schema.py +111 -0
- cognite/neat/_client/_api_client.py +17 -0
- cognite/neat/_client/data_classes/__init__.py +0 -0
- cognite/neat/{_utils/cdf/data_classes.py → _client/data_classes/data_modeling.py} +8 -135
- cognite/neat/_client/data_classes/schema.py +495 -0
- cognite/neat/_constants.py +27 -4
- cognite/neat/_graph/_shared.py +14 -15
- cognite/neat/_graph/extractors/_classic_cdf/_assets.py +14 -154
- cognite/neat/_graph/extractors/_classic_cdf/_base.py +154 -7
- cognite/neat/_graph/extractors/_classic_cdf/_classic.py +25 -14
- cognite/neat/_graph/extractors/_classic_cdf/_data_sets.py +17 -92
- cognite/neat/_graph/extractors/_classic_cdf/_events.py +13 -162
- cognite/neat/_graph/extractors/_classic_cdf/_files.py +15 -179
- cognite/neat/_graph/extractors/_classic_cdf/_labels.py +32 -100
- cognite/neat/_graph/extractors/_classic_cdf/_relationships.py +27 -178
- cognite/neat/_graph/extractors/_classic_cdf/_sequences.py +14 -139
- cognite/neat/_graph/extractors/_classic_cdf/_timeseries.py +15 -173
- cognite/neat/_graph/extractors/_rdf_file.py +6 -7
- cognite/neat/_graph/loaders/_rdf2dms.py +2 -2
- cognite/neat/_graph/queries/_base.py +17 -1
- cognite/neat/_graph/transformers/_classic_cdf.py +74 -147
- cognite/neat/_graph/transformers/_prune_graph.py +1 -1
- cognite/neat/_graph/transformers/_rdfpath.py +1 -1
- cognite/neat/_issues/_base.py +26 -17
- cognite/neat/_issues/errors/__init__.py +4 -2
- cognite/neat/_issues/errors/_external.py +7 -0
- cognite/neat/_issues/errors/_properties.py +2 -7
- cognite/neat/_issues/errors/_resources.py +1 -1
- cognite/neat/_issues/warnings/__init__.py +8 -0
- cognite/neat/_issues/warnings/_external.py +16 -0
- cognite/neat/_issues/warnings/_properties.py +16 -0
- cognite/neat/_issues/warnings/_resources.py +26 -2
- cognite/neat/_issues/warnings/user_modeling.py +4 -4
- cognite/neat/_rules/_constants.py +8 -11
- cognite/neat/_rules/analysis/_base.py +8 -4
- cognite/neat/_rules/exporters/_base.py +3 -4
- cognite/neat/_rules/exporters/_rules2dms.py +33 -46
- cognite/neat/_rules/importers/__init__.py +1 -3
- cognite/neat/_rules/importers/_base.py +1 -1
- cognite/neat/_rules/importers/_dms2rules.py +6 -29
- cognite/neat/_rules/importers/_rdf/__init__.py +5 -0
- cognite/neat/_rules/importers/_rdf/_base.py +34 -11
- cognite/neat/_rules/importers/_rdf/_imf2rules.py +91 -0
- cognite/neat/_rules/importers/_rdf/_inference2rules.py +43 -35
- cognite/neat/_rules/importers/_rdf/_owl2rules.py +80 -0
- cognite/neat/_rules/importers/_rdf/_shared.py +138 -441
- cognite/neat/_rules/models/__init__.py +1 -1
- cognite/neat/_rules/models/_base_rules.py +22 -12
- cognite/neat/_rules/models/dms/__init__.py +4 -2
- cognite/neat/_rules/models/dms/_exporter.py +45 -48
- cognite/neat/_rules/models/dms/_rules.py +20 -17
- cognite/neat/_rules/models/dms/_rules_input.py +52 -8
- cognite/neat/_rules/models/dms/_validation.py +391 -119
- cognite/neat/_rules/models/entities/_single_value.py +32 -4
- cognite/neat/_rules/models/information/__init__.py +2 -0
- cognite/neat/_rules/models/information/_rules.py +0 -67
- cognite/neat/_rules/models/information/_validation.py +9 -9
- cognite/neat/_rules/models/mapping/__init__.py +2 -3
- cognite/neat/_rules/models/mapping/_classic2core.py +36 -146
- cognite/neat/_rules/models/mapping/_classic2core.yaml +343 -0
- cognite/neat/_rules/transformers/__init__.py +2 -2
- cognite/neat/_rules/transformers/_converters.py +110 -11
- cognite/neat/_rules/transformers/_mapping.py +105 -30
- cognite/neat/_rules/transformers/_pipelines.py +1 -1
- cognite/neat/_rules/transformers/_verification.py +31 -3
- cognite/neat/_session/_base.py +24 -8
- cognite/neat/_session/_drop.py +35 -0
- cognite/neat/_session/_inspect.py +17 -5
- cognite/neat/_session/_mapping.py +39 -0
- cognite/neat/_session/_prepare.py +219 -23
- cognite/neat/_session/_read.py +49 -12
- cognite/neat/_session/_to.py +8 -5
- cognite/neat/_session/exceptions.py +4 -0
- cognite/neat/_store/_base.py +27 -24
- cognite/neat/_utils/rdf_.py +34 -5
- cognite/neat/_version.py +1 -1
- cognite/neat/_workflows/steps/lib/current/rules_exporter.py +5 -88
- cognite/neat/_workflows/steps/lib/current/rules_importer.py +3 -14
- cognite/neat/_workflows/steps/lib/current/rules_validator.py +6 -7
- {cognite_neat-0.98.0.dist-info → cognite_neat-0.99.1.dist-info}/METADATA +3 -3
- {cognite_neat-0.98.0.dist-info → cognite_neat-0.99.1.dist-info}/RECORD +87 -92
- cognite/neat/_rules/importers/_rdf/_imf2rules/__init__.py +0 -3
- cognite/neat/_rules/importers/_rdf/_imf2rules/_imf2classes.py +0 -86
- cognite/neat/_rules/importers/_rdf/_imf2rules/_imf2metadata.py +0 -29
- cognite/neat/_rules/importers/_rdf/_imf2rules/_imf2properties.py +0 -130
- cognite/neat/_rules/importers/_rdf/_imf2rules/_imf2rules.py +0 -154
- cognite/neat/_rules/importers/_rdf/_owl2rules/__init__.py +0 -3
- cognite/neat/_rules/importers/_rdf/_owl2rules/_owl2classes.py +0 -58
- cognite/neat/_rules/importers/_rdf/_owl2rules/_owl2metadata.py +0 -65
- cognite/neat/_rules/importers/_rdf/_owl2rules/_owl2properties.py +0 -59
- cognite/neat/_rules/importers/_rdf/_owl2rules/_owl2rules.py +0 -39
- cognite/neat/_rules/models/dms/_schema.py +0 -1101
- cognite/neat/_rules/models/mapping/_base.py +0 -131
- cognite/neat/_utils/cdf/loaders/__init__.py +0 -25
- cognite/neat/_utils/cdf/loaders/_base.py +0 -54
- cognite/neat/_utils/cdf/loaders/_data_modeling.py +0 -339
- cognite/neat/_utils/cdf/loaders/_ingestion.py +0 -167
- /cognite/neat/{_utils/cdf → _client/_api}/__init__.py +0 -0
- {cognite_neat-0.98.0.dist-info → cognite_neat-0.99.1.dist-info}/LICENSE +0 -0
- {cognite_neat-0.98.0.dist-info → cognite_neat-0.99.1.dist-info}/WHEEL +0 -0
- {cognite_neat-0.98.0.dist-info → cognite_neat-0.99.1.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import warnings
|
|
3
|
+
import zipfile
|
|
4
|
+
from collections import ChainMap
|
|
5
|
+
from collections.abc import Iterable, MutableMapping
|
|
6
|
+
from dataclasses import Field, dataclass, field, fields
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, ClassVar, Literal, cast
|
|
9
|
+
|
|
10
|
+
import yaml
|
|
11
|
+
from cognite.client import data_modeling as dm
|
|
12
|
+
from cognite.client.data_classes import DatabaseWrite, TransformationWrite
|
|
13
|
+
from cognite.client.data_classes.data_modeling.views import (
|
|
14
|
+
ReverseDirectRelationApply,
|
|
15
|
+
SingleEdgeConnection,
|
|
16
|
+
SingleEdgeConnectionApply,
|
|
17
|
+
SingleReverseDirectRelation,
|
|
18
|
+
SingleReverseDirectRelationApply,
|
|
19
|
+
ViewProperty,
|
|
20
|
+
ViewPropertyApply,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
from cognite.neat._client.data_classes.data_modeling import (
|
|
24
|
+
CogniteResourceDict,
|
|
25
|
+
ContainerApplyDict,
|
|
26
|
+
NodeApplyDict,
|
|
27
|
+
SpaceApplyDict,
|
|
28
|
+
ViewApplyDict,
|
|
29
|
+
)
|
|
30
|
+
from cognite.neat._issues.errors import (
|
|
31
|
+
NeatYamlError,
|
|
32
|
+
)
|
|
33
|
+
from cognite.neat._issues.warnings import (
|
|
34
|
+
FileTypeUnexpectedWarning,
|
|
35
|
+
ResourcesDuplicatedWarning,
|
|
36
|
+
)
|
|
37
|
+
from cognite.neat._utils.text import to_camel
|
|
38
|
+
|
|
39
|
+
if sys.version_info >= (3, 11):
|
|
40
|
+
from typing import Self
|
|
41
|
+
else:
|
|
42
|
+
from typing_extensions import Self
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class DMSSchema:
|
|
47
|
+
data_model: dm.DataModelApply | None = None
|
|
48
|
+
spaces: SpaceApplyDict = field(default_factory=SpaceApplyDict)
|
|
49
|
+
views: ViewApplyDict = field(default_factory=ViewApplyDict)
|
|
50
|
+
containers: ContainerApplyDict = field(default_factory=ContainerApplyDict)
|
|
51
|
+
node_types: NodeApplyDict = field(default_factory=NodeApplyDict)
|
|
52
|
+
|
|
53
|
+
_FIELD_NAME_BY_RESOURCE_TYPE: ClassVar[dict[str, str]] = {
|
|
54
|
+
"container": "containers",
|
|
55
|
+
"view": "views",
|
|
56
|
+
"datamodel": "data_model",
|
|
57
|
+
"space": "spaces",
|
|
58
|
+
"node": "node_types",
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def from_directory(cls, directory: str | Path) -> Self:
|
|
63
|
+
"""Load a schema from a directory containing YAML files.
|
|
64
|
+
|
|
65
|
+
The directory is expected to follow the Cognite-Toolkit convention
|
|
66
|
+
where each file is named as `resource_type.resource_name.yaml`.
|
|
67
|
+
"""
|
|
68
|
+
data, context = cls._read_directory(Path(directory))
|
|
69
|
+
return cls.load(data, context)
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def _read_directory(cls, directory: Path) -> tuple[dict[str, list[Any]], dict[str, list[Path]]]:
|
|
73
|
+
data: dict[str, Any] = {}
|
|
74
|
+
context: dict[str, list[Path]] = {}
|
|
75
|
+
for yaml_file in directory.rglob("*.yaml"):
|
|
76
|
+
if "." in yaml_file.stem:
|
|
77
|
+
resource_type = yaml_file.stem.rsplit(".", 1)[-1]
|
|
78
|
+
if attr_name := cls._FIELD_NAME_BY_RESOURCE_TYPE.get(resource_type):
|
|
79
|
+
data.setdefault(attr_name, [])
|
|
80
|
+
context.setdefault(attr_name, [])
|
|
81
|
+
try:
|
|
82
|
+
loaded = yaml.safe_load(yaml_file.read_text())
|
|
83
|
+
except Exception as e:
|
|
84
|
+
warnings.warn(
|
|
85
|
+
FileTypeUnexpectedWarning(yaml_file, frozenset([".yaml", ".yml"]), str(e)), stacklevel=2
|
|
86
|
+
)
|
|
87
|
+
continue
|
|
88
|
+
|
|
89
|
+
if isinstance(loaded, list):
|
|
90
|
+
data[attr_name].extend(loaded)
|
|
91
|
+
context[attr_name].extend([yaml_file] * len(loaded))
|
|
92
|
+
else:
|
|
93
|
+
data[attr_name].append(loaded)
|
|
94
|
+
context[attr_name].append(yaml_file)
|
|
95
|
+
return data, context
|
|
96
|
+
|
|
97
|
+
def to_directory(
|
|
98
|
+
self,
|
|
99
|
+
directory: str | Path,
|
|
100
|
+
exclude: set[str] | None = None,
|
|
101
|
+
new_line: str | None = "\n",
|
|
102
|
+
encoding: str | None = "utf-8",
|
|
103
|
+
) -> None:
|
|
104
|
+
"""Save the schema to a directory as YAML files. This is compatible with the Cognite-Toolkit convention.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
directory (str | Path): The directory to save the schema to.
|
|
108
|
+
exclude (set[str]): A set of attributes to exclude from the output.
|
|
109
|
+
new_line (str): The line endings to use in the output files. Defaults to "\n".
|
|
110
|
+
encoding (str): The encoding to use in the output files. Defaults to "utf-8".
|
|
111
|
+
"""
|
|
112
|
+
path_dir = Path(directory)
|
|
113
|
+
exclude_set = exclude or set()
|
|
114
|
+
data_models = path_dir / "data_models"
|
|
115
|
+
data_models.mkdir(parents=True, exist_ok=True)
|
|
116
|
+
if "spaces" not in exclude_set:
|
|
117
|
+
for space in self.spaces.values():
|
|
118
|
+
(data_models / f"{space.space}.space.yaml").write_text(
|
|
119
|
+
space.dump_yaml(), newline=new_line, encoding=encoding
|
|
120
|
+
)
|
|
121
|
+
if "data_models" not in exclude_set and self.data_model:
|
|
122
|
+
(data_models / f"{self.data_model.external_id}.datamodel.yaml").write_text(
|
|
123
|
+
self.data_model.dump_yaml(), newline=new_line, encoding=encoding
|
|
124
|
+
)
|
|
125
|
+
if "views" not in exclude_set and self.views:
|
|
126
|
+
view_dir = data_models / "views"
|
|
127
|
+
view_dir.mkdir(parents=True, exist_ok=True)
|
|
128
|
+
for view in self.views.values():
|
|
129
|
+
(view_dir / f"{view.external_id}.view.yaml").write_text(
|
|
130
|
+
view.dump_yaml(), newline=new_line, encoding=encoding
|
|
131
|
+
)
|
|
132
|
+
if "containers" not in exclude_set and self.containers:
|
|
133
|
+
container_dir = data_models / "containers"
|
|
134
|
+
container_dir.mkdir(parents=True, exist_ok=True)
|
|
135
|
+
for container in self.containers.values():
|
|
136
|
+
(container_dir / f"{container.external_id}.container.yaml").write_text(
|
|
137
|
+
container.dump_yaml(), newline=new_line, encoding=encoding
|
|
138
|
+
)
|
|
139
|
+
if "node_types" not in exclude_set and self.node_types:
|
|
140
|
+
node_dir = data_models / "nodes"
|
|
141
|
+
node_dir.mkdir(parents=True, exist_ok=True)
|
|
142
|
+
for node in self.node_types.values():
|
|
143
|
+
(node_dir / f"{node.external_id}.node.yaml").write_text(
|
|
144
|
+
node.dump_yaml(), newline=new_line, encoding=encoding
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
@classmethod
|
|
148
|
+
def from_zip(cls, zip_file: str | Path) -> Self:
|
|
149
|
+
"""Load a schema from a ZIP file containing YAML files.
|
|
150
|
+
|
|
151
|
+
The ZIP file is expected to follow the Cognite-Toolkit convention
|
|
152
|
+
where each file is named as `resource_type.resource_name.yaml`.
|
|
153
|
+
"""
|
|
154
|
+
data, context = cls._read_zip(Path(zip_file))
|
|
155
|
+
return cls.load(data, context)
|
|
156
|
+
|
|
157
|
+
@classmethod
|
|
158
|
+
def _read_zip(cls, zip_file: Path) -> tuple[dict[str, list[Any]], dict[str, list[Path]]]:
|
|
159
|
+
data: dict[str, list[Any]] = {}
|
|
160
|
+
context: dict[str, list[Path]] = {}
|
|
161
|
+
with zipfile.ZipFile(zip_file, "r") as zip_ref:
|
|
162
|
+
for file_info in zip_ref.infolist():
|
|
163
|
+
if file_info.filename.endswith(".yaml"):
|
|
164
|
+
if "/" not in file_info.filename:
|
|
165
|
+
continue
|
|
166
|
+
filename = Path(file_info.filename.split("/")[-1])
|
|
167
|
+
if "." not in filename.stem:
|
|
168
|
+
continue
|
|
169
|
+
resource_type = filename.stem.rsplit(".", 1)[-1]
|
|
170
|
+
if attr_name := cls._FIELD_NAME_BY_RESOURCE_TYPE.get(resource_type):
|
|
171
|
+
data.setdefault(attr_name, [])
|
|
172
|
+
context.setdefault(attr_name, [])
|
|
173
|
+
try:
|
|
174
|
+
loaded = yaml.safe_load(zip_ref.read(file_info).decode())
|
|
175
|
+
except Exception as e:
|
|
176
|
+
warnings.warn(
|
|
177
|
+
FileTypeUnexpectedWarning(filename, frozenset([".yaml", ".yml"]), str(e)), stacklevel=2
|
|
178
|
+
)
|
|
179
|
+
continue
|
|
180
|
+
if isinstance(loaded, list):
|
|
181
|
+
data[attr_name].extend(loaded)
|
|
182
|
+
context[attr_name].extend([filename] * len(loaded))
|
|
183
|
+
else:
|
|
184
|
+
data[attr_name].append(loaded)
|
|
185
|
+
context[attr_name].append(filename)
|
|
186
|
+
return data, context
|
|
187
|
+
|
|
188
|
+
def to_zip(self, zip_file: str | Path, exclude: set[str] | None = None) -> None:
|
|
189
|
+
"""Save the schema to a ZIP file as YAML files. This is compatible with the Cognite-Toolkit convention.
|
|
190
|
+
|
|
191
|
+
Args:
|
|
192
|
+
zip_file (str | Path): The ZIP file to save the schema to.
|
|
193
|
+
exclude (set[str]): A set of attributes to exclude from the output.
|
|
194
|
+
"""
|
|
195
|
+
exclude_set = exclude or set()
|
|
196
|
+
with zipfile.ZipFile(zip_file, "w") as zip_ref:
|
|
197
|
+
if "spaces" not in exclude_set:
|
|
198
|
+
for space in self.spaces.values():
|
|
199
|
+
zip_ref.writestr(f"data_models/{space.space}.space.yaml", space.dump_yaml())
|
|
200
|
+
if "data_models" not in exclude_set and self.data_model:
|
|
201
|
+
zip_ref.writestr(
|
|
202
|
+
f"data_models/{self.data_model.external_id}.datamodel.yaml", self.data_model.dump_yaml()
|
|
203
|
+
)
|
|
204
|
+
if "views" not in exclude_set:
|
|
205
|
+
for view in self.views.values():
|
|
206
|
+
zip_ref.writestr(f"data_models/views/{view.external_id}.view.yaml", view.dump_yaml())
|
|
207
|
+
if "containers" not in exclude_set:
|
|
208
|
+
for container in self.containers.values():
|
|
209
|
+
zip_ref.writestr(
|
|
210
|
+
f"data_models/containers{container.external_id}.container.yaml", container.dump_yaml()
|
|
211
|
+
)
|
|
212
|
+
if "node_types" not in exclude_set:
|
|
213
|
+
for node in self.node_types.values():
|
|
214
|
+
zip_ref.writestr(f"data_models/nodes/{node.external_id}.node.yaml", node.dump_yaml())
|
|
215
|
+
|
|
216
|
+
@classmethod
|
|
217
|
+
def load(cls, data: str | dict[str, list[Any]], context: dict[str, list[Path]] | None = None) -> Self:
|
|
218
|
+
"""Loads a schema from a dictionary or a YAML or JSON formatted string.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
data: The data to load the schema from. This can be a dictionary, a YAML or JSON formatted string.
|
|
222
|
+
context: This provides linage for where the data was loaded from. This is used in Warnings
|
|
223
|
+
if a single item fails to load.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
DMSSchema: The loaded schema.
|
|
227
|
+
"""
|
|
228
|
+
context = context or {}
|
|
229
|
+
if isinstance(data, str):
|
|
230
|
+
# YAML is a superset of JSON, so we can use the same parser
|
|
231
|
+
try:
|
|
232
|
+
data_dict = yaml.safe_load(data)
|
|
233
|
+
except Exception as e:
|
|
234
|
+
raise NeatYamlError(str(e)) from None
|
|
235
|
+
if not isinstance(data_dict, dict) and all(isinstance(v, list) for v in data_dict.values()):
|
|
236
|
+
raise NeatYamlError(f"Invalid data structure: {type(data)}", "dict[str, list[Any]]") from None
|
|
237
|
+
else:
|
|
238
|
+
data_dict = data
|
|
239
|
+
loaded: dict[str, Any] = {}
|
|
240
|
+
for attr in fields(cls):
|
|
241
|
+
if items := data_dict.get(attr.name) or data_dict.get(to_camel(attr.name)):
|
|
242
|
+
if attr.name == "data_model":
|
|
243
|
+
if isinstance(items, list) and len(items) > 1:
|
|
244
|
+
try:
|
|
245
|
+
data_model_ids = [dm.DataModelId.load(item) for item in items]
|
|
246
|
+
except Exception as e:
|
|
247
|
+
data_model_file = context.get(attr.name, [Path("UNKNOWN")])[0]
|
|
248
|
+
warnings.warn(
|
|
249
|
+
FileTypeUnexpectedWarning(
|
|
250
|
+
data_model_file, frozenset([dm.DataModelApply.__name__]), str(e)
|
|
251
|
+
),
|
|
252
|
+
stacklevel=2,
|
|
253
|
+
)
|
|
254
|
+
else:
|
|
255
|
+
warnings.warn(
|
|
256
|
+
ResourcesDuplicatedWarning(
|
|
257
|
+
frozenset(data_model_ids),
|
|
258
|
+
"data model",
|
|
259
|
+
"Will use the first DataModel.",
|
|
260
|
+
),
|
|
261
|
+
stacklevel=2,
|
|
262
|
+
)
|
|
263
|
+
item = items[0] if isinstance(items, list) else items
|
|
264
|
+
try:
|
|
265
|
+
loaded[attr.name] = dm.DataModelApply.load(item)
|
|
266
|
+
except Exception as e:
|
|
267
|
+
data_model_file = context.get(attr.name, [Path("UNKNOWN")])[0]
|
|
268
|
+
warnings.warn(
|
|
269
|
+
FileTypeUnexpectedWarning(data_model_file, frozenset([dm.DataModelApply.__name__]), str(e)),
|
|
270
|
+
stacklevel=2,
|
|
271
|
+
)
|
|
272
|
+
else:
|
|
273
|
+
try:
|
|
274
|
+
loaded[attr.name] = attr.type.load(items) # type: ignore[union-attr]
|
|
275
|
+
except Exception as e:
|
|
276
|
+
loaded[attr.name] = cls._load_individual_resources(
|
|
277
|
+
items, attr, str(e), context.get(attr.name, [])
|
|
278
|
+
)
|
|
279
|
+
return cls(**loaded)
|
|
280
|
+
|
|
281
|
+
@classmethod
|
|
282
|
+
def _load_individual_resources(cls, items: list, attr: Field, trigger_error: str, resource_context) -> list[Any]:
|
|
283
|
+
type_ = cast(type, attr.type)
|
|
284
|
+
resources = type_([])
|
|
285
|
+
if not hasattr(type_, "_RESOURCE"):
|
|
286
|
+
warnings.warn(
|
|
287
|
+
FileTypeUnexpectedWarning(Path("UNKNOWN"), frozenset([type_.__name__]), trigger_error), stacklevel=2
|
|
288
|
+
)
|
|
289
|
+
return resources
|
|
290
|
+
# Fallback to load individual resources.
|
|
291
|
+
single_cls = type_._RESOURCE
|
|
292
|
+
for no, item in enumerate(items):
|
|
293
|
+
try:
|
|
294
|
+
loaded_instance = single_cls.load(item)
|
|
295
|
+
except Exception as e:
|
|
296
|
+
try:
|
|
297
|
+
filepath = resource_context[no]
|
|
298
|
+
except IndexError:
|
|
299
|
+
filepath = Path("UNKNOWN")
|
|
300
|
+
# We use repr(e) instead of str(e) to include the exception type in the warning message
|
|
301
|
+
warnings.warn(
|
|
302
|
+
FileTypeUnexpectedWarning(filepath, frozenset([single_cls.__name__]), repr(e)), stacklevel=2
|
|
303
|
+
)
|
|
304
|
+
else:
|
|
305
|
+
resources.append(loaded_instance)
|
|
306
|
+
return resources
|
|
307
|
+
|
|
308
|
+
def dump(self, camel_case: bool = True, sort: bool = True) -> dict[str, Any]:
|
|
309
|
+
"""Dump the schema to a dictionary that can be serialized to JSON.
|
|
310
|
+
|
|
311
|
+
Args:
|
|
312
|
+
camel_case (bool): If True, the keys in the output dictionary will be in camel case.
|
|
313
|
+
sort (bool): If True, the items in the output dictionary will be sorted by their ID.
|
|
314
|
+
This is useful for deterministic output which is useful for comparing schemas.
|
|
315
|
+
|
|
316
|
+
Returns:
|
|
317
|
+
dict: The schema as a dictionary.
|
|
318
|
+
"""
|
|
319
|
+
output: dict[str, Any] = {}
|
|
320
|
+
cls_fields = sorted(fields(self), key=lambda f: f.name) if sort else fields(self)
|
|
321
|
+
for attr in cls_fields:
|
|
322
|
+
if items := getattr(self, attr.name):
|
|
323
|
+
key = to_camel(attr.name) if camel_case else attr.name
|
|
324
|
+
if isinstance(items, CogniteResourceDict):
|
|
325
|
+
if sort:
|
|
326
|
+
output[key] = [
|
|
327
|
+
item.dump(camel_case) for item in sorted(items.values(), key=self._to_sortable_identifier)
|
|
328
|
+
]
|
|
329
|
+
else:
|
|
330
|
+
output[key] = items.dump(camel_case)
|
|
331
|
+
else:
|
|
332
|
+
output[key] = items.dump(camel_case=camel_case)
|
|
333
|
+
return output
|
|
334
|
+
|
|
335
|
+
@classmethod
|
|
336
|
+
def _to_sortable_identifier(cls, item: Any) -> str | tuple[str, str] | tuple[str, str, str]:
|
|
337
|
+
if isinstance(item, dm.ContainerApply | dm.ViewApply | dm.DataModelApply | dm.NodeApply):
|
|
338
|
+
identifier = item.as_id().as_tuple()
|
|
339
|
+
if len(identifier) == 3 and identifier[2] is None:
|
|
340
|
+
return identifier[:2] # type: ignore[misc]
|
|
341
|
+
return cast(tuple[str, str] | tuple[str, str, str], identifier)
|
|
342
|
+
elif isinstance(item, dm.SpaceApply):
|
|
343
|
+
return item.space
|
|
344
|
+
elif isinstance(item, TransformationWrite):
|
|
345
|
+
return item.external_id or ""
|
|
346
|
+
elif isinstance(item, DatabaseWrite):
|
|
347
|
+
return item.name or ""
|
|
348
|
+
else:
|
|
349
|
+
raise ValueError(f"Cannot sort item of type {type(item)}")
|
|
350
|
+
|
|
351
|
+
def referenced_spaces(self, include_indirect_references: bool = True) -> set[str]:
|
|
352
|
+
"""Get the spaces referenced by the schema.
|
|
353
|
+
|
|
354
|
+
Args:
|
|
355
|
+
include_indirect_references (bool): If True, the spaces referenced by as view.implements, and
|
|
356
|
+
view.referenced_containers will be included in the output.
|
|
357
|
+
Returns:
|
|
358
|
+
set[str]: The spaces referenced by the schema.
|
|
359
|
+
"""
|
|
360
|
+
referenced_spaces = {view.space for view in self.views.values()}
|
|
361
|
+
referenced_spaces |= {container.space for container in self.containers.values()}
|
|
362
|
+
if include_indirect_references:
|
|
363
|
+
referenced_spaces |= {
|
|
364
|
+
container.space for view in self.views.values() for container in view.referenced_containers()
|
|
365
|
+
}
|
|
366
|
+
referenced_spaces |= {parent.space for view in self.views.values() for parent in view.implements or []}
|
|
367
|
+
referenced_spaces |= {node.space for node in self.node_types.values()}
|
|
368
|
+
if self.data_model:
|
|
369
|
+
referenced_spaces |= {self.data_model.space}
|
|
370
|
+
referenced_spaces |= {view.space for view in self.data_model.views or []}
|
|
371
|
+
referenced_spaces |= {s.space for s in self.spaces.values()}
|
|
372
|
+
return referenced_spaces
|
|
373
|
+
|
|
374
|
+
def referenced_container(self) -> set[dm.ContainerId]:
|
|
375
|
+
referenced_containers = {
|
|
376
|
+
container for view in self.views.values() for container in view.referenced_containers()
|
|
377
|
+
}
|
|
378
|
+
referenced_containers |= set(self.containers.keys())
|
|
379
|
+
return referenced_containers
|
|
380
|
+
|
|
381
|
+
def as_read_model(self) -> dm.DataModel[dm.View]:
|
|
382
|
+
if self.data_model is None:
|
|
383
|
+
raise ValueError("Data model is not defined")
|
|
384
|
+
all_containers = self.containers.copy()
|
|
385
|
+
all_views = self.views.copy()
|
|
386
|
+
views: list[dm.View] = []
|
|
387
|
+
for view in self.views.values():
|
|
388
|
+
referenced_containers = ContainerApplyDict()
|
|
389
|
+
properties: dict[str, ViewProperty] = {}
|
|
390
|
+
# ChainMap is used to merge properties from the view and its parents
|
|
391
|
+
# Note that the order of the ChainMap is important, as the first dictionary has the highest priority
|
|
392
|
+
# So if a child and parent have the same property, the child property will be used.
|
|
393
|
+
write_properties = ChainMap(view.properties, *(all_views[v].properties for v in view.implements or [])) # type: ignore[arg-type]
|
|
394
|
+
for prop_name, prop in write_properties.items():
|
|
395
|
+
read_prop = self._as_read_properties(prop, all_containers)
|
|
396
|
+
if isinstance(read_prop, dm.MappedProperty) and read_prop.container not in referenced_containers:
|
|
397
|
+
referenced_containers[read_prop.container] = all_containers[read_prop.container]
|
|
398
|
+
properties[prop_name] = read_prop
|
|
399
|
+
|
|
400
|
+
read_view = dm.View(
|
|
401
|
+
space=view.space,
|
|
402
|
+
external_id=view.external_id,
|
|
403
|
+
version=view.version,
|
|
404
|
+
description=view.description,
|
|
405
|
+
name=view.name,
|
|
406
|
+
filter=view.filter,
|
|
407
|
+
implements=view.implements.copy(),
|
|
408
|
+
used_for=self._used_for(referenced_containers.values()),
|
|
409
|
+
writable=self._writable(properties.values(), referenced_containers.values()),
|
|
410
|
+
properties=properties,
|
|
411
|
+
is_global=False,
|
|
412
|
+
last_updated_time=0,
|
|
413
|
+
created_time=0,
|
|
414
|
+
)
|
|
415
|
+
views.append(read_view)
|
|
416
|
+
|
|
417
|
+
return dm.DataModel(
|
|
418
|
+
space=self.data_model.space,
|
|
419
|
+
external_id=self.data_model.external_id,
|
|
420
|
+
version=self.data_model.version,
|
|
421
|
+
name=self.data_model.name,
|
|
422
|
+
description=self.data_model.description,
|
|
423
|
+
views=views,
|
|
424
|
+
is_global=False,
|
|
425
|
+
last_updated_time=0,
|
|
426
|
+
created_time=0,
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
@staticmethod
|
|
430
|
+
def _as_read_properties(
|
|
431
|
+
write: ViewPropertyApply, all_containers: MutableMapping[dm.ContainerId, dm.ContainerApply]
|
|
432
|
+
) -> ViewProperty:
|
|
433
|
+
if isinstance(write, dm.MappedPropertyApply):
|
|
434
|
+
container_prop = all_containers[write.container].properties[write.container_property_identifier]
|
|
435
|
+
return dm.MappedProperty(
|
|
436
|
+
container=write.container,
|
|
437
|
+
container_property_identifier=write.container_property_identifier,
|
|
438
|
+
name=write.name,
|
|
439
|
+
description=write.description,
|
|
440
|
+
source=write.source,
|
|
441
|
+
type=container_prop.type,
|
|
442
|
+
nullable=container_prop.nullable,
|
|
443
|
+
auto_increment=container_prop.auto_increment,
|
|
444
|
+
immutable=container_prop.immutable,
|
|
445
|
+
# Likely bug in SDK.
|
|
446
|
+
default_value=container_prop.default_value, # type: ignore[arg-type]
|
|
447
|
+
)
|
|
448
|
+
if isinstance(write, dm.EdgeConnectionApply):
|
|
449
|
+
edge_cls = SingleEdgeConnection if isinstance(write, SingleEdgeConnectionApply) else dm.MultiEdgeConnection
|
|
450
|
+
return edge_cls(
|
|
451
|
+
type=write.type,
|
|
452
|
+
source=write.source,
|
|
453
|
+
name=write.name,
|
|
454
|
+
description=write.description,
|
|
455
|
+
edge_source=write.edge_source,
|
|
456
|
+
direction=write.direction,
|
|
457
|
+
)
|
|
458
|
+
if isinstance(write, ReverseDirectRelationApply):
|
|
459
|
+
relation_cls = (
|
|
460
|
+
SingleReverseDirectRelation
|
|
461
|
+
if isinstance(write, SingleReverseDirectRelationApply)
|
|
462
|
+
else dm.MultiReverseDirectRelation
|
|
463
|
+
)
|
|
464
|
+
return relation_cls(
|
|
465
|
+
source=write.source,
|
|
466
|
+
through=write.through,
|
|
467
|
+
name=write.name,
|
|
468
|
+
description=write.description,
|
|
469
|
+
)
|
|
470
|
+
raise ValueError(f"Cannot convert {write} to read format")
|
|
471
|
+
|
|
472
|
+
@staticmethod
|
|
473
|
+
def _used_for(containers: Iterable[dm.ContainerApply]) -> Literal["node", "edge", "all"]:
|
|
474
|
+
used_for = {container.used_for for container in containers}
|
|
475
|
+
if used_for == {"node"}:
|
|
476
|
+
return "node"
|
|
477
|
+
if used_for == {"edge"}:
|
|
478
|
+
return "edge"
|
|
479
|
+
return "all"
|
|
480
|
+
|
|
481
|
+
@staticmethod
|
|
482
|
+
def _writable(properties: Iterable[ViewProperty], containers: Iterable[dm.ContainerApply]) -> bool:
|
|
483
|
+
used_properties = {
|
|
484
|
+
(prop.container, prop.container_property_identifier)
|
|
485
|
+
for prop in properties
|
|
486
|
+
if isinstance(prop, dm.MappedProperty)
|
|
487
|
+
}
|
|
488
|
+
required_properties = {
|
|
489
|
+
(container.as_id(), prop_id)
|
|
490
|
+
for container in containers
|
|
491
|
+
for prop_id, prop in container.properties.items()
|
|
492
|
+
if not prop.nullable
|
|
493
|
+
}
|
|
494
|
+
# If a container has a required property that is not used by the view, the view is not writable
|
|
495
|
+
return not bool(required_properties - used_properties)
|
cognite/neat/_constants.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
|
|
3
3
|
from cognite.client.data_classes.data_modeling.ids import DataModelId
|
|
4
|
-
from rdflib import DCTERMS, OWL, RDF, RDFS, SKOS, XSD, Namespace, URIRef
|
|
4
|
+
from rdflib import DC, DCTERMS, FOAF, OWL, RDF, RDFS, SH, SKOS, XSD, Namespace, URIRef
|
|
5
5
|
|
|
6
6
|
from cognite import neat
|
|
7
7
|
|
|
@@ -36,7 +36,26 @@ COGNITE_MODELS = (
|
|
|
36
36
|
DataModelId("cdf_cdm", "CogniteCore", "v1"),
|
|
37
37
|
DataModelId("cdf_idm", "CogniteProcessIndustries", "v1"),
|
|
38
38
|
)
|
|
39
|
-
COGNITE_SPACES = frozenset(
|
|
39
|
+
COGNITE_SPACES = frozenset(
|
|
40
|
+
{model.space for model in COGNITE_MODELS}
|
|
41
|
+
| {
|
|
42
|
+
"cdf_360_image_schema",
|
|
43
|
+
"cdf_3d_schema",
|
|
44
|
+
"cdf_apm",
|
|
45
|
+
"cdf_apps_shared",
|
|
46
|
+
"cdf_cdm",
|
|
47
|
+
"cdf_cdm_3d",
|
|
48
|
+
"cdf_cdm_units",
|
|
49
|
+
"cdf_classic",
|
|
50
|
+
"cdf_core",
|
|
51
|
+
"cdf_extraction_extensions",
|
|
52
|
+
"cdf_idm",
|
|
53
|
+
"cdf_industrial_canvas",
|
|
54
|
+
"cdf_infield",
|
|
55
|
+
"cdf_time_series_data",
|
|
56
|
+
"cdf_units",
|
|
57
|
+
}
|
|
58
|
+
)
|
|
40
59
|
DMS_LISTABLE_PROPERTY_LIMIT = 1000
|
|
41
60
|
|
|
42
61
|
EXAMPLE_RULES = PACKAGE_DIRECTORY / "_rules" / "examples"
|
|
@@ -54,13 +73,17 @@ XML_SCHEMA_NAMESPACE = Namespace("http://www.w3.org/2001/XMLSchema#")
|
|
|
54
73
|
|
|
55
74
|
def get_default_prefixes() -> dict[str, Namespace]:
|
|
56
75
|
return {
|
|
76
|
+
"owl": OWL._NS,
|
|
57
77
|
"rdf": RDF._NS,
|
|
58
78
|
"rdfs": RDFS._NS,
|
|
59
|
-
"
|
|
79
|
+
"dcterms": DCTERMS._NS,
|
|
80
|
+
"dc": DC._NS,
|
|
60
81
|
"skos": SKOS._NS,
|
|
61
|
-
"
|
|
82
|
+
"sh": SH._NS,
|
|
62
83
|
"xsd": XSD._NS,
|
|
84
|
+
"imf": Namespace("http://ns.imfid.org/imf#"),
|
|
63
85
|
"pav": Namespace("http://purl.org/pav/"),
|
|
86
|
+
"foaf": FOAF._NS,
|
|
64
87
|
}
|
|
65
88
|
|
|
66
89
|
|
cognite/neat/_graph/_shared.py
CHANGED
|
@@ -4,31 +4,30 @@ MIMETypes: TypeAlias = Literal[
|
|
|
4
4
|
"application/rdf+xml", "text/turtle", "application/n-triple", "application/n-quads", "application/trig"
|
|
5
5
|
]
|
|
6
6
|
|
|
7
|
+
RDFTypes: TypeAlias = Literal["xml", "rdf", "owl", "n3", "ttl", "turtle", "nt", "nq", "nquads", "trig"]
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
def rdflib_to_oxi_type(rdflib_format: str) -> str | None:
|
|
9
11
|
"""Convert an RDFlib format to a MIME type.
|
|
10
12
|
|
|
11
13
|
Args:
|
|
12
14
|
rdflib_format (str): The RDFlib format.
|
|
13
15
|
|
|
14
16
|
Returns:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
!!! note
|
|
18
|
-
This will be replaced once new version of oxrdflib is released.
|
|
17
|
+
Oxi format used to trigger correct plugging in rdflib
|
|
19
18
|
|
|
20
19
|
"""
|
|
21
20
|
|
|
22
21
|
mapping = {
|
|
23
|
-
"xml": "
|
|
24
|
-
"rdf": "
|
|
25
|
-
"owl": "
|
|
26
|
-
"n3": "
|
|
27
|
-
"ttl": "
|
|
28
|
-
"turtle": "
|
|
29
|
-
"nt": "
|
|
30
|
-
"nq": "
|
|
31
|
-
"nquads": "
|
|
32
|
-
"trig": "
|
|
22
|
+
"xml": "ox-xml",
|
|
23
|
+
"rdf": "ox-xml",
|
|
24
|
+
"owl": "ox-xml",
|
|
25
|
+
"n3": "ox-n3",
|
|
26
|
+
"ttl": "ox-ttl",
|
|
27
|
+
"turtle": "ox-turtle",
|
|
28
|
+
"nt": "ox-nt",
|
|
29
|
+
"nq": "ox-nq",
|
|
30
|
+
"nquads": "ox-nquads",
|
|
31
|
+
"trig": "ox-trig",
|
|
33
32
|
}
|
|
34
33
|
return mapping.get(rdflib_format, None)
|