Skip to content

Commit b448fdf

Browse files
authored
Merge pull request #28 from bci-oss/rbs-13551_resolve-elements-from-other-files
Add a test to check element resolving
2 parents 6dcfb9d + 1e559df commit b448fdf

File tree

72 files changed

+315
-161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+315
-161
lines changed

core/esmf-aspect-meta-model-python/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ An Aspect of the Meta Model can be created as follows using the provided `Aspect
1313

1414
```
1515
aspect_loader = AspectLoader()
16-
aspect = aspect_loader.load_aspect_model("path/to/turtle.ttl");
16+
aspect = aspect_loader.load_aspect_model("absolute/path/to/turtle.ttl");
1717
```
1818

1919
or
2020

2121
```
2222
aspect_loader = AspectLoader()
23-
aspect = aspect_loader.load_aspect_model_from_multiple_files(["list/of/paths/to/turtles.ttl"], "aspect_urn");
23+
aspect = aspect_loader.load_aspect_model_from_multiple_files(["list/of/absolute/paths/to/turtles.ttl"], "aspect_urn");
2424
```
2525

2626
## Automatic Deployment

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/base/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ HasUrn
2727
│ ├── SingleEntity
2828
│ ├── StructuredValue
2929
│ ├── Quantifiable
30-
│ │ ├── Duration
31-
│ │ └── Measurement
32-
│ └── Trait
30+
│ │ ├── Duration
31+
│ │ └── Measurement
32+
│ └── Trait
3333
├── Constraint
3434
│ ├── EncodingConstraint
3535
│ ├── FixedPointConstraint
@@ -38,7 +38,7 @@ HasUrn
3838
│ ├── LocaleConstraint
3939
│ ├── RangeConstraint
4040
│ └── RegularExpressionConstraint
41-
├── Event
41+
├── Event
4242
├── Operation
4343
├── Property
4444
├── QuantityKind

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/aspect_loader.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,43 @@ def _get_additional_files_from_dir(file_path: str) -> list[str]:
6464
return additional_files
6565

6666
@staticmethod
67-
def _get_dirs_for_advanced_loading(aspect_graph: rdflib.Graph, base_path: Path) -> list[str]:
67+
def _parse_namespace(prefix_namespace: str) -> tuple[Optional[str], Optional[str]]:
68+
"""Parse the prefix namespace string.
69+
70+
:param prefix_namespace: namespace string of the specific prefix
71+
:return namespace_specific_str: dir of the namespace
72+
:return version: version of the model
73+
"""
74+
namespace_specific_str = None
75+
version = None
76+
77+
namespace_info = prefix_namespace.split(":")
78+
if len(namespace_info) == 4:
79+
urn, namespace_id, namespace_specific_str, version = namespace_info
80+
81+
if urn == "urn" and namespace_id == "samm":
82+
version = version.replace("#", "")
83+
84+
return namespace_specific_str, version
85+
86+
def _get_dirs_for_advanced_loading(self, aspect_graph: rdflib.Graph, file_path: str) -> list[str]:
6887
"""Get directories from graph namespaces for advanced loading.
6988
7089
:param aspect_graph:rdflib.Graph
90+
:param file_path: str path to the main file
7191
:return: list of str path for further advanced files loading
7292
"""
7393
paths_for_advanced_loading = []
94+
base_path = Path(file_path).parents[2]
7495

75-
for _, namespace in aspect_graph.namespace_manager.namespaces():
76-
if namespace.startswith("urn:samm:com."):
77-
namespace_path, version = namespace.split(":")[2:4]
78-
version = version.replace("#", "")
79-
paths_for_advanced_loading.append(join(base_path, namespace_path, version))
96+
for prefix, namespace in aspect_graph.namespace_manager.namespaces():
97+
namespace_specific_str, version = self._parse_namespace(namespace)
98+
if namespace_specific_str and version:
99+
paths_for_advanced_loading.append(join(base_path, namespace_specific_str, version))
80100

81101
return paths_for_advanced_loading
82102

83-
def _get_list_of_additional_files(self, aspect_graph: rdflib.Graph, base_path: Path) -> list[str]:
103+
def _get_list_of_additional_files(self, aspect_graph: rdflib.Graph, file_path: str) -> list[str]:
84104
"""Get a list of additional files for parsing in graph.
85105
86106
:param aspect_graph: rdflib.Graph
@@ -89,35 +109,50 @@ def _get_list_of_additional_files(self, aspect_graph: rdflib.Graph, base_path: P
89109
"""
90110
additional_files = []
91111

92-
for file_path in self._get_dirs_for_advanced_loading(aspect_graph, base_path):
112+
for file_path in self._get_dirs_for_advanced_loading(aspect_graph, file_path):
93113
additional_files += self._get_additional_files_from_dir(file_path)
94114

95115
return list(set(additional_files))
96116

97-
def _extend_graph_with_prefix_files(self, aspect_graph: rdflib.Graph, base_path: Path) -> None:
117+
def _extend_graph_with_prefix_files(self, aspect_graph: rdflib.Graph, file_path: str) -> None:
98118
"""Extend graph with models from prefix namespaces.
99119
100120
:param aspect_graph: rdflib.Graph
101-
:param base_path: base path of the main graph file
121+
:param file_path: str path of the base graph file
102122
"""
103-
for file_path in self._get_list_of_additional_files(aspect_graph, base_path):
123+
additional_files = self._get_list_of_additional_files(aspect_graph, file_path)
124+
125+
if file_path in additional_files:
126+
additional_files.remove(file_path)
127+
128+
for file_path in additional_files:
104129
aspect_graph.parse(file_path, format="turtle")
105130

131+
@staticmethod
132+
def _prepare_file_paths(file_paths: list[Union[str, Path]]):
133+
"""Check and prepare file paths."""
134+
prepared_file_paths = []
135+
136+
for file_path in file_paths:
137+
if not exists(Path(file_path)):
138+
raise FileNotFoundError(f"Could not find a file {file_path}")
139+
140+
prepared_file_paths.append(str(file_path))
141+
142+
return prepared_file_paths
143+
106144
def _get_graph(self, file_paths: list[Union[str, Path]]) -> rdflib.Graph:
107145
"""Get RDF graph object.
108146
109-
:param file_paths: path list to the turtle files.
147+
:param file_paths: list of absolute paths to the turtle files.
110148
:return: parsed rdflib Graph.
111149
"""
112150

113151
aspect_graph = rdflib.Graph()
114152

115-
# Cast file_path to str
116-
file_paths = [str(file_path) if isinstance(file_path, Path) else file_path for file_path in file_paths]
117-
118-
for file_path in file_paths:
153+
for file_path in self._prepare_file_paths(file_paths):
119154
aspect_graph.parse(file_path, format="turtle")
120-
self._extend_graph_with_prefix_files(aspect_graph, Path(file_path).parents[2])
155+
self._extend_graph_with_prefix_files(aspect_graph, file_path)
121156

122157
return aspect_graph
123158

@@ -133,7 +168,7 @@ def load_aspect_model_from_multiple_files(
133168
instance to make querying them more efficient
134169
135170
:param file_paths: path/string list to the turtle files
136-
:aspect_urn: urn of the Aspect property
171+
:param aspect_urn: urn of the Aspect property
137172
:return: instance of the aspect graph
138173
"""
139174
self._cache.reset()
@@ -152,7 +187,8 @@ def load_aspect_model_from_multiple_files(
152187

153188
return model_element_factory.create_element(aspect_urn) # type: ignore
154189

155-
def __extract_samm_version(self, aspect_graph: rdflib.Graph) -> str:
190+
@staticmethod
191+
def __extract_samm_version(aspect_graph: rdflib.Graph) -> str:
156192
"""Get samm version.
157193
158194
searches the aspect graph for the currently used version of the SAMM and returns it

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithBlankNode.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithBlankNode.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithCode.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithCode.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithCollection.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithCollection.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithCollectionWithElementCharacteristic.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithCollectionWithElementCharacteristic.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithDuration.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithDuration.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithList.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithList.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

core/esmf-aspect-meta-model-python/tests/integration/resources/characteristics/AspectWithMeasurement.ttl renamed to core/esmf-aspect-meta-model-python/tests/integration/resources/org.eclipse.esmf.test.characteristics/2.0.0/AspectWithMeasurement.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# SPDX-License-Identifier: MPL-2.0
1111

12-
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
12+
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
1313
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
1515
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

0 commit comments

Comments
 (0)