industrial-model 0.1.0__tar.gz
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.
- industrial_model-0.1.0/.gitignore +174 -0
- industrial_model-0.1.0/.python-version +1 -0
- industrial_model-0.1.0/PKG-INFO +23 -0
- industrial_model-0.1.0/README.md +3 -0
- industrial_model-0.1.0/assets/logo.jpeg +0 -0
- industrial_model-0.1.0/industrial_model/__init__.py +27 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/__init__.py +56 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/filter_mapper.py +99 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/optimizer.py +74 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/query_mapper.py +181 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/query_result_mapper.py +216 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/schemas.py +121 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/sort_mapper.py +21 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/utils.py +33 -0
- industrial_model-0.1.0/industrial_model/cognite_adapters/view_mapper.py +16 -0
- industrial_model-0.1.0/industrial_model/config.py +10 -0
- industrial_model-0.1.0/industrial_model/constants.py +24 -0
- industrial_model-0.1.0/industrial_model/engines/__init__.py +4 -0
- industrial_model-0.1.0/industrial_model/engines/async_engine.py +37 -0
- industrial_model-0.1.0/industrial_model/engines/engine.py +62 -0
- industrial_model-0.1.0/industrial_model/models/__init__.py +19 -0
- industrial_model-0.1.0/industrial_model/models/base.py +46 -0
- industrial_model-0.1.0/industrial_model/models/entities.py +55 -0
- industrial_model-0.1.0/industrial_model/py.typed +0 -0
- industrial_model-0.1.0/industrial_model/queries/__init__.py +10 -0
- industrial_model-0.1.0/industrial_model/queries/models.py +37 -0
- industrial_model-0.1.0/industrial_model/queries/params.py +42 -0
- industrial_model-0.1.0/industrial_model/statements/__init__.py +70 -0
- industrial_model-0.1.0/industrial_model/statements/expressions.py +165 -0
- industrial_model-0.1.0/industrial_model/utils.py +38 -0
- industrial_model-0.1.0/pyproject.toml +54 -0
- industrial_model-0.1.0/scripts/build.sh +1 -0
- industrial_model-0.1.0/scripts/format.sh +2 -0
- industrial_model-0.1.0/scripts/lint.sh +3 -0
- industrial_model-0.1.0/tests/__init__.py +0 -0
- industrial_model-0.1.0/tests/cognite-sdk-config.yaml +15 -0
- industrial_model-0.1.0/tests/hubs.py +38 -0
- industrial_model-0.1.0/tests/tests_adapter.py +70 -0
- industrial_model-0.1.0/uv.lock +574 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# PyPI configuration file
|
|
174
|
+
.pypirc
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.11
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: industrial-model
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Industrial Model ORM
|
|
5
|
+
Author-email: Lucas Alves <lucasrosaalves@gmail.com>
|
|
6
|
+
Classifier: Programming Language :: Python
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Topic :: Database
|
|
13
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Requires-Dist: anyio>=4.9.0
|
|
17
|
+
Requires-Dist: cognite-sdk>=7.75.0
|
|
18
|
+
Requires-Dist: pydantic>=2.11.4
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# capybara
|
|
22
|
+
|
|
23
|
+

|
|
Binary file
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from .config import DataModelId
|
|
2
|
+
from .engines import AsyncEngine, Engine
|
|
3
|
+
from .models import (
|
|
4
|
+
InstanceId,
|
|
5
|
+
PaginatedResult,
|
|
6
|
+
TViewInstance,
|
|
7
|
+
ValidationMode,
|
|
8
|
+
ViewInstance,
|
|
9
|
+
ViewInstanceConfig,
|
|
10
|
+
)
|
|
11
|
+
from .statements import and_, col, or_, select
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"and_",
|
|
15
|
+
"or_",
|
|
16
|
+
"col",
|
|
17
|
+
"select",
|
|
18
|
+
"ViewInstance",
|
|
19
|
+
"InstanceId",
|
|
20
|
+
"TViewInstance",
|
|
21
|
+
"DataModelId",
|
|
22
|
+
"ValidationMode",
|
|
23
|
+
"Engine",
|
|
24
|
+
"AsyncEngine",
|
|
25
|
+
"PaginatedResult",
|
|
26
|
+
"ViewInstanceConfig",
|
|
27
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from cognite.client import CogniteClient
|
|
4
|
+
|
|
5
|
+
from industrial_model.cognite_adapters.optimizer import QueryOptimizer
|
|
6
|
+
from industrial_model.config import DataModelId
|
|
7
|
+
from industrial_model.models import TViewInstance
|
|
8
|
+
from industrial_model.statements import Statement
|
|
9
|
+
|
|
10
|
+
from .query_mapper import QueryMapper
|
|
11
|
+
from .query_result_mapper import (
|
|
12
|
+
QueryResultMapper,
|
|
13
|
+
)
|
|
14
|
+
from .view_mapper import ViewMapper
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CogniteAdapter:
|
|
18
|
+
def __init__(
|
|
19
|
+
self, cognite_client: CogniteClient, data_model_id: DataModelId
|
|
20
|
+
):
|
|
21
|
+
self._cognite_client = cognite_client
|
|
22
|
+
|
|
23
|
+
dm = cognite_client.data_modeling.data_models.retrieve(
|
|
24
|
+
ids=data_model_id.as_tuple(),
|
|
25
|
+
inline_views=True,
|
|
26
|
+
).latest_version()
|
|
27
|
+
view_mapper = ViewMapper(dm.views)
|
|
28
|
+
self._query_mapper = QueryMapper(view_mapper)
|
|
29
|
+
self._result_mapper = QueryResultMapper(view_mapper)
|
|
30
|
+
self._optmizer = QueryOptimizer(cognite_client)
|
|
31
|
+
|
|
32
|
+
def query(
|
|
33
|
+
self, statement: Statement[TViewInstance], all_pages: bool
|
|
34
|
+
) -> tuple[list[dict[str, Any]], str | None]:
|
|
35
|
+
self._optmizer.optimize(statement)
|
|
36
|
+
cognite_query = self._query_mapper.map(statement)
|
|
37
|
+
view_external_id = statement.entity.get_view_external_id()
|
|
38
|
+
|
|
39
|
+
data: list[dict[str, Any]] = []
|
|
40
|
+
while True:
|
|
41
|
+
query_result = self._cognite_client.data_modeling.instances.query(
|
|
42
|
+
cognite_query
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
page_result, next_cursor = self._result_mapper.map_nodes(
|
|
46
|
+
view_external_id, query_result
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
data.extend(page_result)
|
|
50
|
+
|
|
51
|
+
last_page = len(page_result) < statement.limit_ or not next_cursor
|
|
52
|
+
next_cursor_ = None if last_page else next_cursor
|
|
53
|
+
cognite_query.cursors = {view_external_id: next_cursor_}
|
|
54
|
+
|
|
55
|
+
if not all_pages or last_page:
|
|
56
|
+
return data, next_cursor
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
import cognite.client.data_classes.filters as cdf_filters
|
|
4
|
+
from cognite.client.data_classes.data_modeling import MappedProperty, View
|
|
5
|
+
|
|
6
|
+
from industrial_model.cognite_adapters.utils import get_property_ref
|
|
7
|
+
from industrial_model.statements import (
|
|
8
|
+
BoolExpression,
|
|
9
|
+
Expression,
|
|
10
|
+
LeafExpression,
|
|
11
|
+
)
|
|
12
|
+
from industrial_model.utils import datetime_to_ms_iso_timestamp
|
|
13
|
+
|
|
14
|
+
from .view_mapper import ViewMapper
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class FilterMapper:
|
|
18
|
+
def __init__(self, view_mapper: ViewMapper):
|
|
19
|
+
self._view_mapper = view_mapper
|
|
20
|
+
|
|
21
|
+
def map(
|
|
22
|
+
self, expressions: list[Expression], root_view: View
|
|
23
|
+
) -> list[cdf_filters.Filter]:
|
|
24
|
+
result: list[cdf_filters.Filter] = []
|
|
25
|
+
for expression in expressions:
|
|
26
|
+
if isinstance(expression, BoolExpression):
|
|
27
|
+
result.append(self.to_cdf_filter_bool(expression, root_view))
|
|
28
|
+
elif isinstance(expression, LeafExpression):
|
|
29
|
+
result.append(self.to_cdf_filter_leaf(expression, root_view))
|
|
30
|
+
else:
|
|
31
|
+
cls_name = expression.__class__.__name__
|
|
32
|
+
raise ValueError(f"Expression not implemented {cls_name}")
|
|
33
|
+
return result
|
|
34
|
+
|
|
35
|
+
def to_cdf_filter_bool(
|
|
36
|
+
self, expression: BoolExpression, root_view: View
|
|
37
|
+
) -> cdf_filters.Filter:
|
|
38
|
+
arguments = self.map(expression.filters, root_view)
|
|
39
|
+
|
|
40
|
+
if expression.operator == "and":
|
|
41
|
+
return cdf_filters.And(*arguments)
|
|
42
|
+
elif expression.operator == "or":
|
|
43
|
+
return cdf_filters.Or(*arguments)
|
|
44
|
+
elif expression.operator == "not":
|
|
45
|
+
return cdf_filters.Not(*arguments)
|
|
46
|
+
|
|
47
|
+
raise NotImplementedError(f"Operator {self.operator} not implemented")
|
|
48
|
+
|
|
49
|
+
def to_cdf_filter_leaf(
|
|
50
|
+
self,
|
|
51
|
+
expression: LeafExpression,
|
|
52
|
+
root_view: View,
|
|
53
|
+
) -> cdf_filters.Filter:
|
|
54
|
+
property_ref = get_property_ref(expression.property, root_view)
|
|
55
|
+
|
|
56
|
+
value_ = expression.value
|
|
57
|
+
if isinstance(value_, datetime):
|
|
58
|
+
value_ = datetime_to_ms_iso_timestamp(value_)
|
|
59
|
+
|
|
60
|
+
if expression.operator == "==":
|
|
61
|
+
return cdf_filters.Equals(property_ref, value_)
|
|
62
|
+
elif expression.operator == "in":
|
|
63
|
+
return cdf_filters.In(property_ref, value_)
|
|
64
|
+
elif expression.operator == ">":
|
|
65
|
+
return cdf_filters.Range(property_ref, gt=value_)
|
|
66
|
+
elif expression.operator == ">=":
|
|
67
|
+
return cdf_filters.Range(property_ref, gte=value_)
|
|
68
|
+
elif expression.operator == "<":
|
|
69
|
+
return cdf_filters.Range(property_ref, lt=value_)
|
|
70
|
+
elif expression.operator == "<=":
|
|
71
|
+
return cdf_filters.Range(property_ref, lte=value_)
|
|
72
|
+
elif expression.operator == "nested":
|
|
73
|
+
target_view = self._get_nested_target_view(
|
|
74
|
+
expression.property, root_view
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
assert isinstance(value_, Expression)
|
|
78
|
+
|
|
79
|
+
return cdf_filters.Nested(
|
|
80
|
+
property_ref,
|
|
81
|
+
self.map([value_], target_view)[0],
|
|
82
|
+
)
|
|
83
|
+
elif expression.operator == "exists":
|
|
84
|
+
return cdf_filters.Exists(property_ref)
|
|
85
|
+
elif expression.operator == "prefix":
|
|
86
|
+
return cdf_filters.Prefix(property_ref, value_)
|
|
87
|
+
elif expression.operator == "containsAll":
|
|
88
|
+
return cdf_filters.ContainsAll(property_ref, value_)
|
|
89
|
+
elif expression.operator == "containsAny":
|
|
90
|
+
return cdf_filters.ContainsAny(property_ref, value_)
|
|
91
|
+
raise NotImplementedError(
|
|
92
|
+
f"Operator {expression.operator} not implemented"
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def _get_nested_target_view(self, property: str, root_view: View) -> View:
|
|
96
|
+
view_definiton = root_view.properties[property]
|
|
97
|
+
assert isinstance(view_definiton, MappedProperty)
|
|
98
|
+
assert view_definiton.source
|
|
99
|
+
return self._view_mapper.get_view(view_definiton.source.external_id)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from cognite.client import CogniteClient
|
|
2
|
+
|
|
3
|
+
from industrial_model.models import TViewInstance
|
|
4
|
+
from industrial_model.statements import (
|
|
5
|
+
BoolExpression,
|
|
6
|
+
Expression,
|
|
7
|
+
LeafExpression,
|
|
8
|
+
Statement,
|
|
9
|
+
col,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
SPACE_PROPERTY = "space"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class QueryOptimizer:
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
cognite_client: CogniteClient,
|
|
19
|
+
):
|
|
20
|
+
self._all_spaces: list[str] | None = None
|
|
21
|
+
self._cognite_client = cognite_client
|
|
22
|
+
|
|
23
|
+
def optimize(self, statement: Statement[TViewInstance]) -> None:
|
|
24
|
+
instance_spaces = statement.entity.view_config.get("instance_spaces")
|
|
25
|
+
instance_spaces_prefix = statement.entity.view_config.get(
|
|
26
|
+
"instance_spaces_prefix"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
if not instance_spaces and not instance_spaces_prefix:
|
|
30
|
+
return
|
|
31
|
+
|
|
32
|
+
if self._has_space_filter(statement.where_clauses):
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
filter_spaces = (
|
|
36
|
+
self._find_spaces(instance_spaces_prefix)
|
|
37
|
+
if instance_spaces_prefix
|
|
38
|
+
else []
|
|
39
|
+
)
|
|
40
|
+
if instance_spaces:
|
|
41
|
+
filter_spaces.extend(instance_spaces)
|
|
42
|
+
|
|
43
|
+
if filter_spaces:
|
|
44
|
+
statement.where(col(SPACE_PROPERTY).in_(filter_spaces))
|
|
45
|
+
|
|
46
|
+
def _has_space_filter(self, where_clauses: list[Expression]) -> bool:
|
|
47
|
+
for where_clause in where_clauses:
|
|
48
|
+
if isinstance(
|
|
49
|
+
where_clause, BoolExpression
|
|
50
|
+
) and self._has_space_filter(where_clause.filters):
|
|
51
|
+
return True
|
|
52
|
+
elif isinstance(where_clause, LeafExpression):
|
|
53
|
+
return where_clause.property == SPACE_PROPERTY
|
|
54
|
+
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
def _find_spaces(self, instance_spaces_prefix: str) -> list[str]:
|
|
58
|
+
all_spaces = self._get_all_spaces()
|
|
59
|
+
|
|
60
|
+
return [
|
|
61
|
+
space
|
|
62
|
+
for space in all_spaces
|
|
63
|
+
if space.startswith(instance_spaces_prefix)
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
def _get_all_spaces(self) -> list[str]:
|
|
67
|
+
all_spaces = self._all_spaces
|
|
68
|
+
if all_spaces is None:
|
|
69
|
+
all_spaces = self._cognite_client.data_modeling.spaces.list(
|
|
70
|
+
limit=-1
|
|
71
|
+
).as_ids()
|
|
72
|
+
|
|
73
|
+
self._all_spaces = all_spaces
|
|
74
|
+
return all_spaces
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import cognite.client.data_classes.filters as filters
|
|
2
|
+
from cognite.client.data_classes.data_modeling import (
|
|
3
|
+
EdgeConnection,
|
|
4
|
+
MappedProperty,
|
|
5
|
+
View,
|
|
6
|
+
ViewId,
|
|
7
|
+
)
|
|
8
|
+
from cognite.client.data_classes.data_modeling.query import (
|
|
9
|
+
EdgeResultSetExpression,
|
|
10
|
+
NodeResultSetExpression,
|
|
11
|
+
ResultSetExpression,
|
|
12
|
+
Select,
|
|
13
|
+
SourceSelector,
|
|
14
|
+
)
|
|
15
|
+
from cognite.client.data_classes.data_modeling.query import (
|
|
16
|
+
Query as CogniteQuery,
|
|
17
|
+
)
|
|
18
|
+
from cognite.client.data_classes.data_modeling.views import (
|
|
19
|
+
MultiReverseDirectRelation,
|
|
20
|
+
SingleReverseDirectRelation,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
from industrial_model.constants import EDGE_MARKER, MAX_LIMIT, NESTED_SEP
|
|
24
|
+
from industrial_model.models import TViewInstance
|
|
25
|
+
from industrial_model.statements import Statement
|
|
26
|
+
|
|
27
|
+
from .filter_mapper import (
|
|
28
|
+
FilterMapper,
|
|
29
|
+
)
|
|
30
|
+
from .schemas import get_schema_properties
|
|
31
|
+
from .sort_mapper import SortMapper
|
|
32
|
+
from .view_mapper import ViewMapper
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class QueryMapper:
|
|
36
|
+
def __init__(self, view_mapper: ViewMapper):
|
|
37
|
+
self._view_mapper = view_mapper
|
|
38
|
+
self._filter_mapper = FilterMapper(view_mapper)
|
|
39
|
+
self._sort_mapper = SortMapper()
|
|
40
|
+
|
|
41
|
+
def map(self, statement: Statement[TViewInstance]) -> CogniteQuery:
|
|
42
|
+
root_node = statement.entity.get_view_external_id()
|
|
43
|
+
|
|
44
|
+
root_view = self._view_mapper.get_view(root_node)
|
|
45
|
+
root_view_id = root_view.as_id()
|
|
46
|
+
|
|
47
|
+
filters_: list[filters.Filter] = [
|
|
48
|
+
filters.HasData(views=[root_view_id])
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
filters_.extend(
|
|
52
|
+
self._filter_mapper.map(statement.where_clauses, root_view)
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
with_: dict[str, ResultSetExpression] = {
|
|
56
|
+
root_node: NodeResultSetExpression(
|
|
57
|
+
filter=filters.And(*filters_),
|
|
58
|
+
sort=self._sort_mapper.map(statement.sort_clauses, root_view),
|
|
59
|
+
limit=statement.limit_,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
select_: dict[str, Select] = {}
|
|
63
|
+
|
|
64
|
+
relations = get_schema_properties(
|
|
65
|
+
statement.entity, NESTED_SEP, root_node
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
properties = self._include_statements(
|
|
69
|
+
root_node, root_view, relations, with_, select_
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
select_[root_node] = self._get_select(root_view_id, properties)
|
|
73
|
+
|
|
74
|
+
return CogniteQuery(
|
|
75
|
+
with_=with_, select=select_, cursors={root_node: statement.cursor_}
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def _get_select(self, view_id: ViewId, properties: list[str]) -> Select:
|
|
79
|
+
return (
|
|
80
|
+
Select(
|
|
81
|
+
sources=[SourceSelector(source=view_id, properties=properties)]
|
|
82
|
+
)
|
|
83
|
+
if properties
|
|
84
|
+
else Select()
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
def _include_statements(
|
|
88
|
+
self,
|
|
89
|
+
key: str,
|
|
90
|
+
view: View,
|
|
91
|
+
relations_to_include: list[str] | None,
|
|
92
|
+
with_: dict[str, ResultSetExpression],
|
|
93
|
+
select_: dict[str, Select],
|
|
94
|
+
) -> list[str]:
|
|
95
|
+
if not relations_to_include:
|
|
96
|
+
return []
|
|
97
|
+
|
|
98
|
+
select_properties: list[str] = []
|
|
99
|
+
for property_name, property in view.properties.items():
|
|
100
|
+
property_key = f"{key}{NESTED_SEP}{property_name}"
|
|
101
|
+
if property_key not in relations_to_include:
|
|
102
|
+
continue
|
|
103
|
+
|
|
104
|
+
if isinstance(property, MappedProperty) and not property.source:
|
|
105
|
+
select_properties.append(property_name)
|
|
106
|
+
elif isinstance(property, MappedProperty) and property.source:
|
|
107
|
+
select_properties.append(property_name)
|
|
108
|
+
|
|
109
|
+
props = self._include_statements(
|
|
110
|
+
property_key,
|
|
111
|
+
self._view_mapper.get_view(property.source.external_id),
|
|
112
|
+
relations_to_include,
|
|
113
|
+
with_,
|
|
114
|
+
select_,
|
|
115
|
+
)
|
|
116
|
+
if props:
|
|
117
|
+
with_[property_key] = NodeResultSetExpression(
|
|
118
|
+
from_=key,
|
|
119
|
+
through=view.as_property_ref(property_name),
|
|
120
|
+
limit=MAX_LIMIT,
|
|
121
|
+
)
|
|
122
|
+
select_[property_key] = self._get_select(
|
|
123
|
+
property.source, props
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
elif (
|
|
127
|
+
isinstance(property, MultiReverseDirectRelation)
|
|
128
|
+
or isinstance(property, SingleReverseDirectRelation)
|
|
129
|
+
and property.source
|
|
130
|
+
):
|
|
131
|
+
props = self._include_statements(
|
|
132
|
+
property_key,
|
|
133
|
+
self._view_mapper.get_view(property.source.external_id),
|
|
134
|
+
relations_to_include,
|
|
135
|
+
with_,
|
|
136
|
+
select_,
|
|
137
|
+
)
|
|
138
|
+
if not props:
|
|
139
|
+
with_[property_key] = NodeResultSetExpression(
|
|
140
|
+
from_=key,
|
|
141
|
+
direction="inwards",
|
|
142
|
+
through=property.source.as_property_ref(
|
|
143
|
+
property.through.property
|
|
144
|
+
),
|
|
145
|
+
limit=MAX_LIMIT,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
select_[property_key] = self._get_select(
|
|
149
|
+
property.source, props
|
|
150
|
+
)
|
|
151
|
+
elif isinstance(property, EdgeConnection) and property.source:
|
|
152
|
+
edge_property_key = f"{property_key}{NESTED_SEP}{EDGE_MARKER}"
|
|
153
|
+
|
|
154
|
+
with_[edge_property_key] = EdgeResultSetExpression(
|
|
155
|
+
from_=key,
|
|
156
|
+
max_distance=1,
|
|
157
|
+
filter=filters.Equals(
|
|
158
|
+
["edge", "type"], property.type.dump()
|
|
159
|
+
),
|
|
160
|
+
direction=property.direction,
|
|
161
|
+
limit=MAX_LIMIT,
|
|
162
|
+
)
|
|
163
|
+
with_[property_key] = NodeResultSetExpression(
|
|
164
|
+
from_=edge_property_key,
|
|
165
|
+
limit=MAX_LIMIT,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
select_[edge_property_key] = Select()
|
|
169
|
+
|
|
170
|
+
props = self._include_statements(
|
|
171
|
+
property_key,
|
|
172
|
+
self._view_mapper.get_view(property.source.external_id),
|
|
173
|
+
relations_to_include,
|
|
174
|
+
with_,
|
|
175
|
+
select_,
|
|
176
|
+
)
|
|
177
|
+
select_[property_key] = self._get_select(
|
|
178
|
+
property.source, props
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
return select_properties
|