Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test to check element resolving #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/esmf-aspect-meta-model-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ An Aspect of the Meta Model can be instantiated as follows using the provided `A

```
aspect_loader = AspectLoader()
aspect = aspect_loader.load_aspect_model("path/to/turtle.ttl");
aspect = aspect_loader.load_aspect_model("absolute/path/to/turtle.ttl");
```

or

```
aspect_loader = AspectLoader()
aspect = aspect_loader.load_aspect_model_from_multiple_files(["list/of/paths/to/turtles.ttl"], "aspect_urn");
aspect = aspect_loader.load_aspect_model_from_multiple_files(["list/of/absolute/paths/to/turtles.ttl"], "aspect_urn");
```

## Automatic Deployment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ HasUrn
│ ├── SingleEntity
│ ├── StructuredValue
│ ├── Quantifiable
│ │ ├── Duration
│ │ └── Measurement
│ └── Trait
│ │ ├── Duration
│ │ └── Measurement
│ └── Trait
├── Constraint
│ ├── EncodingConstraint
│ ├── FixedPointConstraint
Expand All @@ -38,7 +38,7 @@ HasUrn
│ ├── LocaleConstraint
│ ├── RangeConstraint
│ └── RegularExpressionConstraint
├── Event
├── Event
├── Operation
├── Property
├── QuantityKind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,43 @@ def _get_additional_files_from_dir(file_path: str) -> list[str]:
return additional_files

@staticmethod
def _get_dirs_for_advanced_loading(aspect_graph: rdflib.Graph, base_path: Path) -> list[str]:
def _parse_namespace(prefix_namespace: str) -> tuple[Optional[str], Optional[str]]:
"""Parse the prefix namespace string.

:param prefix_namespace: namespace string of the specific prefix
:return namespace_specific_str: dir of the namespace
:return version: version of the model
"""
namespace_specific_str = None
version = None

namespace_info = prefix_namespace.split(":")
if len(namespace_info) == 4:
urn, namespace_id, namespace_specific_str, version = namespace_info

if urn == "urn" and namespace_id == "samm":
version = version.replace("#", "")

return namespace_specific_str, version

def _get_dirs_for_advanced_loading(self, aspect_graph: rdflib.Graph, file_path: str) -> list[str]:
"""Get directories from graph namespaces for advanced loading.

:param aspect_graph:rdflib.Graph
:param file_path: str path to the main file
:return: list of str path for further advanced files loading
"""
paths_for_advanced_loading = []
base_path = Path(file_path).parents[2]

for _, namespace in aspect_graph.namespace_manager.namespaces():
if namespace.startswith("urn:samm:com."):
namespace_path, version = namespace.split(":")[2:4]
version = version.replace("#", "")
paths_for_advanced_loading.append(join(base_path, namespace_path, version))
for prefix, namespace in aspect_graph.namespace_manager.namespaces():
namespace_specific_str, version = self._parse_namespace(namespace)
if namespace_specific_str and version:
paths_for_advanced_loading.append(join(base_path, namespace_specific_str, version))

return paths_for_advanced_loading

def _get_list_of_additional_files(self, aspect_graph: rdflib.Graph, base_path: Path) -> list[str]:
def _get_list_of_additional_files(self, aspect_graph: rdflib.Graph, file_path: str) -> list[str]:
"""Get a list of additional files for parsing in graph.

:param aspect_graph: rdflib.Graph
Expand All @@ -89,35 +109,50 @@ def _get_list_of_additional_files(self, aspect_graph: rdflib.Graph, base_path: P
"""
additional_files = []

for file_path in self._get_dirs_for_advanced_loading(aspect_graph, base_path):
for file_path in self._get_dirs_for_advanced_loading(aspect_graph, file_path):
additional_files += self._get_additional_files_from_dir(file_path)

return list(set(additional_files))

def _extend_graph_with_prefix_files(self, aspect_graph: rdflib.Graph, base_path: Path) -> None:
def _extend_graph_with_prefix_files(self, aspect_graph: rdflib.Graph, file_path: str) -> None:
"""Extend graph with models from prefix namespaces.

:param aspect_graph: rdflib.Graph
:param base_path: base path of the main graph file
:param file_path: str path of the base graph file
"""
for file_path in self._get_list_of_additional_files(aspect_graph, base_path):
additional_files = self._get_list_of_additional_files(aspect_graph, file_path)

if file_path in additional_files:
additional_files.remove(file_path)

for file_path in additional_files:
aspect_graph.parse(file_path, format="turtle")

@staticmethod
def _prepare_file_paths(file_paths: list[Union[str, Path]]):
"""Check and prepare file paths."""
prepared_file_paths = []

for file_path in file_paths:
if not exists(Path(file_path)):
raise FileNotFoundError(f"Could not find a file {file_path}")

prepared_file_paths.append(str(file_path))

return prepared_file_paths

def _get_graph(self, file_paths: list[Union[str, Path]]) -> rdflib.Graph:
"""Get RDF graph object.

:param file_paths: path list to the turtle files.
:param file_paths: list of absolute paths to the turtle files.
:return: parsed rdflib Graph.
"""

aspect_graph = rdflib.Graph()

# Cast file_path to str
file_paths = [str(file_path) if isinstance(file_path, Path) else file_path for file_path in file_paths]

for file_path in file_paths:
for file_path in self._prepare_file_paths(file_paths):
aspect_graph.parse(file_path, format="turtle")
self._extend_graph_with_prefix_files(aspect_graph, Path(file_path).parents[2])
self._extend_graph_with_prefix_files(aspect_graph, file_path)

return aspect_graph

Expand All @@ -133,7 +168,7 @@ def load_aspect_model_from_multiple_files(
instance to make querying them more efficient

:param file_paths: path/string list to the turtle files
:aspect_urn: urn of the Aspect property
:param aspect_urn: urn of the Aspect property
:return: instance of the aspect graph
"""
self._cache.reset()
Expand All @@ -152,7 +187,8 @@ def load_aspect_model_from_multiple_files(

return model_element_factory.create_element(aspect_urn) # type: ignore

def __extract_samm_version(self, aspect_graph: rdflib.Graph) -> str:
@staticmethod
def __extract_samm_version(aspect_graph: rdflib.Graph) -> str:
"""Get samm version.

searches the aspect graph for the currently used version of the SAMM and returns it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.examples:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.characteristics:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.samm:test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.samm:test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.samm:test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.samm:test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.samm:test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.samm:test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.samm:test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.samm:test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.constraints:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.samm.test:1.0.0#> .
@prefix : <urn:samm:org.eclipse.esmf.test.entity:2.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
Expand Down
Loading
Loading