diff --git a/.gitignore b/.gitignore index e6a5e85..637b0d4 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,4 @@ dmypy.json # SAMM core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/samm_aspect_meta_model/samm/ /core/esmf-aspect-meta-model-python/samm-cli/ +/core/esmf-aspect-meta-model-python/tests/integration/java_models/resources/ diff --git a/core/esmf-aspect-meta-model-python/README.md b/core/esmf-aspect-meta-model-python/README.md index cb61084..9873d4b 100644 --- a/core/esmf-aspect-meta-model-python/README.md +++ b/core/esmf-aspect-meta-model-python/README.md @@ -1,32 +1,18 @@ The Aspect Model Loader as part of the Python SDK provided by the [*Eclipse Semantic Modeling Framework*]( https://projects.eclipse.org/projects/dt.esmf). - -* [An Aspect of the Meta Model](#an-aspect-of-the-meta-model) - * [Set Up SAMM Aspect Meta Model](#set-up-samm-aspect-meta-model) - * [Install poetry](#install-poetry) - * [Install project dependencies](#install-project-dependencies) - * [Download SAMM files](#download-samm-files) - * [Download SAMM release](#download-samm-release) - * [Download SAMM branch](#download-samm-branch) - * [Input data handler usage](#input-data-handler-usage) - * [Aspect Meta Model Loader usage](#aspect-meta-model-loader-usage) - * [Samm Units](#samm-units) - * [SAMM CLI wrapper class](#samm-cli-wrapper-class) -* [Scripts](#scripts) -* [Automation Tasks](#automation-tasks) - * [tox](#tox) - * [GitHub actions](#github-actions) - * [Check New Pull Request](#check-new-pull-request) - * [Build release](#build-release) - - # An Aspect of the Meta Model The `esmf-aspect-model-loader` package provides the Python implementation for the SAMM Aspect Meta Model, or SAMM. Each Meta Model element and each Characteristic class is represented by an interface with a corresponding implementation. +## Documentation + +* Check the [developer documentation](https://eclipse-esmf.github.io) +* Check the SAMM [specification](https://eclipse-esmf.github.io/samm-specification/snapshot/index.html) +* Having issues with the ESMF SDK Python Aspect Model Loader? Open a [GitHub issue](https://github.com/eclipse-esmf/esmf-sdk-py-aspect-model-loader/issues). + ## Set Up SAMM Aspect Meta Model Before getting started to use the `esmf-aspect-model-loader` library you need to apply some set up actions: @@ -85,55 +71,42 @@ poetry run download-samm-branch ``` Link to all branches: [SAMM Releases](https://github.com/eclipse-esmf/esmf-semantic-aspect-meta-model/branches) -## Input data handler usage - -The InputHandler is a general-purpose class designed for loading input data into an RDF graph. -It easily accommodates different input sources such as local files (.ttl) or direct data strings containing -RDF formatted data. +## SAMM Aspect Model Graph usage +SAMM Aspect Model Graph is a class that allows you to load and interact with the Semantic Data Aspect Meta Model graph. +Below is an example of how to use SAMM Aspect Model Graph in your Python code: ```python -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import SAMMGraph -# Instantiating the Handler -# The InputHandler requires a path or data string upon instantiation, which defines the source of RDF data -# local file -model_path = "path/to/local/file/AspectName.ttl" -handler = InputHandler(model_path) -graph, aspect_urn = handler.get_rdf_graph() +# Define the path to your Turtle file +model_path = "absolute/path/to/turtle.ttl" -# returns a tuple containing the RDF graph and the aspect URN derived from the provided data source -``` +# Create an instance of SAMMGraph +samm_graph = SAMMGraph() -```python -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +# Parse the Turtle file to load the graph +samm_graph.parse(model_path) -# Alternatively, if you have RDF data in a string format, you can directly pass it as follows: -rdf_data_string = "your RDF data string here" -handler = InputHandler(rdf_data_string) -graph, aspect_urn = handler.get_rdf_graph() +# Load the aspect model from the graph +aspect = samm_graph.load_aspect_model() ``` -## Aspect Meta Model Loader usage - -An Aspect of the Meta Model can be loaded as follows: +The `load_model_elements` method in the SAMMGraph class creates Python objects to represent all nodes from the Aspect +model graph. It retrieves all SAMM elements from the RDF graph and converts them into structured Python objects. ```python -from esmf_aspect_meta_model_python import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import SAMMGraph +# Define the path to your Turtle file model_path = "absolute/path/to/turtle.ttl" -handler = InputHandler(model_path) -graph, aspect_urn = handler.get_rdf_graph() -loader = AspectLoader() -model_elements = loader.load_aspect_model(graph, aspect_urn) -aspect = model_elements[0] +# Create an instance of SAMMGraph +samm_graph = SAMMGraph() -# or you can provide an Aspect URN +# Parse the Turtle file to load the graph +samm_graph.parse(model_path) -loader = AspectLoader() -aspect_urn = "urn:samm:org.eclipse.esmf.samm:aspect.model:0.0.1#AspectName" -model_elements = loader.load_aspect_model("absolute/path/to/turtle.ttl", aspect_urn) -aspect = model_elements[0] +# Load all model elements from the graph +model_elements = samm_graph.load_model_elements() ``` ## Samm Units @@ -200,7 +173,7 @@ Provided scripts: All scripts run like a poetry command. The poetry is available from the folder where [pyproject.toml](pyproject.toml) is located. -# Automation Tasks +# Tests running ## tox `tox` is used for the tests automation purpose. There are two environments with different purposes and tests can diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/__init__.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/__init__.py index e26b96a..c3608b5 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/__init__.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/__init__.py @@ -85,6 +85,5 @@ DefaultTrait, DefaultUnit, ) -from .loader.aspect_loader import AspectLoader from .loader.samm_graph import SAMMGraph from .resolver.handler import InputHandler diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/base/characteristics/characteristic.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/base/characteristics/characteristic.py index 9f274e0..b994e6b 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/base/characteristics/characteristic.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/base/characteristics/characteristic.py @@ -8,7 +8,6 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. # # SPDX-License-Identifier: MPL-2.0 - from abc import ABC, abstractmethod from esmf_aspect_meta_model_python.base.base import Base diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/__init__.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/__init__.py index 0a203f9..dc01586 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/__init__.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/__init__.py @@ -42,6 +42,6 @@ from .default_either import DefaultEither from .default_event import DefaultEvent from .default_operation import DefaultOperation -from .default_property import DefaultProperty +from .default_property import DefaultBlankProperty, DefaultProperty, DefaultPropertyWithExtends from .default_quantity_kind import DefaultQuantityKind from .default_unit import DefaultUnit diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/collection/default_collection.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/collection/default_collection.py index 3b6e296..04075ac 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/collection/default_collection.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/collection/default_collection.py @@ -26,7 +26,7 @@ class DefaultCollection(DefaultCharacteristic, Collection): def __init__( self, meta_model_base_attributes: MetaModelBaseAttributes, - data_type: DataType, + data_type: Optional[DataType], element_characteristic: Optional[Characteristic], ): super().__init__(meta_model_base_attributes, data_type) @@ -35,7 +35,7 @@ def __init__( def _set_parent_element_on_child_element(self) -> None: """Set a parent element on child elements.""" - if self._element_characteristic is not None: + if self._element_characteristic: self._element_characteristic.append_parent_element(self) @property diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/default_characteristic.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/default_characteristic.py index 7f7ee2d..e4dcc30 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/default_characteristic.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/characteristics/default_characteristic.py @@ -8,6 +8,7 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. # # SPDX-License-Identifier: MPL-2.0 +from typing import Optional from esmf_aspect_meta_model_python.base.characteristics.characteristic import Characteristic from esmf_aspect_meta_model_python.base.data_types.complex_type import ComplexType @@ -21,8 +22,12 @@ class DefaultCharacteristic(BaseImpl, Characteristic): SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + ["data_type"] - def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, data_type: DataType): + def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, data_type: Optional[DataType]): + if data_type is None: + raise ValueError("Attribute 'data_type' is required for Characteristic class.") + super().__init__(meta_model_base_attributes) + self._data_type = data_type if isinstance(self._data_type, ComplexType): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/data_types/default_abstract_entity.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/data_types/default_abstract_entity.py index f7b54a1..01019a2 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/data_types/default_abstract_entity.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/data_types/default_abstract_entity.py @@ -36,4 +36,10 @@ def __init__( @property def extending_elements(self) -> List[ComplexType]: """Extending elements.""" - return [DefaultComplexType._instances[element_subject] for element_subject in self.__extending_elements] + extending_elements = [] + for element_subject in self.__extending_elements: + element = DefaultComplexType._instances.get(element_subject) + if element: + extending_elements.append(element) + + return extending_elements diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/default_property.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/default_property.py index de64166..38004e8 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/default_property.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/default_property.py @@ -8,13 +8,19 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. # # SPDX-License-Identifier: MPL-2.0 +from typing import TYPE_CHECKING, Any, Dict, List, Optional -from typing import Any, Dict, List, Optional +from rdflib.term import BNode, IdentifiedNode from esmf_aspect_meta_model_python.base.characteristics.characteristic import Characteristic from esmf_aspect_meta_model_python.base.property import Property from esmf_aspect_meta_model_python.impl.base_impl import BaseImpl from esmf_aspect_meta_model_python.loader.meta_model_base_attributes import MetaModelBaseAttributes +from esmf_aspect_meta_model_python.loader.model_element_factory import ModelElementFactory +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM + +if TYPE_CHECKING: + from esmf_aspect_meta_model_python.loader.instantiator.property_instantiator import PropertyInstantiator class DefaultProperty(BaseImpl, Property): @@ -32,8 +38,10 @@ class DefaultProperty(BaseImpl, Property): def __init__( self, meta_model_base_attributes: MetaModelBaseAttributes, - characteristic: Optional[Characteristic], - example_value: Optional[Any], + elements_factory: Optional[ModelElementFactory] = None, + graph_node: Optional[IdentifiedNode] = None, + characteristic: Optional[Characteristic] = None, + example_value: Optional[Any] = None, extends: Optional[Property] = None, abstract: bool = False, optional: bool = False, @@ -42,9 +50,9 @@ def __init__( ): super().__init__(meta_model_base_attributes) - if characteristic is not None: - characteristic.append_parent_element(self) - self._characteristic = characteristic + self._elements_factory = elements_factory + self._graph_node = graph_node + self._set_characteristic(characteristic) self._example_value = example_value self._is_abstract = abstract self._extends = extends @@ -52,14 +60,54 @@ def __init__( self._not_in_payload = not_in_payload self._payload_name = payload_name + def _get_instantiator_class(self) -> Optional["PropertyInstantiator"]: + """Get instantiator factory class.""" + instantiator_class = None + if self._elements_factory: + from esmf_aspect_meta_model_python.loader.instantiator.property_instantiator import PropertyInstantiator + + element_type = self._elements_factory._get_element_type(self._graph_node) + instantiator = self._elements_factory._instantiators.get( + str(self._graph_node), + self._elements_factory._create_instantiator(element_type), + ) + if isinstance(instantiator, PropertyInstantiator): + instantiator_class = instantiator + + return instantiator_class + + def _set_characteristic(self, characteristic: Optional[Characteristic]): + """Set self as parent element for all child nodes.""" + self._characteristic = characteristic + if self._characteristic: + self._characteristic.append_parent_element(self) + @property def characteristic(self) -> Optional[Characteristic]: """Characteristic.""" + if not self._characteristic and self._graph_node: + instantiator_class = self._get_instantiator_class() + if instantiator_class: + characteristic = instantiator_class._get_child( + self._graph_node, + instantiator_class._samm.get_urn(SAMM.characteristic), + required=True, + ) + self._set_characteristic(characteristic) + return self._characteristic @property def example_value(self) -> Optional[Any]: """Example of value.""" + if not self._example_value: + instantiator_class = self._get_instantiator_class() + if instantiator_class: + self._example_value = instantiator_class._aspect_graph.value( + subject=self._graph_node, + predicate=instantiator_class._samm.get_urn(SAMM.example_value), + ) + return self._example_value @property @@ -85,7 +133,7 @@ def is_not_in_payload(self) -> bool: @property def payload_name(self) -> str: """Payload name.""" - return self.name if self._payload_name is None else self._payload_name + return self._payload_name if self._payload_name else self.name @property def preferred_names(self) -> Dict[str, str]: @@ -95,9 +143,11 @@ def preferred_names(self) -> Dict[str, str]: If both, the property and the abstract property have a preferred name for the same language, then the preferred name of the concrete property is used. """ - if self.extends is None: - return self._preferred_names - return self.extends.preferred_names | self._preferred_names + preferred_names = ( + self.extends.preferred_names | self._preferred_names if self.extends else self._preferred_names + ) + + return preferred_names @property def descriptions(self) -> Dict[str, str]: @@ -107,9 +157,9 @@ def descriptions(self) -> Dict[str, str]: If both, the property and the abstract property have a description for the same language, then the description of the concrete property is used. """ - if self.extends is None: - return self._descriptions - return self.extends.descriptions | self._descriptions + descriptions = self.extends.descriptions | self._descriptions if self.extends else self._descriptions + + return descriptions @property def see(self) -> List[str]: @@ -117,4 +167,85 @@ def see(self) -> List[str]: Returns a combined list of all see elements of self and the extended abstract property. """ - return self._see if self.extends is None else self._see + self.extends.see + see = self._see + + if self.extends: + self._see += self.extends.see + + return see + + +class DefaultBlankProperty(DefaultProperty): + def __init__(self, base_element_node: BNode, *args, **kwargs): + super().__init__(*args, **kwargs) + self._base_element_node = base_element_node + + @property + def is_optional(self) -> bool: + """Is optional flag.""" + if not self._optional: + instantiator_class = self._get_instantiator_class() + if instantiator_class: + optional_node = instantiator_class._aspect_graph.value( + subject=self._base_element_node, + predicate=instantiator_class._samm.get_urn(SAMM.optional), + ) + self._optional = optional_node is not None + + return self._optional + + @property + def is_not_in_payload(self) -> bool: + """Is not in payload flag.""" + if not self._not_in_payload: + instantiator_class = self._get_instantiator_class() + if instantiator_class: + not_in_payload_node = instantiator_class._aspect_graph.value( + subject=self._base_element_node, + predicate=instantiator_class._samm.get_urn(SAMM.not_in_payload), + ) + self._not_in_payload = not_in_payload_node is not None + + return self._not_in_payload + + @property + def payload_name(self) -> str: + """Payload name.""" + if not self._payload_name: + instantiator_class = self._get_instantiator_class() + if instantiator_class: + self._payload_name = instantiator_class._get_child( + self._base_element_node, + instantiator_class._samm.get_urn(SAMM.payload_name), + ) + + return self._payload_name if self._payload_name else self.name + + +class DefaultPropertyWithExtends(DefaultProperty): + @property + def payload_name(self) -> str: + """Payload name.""" + if not self._payload_name: + instantiator_class = self._get_instantiator_class() + if instantiator_class and self._graph_node: + self._payload_name = instantiator_class._get_child( + self._graph_node, + instantiator_class._samm.get_urn(SAMM.payload_name), + ) + + return self._payload_name if self._payload_name else self.name + + @property + def extends(self) -> Optional[Property]: + """Extends.""" + if not self._extends: + instantiator_class = self._get_instantiator_class() + if instantiator_class and self._graph_node: + self._extends = instantiator_class._get_child( + self._graph_node, + instantiator_class._samm.get_urn(SAMM.extends), + required=True, + ) + + return self._extends diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/aspect_loader.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/aspect_loader.py deleted file mode 100644 index afb5db9..0000000 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/aspect_loader.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH -# -# See the AUTHORS file(s) distributed with this work for additional -# information regarding authorship. -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at https://mozilla.org/MPL/2.0/. -# -# SPDX-License-Identifier: MPL-2.0 - -from typing import List - -from rdflib import Graph, URIRef - -from esmf_aspect_meta_model_python.base.aspect import Aspect -from esmf_aspect_meta_model_python.loader.samm_graph import SAMMGraph - - -class AspectLoader: - """Entry point to load an aspect model. To load an aspect model from - a turtle file call AspectLoader.load_aspect_model(file_path) - - cache strategy to cache created elements to ensure uniqueness and a fast lookup of it. - The default cache strategy ignores inline defined elements. - """ - - def __init__(self): - self.graph = None - - def load_aspect_model(self, rdf_graph: Graph, aspect_urn: URIRef | str = "") -> List[Aspect]: - """ - Creates a python object(s) to represent the Aspect model graph. - - This function takes an RDF graph and a URN for an Aspect node and converts it into - a set of structured and connected Python objects that represents the Aspect model graph. The output is a - list of Python objects derived from the RDF graph centered around the specified Aspect node. - - Args: - rdf_graph (RDFGraph): The RDF graph from which to create the model. - aspect_urn (str): The URN identifier for the main Aspect node in the RDF graph. - - Returns: - list: A list of Python objects that represent the Aspect elements of the Aspect model graph. - - Examples: - # Assuming 'graph' is a predefined RDFGraph object and 'aspect_urn' is defined: - aspect_model = create_aspect_model_graph(graph, "urn:example:aspectNode") - print(aspect_model) # This prints the list of Python objects. - - Notes: - It's crucial that the aspect_urn corresponds to a valid Aspect node within the RDF graph; - otherwise, the function may not perform as expected. - """ - self.graph = SAMMGraph(graph=rdf_graph) - loaded_aspect_model = self.graph.to_python(aspect_urn) - - # Add check that loaded_aspect_model is not empty - # Add check that aspect_urn is ref to an Aspect node - - return loaded_aspect_model diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_entity_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_entity_instantiator.py index 8cd4ee4..e5f0c57 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_entity_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_entity_instantiator.py @@ -19,7 +19,7 @@ from esmf_aspect_meta_model_python.base.property import Property from esmf_aspect_meta_model_python.impl.data_types.default_abstract_entity import DefaultAbstractEntity from esmf_aspect_meta_model_python.loader.instantiator.complex_type_instantiator import ComplexTypeInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class AbstractEntityInstantiator(ComplexTypeInstantiator[AbstractEntity]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_property_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_property_instantiator.py index 8ccb13b..228fd5e 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_property_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/abstract_property_instantiator.py @@ -16,7 +16,7 @@ from esmf_aspect_meta_model_python.base.property import Property from esmf_aspect_meta_model_python.impl.default_property import DefaultProperty from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class AbstractPropertyInstantiator(InstantiatorBase[Property]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/aspect_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/aspect_instantiator.py index 25e0f1e..b9e975f 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/aspect_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/aspect_instantiator.py @@ -21,7 +21,7 @@ from esmf_aspect_meta_model_python.base.property import Property from esmf_aspect_meta_model_python.impl.default_aspect import DefaultAspect from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class AspectInstantiator(InstantiatorBase[Aspect]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/collection_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/collection_instantiator.py index fdd8f0b..4d16269 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/collection_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/collection_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.collection.default_collection import DefaultCollection from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class CollectionInstantiator(InstantiatorBase[Collection]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/complex_type_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/complex_type_instantiator.py index 526b995..119df93 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/complex_type_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/complex_type_instantiator.py @@ -17,7 +17,7 @@ from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase, T from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class ComplexTypeInstantiator(InstantiatorBase[T], metaclass=abc.ABCMeta): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/duration_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/duration_instantiator.py index feb6a58..72e394f 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/duration_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/duration_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.quantifiable.default_duration import DefaultDuration from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class DurationInstantiator(InstantiatorBase[Duration]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/either_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/either_instantiator.py index 519e14d..d1ee5c6 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/either_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/either_instantiator.py @@ -14,7 +14,7 @@ from esmf_aspect_meta_model_python.base.either import Either from esmf_aspect_meta_model_python.impl.default_either import DefaultEither from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class EitherInstantiator(InstantiatorBase[Either]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/encoding_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/encoding_constraint_instantiator.py index 4e6545b..a948d42 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/encoding_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/encoding_constraint_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.constraints.default_encoding_constraint import DefaultEncodingConstraint from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class EncodingConstraintInstantiator(InstantiatorBase[EncodingConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/entity_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/entity_instantiator.py index 9c01a2d..d044647 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/entity_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/entity_instantiator.py @@ -18,7 +18,7 @@ from esmf_aspect_meta_model_python.base.property import Property from esmf_aspect_meta_model_python.impl.data_types.default_entity import DefaultEntity from esmf_aspect_meta_model_python.loader.instantiator.complex_type_instantiator import ComplexTypeInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class EntityInstantiator(ComplexTypeInstantiator[Entity]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/enumeration_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/enumeration_instantiator.py index c8d46cd..e9a283d 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/enumeration_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/enumeration_instantiator.py @@ -20,8 +20,8 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class EnumerationInstantiator(InstantiatorBase[Enumeration]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/event_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/event_instantiator.py index c1ce8db..7ea9cfd 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/event_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/event_instantiator.py @@ -17,7 +17,7 @@ from esmf_aspect_meta_model_python.base.event import Event from esmf_aspect_meta_model_python.impl.default_event import DefaultEvent from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class EventInstantiator(InstantiatorBase[Event]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/fixed_point_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/fixed_point_constraint_instantiator.py index 811b72d..749aa45 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/fixed_point_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/fixed_point_constraint_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.constraints.default_fixed_point_constraint import DefaultFixedPointConstraint from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class FixedPointConstraintInstantiator(InstantiatorBase[FixedPointConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/language_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/language_constraint_instantiator.py index 6c9dee1..b1f075b 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/language_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/language_constraint_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.constraints.default_language_constraint import DefaultLanguageConstraint from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class LanguageConstraintInstantiator(InstantiatorBase[LanguageConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/length_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/length_constraint_instantiator.py index e3bdd24..436cd32 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/length_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/length_constraint_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.constraints.default_length_constraint import DefaultLengthConstraint from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class LengthConstraintInstantiator(InstantiatorBase[LengthConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/list_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/list_instantiator.py index bf09cfb..cc748b7 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/list_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/list_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.collection.default_list import DefaultList from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class ListInstantiator(InstantiatorBase[List]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/locale_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/locale_constraint_instantiator.py index 8abc603..5e775ad 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/locale_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/locale_constraint_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.constraints.default_locale_constraint import DefaultLocaleConstraint from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class LocaleConstraintInstantiator(InstantiatorBase[LocaleConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/measurement_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/measurement_instantiator.py index de1a73d..cb34511 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/measurement_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/measurement_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.quantifiable.default_measurement import DefaultMeasurement from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class MeasurementInstantiator(InstantiatorBase[Measurement]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/operation_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/operation_instantiator.py index fca649e..8b77973 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/operation_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/operation_instantiator.py @@ -17,7 +17,7 @@ from esmf_aspect_meta_model_python.base.property import Property from esmf_aspect_meta_model_python.impl.default_operation import DefaultOperation from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class OperationInstantiator(InstantiatorBase[Operation]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/property_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/property_instantiator.py index f095151..2d1b64b 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/property_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/property_instantiator.py @@ -9,15 +9,16 @@ # # SPDX-License-Identifier: MPL-2.0 -import rdflib # type: ignore +from rdflib import BNode, Node, URIRef -from rdflib.term import Node - -from esmf_aspect_meta_model_python.base.characteristics.characteristic import Characteristic from esmf_aspect_meta_model_python.base.property import Property -from esmf_aspect_meta_model_python.impl.default_property import DefaultProperty +from esmf_aspect_meta_model_python.impl.default_property import ( + DefaultBlankProperty, + DefaultProperty, + DefaultPropertyWithExtends, +) from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class PropertyInstantiator(InstantiatorBase[Property]): @@ -49,87 +50,52 @@ def _create_instance(self, element_node: Node) -> Property: :return: an instance of the property """ - if isinstance(element_node, rdflib.URIRef): - return self._create_property_direct_reference(element_node) + property_instance = None + + if isinstance(element_node, URIRef): + property_instance = self._create_property_direct_reference(element_node) - elif isinstance(element_node, rdflib.BNode): + elif isinstance(element_node, BNode): if self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.property)) is not None: - return self._create_property_blank_node(element_node) + property_instance = self._create_property_blank_node(element_node) elif self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.extends)) is not None: - return self._create_property_with_extends(element_node) + property_instance = self._create_property_with_extends(element_node) - raise ValueError("The syntax of the property is not allowed.") + if not property_instance: + raise ValueError("The syntax of the property is not allowed.") - def _create_property_direct_reference(self, element_node: rdflib.URIRef) -> Property: - """The given node is a named node representing the property""" - meta_model_base_attributes = self._get_base_attributes(element_node) + return property_instance - characteristic: Characteristic = self._get_child( - element_node, - self._samm.get_urn(SAMM.characteristic), - required=True, + def _create_property_direct_reference(self, element_node: URIRef) -> Property: + """The given node is a named node representing the property""" + return DefaultProperty( + meta_model_base_attributes=self._get_base_attributes(element_node), + elements_factory=self._model_element_factory, + graph_node=element_node, ) - example_value = self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.example_value)) - - return DefaultProperty(meta_model_base_attributes, characteristic, example_value) - - def _create_property_blank_node(self, element_node: rdflib.BNode) -> Property: + def _create_property_blank_node(self, element_node: BNode) -> Property: """The given node is a blank node holding a reference to the property - and having additional attributes like optional or not_in_payload""" - optional = ( - self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.optional)) is not None - ) - not_in_payload = ( - self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.not_in_payload)) - is not None - ) - payload_name = self._get_child(element_node, self._samm.get_urn(SAMM.payload_name)) - - property_node = self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.property)) - - meta_model_base_attributes = self._get_base_attributes(property_node) # type: ignore - - characteristic: Characteristic = self._get_child( - property_node, # type: ignore - self._samm.get_urn(SAMM.characteristic), - required=True, - ) - - example_value = self._aspect_graph.value( - subject=property_node, - predicate=self._samm.get_urn(SAMM.example_value), + and having additional attributes like optional or not_in_payload.""" + property_node = self._aspect_graph.value( + subject=element_node, + predicate=self._samm.get_urn(SAMM.property), ) - - return DefaultProperty( - meta_model_base_attributes, - characteristic, - example_value, - optional=optional, - not_in_payload=not_in_payload, - payload_name=payload_name, + if not property_node: + raise ValueError(f"Could not find property for the node {element_node}") + + return DefaultBlankProperty( + base_element_node=element_node, + meta_model_base_attributes=self._get_base_attributes(property_node), + elements_factory=self._model_element_factory, + graph_node=property_node, ) - def _create_property_with_extends(self, element_node: rdflib.BNode) -> Property: + def _create_property_with_extends(self, element_node: BNode) -> Property: """The given node is a blank node representing a property extending another property.""" - payload_name = self._get_child(element_node, self._samm.get_urn(SAMM.payload_name)) - extends = self._get_child(element_node, self._samm.get_urn(SAMM.extends), required=True) - - meta_model_base_attributes = self._get_base_attributes(element_node) - - characteristic: Characteristic = self._get_child( - element_node, - self._samm.get_urn(SAMM.characteristic), - required=True, - ) - - example_value = self._aspect_graph.value(subject=element_node, predicate=self._samm.get_urn(SAMM.example_value)) - - return DefaultProperty( - meta_model_base_attributes, - characteristic, - example_value, - extends, - payload_name=payload_name, + return DefaultPropertyWithExtends( + meta_model_base_attributes=self._get_base_attributes(element_node), + elements_factory=self._model_element_factory, + graph_node=element_node, ) diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantifiable_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantifiable_instantiator.py index 2a40b06..4204e41 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantifiable_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantifiable_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.quantifiable.default_quantifiable import DefaultQuantifiable from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class QuantifiableInstantiator(InstantiatorBase[Quantifiable]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantity_kind_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantity_kind_instantiator.py new file mode 100644 index 0000000..960bc4f --- /dev/null +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/quantity_kind_instantiator.py @@ -0,0 +1,23 @@ +# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH +# +# See the AUTHORS file(s) distributed with this work for additional +# information regarding authorship. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 + +from rdflib.term import Node + +from esmf_aspect_meta_model_python.base.quantity_kind import QuantityKind +from esmf_aspect_meta_model_python.impl.default_quantity_kind import DefaultQuantityKind +from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase + + +class QuantityKindInstantiator(InstantiatorBase[QuantityKind]): + def _create_instance(self, element_node: Node) -> QuantityKind: + meta_model_base_attributes = self._get_base_attributes(element_node) + + return DefaultQuantityKind(meta_model_base_attributes) diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/range_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/range_constraint_instantiator.py index c23179b..2c7aada 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/range_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/range_constraint_instantiator.py @@ -16,7 +16,7 @@ from esmf_aspect_meta_model_python.base.contraints.range_constraint import RangeConstraint from esmf_aspect_meta_model_python.impl.constraints.default_range_constraint import DefaultRangeConstraint from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class RangeConstraintInstantiator(InstantiatorBase[RangeConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/regular_expression_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/regular_expression_constraint_instantiator.py index 649b932..4705bb0 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/regular_expression_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/regular_expression_constraint_instantiator.py @@ -17,7 +17,7 @@ ) from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class RegularExpressionConstraintInstantiator(InstantiatorBase[RegularExpressionConstraint]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/set_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/set_instantiator.py index 2757614..1f1e7c9 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/set_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/set_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.collection.default_set import DefaultSet from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class SetInstantiator(InstantiatorBase[Set]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/sorted_set_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/sorted_set_instantiator.py index 84a21dd..628ca44 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/sorted_set_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/sorted_set_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.collection.default_sorted_set import DefaultSortedSet from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class SortedSetInstantiator(InstantiatorBase[SortedSet]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/state_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/state_instantiator.py index ba067d5..0413b27 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/state_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/state_instantiator.py @@ -20,8 +20,8 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.enumeration_instantiator import EnumerationInstantiator from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class StateInstantiator(EnumerationInstantiator): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/structured_value_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/structured_value_instantiator.py index d7f5928..f1eddc4 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/structured_value_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/structured_value_instantiator.py @@ -19,7 +19,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class StructuredValueInstantiator(InstantiatorBase[StructuredValue]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/time_series_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/time_series_instantiator.py index 5a66929..248298c 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/time_series_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/time_series_instantiator.py @@ -15,7 +15,7 @@ from esmf_aspect_meta_model_python.impl.characteristics.collection.default_time_series import DefaultTimeSeries from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TimeSeriesInstantiator(InstantiatorBase[TimeSeries]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/trait_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/trait_instantiator.py index f422de3..15024cd 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/trait_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/trait_instantiator.py @@ -17,7 +17,7 @@ from esmf_aspect_meta_model_python.base.contraints.constraint import Constraint from esmf_aspect_meta_model_python.impl.characteristics.default_trait import DefaultTrait from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TraitInstantiator(InstantiatorBase[Trait]): @@ -29,9 +29,14 @@ def _create_instance(self, element_node: Node) -> Trait: predicate=self._sammc.get_urn(SAMMC.constraint), ) - constraints: List[Constraint] = [ - self._model_element_factory.create_element(constraint_subject) for constraint_subject in constraint_subjects - ] + constraints: List[Constraint] = [] + for constraint_subject in constraint_subjects: + element = self._model_element_factory.create_element(constraint_subject) + if isinstance(element, Constraint): + constraints.append(element) + else: + raise ValueError(f"Trait {element_node} has element {element} that is not a Constraint.") + if not constraints: raise ValueError("Trait must have at least one constraint.") diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/unit_instantiator.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/unit_instantiator.py index 5cbe59d..806f9fb 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/unit_instantiator.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/unit_instantiator.py @@ -20,7 +20,7 @@ from esmf_aspect_meta_model_python.impl.default_unit import DefaultUnit from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class UnitInstantiator(InstantiatorBase[Unit]): diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator_base.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator_base.py index a8136e2..7035bb8 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator_base.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator_base.py @@ -13,15 +13,15 @@ from typing import TYPE_CHECKING, Any, Dict, Generic, Optional, TypeVar -import rdflib # type: ignore +import rdflib from rdflib.term import Node from esmf_aspect_meta_model_python.base.data_types.data_type import DataType from esmf_aspect_meta_model_python.loader.meta_model_base_attributes import MetaModelBaseAttributes from esmf_aspect_meta_model_python.loader.rdf_helper import RdfHelper -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC if TYPE_CHECKING: # Only import the module during type checking and not during runtime. @@ -69,14 +69,14 @@ def get_instance(self, element_node: Node) -> T: Returns: An instance of the model element """ - element_urn = RdfHelper.to_python(element_node) - if self._existing_instances.keys().__contains__(element_urn): - return self._existing_instances[element_urn] + instance = self._existing_instances.get(element_urn) + + if not instance: + instance = self._create_instance(element_node) + self._existing_instances[element_urn] = instance - new_instance = self._create_instance(element_node) - self._existing_instances[element_urn] = new_instance - return new_instance + return instance @abc.abstractmethod def _create_instance(self, element_node: Node) -> T: @@ -170,6 +170,7 @@ def _get_data_type(self, element_node: Node) -> Optional[DataType]: subject=element_node, predicate=self._sammc.get_urn(SAMMC.element_characteristic), ) + if element_characteristic_node: # some characteristics (Collection, List, TimeSeries, etc.) may have # an attribute "element_characteristic". If it is given, then take @@ -178,6 +179,7 @@ def _get_data_type(self, element_node: Node) -> Optional[DataType]: subject=element_characteristic_node, predicate=self._samm.get_urn(SAMM.data_type), ) + if not data_type_node: data_type_node = self._aspect_graph.value( subject=element_characteristic_node, @@ -189,8 +191,10 @@ def _get_data_type(self, element_node: Node) -> Optional[DataType]: predicate=self._samm.get_urn(SAMM.data_type), ) - data_type_element = None + data_type_element: Optional[DataType] = None if data_type_node: - return self._model_element_factory.create_element(data_type_node) + instance = self._model_element_factory.create_element(data_type_node) + if isinstance(instance, DataType): + data_type_element = instance return data_type_element diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/meta_model_base_attributes.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/meta_model_base_attributes.py index 5d49196..6fec2f8 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/meta_model_base_attributes.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/meta_model_base_attributes.py @@ -16,7 +16,7 @@ from rdflib.term import Node -from ..vocabulary.SAMM import SAMM +from ..vocabulary.samm import SAMM from .rdf_helper import RdfHelper diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/model_element_factory.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/model_element_factory.py index d3bf139..5db1fa9 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/model_element_factory.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/model_element_factory.py @@ -12,19 +12,20 @@ import importlib import re -from typing import Dict, Optional, Tuple +from typing import Dict, Optional, Tuple, Type, Union import rdflib from rdflib.term import Node from esmf_aspect_meta_model_python.base.base import Base +from esmf_aspect_meta_model_python.base.data_types.data_type import DataType from esmf_aspect_meta_model_python.loader import instantiator from esmf_aspect_meta_model_python.loader.default_element_cache import DefaultElementCache from esmf_aspect_meta_model_python.loader.instantiator_base import InstantiatorBase, T -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC -from esmf_aspect_meta_model_python.vocabulary.UNIT import UNIT +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC +from esmf_aspect_meta_model_python.vocabulary.unit import UNIT class ModelElementFactory: @@ -71,7 +72,12 @@ def create_all_graph_elements(self, create_nodes: list[Node]): return all_nodes - def create_element(self, element_node: Node): + def _add_to_cache(self, instance): + """Add an instance to the cache.""" + if isinstance(instance, Base): + self._cache.resolve_instance(instance) + + def create_element(self, element_node: Node) -> Union[Type[Base], DataType, Type[DataType]]: """ searches for the right instantiator to create a new instance or find an existing one. @@ -86,17 +92,11 @@ def create_element(self, element_node: Node): an instance of the element with all the child attributes """ element_type = self._get_element_type(element_node) + instantiator_class = self._instantiators.get(element_type, self._create_instantiator(element_type)) + instance = instantiator_class.get_instance(element_node) + self._add_to_cache(instance) - if element_type in self._instantiators: - instantiator = self._instantiators[element_type] - else: - instantiator = self._create_instantiator(element_type) - - instance = instantiator.get_instance(element_node) - if isinstance(instance, Base): - self._cache.resolve_instance(instance) - - return instance # type: ignore + return instance def _get_element_type(self, element_node: Optional[Node]) -> str: """Gets the element type of a node and returns it.""" @@ -120,6 +120,7 @@ def _get_element_type(self, element_node: Optional[Node]) -> str: element_type = self._get_element_type(property_node) else: element_type = "Scalar" + return element_type def _create_instantiator(self, element_type: str) -> InstantiatorBase[T]: diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/samm_graph.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/samm_graph.py index 6dae5a0..3020bf2 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/samm_graph.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/samm_graph.py @@ -9,114 +9,227 @@ # # SPDX-License-Identifier: MPL-2.0 +from pathlib import Path from typing import List, Optional, Union -from rdflib import RDF, Graph, URIRef -from rdflib.graph import Node +from rdflib import RDF, Graph, Node from esmf_aspect_meta_model_python.base.aspect import Aspect from esmf_aspect_meta_model_python.base.base import Base from esmf_aspect_meta_model_python.base.property import Property +from esmf_aspect_meta_model_python.impl.base_impl import BaseImpl from esmf_aspect_meta_model_python.loader.default_element_cache import DefaultElementCache from esmf_aspect_meta_model_python.loader.model_element_factory import ModelElementFactory +from esmf_aspect_meta_model_python.resolver.handler import InputHandler from esmf_aspect_meta_model_python.resolver.meta_model import AspectMetaModelResolver -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class SAMMGraph: - """SAMM graph.""" + """Class representing the SAMM graph and its operations.""" - samm_prefix = "urn:samm:org.eclipse.esmf.samm" + samm_namespace_prefix = "samm" - def __init__( - self, - graph: Graph | None = None, - cache: Union[DefaultElementCache, None] = None, - ): - self._graph = graph if graph else Graph() - self._cache = cache if cache else DefaultElementCache() - self._samm_version = "" + def __init__(self): + self.rdf_graph = Graph() + self.samm_graph = Graph() + self._cache = DefaultElementCache() - self.populate_with_meta_data() + self.samm_version = None + self.aspect = None + self.model_elements = None + self._samm = None + self._reader = None + + def __str__(self) -> str: + """Object string representation.""" + str_data = "SAMMGraph" + if self.samm_version: + str_data += f" v{self.samm_version}" + + return str_data def __repr__(self) -> str: - return repr(self._graph) + """Object representation.""" + return ( + f")>" + ) - def __str__(self) -> str: - return f"SAMM {self._graph}" + def _get_rdf_graph(self, input_data: Union[str, Path], input_type: Optional[str] = None): + """Read the RDF graph from the given input data. + + This method initializes the `InputHandler` with the provided input data and type, + retrieves the reader, and reads the RDF graph into `self.rdf_graph`. - def get_rdf_graph(self) -> Graph: - """Get RDF graph.""" - return self._graph + Args: + input_data (Union[str, Path]): The input data to read the RDF graph from. This can be a file path or a str. + input_type (Optional[str]): The type of the input data. If not provided, the type will be inferred. - def _get_samm_version_from_graph(self): - """Get SAMM version from the graph.""" + Returns: + None + """ + self._reader = InputHandler(input_data, input_type).get_reader() + self.rdf_graph = self._reader.read(input_data) + + def _get_samm_version_from_rdf_graph(self) -> str: + """Extracts the SAMM version from the RDF graph. + + This method searches through the RDF graph namespaces to find a prefix that indicates the SAMM version. + + Returns: + str: The SAMM version as a string extracted from the graph. Returns an empty string if no version + can be conclusively identified. + """ version = "" - for prefix, namespace in self._graph.namespace_manager.namespaces(): - if prefix == "samm": + for prefix, namespace in self.rdf_graph.namespace_manager.namespaces(): + if prefix == self.samm_namespace_prefix: urn_parts = namespace.split(":") version = urn_parts[-1].replace("#", "") return version - def get_samm_version(self): - """Get SAMM version from the graph.""" - version = self._get_samm_version_from_graph() + def _get_samm_version(self): + """Retrieve and set the SAMM version from the RDF graph. - if not version: - raise ValueError("SAMM version not found in the Graph.") - else: - self._samm_version = version + This method extracts the SAMM version from the RDF graph and assigns it to the `samm_version` attribute. + If the SAMM version is not found, it raises a ValueError. + + Raises: + ValueError: If the SAMM version is not found in the RDF graph. + """ + self.samm_version = self._get_samm_version_from_rdf_graph() - def populate_with_meta_data(self): - """Populate RDF graph with SAMM data.""" - if not self._samm_version: - self.get_samm_version() + if not self.samm_version: + raise ValueError( + f"SAMM version number was not found in graph. Could not process RDF graph {self.rdf_graph}." + ) - meta_model_reader = AspectMetaModelResolver() - meta_model_reader.parse(self._graph, self._samm_version) + def _get_samm(self): + """Initialize the SAMM object with the current SAMM version.""" + self._samm = SAMM(self.samm_version) - def get_aspect_nodes_from_graph(self) -> List[Node]: - """Get a list of Aspect nodes from the graph.""" - nodes = [] - samm = SAMM(self._samm_version) + def _get_samm_graph(self): + """Parse SAMM graph base data. + + This method uses the AspectMetaModelResolver to populate samm_graph with info about SAMM elements + based on the current SAMM version. + """ + AspectMetaModelResolver().parse(self.samm_graph, self.samm_version) - # Search for Aspect elements - for subject in self._graph.subjects(predicate=RDF.type, object=samm.get_urn(SAMM.aspect)): # type: ignore - nodes.append(subject) + def parse(self, input_data: Union[str, Path], input_type: Optional[str] = None): + """Parse the RDF graph and initialize SAMM elements. - return nodes + This method reads the RDF graph from the given input data, retrieves and sets the SAMM version, + initializes the SAMM object, and populates the SAMM graph with base data. - def get_base_nodes(self, aspect_urn: URIRef | str = "") -> List[Node]: - """Get a list of base graph elements. + Args: + input_data (Union[str, Path]): The input data to read the RDF graph from. + This can be a file path or a string. + input_type (Optional[str]): The type of the input data. If not provided, the type will be inferred. - :param model_pointer: pointer to the model - :return: List of base graph elements. + Returns: + SAMMGraph: The instance of the SAMMGraph with the parsed data. """ - base_elements: list[Node] = [] + self._get_rdf_graph(input_data, input_type) + self._get_samm_version() + self._get_samm() + self._get_samm_graph() - if aspect_urn: - base_elements += [aspect_urn if isinstance(aspect_urn, URIRef) else URIRef(aspect_urn)] + return self + + def get_aspect_urn(self) -> Node: + """Retrieves the URN pointing to the main aspect node of the RDF graph. + + This method searches the RDF graph for the node with predicate RDF.type and object a SAMM Aspect, + The URN (Uniform Resource Name) of this node is then returned. This method assumes + that the graph contains exactly one main aspect node. + + Returns: + URIRef: reference to the Aspect node. + """ + for subject in self.rdf_graph.subjects(predicate=RDF.type, object=self._samm.get_urn(self._samm.Aspect)): + aspect_urn = subject + break else: - base_elements += self.get_aspect_nodes_from_graph() + raise ValueError("Could not found Aspect node in the RDF graph.") + + return aspect_urn + + def _get_node_from_graph(self, node: Node) -> List[Node]: + """Retrieve nodes from the RDF graph that match the given node type. + + Args: + node (Node): The RDF node type to search for in the graph. + + Returns: + List[Optional[Node]]: A list of nodes from the RDF graph that match the given node type. + """ + return [subject for subject in self.rdf_graph.subjects(predicate=RDF.type, object=node) if subject] + + def get_all_model_elements(self) -> List[Node]: + """Retrieve all SAMM elements from the RDF graph. + + Returns: + List[Node]: A list of nodes representing all model elements in the RDF graph. + + Raises: + ValueError: If no SAMM elements are found in the RDF graph. + """ + model_elements: List[Node] = [] + for element in self._samm.meta_model_elements: + model_elements += self._get_node_from_graph(self._samm.get_urn(element)) + + if not model_elements: + raise ValueError("There are no SAMM elements in the RDF graph.") + + return model_elements + + def load_aspect_model(self) -> Aspect: + """Creates a python object(s) to represent the Aspect model graph. + + This function takes an RDF graph and a URN for an Aspect node and converts it into + a set of structured and connected Python objects that represents the Aspect model graph. The output is a + list of Python objects derived from the RDF graph centered around the specified Aspect node. + + Args: + rdf_graph (RDFGraph): The RDF graph from which to create the model. + aspect_urn (str): The URN identifier for the main Aspect node in the RDF graph. + + Returns: + list: A list of Python objects that represent the Aspect elements of the Aspect model graph. + + Examples: + # Assuming 'graph' is a predefined RDFGraph object and 'aspect_urn' is defined: + aspect_model = create_aspect_model_graph(graph, "urn:example:aspectNode") + print(aspect_model) # This prints the list of Python objects. + + Notes: + It's crucial that the aspect_urn corresponds to a valid Aspect node within the RDF graph; + otherwise, the function may not perform as expected. + """ + if not self.aspect: + aspect_urn = self.get_aspect_urn() + + graph = self.rdf_graph + self.samm_graph + self._reader.prepare_aspect_model(graph) - return base_elements + model_element_factory = ModelElementFactory(self.samm_version, graph, self._cache) + self.aspect = model_element_factory.create_element(aspect_urn) - def to_python(self, aspect_urn: URIRef | str = "") -> List[Aspect]: - """Convert SAMM graph to Python objects.""" - base_nodes = self.get_base_nodes(aspect_urn) - if not base_nodes: - error_message = "Could not found Aspect node in the model" - if aspect_urn: - error_message += f" by the URN {aspect_urn}" + return self.aspect - raise ValueError(error_message) + def load_model_elements(self) -> list[BaseImpl]: + """Creates a python object(s) to represent the Aspect model graph.""" + if self.model_elements is None: + model_elements = self.get_all_model_elements() + graph = self.rdf_graph + self.samm_graph + self._reader.prepare_aspect_model(graph) - model_element_factory = ModelElementFactory(self._samm_version, self._graph, self._cache) - aspect_elements = model_element_factory.create_all_graph_elements(base_nodes) + model_element_factory = ModelElementFactory(self.samm_version, graph, self._cache) + self.model_elements = model_element_factory.create_all_graph_elements(model_elements) - return aspect_elements + return self.model_elements def find_by_name(self, element_name: str) -> list[Base]: """Find a specific model element by name, and returns the found elements diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/base.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/base.py index ceb15a6..0d3971c 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/base.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/base.py @@ -10,6 +10,8 @@ # SPDX-License-Identifier: MPL-2.0 from abc import ABC, abstractmethod +from pathlib import Path +from typing import Union from rdflib import Graph @@ -32,11 +34,11 @@ class ResolverInterface(ABC): def __init__(self): self.graph = Graph() - self.aspect_urn = "" + self.samm_graph = None self.samm_version = "" @abstractmethod - def read(self, input_data: str) -> Graph: + def read(self, input_data: Union[str, Path]): """ Abstract method to read data. @@ -50,17 +52,6 @@ def read(self, input_data: str) -> Graph: Data read from the source, the type of the data can be decided based on the specific subclass. """ - @abstractmethod - def get_aspect_urn(self) -> str: - """ - Abstract method to get an aspect urn. - - Subclasses must implement this method to handle the specific details of getting a URN of aspect. - - Returns: - String with URN of the aspect. - """ - @staticmethod def _validate_samm_version(samm_version: str): """ @@ -79,7 +70,7 @@ def _validate_samm_version(samm_version: str): elif samm_version > SammUnitsGraph.SAMM_VERSION: raise ValueError(f"{samm_version} is not supported SAMM version.") - def _get_samm_version_from_graph(self): + def _get_samm_version_from_graph(self) -> str: """ Extracts the SAMM version from the RDF graph. @@ -119,3 +110,6 @@ def get_samm_version(self) -> str: self.samm_version = version return version + + def prepare_aspect_model(self, graph: Graph): + """Resolve all additional graph elements if needed.""" diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/data_string.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/data_string.py index 4d33b8c..541e903 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/data_string.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/data_string.py @@ -9,16 +9,18 @@ # # SPDX-License-Identifier: MPL-2.0 -from rdflib import RDF, Graph +from pathlib import Path +from typing import Union + +from rdflib import Graph from esmf_aspect_meta_model_python.resolver.base import ResolverInterface -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM class DataStringResolver(ResolverInterface): """String aspect model presenter resolver.""" - def read(self, data_string: str): + def read(self, data_string: Union[str, Path]): """ Parses the provided data string into an RDF graph. @@ -32,22 +34,6 @@ def read(self, data_string: str): RDFGraph: An object representing the RDF graph constructed from the input data. """ self.graph = Graph() - self.graph.parse(data=data_string) + self.graph.parse(data=str(data_string) if isinstance(data_string, Path) else data_string) return self.graph - - def get_aspect_urn(self): - """ - Retrieves the URN pointing to the main aspect node of the RDF graph. - - This method searches the RDF graph for the node with predicate RDF.type and object a SAMM Aspect, - The URN (Uniform Resource Name) of this node is then returned. This method assumes - that the graph contains exactly one main aspect node. - - Returns: - str: The URN of the SAMM aspect node in the RDF graph. - """ - samm = SAMM(self.get_samm_version()) - self.aspect_urn = self.graph.value(predicate=RDF.type, object=samm.get_urn(SAMM.aspect), any=False) - - return self.aspect_urn diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/handler.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/handler.py index d80fd8a..5b573ac 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/handler.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/handler.py @@ -11,10 +11,10 @@ import os -from typing import Optional, Tuple - -from rdflib import Graph +from pathlib import Path +from typing import Optional, Union +from esmf_aspect_meta_model_python.resolver.base import ResolverInterface from esmf_aspect_meta_model_python.resolver.data_string import DataStringResolver from esmf_aspect_meta_model_python.resolver.local_file import LocalFileResolver @@ -43,7 +43,7 @@ class InputHandler: DATA_STRING = "data_string" FILE_PATH_TYPE = "file_path" - def __init__(self, input_data: str, input_type: Optional[str] = None): + def __init__(self, input_data: Union[str, Path], input_type: Optional[str] = None): """ Initializes the InputHandler with provided input data and optional input type. @@ -55,7 +55,7 @@ def __init__(self, input_data: str, input_type: Optional[str] = None): self.input_data = input_data self.input_type = input_type if input_type else self.guess_input_type(input_data) - def get_reader(self): + def get_reader(self) -> ResolverInterface: """ Factory method that returns a reader instance based on the input type. @@ -68,7 +68,7 @@ def get_reader(self): Raises: ValueError: If the 'input_type' does not correspond to a known reader type. """ - reader = None + reader: Optional[ResolverInterface] = None if self.input_type == self.FILE_PATH_TYPE: reader = LocalFileResolver() @@ -80,20 +80,7 @@ def get_reader(self): return reader - def get_rdf_graph(self) -> Tuple[Graph, str]: - """ - Determines the type of input and retrieves the RDF graph from it. - - Returns: - An RDF graph object based on the parsed input data. - """ - reader = self.get_reader() - graph = reader.read(self.input_data) - aspect_urn = reader.get_aspect_urn() - - return graph, aspect_urn - - def guess_input_type(self, input_str: str) -> str: + def guess_input_type(self, input_str: Union[str, Path]) -> str: """ Guesses the type of input based on its content and configuration. @@ -103,6 +90,9 @@ def guess_input_type(self, input_str: str) -> str: Returns: str: Guessed input type ('file' or 'string'). """ + if isinstance(input_str, Path): + input_str = str(input_str) + if not self.contains_newline(input_str) and os.path.isfile(input_str): return self.FILE_PATH_TYPE return self.DATA_STRING diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/local_file.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/local_file.py index 1b69506..e4dadfe 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/local_file.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/local_file.py @@ -11,24 +11,25 @@ from os.path import exists, join from pathlib import Path -from typing import Dict, List, Optional, Tuple +from typing import Dict, List, Optional, Tuple, Union -from rdflib import RDF, Graph +from rdflib import Graph from esmf_aspect_meta_model_python.resolver.base import ResolverInterface -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM class LocalFileResolver(ResolverInterface): """Local storage aspect model file resolver.""" + samm_namespace_prefix = "samm" + def __init__(self): super().__init__() self.file_path = None @staticmethod - def validate_file(file_path: str): + def validate_file(file_path: Union[str, Path]): """Validate a SAMM file. :param file_path: path to the file @@ -36,6 +37,27 @@ def validate_file(file_path: str): if not exists(file_path): raise FileNotFoundError(f"Could not find a file {file_path}") + def read(self, file_path: Union[str, Path]) -> Graph: + """ + Read an RDF graph stored in the local file. + + This method takes a string with a path to the file with RDF graph description in a serialization format + (such as Turtle, XML, or JSON-LD) and converts it into an RDF graph object. + + Args: + file_path (str): A string with path to the file with RDF graph description. + + Returns: + RDFGraph: An object representing the RDF graph constructed from the input data. + """ + self.file_path = file_path + + self.validate_file(self.file_path) + self.graph = Graph() + self.graph.parse(self.file_path) + + return self.graph + @staticmethod def _parse_namespace(prefix_namespace: str) -> Tuple[Optional[str], Optional[str]]: """Parse the prefix namespace string. @@ -139,59 +161,13 @@ def _get_dependency_files( return file_dependencies - def parse_namespaces(self, aspect_file_path: str): + def prepare_aspect_model(self, graph: Graph): """Parse namespaces from the Aspect model. - :param aspect_graph: RDF Graph - :param aspect_file_path: path to the Aspect model file + :param graph: RDF Graph """ + self.graph = graph file_dependencies: Dict[str, List[str]] = {} folder_dependencies: Dict[str, List[str]] = {} - self._get_dependency_files(file_dependencies, folder_dependencies, aspect_file_path) - - def read(self, file_path: str) -> Graph: - """ - Read an RDF graph stored in the local file. - - This method takes a string with a path to the file with RDF graph description in a serialization format - (such as Turtle, XML, or JSON-LD) and converts it into an RDF graph object. - - Args: - file_path (str): A string with path to the file with RDF graph description. - - Returns: - RDFGraph: An object representing the RDF graph constructed from the input data. - """ - self.file_path = file_path - self.graph.parse(file_path) - self._find_aspect_urn() - self.parse_namespaces(self.file_path) - - return self.graph - - def _find_aspect_urn(self): - """ - Find a main aspect node in the RDF graph. - - Searches the node with RDF.type predicate and SAMM Aspect obkect type. - """ - samm = SAMM(self.get_samm_version()) - file_name = Path(self.file_path).stem - for aspect in self.graph.subjects(predicate=RDF.type, object=samm.get_urn(SAMM.aspect)): - if str(aspect).split("#")[-1] == file_name: - self.aspect_urn = aspect - break - - def get_aspect_urn(self): - """ - Retrieves the URN pointing to the main aspect node of the RDF graph. - - This method searches the RDF graph for the node with predicate RDF.type and object a SAMM Aspect, - The URN (Uniform Resource Name) of this node is then returned. This method assumes - that the graph contains exactly one main aspect node. - - Returns: - str: The URN of the SAMM aspect node in the RDF graph. - """ - return self.aspect_urn + self._get_dependency_files(file_dependencies, folder_dependencies, self.file_path) diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/meta_model.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/meta_model.py index a1f67e5..3e38ba7 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/meta_model.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/resolver/meta_model.py @@ -16,6 +16,8 @@ from rdflib import Graph +from esmf_aspect_meta_model_python.vocabulary import SAMM, SAMMC, SAMME, UNIT + class AspectMetaModelResolver: """SAMM meta-model resolver class.""" @@ -64,3 +66,13 @@ def parse(self, rdf_graph: Graph, meta_model_version: str): for file_path in self._get_samm_files_path(meta_model_version): self.validate_file(file_path) rdf_graph.parse(file_path, format="turtle") + + @staticmethod + def get_samm_prefixes(meta_model_version: str) -> list[str]: + """Get all SAMM prefix values.""" + return [ + SAMM.samm_prefix[:-1], + SAMMC.sammc_prefix[:-1], + SAMME.samme_prefix[:-1], + UNIT.samm_prefix[:-1], + ] diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMM.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMM.py deleted file mode 100644 index 5e0ced8..0000000 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMM.py +++ /dev/null @@ -1,75 +0,0 @@ -# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH -# -# See the AUTHORS file(s) distributed with this work for additional -# information regarding authorship. -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at https://mozilla.org/MPL/2.0/. -# -# SPDX-License-Identifier: MPL-2.0 - -import rdflib # type: ignore - -from .namespace import Namespace - - -class SAMM(Namespace): - __samm_prefix = "urn:samm:org.eclipse.esmf.samm:meta-model:" - - aspect = "Aspect" - abstract_entity = "AbstractEntity" - baseCharacteristic = "baseCharacteristic" - Characteristic = "Characteristic" - characteristic = "characteristic" - commonCode = "commonCode" - Constraint = "Constraint" - conversionFactor = "conversionFactor" - curie = "curie" - data_type = "dataType" - description = "description" - entity = "Entity" - Event = "Event" - events = "events" - example_value = "exampleValue" - extends = "extends" - input = "input" - listType = "listType" - name = "name" - not_in_payload = "notInPayload" - numericConversionFactor = "numericConversionFactor" - Operation = "Operation" - operations = "operations" - optional = "optional" - output = "output" - parameters = "parameters" - payload_name = "payloadName" - preferred_name = "preferredName" - properties = "properties" - property = "property" - Property = "Property" - quantityKind = "quantityKind" - QuantityKind = "QuantityKind" - referenceUnit = "referenceUnit" - see = "see" - symbol = "symbol" - Unit = "Unit" - value = "value" - common_code = "commonCode" - conversion_factor = "conversionFactor" - numeric_conversion_factor = "numericConversionFactor" - quantity_kind = "quantityKind" - reference_unit = "referenceUnit" - unit = "unit" - - def __init__(self, meta_model_version: str): - self.__meta_model_version: str = meta_model_version - - def get_urn(self, element_type: str) -> rdflib.URIRef: - """returns the URN string of the given element type. - Example: get_urn(SAMM.characteristic) -> "urn:samm:org.eclipse.esmf.samm:meta-model:1.0.0#characteristic" - """ - - samm_prefix = SAMM.__samm_prefix - meta_model_version = self.__meta_model_version - return rdflib.URIRef(f"{samm_prefix}{meta_model_version}#{element_type}") diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMMC.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMMC.py deleted file mode 100644 index 9c3e6ee..0000000 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMMC.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH -# -# See the AUTHORS file(s) distributed with this work for additional -# information regarding authorship. -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at https://mozilla.org/MPL/2.0/. -# -# SPDX-License-Identifier: MPL-2.0 - -from typing import List - -import rdflib # type: ignore - -from esmf_aspect_meta_model_python.vocabulary.namespace import Namespace - - -class SAMMC(Namespace): - __sammc_prefix = "urn:samm:org.eclipse.esmf.samm:characteristic:" - - base_characteristic = "baseCharacteristic" - code = "Code" - collection = "Collection" - constraint = "constraint" - deconstruction_rule = "deconstructionRule" - default_value = "defaultValue" - duration = "Duration" - either = "Either" - element_characteristic = "elementCharacteristic" - elements = "elements" - encoding_constraint = "EncodingConstraint" - enumeration = "Enumeration" - fixed_point_constraint = "FixedPointConstraint" - integer = "integer" - language_code = "languageCode" - language_constraint = "LanguageConstraint" - left = "left" - length_constraint = "LengthConstraint" - list = "List" - locale_code = "localeCode" - locale_constraint = "LocaleConstraint" - lower_bound_definition = "lowerBoundDefinition" - max_value = "maxValue" - measurement = "Measurement" - min_value = "minValue" - quantifiable = "Quantifiable" - range_constraint = "RangeConstraint" - regular_expression_constraint = "RegularExpressionConstraint" - right = "right" - scale = "scale" - set = "Set" - single_entity = "SingleEntity" - sorted_set = "SortedSet" - state = "State" - structured_value = "StructuredValue" - time_series = "TimeSeries" - trait = "Trait" - unit = "unit" - upper_bound_definition = "upperBoundDefinition" - values = "values" - - def __init__(self, meta_model_version: str): - self.__meta_model_version: str = meta_model_version - - def get_urn(self, element_type: str) -> rdflib.URIRef: - """returns the URN string of the given element type. - Example: get_urn(SAMM.scale) -> "urn:samm:org.eclipse.esmf.samm:characteristic:1.0.0#scale" - """ - - sammc_prefix = self.__sammc_prefix - meta_model_version = self.__meta_model_version - return rdflib.URIRef(f"{sammc_prefix}{meta_model_version}#{element_type}") - - def collections_urns(self) -> List[rdflib.URIRef]: - return [ - self.get_urn(SAMMC.collection), - self.get_urn(SAMMC.set), - self.get_urn(SAMMC.sorted_set), - self.get_urn(SAMMC.list), - self.get_urn(SAMMC.time_series), - ] diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/__init__.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/__init__.py index 4426c4b..5fd311a 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/__init__.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/__init__.py @@ -8,3 +8,9 @@ # file, You can obtain one at https://mozilla.org/MPL/2.0/. # # SPDX-License-Identifier: MPL-2.0 + + +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC +from esmf_aspect_meta_model_python.vocabulary.samme import SAMME +from esmf_aspect_meta_model_python.vocabulary.unit import UNIT diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/constants.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/constants.py new file mode 100644 index 0000000..b7cb284 --- /dev/null +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/constants.py @@ -0,0 +1,201 @@ +class MetaModelElements: + # Meta Model Elements + AbstractEntity = "AbstractEntity" + AbstractProperty = "AbstractProperty" + Aspect = "Aspect" + Boolean = "Boolean" + Characteristic = "Characteristic" + Code = "Code" + Collection = "Collection" + Constraint = "Constraint" + Duration = "Duration" + Either = "Either" + EncodingConstraint = "EncodingConstraint" + Entity = "Entity" + Enumeration = "Enumeration" + Event = "Event" + FileResource = "FileResource" + FixedPointConstraint = "FixedPointConstraint" + Language = "Language" + LanguageConstraint = "LanguageConstraint" + LengthConstraint = "LengthConstraint" + List = "List" + Locale = "Locale" + LocaleConstraint = "LocaleConstraint" + Measurement = "Measurement" + MimeType = "MimeType" + MultiLanguageText = "MultiLanguageText" + Operation = "Operation" + Point3d = "Point3d" + Property = "Property" + Quantifiable = "Quantifiable" + QuantityKind = "QuantityKind" + RangeConstraint = "RangeConstraint" + RegularExpressionConstraint = "RegularExpressionConstraint" + ResourcePath = "ResourcePath" + Set = "Set" + SingleEntity = "SingleEntity" + SortedSet = "SortedSet" + State = "State" + StructuredValue = "StructuredValue" + Text = "Text" + TimeSeries = "TimeSeries" + TimeSeriesEntity = "TimeSeriesEntity" + Timestamp = "Timestamp" + Trait = "Trait" + Unit = "Unit" + UnitReference = "UnitReference" + + meta_model_elements = ( + AbstractEntity, + Aspect, + Characteristic, + Constraint, + Entity, + Event, + Operation, + Property, + Quantifiable, + QuantityKind, + Trait, + Unit, + ) + + +class CharacteristicElements: + # Characteristic Elements + Code = "Code" + Collection = "Collection" + Duration = "Duration" + Either = "Either" + Encoding_constraint = "EncodingConstraint" + Enumeration = "Enumeration" + Fixed_point_constraint = "FixedPointConstraint" + Language_constraint = "LanguageConstraint" + Length_constraint = "LengthConstraint" + Locale_constraint = "LocaleConstraint" + Measurement = "Measurement" + Quantifiable = "Quantifiable" + Range_constraint = "RangeConstraint" + Regular_expression_constraint = "RegularExpressionConstraint" + List = "List" + Set = "Set" + Single_entity = "SingleEntity" + Sorted_set = "SortedSet" + State = "State" + Structured_value = "StructuredValue" + Time_series = "TimeSeries" + Trait = "Trait" + + collections = ( + Collection, + List, + Set, + Sorted_set, + Time_series, + ) + + +class SAMMEElements: + FileResource = "FileResource" + Point3d = "Point3d" + TimeSeriesEntity = "TimeSeriesEntity" + + +class MetaModelElementAttributes: + # Attributes of Meta Model Elements + allow_duplicates = "allowDuplicates" + AT_LEAST = "AT_LEAST" + AT_MOST = "AT_MOST" + base_characteristic = "baseCharacteristic" + characteristic = "characteristic" + common_code = "commonCode" + constraint = "constraint" + conversion_factor = "conversionFactor" + curie = "curie" + data_type = "dataType" + deconstruction_rule = "deconstructionRule" + default_value = "defaultValue" + description = "description" + element_characteristic = "elementCharacteristic" + elements = "elements" + events = "events" + example_value = "exampleValue" + extends = "extends" + GREATER_THAN = "GREATER_THAN" + input = "input" + integer = "integer" + language_code = "languageCode" + left = "left" + LESS_THAN = "LESS_THAN" + list_type = "listType" + locale_code = "localeCode" + lower_bound_definition = "lowerBoundDefinition" + max_value = "maxValue" + mime_type = "mimeType" + min_value = "minValue" + name = "name" + not_in_payload = "notInPayload" + numeric_conversion_factor = "numericConversionFactor" + operations = "operations" + optional = "optional" + ordered = "ordered" + output = "output" + parameters = "parameters" + payload_name = "payloadName" + preferred_name = "preferredName" + properties = "properties" + quantity_kind = "quantityKind" + reference_unit = "referenceUnit" + resource = "resource" + right = "right" + property = "property" + scale = "scale" + see = "see" + symbol = "symbol" + timestamp = "timestamp" + unit = "unit" + upper_bound_definition = "upperBoundDefinition" + value = "value" + values = "values" + x = "x" + y = "y" + z = "z" + TimeSeriesEntity = "TimeSeriesEntity" + Point3d = "Point3d" + + +class CharacteristicElementAttributes: + # Attributes of Characteristic Elements + allow_duplicates = "allowDuplicates" + AT_LEAST = "AT_LEAST" + AT_MOST = "AT_MOST" + base_characteristic = "baseCharacteristic" + constraint = "constraint" + deconstruction_rule = "deconstructionRule" + default_value = "defaultValue" + element_characteristic = "elementCharacteristic" + elements = "elements" + GREATER_THAN = "GREATER_THAN" + integer = "integer" + language_code = "languageCode" + left = "left" + LESS_THAN = "LESS_THAN" + locale_code = "localeCode" + lower_bound_definition = "lowerBoundDefinition" + max_value = "maxValue" + min_value = "minValue" + ordered = "ordered" + right = "right" + scale = "scale" + unit = "unit" + upper_bound_definition = "upperBoundDefinition" + values = "values" + + +class SAMMEElementAttributes: + timestamp = "timestamp" + value = "value" + x = "x" + y = "y" + z = "z" diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/samm.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/samm.py new file mode 100644 index 0000000..26b5da3 --- /dev/null +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/samm.py @@ -0,0 +1,29 @@ +# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH +# +# See the AUTHORS file(s) distributed with this work for additional +# information regarding authorship. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 + +import rdflib # type: ignore + +from esmf_aspect_meta_model_python.vocabulary.constants import MetaModelElementAttributes, MetaModelElements +from esmf_aspect_meta_model_python.vocabulary.namespace import Namespace + + +class SAMM(Namespace, MetaModelElements, MetaModelElementAttributes): + samm_prefix = "urn:samm:org.eclipse.esmf.samm:meta-model:" + + # Constants listed in the constant classes + def __init__(self, meta_model_version: str): + self.meta_model_version: str = meta_model_version + + def get_urn(self, element_type: str) -> rdflib.URIRef: + """returns the URN string of the given element type. + Example: get_urn(SAMM.characteristic) -> "urn:samm:org.eclipse.esmf.samm:meta-model:1.0.0#characteristic" + """ + return rdflib.URIRef(f"{SAMM.samm_prefix}{self.meta_model_version}#{element_type}") diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/sammc.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/sammc.py new file mode 100644 index 0000000..31aa18e --- /dev/null +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/sammc.py @@ -0,0 +1,36 @@ +# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH +# +# See the AUTHORS file(s) distributed with this work for additional +# information regarding authorship. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. +# +# SPDX-License-Identifier: MPL-2.0 + +from typing import List, Optional + +from rdflib import URIRef + +from esmf_aspect_meta_model_python.vocabulary.constants import CharacteristicElementAttributes, CharacteristicElements +from esmf_aspect_meta_model_python.vocabulary.namespace import Namespace + + +class SAMMC(Namespace, CharacteristicElements, CharacteristicElementAttributes): + sammc_prefix = "urn:samm:org.eclipse.esmf.samm:characteristic:" + + # Constants listed in the constant classes + def __init__(self, meta_model_version: str): + self.meta_model_version: str = meta_model_version + + def get_urn(self, element_type: str) -> URIRef: + """Get urn of the given element type. + + Example: get_urn(SAMM.scale) -> "urn:samm:org.eclipse.esmf.samm:characteristic:1.0.0#scale" + """ + return URIRef(f"{SAMMC.sammc_prefix}{self.meta_model_version}#{element_type}") + + def collections_urns(self) -> List[Optional[URIRef]]: + """Get a list of urns of collection characteristics.""" + return [self.get_urn(element) for element in SAMMC.collections] diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMME.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/samme.py similarity index 65% rename from core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMME.py rename to core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/samme.py index 5cbb3e1..12333eb 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/SAMME.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/samme.py @@ -11,24 +11,19 @@ import rdflib # type: ignore +from esmf_aspect_meta_model_python.vocabulary.constants import SAMMEElementAttributes, SAMMEElements from esmf_aspect_meta_model_python.vocabulary.namespace import Namespace -class SAMME(Namespace): - __samme_prefix = "urn:samme:org.eclipse.esmf.samm:meta-model:" - TimeSeriesEntity = "TimeSeriesEntity" - Point3d = "Point3d" - timestamp = "timestamp" - value = "value" - x = "x" - y = "y" - z = "z" +class SAMME(Namespace, SAMMEElements, SAMMEElementAttributes): + samme_prefix = "urn:samme:org.eclipse.esmf.samm:meta-model:" + # Constants listed in the constant classes def __init__(self, meta_model_version: str): - self.__meta_model_version: str = meta_model_version + self.meta_model_version: str = meta_model_version def get_urn(self, element_type: str) -> rdflib.URIRef: """returns the URN string of the given element type. Example: get_urn(SAMM.characteristic) -> "urn:samm:org.eclipse.esmf.samm:meta-model:1.0.0#characteristic" """ - return rdflib.URIRef(f"{self.__samme_prefix}{self.__meta_model_version}#{element_type}") + return rdflib.URIRef(f"{SAMME.samme_prefix}{self.meta_model_version}#{element_type}") diff --git a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/UNIT.py b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/unit.py similarity index 79% rename from core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/UNIT.py rename to core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/unit.py index ac5bff3..33e8f8e 100644 --- a/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/UNIT.py +++ b/core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/vocabulary/unit.py @@ -15,13 +15,13 @@ class UNIT(Namespace): - __samm_prefix = "urn:samm:org.eclipse.esmf.samm:unit:" + samm_prefix = "urn:samm:org.eclipse.esmf.samm:unit:" def __init__(self, meta_model_version: str): - self.__meta_model_version: str = meta_model_version + self.meta_model_version: str = meta_model_version def get_urn(self, element_type: str) -> rdflib.URIRef: """returns the URN string of the given element type. Example: get_urn(SAMM.reference_unit) -> "urn:samm:org.eclipse.esmf.samm:unit:2.0.0#referenceUnit" """ - return rdflib.URIRef(f"{UNIT.__samm_prefix}{self.__meta_model_version}#{element_type}") + return rdflib.URIRef(f"{UNIT.samm_prefix}{self.meta_model_version}#{element_type}") diff --git a/core/esmf-aspect-meta-model-python/pyproject.toml b/core/esmf-aspect-meta-model-python/pyproject.toml index 740f6ea..da359d8 100644 --- a/core/esmf-aspect-meta-model-python/pyproject.toml +++ b/core/esmf-aspect-meta-model-python/pyproject.toml @@ -27,10 +27,10 @@ documentation = "https://eclipse-esmf.github.io/python-sdk-guide/index.html" [tool.poetry.dependencies] python = "^3.10" keyboard = "^0.13" -rdflib = "^6.2.0" requests = "^2.28.1" tox = "^4.5.2" zipfile37 = "^0.1.3" +rdflib = "^7.1.3" [tool.poetry.dev-dependencies] black = "^23.3.0" diff --git a/core/esmf-aspect-meta-model-python/samm_elements/__init__.py b/core/esmf-aspect-meta-model-python/samm_elements/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py b/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py index e065f4e..5b7d417 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/java_models/test_loading_aspects.py @@ -7,8 +7,7 @@ from os.path import exists, join from pathlib import Path -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import SAMMGraph from scripts.constants import TestModelConstants from scripts.download_test_models import download_test_models @@ -39,7 +38,7 @@ def get_test_files(): return test_model_files -def load_test_models(): +def load_aspect_test_models(): """Test for loading Aspect models.""" test_files = get_test_files() if not test_files: @@ -66,12 +65,11 @@ def load_test_models(): } try: - handler = InputHandler(str(test_file)) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - if not model_elements: - raise Exception("No elements loaded") + samm_graph = SAMMGraph() + samm_graph.parse(test_file_path) + aspect = samm_graph.load_aspect_model() + if not aspect: + raise Exception("Aspect has not been loaded") except Exception as error: data["error"] = str(error) data["status"] = "exception" @@ -85,12 +83,12 @@ def load_test_models(): return result -def run_test_loading(): +def run_aspect_load_test(): """Run loading of all test Aspect models.""" - report = load_test_models() + report = load_aspect_test_models() base_path = Path(__file__).parent.absolute() - with open(join(base_path, "loading_models_test_report.csv"), "w", newline="") as csvfile: + with open(join(base_path, "test_java_models_aspect_load_report.csv"), "w", newline="") as csvfile: fieldnames = ["folder_name", "file_name", "status", "error"] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) @@ -99,5 +97,65 @@ def run_test_loading(): writer.writerow(row) -if __name__ == "__main__": - run_test_loading() +def load_model_elements_test_models(): + """Test for loading Aspect models.""" + test_files = get_test_files() + if not test_files: + check_resources_folder() + test_files = get_test_files() + + result = [] + all_test_files = len(test_files) + i = 0 + step = 10 + print("Loading test Aspect models...") + + for test_file in test_files: + i += 1 + if i % step == 0: + print(f"{i}/{all_test_files}") + + test_file_path = Path(test_file) + data = { + "file_name": test_file_path.name, + "folder_name": join(test_file_path.parents[1].name, test_file_path.parents[0].name), + "status": "initializing", + "error": None, + } + + try: + samm_graph = SAMMGraph() + samm_graph.parse(test_file_path) + elements = samm_graph.load_model_elements() + if not elements: + raise Exception("Aspect model elements has not been loaded") + except Exception as error: + data["error"] = str(error) + data["status"] = "exception" + else: + data["status"] = "success" + + result.append(data) + + print(f"{i}/{all_test_files}") + + return result + + +def run_model_elements_load_test(): + """Run loading of all test Aspect models.""" + report = load_model_elements_test_models() + + base_path = Path(__file__).parent.absolute() + with open(join(base_path, "test_java_models_elements_load_report.csv"), "w", newline="") as csvfile: + fieldnames = ["folder_name", "file_name", "status", "error"] + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + + writer.writeheader() + for row in report: + writer.writerow(row) + + +def test_loading_aspects(): + run_aspect_load_test() + run_model_elements_load_test() diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_aspect_functionality.py b/core/esmf-aspect-meta-model-python/tests/integration/test_aspect_functionality.py index ef2f5ed..c449512 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_aspect_functionality.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_aspect_functionality.py @@ -12,32 +12,27 @@ from os import getcwd from pathlib import Path -from esmf_aspect_meta_model_python import BaseImpl -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import BaseImpl, SAMMGraph RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.general/2.1.0") def test_get_access_path(): file_path = RESOURCE_PATH / "Movement.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] - graph = loader.graph - path = graph.determine_element_access_path(aspect.properties[2].data_type.properties[2]) # type: ignore + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + path = samm_graph.determine_element_access_path(aspect.properties[2].data_type.properties[2]) assert path[0][0] == "position" assert path[0][1] == "z" - path = graph.determine_access_path("y") + path = samm_graph.determine_access_path("y") assert path[0][0] == "position" assert path[0][1] == "y" - path = graph.determine_access_path("x") + path = samm_graph.determine_access_path("x") assert path[0][0] == "position" assert path[0][1] == "x" @@ -45,30 +40,25 @@ def test_get_access_path(): def test_get_access_path_input_property(): file_path = RESOURCE_PATH / "AspectWithOperationNoOutput.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] - graph = loader.graph - path = graph.determine_element_access_path(aspect.operations[0].input_properties[0]) + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + path = samm_graph.determine_element_access_path(aspect.operations[0].input_properties[0]) assert path[0][0] == "input" - path = graph.determine_element_access_path(aspect.operations[1].input_properties[0]) + path = samm_graph.determine_element_access_path(aspect.operations[1].input_properties[0]) assert path[0][0] == "input" def test_find_properties_by_name() -> None: file_path = RESOURCE_PATH / "AspectWithProperties.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + _ = samm_graph.load_aspect_model() - result = graph.find_by_name("testPropertyOne") + result = samm_graph.find_by_name("testPropertyOne") assert result is not None assert len(result) == 1 assert isinstance(result[0], BaseImpl) @@ -78,7 +68,7 @@ def test_find_properties_by_name() -> None: assert len(result[0].see) == 0 assert len(result[0].descriptions) == 0 - result = graph.find_by_name("testPropertyTwo") + result = samm_graph.find_by_name("testPropertyTwo") assert result is not None assert len(result) == 1 assert isinstance(result[0], BaseImpl) @@ -88,18 +78,17 @@ def test_find_properties_by_name() -> None: assert len(result[0].see) == 0 assert len(result[0].descriptions) == 0 - result = graph.find_by_name("Unknown") + result = samm_graph.find_by_name("Unknown") assert len(result) == 0 def test_find_property_chaticaristic_by_name() -> None: file_path = RESOURCE_PATH / "AspectWithPropertyWithAllBaseAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph - result = graph.find_by_name("BooleanTestCharacteristic") + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + _ = aspect.properties[0].characteristic + result = samm_graph.find_by_name("BooleanTestCharacteristic") assert result is not None assert len(result) == 1 @@ -113,12 +102,10 @@ def test_find_property_chaticaristic_by_name() -> None: def test_find_properties_by_urn() -> None: file_path = RESOURCE_PATH / "AspectWithProperties.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph - element = graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyOne") + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + _ = samm_graph.load_aspect_model() + element = samm_graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyOne") assert element is not None assert isinstance(element, BaseImpl) @@ -128,7 +115,7 @@ def test_find_properties_by_urn() -> None: assert len(element.see) == 0 assert len(element.descriptions) == 0 - element = graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyTwo") + element = samm_graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyTwo") assert element is not None assert isinstance(element, BaseImpl) assert element.name == "testPropertyTwo" @@ -137,18 +124,17 @@ def test_find_properties_by_urn() -> None: assert len(element.see) == 0 assert len(element.descriptions) == 0 - element = graph.find_by_urn("Unknown") + element = samm_graph.find_by_urn("Unknown") assert element is None -def test_find_property_chaticaristic_by_urn() -> None: +def test_find_property_characteristic_by_urn() -> None: file_path = RESOURCE_PATH / "AspectWithPropertyWithAllBaseAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph - element = graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#BooleanTestCharacteristic") + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + _ = aspect.properties[0].characteristic + element = samm_graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#BooleanTestCharacteristic") assert element is not None assert isinstance(element, BaseImpl) @@ -158,5 +144,5 @@ def test_find_property_chaticaristic_by_urn() -> None: assert len(element.see) == 0 assert len(element.descriptions) == 0 - element = graph.find_by_urn("Unknown") + element = samm_graph.find_by_urn("Unknown") assert element is None diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_characteristics.py b/core/esmf-aspect-meta-model-python/tests/integration/test_characteristics.py index 3ce0240..e34759a 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_characteristics.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_characteristics.py @@ -20,22 +20,19 @@ Enumeration, Measurement, Quantifiable, + SAMMGraph, State, StructuredValue, ) -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.characteristics/2.1.0") def test_loading_aspect_with_collection(): file_path = RESOURCE_PATH / "AspectWithCollection.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.name == "AspectWithCollection" assert aspect.get_preferred_name("en") == "Test Aspect" @@ -65,35 +62,36 @@ def test_loading_aspect_with_collection(): def test_loading_aspect_with_set(): file_path = RESOURCE_PATH / "AspectWithSet.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - aspect = loader.load_aspect_model(rdf_graph, aspect_urn) # noqa: F841 + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + + assert aspect is not None def test_loading_aspect_with_sorted_set(): file_path = RESOURCE_PATH / "AspectWithSortedSet.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - aspect = loader.load_aspect_model(rdf_graph, aspect_urn) # noqa: F841 + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + + assert aspect is not None def test_loading_aspect_with_list(): file_path = RESOURCE_PATH / "AspectWithList.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - aspect = loader.load_aspect_model(rdf_graph, aspect_urn) # noqa: F841 + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + + assert aspect is not None def test_loading_aspect_with_collection_with_element_characteristic(): file_path = RESOURCE_PATH / "AspectWithCollectionWithElementCharacteristic.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] collection_characteristic = first_property.characteristic @@ -112,11 +110,9 @@ def test_loading_aspect_with_collection_with_element_characteristic(): def test_loading_aspect_with_simple_enum(): file_path = RESOURCE_PATH / "AspectWithSimpleEnum.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] enum_characteristic = first_property.characteristic @@ -133,11 +129,9 @@ def test_loading_aspect_with_simple_enum(): def test_loading_aspect_with_simple_state(): print(RESOURCE_PATH) file_path = RESOURCE_PATH / "AspectWithState.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] state_characteristic = first_property.characteristic @@ -155,11 +149,9 @@ def test_loading_aspect_with_simple_state(): def test_loading_aspect_with_quantifiable(): file_path = RESOURCE_PATH / "AspectWithQuantifiableAndUnit.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] quantifiable_characteristic = first_property.characteristic @@ -181,11 +173,9 @@ def test_loading_aspect_with_quantifiable(): def test_loading_aspect_with_duration(): file_path = RESOURCE_PATH / "AspectWithDuration.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] duration_characteristic = first_property.characteristic @@ -197,11 +187,9 @@ def test_loading_aspect_with_duration(): def test_loading_aspect_with_measurement(): file_path = RESOURCE_PATH / "AspectWithMeasurement.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] measurement_characteristic = first_property.characteristic @@ -213,11 +201,9 @@ def test_loading_aspect_with_measurement(): def test_loading_aspect_with_structured_value(): file_path = RESOURCE_PATH / "AspectWithStructuredValue.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] structured_value_characteristic = first_property.characteristic @@ -242,11 +228,9 @@ def test_loading_aspect_with_structured_value(): def test_loading_aspect_with_code(): file_path = RESOURCE_PATH / "AspectWithCode.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] code_characteristic = first_property.characteristic @@ -256,11 +240,9 @@ def test_loading_aspect_with_code(): def test_loading_aspect_with_blank_node() -> None: file_path = RESOURCE_PATH / "AspectWithBlankNode.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] assert first_property.name == "list" diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_constraints.py b/core/esmf-aspect-meta-model-python/tests/integration/test_constraints.py index 6456aa4..e66dd47 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_constraints.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_constraints.py @@ -13,7 +13,6 @@ from pathlib import Path from esmf_aspect_meta_model_python import ( - AspectLoader, BoundDefinition, EncodingConstraint, FixedPointConstraint, @@ -24,20 +23,18 @@ Quantifiable, RangeConstraint, RegularExpressionConstraint, + SAMMGraph, Trait, ) -from esmf_aspect_meta_model_python.resolver.handler import InputHandler RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.constraints/2.1.0") def test_loading_aspect_with_constrained_collection(): file_path = RESOURCE_PATH / "AspectWithConstrainedCollection.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -59,11 +56,9 @@ def test_loading_aspect_with_constrained_collection(): def test_loading_aspect_with_range_constraint(): file_path = RESOURCE_PATH / "AspectWithRangeConstraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -86,11 +81,9 @@ def test_loading_aspect_with_range_constraint(): def test_loading_aspect_with_multiple_constraints(): file_path = RESOURCE_PATH / "AspectWithMultipleConstraints.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -116,11 +109,9 @@ def test_loading_aspect_with_multiple_constraints(): def test_loading_aspect_with_multiple_one_value_constraints(): file_path = RESOURCE_PATH / "AspectWithMultipleOneValueConstraints.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -153,11 +144,9 @@ def test_loading_aspect_with_multiple_one_value_constraints(): def test_loading_aspect_with_range_constraint_incl_bound_definition(): file_path = RESOURCE_PATH / "AspectWithRangeConstraintInclBoundDefinitionProperties.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -181,11 +170,9 @@ def test_loading_aspect_with_range_constraint_incl_bound_definition(): def test_loading_aspect_with_language_constraint(): file_path = RESOURCE_PATH / "AspectWithLanguageConstraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -200,11 +187,9 @@ def test_loading_aspect_with_language_constraint(): def test_loading_aspect_with_locale_constraint(): file_path = RESOURCE_PATH / "AspectWithLocaleConstraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -219,11 +204,9 @@ def test_loading_aspect_with_locale_constraint(): def test_loading_aspect_with_fixed_point_constraint(): file_path = RESOURCE_PATH / "AspectWithFixedPoint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -241,11 +224,9 @@ def test_loading_aspect_with_fixed_point_constraint(): def test_loading_aspect_with_encoding_constraint(): file_path = RESOURCE_PATH / "AspectWithEncodingConstraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -259,11 +240,9 @@ def test_loading_aspect_with_encoding_constraint(): def test_loading_aspect_with_regular_expression_constraint(): file_path = RESOURCE_PATH / "AspectWithRegularExpressionConstraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic @@ -276,11 +255,9 @@ def test_loading_aspect_with_regular_expression_constraint(): def test_loading_aspect_with_length_constraint(): file_path = RESOURCE_PATH / "AspectWithLengthConstraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] trait_characteristic = first_property.characteristic diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_entities.py b/core/esmf-aspect-meta-model-python/tests/integration/test_entities.py index 42c7b62..88669b9 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_entities.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_entities.py @@ -12,20 +12,16 @@ from os import getcwd from pathlib import Path -from esmf_aspect_meta_model_python import AbstractEntity, ComplexType, Enumeration, Quantifiable -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import AbstractEntity, ComplexType, Enumeration, Quantifiable, SAMMGraph RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.entity/2.1.0") def test_loading_aspect_with_entity_enum(): file_path = RESOURCE_PATH / "AspectWithEntityEnum.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 1 @@ -61,11 +57,9 @@ def test_loading_aspect_with_entity_enum(): def test_loading_aspect_with_entity(): file_path = RESOURCE_PATH / "AspectWithEntity.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property = aspect.properties[0] single_entity_characteristic = property.characteristic @@ -86,11 +80,9 @@ def test_loading_aspect_with_entity(): def test_aspect_with_abstract_entity(): file_path = RESOURCE_PATH / "AspectWithAbstractEntity.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() aspect_properties = aspect.properties assert len(aspect_properties) == 1 @@ -118,11 +110,9 @@ def test_aspect_with_abstract_entity(): def test_aspect_with_multiple_entities_same_extend(): file_path = RESOURCE_PATH / "AspectWithMultipleEntitiesSameExtend.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 2 @@ -151,11 +141,9 @@ def test_aspect_with_unused_extending_entity() -> None: but extends an abstract entity that is connected to an aspect. """ file_path = RESOURCE_PATH / "AspectWithUnusedExtendingEntity.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 1 @@ -185,11 +173,9 @@ def test_aspect_with_unused_extending_entity() -> None: def test_aspect_with_abstract_coordinate_properties_list() -> None: file_path = RESOURCE_PATH / "AspectWithPoint3d.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 1 @@ -224,11 +210,9 @@ def test_attribute_inheritance_entity() -> None: for extending entities. """ file_path = RESOURCE_PATH / "AspectWithAbstractEntityMultipleAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 1 @@ -264,11 +248,9 @@ def test_multiple_attribute_inheritance_entity() -> None: for multiple extending entities. """ file_path = RESOURCE_PATH / "AspectWithMultipleAbstractEntitiesMultipleAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 1 @@ -305,11 +287,9 @@ def test_multiple_attribute_inheritance_entity() -> None: def test_attribute_inheritance_property() -> None: file_path = RESOURCE_PATH / "AspectWithAbstractPropertyMultipleAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() properties = aspect.properties assert len(properties) == 1 @@ -322,7 +302,10 @@ def test_attribute_inheritance_property() -> None: extends = entity.extends assert extends is not None - assert entity.properties[0].extends is extends.properties[0] + assert entity.properties[0].extends + assert extends.properties[0] + assert entity.properties[0].extends.get_preferred_name("en") == extends.properties[0].get_preferred_name("en") + assert entity.properties[0].extends.get_description("en") == extends.properties[0].get_description("en") extending_property = entity.properties[0] assert len(extending_property.see) == 2 @@ -336,11 +319,9 @@ def test_attribute_inheritance_property() -> None: def test_multiple_properties_same_extend() -> None: file_path = RESOURCE_PATH / "AspectWithMultiplePropertiesSameExtend.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert len(aspect.properties) == 2 characteristic = aspect.properties[0].characteristic @@ -356,7 +337,10 @@ def test_multiple_properties_same_extend() -> None: extending_property2 = entity2.properties[0] assert extending_property1.extends is not None - assert extending_property1.extends is extending_property2.extends + assert extending_property1.extends + assert extending_property2.extends + assert extending_property1.extends.get_preferred_name("en") == extending_property2.extends.get_preferred_name("en") + assert extending_property1.extends.get_description("en") == extending_property2.extends.get_description("en") assert extending_property1.get_preferred_name("en") == "velocity" characteristic1 = extending_property1.characteristic @@ -373,11 +357,9 @@ def test_multiple_properties_same_extend() -> None: def test_abstract_property_blank_node() -> None: file_path = RESOURCE_PATH / "AspectWithAbstractPropertyBlankNode.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert len(aspect.properties) == 1 characteristic = aspect.properties[0].characteristic @@ -409,11 +391,9 @@ def test_abstract_property_blank_node() -> None: def test_abstract_property_multiple_abstract_entities() -> None: file_path = RESOURCE_PATH / "AspectWithAbstractPropertyMultipleAbstractEntities.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert len(aspect.properties) == 1 characteristic = aspect.properties[0].characteristic @@ -452,11 +432,9 @@ def test_abstract_property_multiple_abstract_entities() -> None: def test_aspect_with_time_series(): file_path = RESOURCE_PATH / "AspectWithTimeSeries.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] time_series_characteristic = property1.characteristic @@ -485,7 +463,8 @@ def test_aspect_with_time_series(): timestamp = property assert None not in [extending_value, abstract_value, timestamp] - assert extending_value.extends is abstract_value + assert extending_value.extends.get_preferred_name("en") is abstract_value.get_preferred_name("en") + assert extending_value.extends.get_description("en") is abstract_value.get_description("en") assert extending_value.is_abstract is False assert abstract_value.is_abstract is True assert timestamp.is_abstract is False @@ -501,11 +480,9 @@ def test_aspect_with_time_series(): def test_aspect_with_time_series_with_complex_type() -> None: file_path = RESOURCE_PATH / "AspectWithTimeSeriesWithComplexType.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] time_series_characteristic = property1.characteristic @@ -541,11 +518,9 @@ def test_aspect_with_time_series_with_complex_type() -> None: def test_aspect_with_file_resource_entity() -> None: file_path = RESOURCE_PATH / "AspectWithFileResourceEntity.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() characteristic = aspect.properties[0].characteristic assert characteristic is not None @@ -577,11 +552,9 @@ def test_aspect_with_file_resource_entity() -> None: def test_aspect_with_entity_extending_file_resource() -> None: file_path = RESOURCE_PATH / "AspectWithEntityExtendingFileResource.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() characteristic = aspect.properties[0].characteristic assert characteristic is not None diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_event.py b/core/esmf-aspect-meta-model-python/tests/integration/test_event.py index a0e26a5..6eafe90 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_event.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_event.py @@ -12,20 +12,16 @@ from os import getcwd from pathlib import Path -from esmf_aspect_meta_model_python import Event -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import Event, SAMMGraph RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.event/2.1.0") def test_loading_aspect_with_event() -> None: file_path = RESOURCE_PATH / "aspect_with_event.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.events is not None assert len(aspect.events) == 1 @@ -36,11 +32,9 @@ def test_loading_aspect_with_event() -> None: def test_loading_aspect_with_event_with_parameters() -> None: file_path = RESOURCE_PATH / "aspect_with_event_with_parameters.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.events is not None assert len(aspect.events) == 1 @@ -54,11 +48,9 @@ def test_loading_aspect_with_event_with_parameters() -> None: def test_loading_aspect_with_multiple_event() -> None: file_path = RESOURCE_PATH / "aspect_with_multiple_event.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.events is not None assert len(aspect.events) == 3 diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_general.py b/core/esmf-aspect-meta-model-python/tests/integration/test_general.py index 921cb94..453084d 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_general.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_general.py @@ -12,20 +12,16 @@ from os import getcwd from pathlib import Path -from esmf_aspect_meta_model_python import BaseImpl, ComplexType, Either -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import BaseImpl, ComplexType, Either, SAMMGraph RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.general/2.1.0") def test_aspect_with_multiple_attributes(): file_path = RESOURCE_PATH / "AspectWithMultipleAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.get_preferred_name("en") == "Test Aspect" assert aspect.get_preferred_name("de") == "Test Aspekt" @@ -39,11 +35,9 @@ def test_aspect_with_multiple_attributes(): def test_aspect(): file_path = RESOURCE_PATH / "AspectWithProperties.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.meta_model_version == "2.1.0" assert aspect.name == "TestAspect" @@ -69,8 +63,7 @@ def test_aspect(): "Describes a Property which contains plain text. This is intended exclusively for human readable strings, " "not for identifiers, measurement values, etc." ) - assert characteristic.parent_elements[0].urn == "urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyOne" - assert characteristic.parent_elements[1].urn == "urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyTwo" + assert characteristic.parent_elements[0].urn == "urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyTwo" data_type = characteristic.data_type assert data_type.is_scalar is True @@ -80,11 +73,9 @@ def test_aspect(): def test_aspect_with_operation(): file_path = RESOURCE_PATH / "AspectWithOperation.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.meta_model_version == "2.1.0" assert aspect.name == "AspectWithOperation" @@ -143,11 +134,9 @@ def test_aspect_with_operation(): def test_aspect_with_operation_no_output(): file_path = RESOURCE_PATH / "AspectWithOperationNoOutput.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.meta_model_version == "2.1.0" assert aspect.name == "AspectWithOperationNoOutput" @@ -200,11 +189,9 @@ def test_aspect_with_operation_no_output(): def test_aspect_with_property_multiple_references() -> None: file_path = RESOURCE_PATH / "AspectWithPropertyMultipleReferences.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] property2 = aspect.properties[1] @@ -222,11 +209,9 @@ def test_aspect_with_property_multiple_references() -> None: def test_aspect_with_property_with_payload_name() -> None: file_path = RESOURCE_PATH / "AspectWithPropertyWithPayloadName.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] assert property1.is_optional is False @@ -236,11 +221,9 @@ def test_aspect_with_property_with_payload_name() -> None: def test_aspect_with_optional_property_with_payload_name() -> None: file_path = RESOURCE_PATH / "AspectWithOptionalPropertyWithPayloadName.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] assert property1.is_optional is True @@ -250,11 +233,9 @@ def test_aspect_with_optional_property_with_payload_name() -> None: def test_aspect_with_duplicate_property_with_payload_name() -> None: file_path = RESOURCE_PATH / "AspectWithDuplicatePropertyWithPayloadName.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] assert property1.name == "testProperty" @@ -266,11 +247,9 @@ def test_aspect_with_duplicate_property_with_payload_name() -> None: def test_aspect_with_duplicate_property_with_different_payload_names() -> None: file_path = RESOURCE_PATH / "AspectWithDuplicatePropertyWithDifferentPayloadNames.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() property1 = aspect.properties[0] assert property1.name == "testProperty" @@ -282,11 +261,9 @@ def test_aspect_with_duplicate_property_with_different_payload_names() -> None: def test_aspect_with_extending_property_with_payload_name() -> None: file_path = RESOURCE_PATH / "AspectWithExtendingPropertyWithPayloadName.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.properties[0].characteristic is not None entity = aspect.properties[0].characteristic.data_type @@ -302,13 +279,11 @@ def test_aspect_with_extending_property_with_payload_name() -> None: def test_find_properties_by_name() -> None: file_path = RESOURCE_PATH / "AspectWithProperties.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + _ = samm_graph.load_aspect_model() - result = graph.find_by_name("testPropertyOne") + result = samm_graph.find_by_name("testPropertyOne") assert result is not None assert len(result) == 1 assert isinstance(result[0], BaseImpl) @@ -318,7 +293,7 @@ def test_find_properties_by_name() -> None: assert len(result[0].see) == 0 assert len(result[0].descriptions) == 0 - result = graph.find_by_name("testPropertyTwo") + result = samm_graph.find_by_name("testPropertyTwo") assert result is not None assert len(result) == 1 assert isinstance(result[0], BaseImpl) @@ -328,19 +303,18 @@ def test_find_properties_by_name() -> None: assert len(result[0].see) == 0 assert len(result[0].descriptions) == 0 - result = graph.find_by_name("Unknown") + result = samm_graph.find_by_name("Unknown") assert len(result) == 0 def test_find_property_characteristic_by_name() -> None: file_path = RESOURCE_PATH / "AspectWithPropertyWithAllBaseAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + _ = aspect.properties[0].characteristic + result = samm_graph.find_by_name("BooleanTestCharacteristic") - result = graph.find_by_name("BooleanTestCharacteristic") assert result is not None assert len(result) == 1 assert isinstance(result[0], BaseImpl) @@ -353,13 +327,11 @@ def test_find_property_characteristic_by_name() -> None: def test_find_properties_by_urn() -> None: file_path = RESOURCE_PATH / "AspectWithProperties.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + _ = samm_graph.load_aspect_model() - result = graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyOne") + result = samm_graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyOne") assert result is not None assert isinstance(result, BaseImpl) assert result.name == "testPropertyOne" @@ -368,7 +340,7 @@ def test_find_properties_by_urn() -> None: assert len(result.see) == 0 assert len(result.descriptions) == 0 - result = graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyTwo") + result = samm_graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#testPropertyTwo") assert result is not None assert isinstance(result, BaseImpl) assert result.name == "testPropertyTwo" @@ -377,18 +349,17 @@ def test_find_properties_by_urn() -> None: assert len(result.see) == 0 assert len(result.descriptions) == 0 - result = graph.find_by_urn("Unknown") + result = samm_graph.find_by_urn("Unknown") assert result is None def test_find_property_characteristic_by_urn() -> None: file_path = RESOURCE_PATH / "AspectWithPropertyWithAllBaseAttributes.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) - graph = loader.graph - result = graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#BooleanTestCharacteristic") + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() + _ = aspect.properties[0].characteristic + result = samm_graph.find_by_urn("urn:samm:org.eclipse.esmf.test.general:2.1.0#BooleanTestCharacteristic") assert result is not None assert isinstance(result, BaseImpl) @@ -398,17 +369,15 @@ def test_find_property_characteristic_by_urn() -> None: assert len(result.see) == 0 assert len(result.descriptions) == 0 - result = graph.find_by_urn("Unknown") + result = samm_graph.find_by_urn("Unknown") assert result is None def test_loading_aspect_with_either(): file_path = RESOURCE_PATH / "AspectWithEither.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() first_property = aspect.properties[0] either_characteristic = first_property.characteristic diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_resolve_elements_referencies.py b/core/esmf-aspect-meta-model-python/tests/integration/test_resolve_elements_referencies.py index 4520f5c..5ba2cd4 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_resolve_elements_referencies.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_resolve_elements_referencies.py @@ -12,19 +12,16 @@ from os import getcwd from pathlib import Path -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import SAMMGraph RESOURCE_PATH = getcwd() / Path("tests/integration/resources/org.eclipse.esmf.test.general_with_references/2.1.0") def test_resolve_elements_references(): file_path = RESOURCE_PATH / "AspectWithReferences.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() - loader = AspectLoader() - model_elements = loader.load_aspect_model(rdf_graph, aspect_urn) - aspect = model_elements[0] + samm_graph = SAMMGraph() + samm_graph.parse(file_path) + aspect = samm_graph.load_aspect_model() assert aspect.name == "test_aspect" assert aspect.get_preferred_name("en") == "Aspect with references" diff --git a/core/esmf-aspect-meta-model-python/tests/integration/test_vocabulary.py b/core/esmf-aspect-meta-model-python/tests/integration/test_vocabulary.py index 4996184..8f331d7 100644 --- a/core/esmf-aspect-meta-model-python/tests/integration/test_vocabulary.py +++ b/core/esmf-aspect-meta-model-python/tests/integration/test_vocabulary.py @@ -14,10 +14,10 @@ from rdflib import URIRef # type: ignore from esmf_aspect_meta_model_python.vocabulary.namespace import Namespace -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC -from esmf_aspect_meta_model_python.vocabulary.SAMME import SAMME -from esmf_aspect_meta_model_python.vocabulary.UNIT import UNIT +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC +from esmf_aspect_meta_model_python.vocabulary.samme import SAMME +from esmf_aspect_meta_model_python.vocabulary.unit import UNIT SAMM_VERSION = "2.1.0" UNIT_URN = f"urn:samm:org.eclipse.esmf.samm:unit:{SAMM_VERSION}#referenceUnit" @@ -51,7 +51,7 @@ def test_samm_unit_fields() -> None: def test_samm_unit_get_urn() -> None: unit = UNIT(SAMM_VERSION) - uri_ref = unit.get_urn(SAMM.referenceUnit) + uri_ref = unit.get_urn(SAMM.reference_unit) assert issubclass(type(uri_ref), URIRef) assert uri_ref.toPython() == UNIT_URN @@ -84,7 +84,7 @@ def test_samm_samme_fields() -> None: def test_samm_samme_get_urn() -> None: samme = SAMME(SAMM_VERSION) - uri_ref = samme.get_urn(SAMM.referenceUnit) + uri_ref = samme.get_urn(SAMM.reference_unit) assert issubclass(type(uri_ref), URIRef) assert uri_ref.toPython() == SAMME_URN @@ -213,7 +213,7 @@ def test_samm_samm_fields() -> None: def test_samm_samm_get_urn() -> None: samm = SAMM(SAMM_VERSION) - uri_ref = samm.get_urn(SAMM.aspect) + uri_ref = samm.get_urn(SAMM.Aspect) assert issubclass(type(uri_ref), URIRef) assert uri_ref.toPython() == SAMM_URN diff --git a/core/esmf-aspect-meta-model-python/tests/unit/impl/test_property.py b/core/esmf-aspect-meta-model-python/tests/unit/impl/test_property.py index d256883..877d875 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/impl/test_property.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/impl/test_property.py @@ -2,13 +2,17 @@ from unittest import mock -from esmf_aspect_meta_model_python.impl import DefaultProperty +from esmf_aspect_meta_model_python.impl import DefaultBlankProperty, DefaultProperty, DefaultPropertyWithExtends +from esmf_aspect_meta_model_python.loader.instantiator.property_instantiator import PropertyInstantiator +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestDefaultProperty: """DefaultProperty unit tests class.""" meta_model_mock = mock.MagicMock(name="meta_model_base_attributes") + elements_factory = mock.MagicMock(name="elements_factory") + graph_node = mock.MagicMock(name="graph_node") characteristic_mock = mock.MagicMock(name="characteristic") example_value = "example_value" property_mock = mock.MagicMock(name="property") @@ -17,10 +21,11 @@ class TestDefaultProperty: not_in_payload = True payload_name = "payload_name" - @mock.patch("esmf_aspect_meta_model_python.impl.default_property.BaseImpl.__init__") - def test_init(self, super_mock): - result = DefaultProperty( + def _get_property_class(self): + return DefaultProperty( meta_model_base_attributes=self.meta_model_mock, + elements_factory=self.elements_factory, + graph_node=self.graph_node, characteristic=self.characteristic_mock, example_value=self.example_value, extends=self.property_mock, @@ -30,8 +35,14 @@ def test_init(self, super_mock): payload_name=self.payload_name, ) + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.BaseImpl.__init__") + def test_init(self, super_mock): + result = self._get_property_class() + super_mock.assert_called_once_with(self.meta_model_mock) self.characteristic_mock.append_parent_element.assert_called_once_with(result) + assert result._elements_factory == self.elements_factory + assert result._graph_node == self.graph_node assert result._characteristic == self.characteristic_mock assert result._example_value == self.example_value assert result._is_abstract == self.abstract @@ -40,90 +51,199 @@ def test_init(self, super_mock): assert result._not_in_payload == self.not_in_payload assert result._payload_name == self.payload_name + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.isinstance") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_get_instantiator_class(self, _, isinstance_mock): + instantiator_mock = mock.MagicMock(name="instantiator_class") + self.elements_factory._get_element_type.return_value = "element_type" + self.elements_factory._instantiators = {} + self.elements_factory._create_instantiator.return_value = instantiator_mock + isinstance_mock.return_value = True + property_cls = self._get_property_class() + result = property_cls._get_instantiator_class() + + assert result == instantiator_mock + self.elements_factory._get_element_type.assert_called_once_with(self.graph_node) + self.elements_factory._create_instantiator.assert_called_once_with("element_type") + isinstance_mock.assert_called_once_with(instantiator_mock, PropertyInstantiator) + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_set_characteristic(self, _): + property_cls = self._get_property_class() + + assert self.characteristic_mock.append_parent_element.called + self.characteristic_mock.append_parent_element.has_call(mock.call(property_cls)) + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_characteristic(self, _): - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=self.property_mock, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) + property_cls = self._get_property_class() result = property_cls.characteristic assert result == self.characteristic_mock + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultProperty._set_characteristic") + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultProperty._get_instantiator_class") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_characteristic_get_value(self, _, get_instantiator_class_mock, set_characteristic_mock): + property_cls = self._get_property_class() + property_cls._characteristic = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._get_child.return_value = "characteristic" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.characteristic + + assert result is None + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.characteristic) + instantiator_class_mock._get_child.assert_called_once_with(self.graph_node, "urn", required=True) + set_characteristic_mock.has_call(mock.call("characteristic")) + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_example_value(self, _): - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=self.property_mock, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) + property_cls = self._get_property_class() result = property_cls.example_value assert result == self.example_value + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultProperty._get_instantiator_class") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_example_value_get_value(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._example_value = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._aspect_graph.value.return_value = "example_value" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.example_value + + assert result == "example_value" + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.example_value) + instantiator_class_mock._aspect_graph.value.assert_called_once_with(subject=self.graph_node, predicate="urn") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_is_abstract(self, _): - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=self.property_mock, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) + property_cls = self._get_property_class() result = property_cls.is_abstract assert result == self.abstract @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_extends(self, _): - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=self.property_mock, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) + property_cls = self._get_property_class() result = property_cls.extends assert result == self.property_mock @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_is_optional(self, _): - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=self.property_mock, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) + property_cls = self._get_property_class() result = property_cls.is_optional assert result == self.optional @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_is_not_in_payload(self, _): - property_cls = DefaultProperty( + property_cls = self._get_property_class() + result = property_cls.is_not_in_payload + + assert result == self.not_in_payload + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_payload_name(self, _): + property_cls = self._get_property_class() + result = property_cls.payload_name + + assert result == self.payload_name + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_preferred_names_no_extends(self, _): + property_cls = self._get_property_class() + property_cls._extends = None + property_cls._preferred_names = "preferred_names" + result = property_cls.preferred_names + + assert result == "preferred_names" + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_preferred_names_extends(self, _): + property_mock = mock.MagicMock(name="property") + property_mock.preferred_names = {"extend_property_preferred_names": "preferred_names"} + property_cls = self._get_property_class() + property_cls._preferred_names = {"base_preferred_names": "preferred_names"} + property_cls._extends = property_mock + result = property_cls.preferred_names + + assert "base_preferred_names" in result + assert result["base_preferred_names"] == "preferred_names" + assert "extend_property_preferred_names" in result + assert result["extend_property_preferred_names"] == "preferred_names" + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_descriptions_no_extends(self, _): + property_cls = self._get_property_class() + property_cls._extends = None + property_cls._descriptions = "descriptions" + result = property_cls.descriptions + + assert result == "descriptions" + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_descriptions_extends(self, _): + property_mock = mock.MagicMock(name="property") + property_mock.descriptions = {"extend_property_descriptions": "descriptions"} + property_cls = self._get_property_class() + property_cls._extends = property_mock + property_cls._descriptions = {"base_descriptions": "descriptions"} + result = property_cls.descriptions + + assert "base_descriptions" in result + assert result["base_descriptions"] == "descriptions" + assert "extend_property_descriptions" in result + assert result["extend_property_descriptions"] == "descriptions" + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_see_no_extends(self, _): + property_cls = self._get_property_class() + property_cls._extends = None + property_cls._see = "see" + result = property_cls.see + + assert result == "see" + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_see_extends(self, _): + property_mock = mock.MagicMock(name="property") + property_mock.see = ["extend_property_see"] + property_cls = self._get_property_class() + property_cls._extends = property_mock + property_cls._see = ["base_see"] + result = property_cls.see + + assert result == ["base_see", "extend_property_see"] + + +class TestDefaultBlankProperty: + """DefaultBlankProperty unit tests class.""" + + base_element_node = mock.MagicMock(name="base_element_node") + meta_model_mock = mock.MagicMock(name="meta_model_base_attributes") + elements_factory = mock.MagicMock(name="elements_factory") + graph_node = mock.MagicMock(name="graph_node") + characteristic_mock = mock.MagicMock(name="characteristic") + example_value = "example_value" + property_mock = mock.MagicMock(name="property") + abstract = True + optional = True + not_in_payload = True + payload_name = "payload_name" + + def _get_property_class(self): + return DefaultBlankProperty( + base_element_node=self.base_element_node, meta_model_base_attributes=self.meta_model_mock, + elements_factory=self.elements_factory, + graph_node=self.graph_node, characteristic=self.characteristic_mock, example_value=self.example_value, extends=self.property_mock, @@ -132,14 +252,118 @@ def test_is_not_in_payload(self, _): not_in_payload=self.not_in_payload, payload_name=self.payload_name, ) + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_is_optional(self, _): + property_cls = self._get_property_class() + result = property_cls.is_optional + + assert result == self.optional + + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultBlankProperty._get_instantiator_class") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_is_optional_get_value(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._optional = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._aspect_graph.value.return_value = None + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.is_optional + + assert result is False + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.optional) + instantiator_class_mock._aspect_graph.value.assert_called_once_with( + subject=self.base_element_node, + predicate="urn", + ) + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_is_not_in_payload(self, _): + property_cls = self._get_property_class() result = property_cls.is_not_in_payload assert result == self.not_in_payload + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultBlankProperty._get_instantiator_class") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_is_not_in_payload_get_value(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._not_in_payload = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._aspect_graph.value.return_value = None + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.is_not_in_payload + + assert result is False + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.not_in_payload) + instantiator_class_mock._aspect_graph.value.assert_called_once_with( + subject=self.base_element_node, + predicate="urn", + ) + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") def test_payload_name(self, _): - property_cls = DefaultProperty( + property_cls = self._get_property_class() + result = property_cls.payload_name + + assert result == self.payload_name + + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultBlankProperty._get_instantiator_class") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_payload_name_get_value(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._payload_name = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._get_child.return_value = "child_payload_name" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.payload_name + + assert result == "child_payload_name" + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.payload_name) + instantiator_class_mock._get_child.assert_called_once_with(self.base_element_node, "urn") + + @mock.patch("esmf_aspect_meta_model_python.impl.default_property.DefaultBlankProperty._get_instantiator_class") + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_payload_name_empty(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._payload_name = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._get_child.return_value = "" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.payload_name + + assert result == property_cls.name + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.payload_name) + instantiator_class_mock._get_child.assert_called_once_with(self.base_element_node, "urn") + + +class TestDefaultPropertyWithExtends: + """DefaultPropertyWithExtends unit tests class.""" + + meta_model_mock = mock.MagicMock(name="meta_model_base_attributes") + elements_factory = mock.MagicMock(name="elements_factory") + graph_node = mock.MagicMock(name="graph_node") + characteristic_mock = mock.MagicMock(name="characteristic") + example_value = "example_value" + property_mock = mock.MagicMock(name="property") + abstract = True + optional = True + not_in_payload = True + payload_name = "payload_name" + + def _get_property_class(self): + return DefaultPropertyWithExtends( meta_model_base_attributes=self.meta_model_mock, + elements_factory=self.elements_factory, + graph_node=self.graph_node, characteristic=self.characteristic_mock, example_value=self.example_value, extends=self.property_mock, @@ -148,43 +372,71 @@ def test_payload_name(self, _): not_in_payload=self.not_in_payload, payload_name=self.payload_name, ) + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_payload_name(self, _): + property_cls = self._get_property_class() result = property_cls.payload_name assert result == self.payload_name + @mock.patch( + "esmf_aspect_meta_model_python.impl.default_property.DefaultPropertyWithExtends._get_instantiator_class" + ) @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") - def test_preferred_names_no_extends(self, _): - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=None, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) - property_cls._preferred_names = "preferred_names" - result = property_cls.preferred_names + def test_payload_name_get_value(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._payload_name = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._get_child.return_value = "child_payload_name" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.payload_name - assert result == "preferred_names" + assert result == "child_payload_name" + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.payload_name) + instantiator_class_mock._get_child.assert_called_once_with(self.graph_node, "urn") + @mock.patch( + "esmf_aspect_meta_model_python.impl.default_property.DefaultPropertyWithExtends._get_instantiator_class" + ) @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") - def test_preferred_names_extends(self, _): - property_mock = mock.MagicMock(name="property") - property_mock.preferred_names = {"property_preferred_names": "preferred_names"} - property_cls = DefaultProperty( - meta_model_base_attributes=self.meta_model_mock, - characteristic=self.characteristic_mock, - example_value=self.example_value, - extends=property_mock, - abstract=self.abstract, - optional=self.optional, - not_in_payload=self.not_in_payload, - payload_name=self.payload_name, - ) - property_cls._preferred_names = {"base_preferred_names": "preferred_names"} - result = property_cls.preferred_names + def test_payload_name_empty(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._payload_name = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._get_child.return_value = "" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.payload_name - assert "property_preferred_names" in result - assert "base_preferred_names" in result + assert result == property_cls.name + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.payload_name) + instantiator_class_mock._get_child.assert_called_once_with(self.graph_node, "urn") + + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_extends(self, _): + property_cls = self._get_property_class() + result = property_cls.extends + + assert result == self.property_mock + + @mock.patch( + "esmf_aspect_meta_model_python.impl.default_property.DefaultPropertyWithExtends._get_instantiator_class" + ) + @mock.patch("esmf_aspect_meta_model_python.impl.default_operation.super") + def test_extends_get_value(self, _, get_instantiator_class_mock): + property_cls = self._get_property_class() + property_cls._extends = None + instantiator_class_mock = mock.MagicMock(name="instantiator_class") + instantiator_class_mock._samm.get_urn.return_value = "urn" + instantiator_class_mock._get_child.return_value = "extend_property" + get_instantiator_class_mock.return_value = instantiator_class_mock + result = property_cls.extends + + assert result == "extend_property" + get_instantiator_class_mock.assert_called_once() + instantiator_class_mock._samm.get_urn.assert_called_once_with(SAMM.extends) + instantiator_class_mock._get_child.assert_called_once_with(self.graph_node, "urn", required=True) diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_entity_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_entity_instantiator.py index f46e346..0ef7008 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_entity_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_entity_instantiator.py @@ -5,7 +5,7 @@ import pytest from esmf_aspect_meta_model_python.loader.instantiator.abstract_entity_instantiator import AbstractEntityInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestAbstractEntityInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_property_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_property_instantiator.py index c326b6b..4cf54ed 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_property_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_abstract_property_instantiator.py @@ -7,7 +7,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.abstract_property_instantiator import ( AbstractPropertyInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestAbstractPropertyInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_aspect_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_aspect_instantiator.py index 1ba9f38..8d3f7be 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_aspect_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_aspect_instantiator.py @@ -5,7 +5,7 @@ import pytest from esmf_aspect_meta_model_python.loader.instantiator.aspect_instantiator import AspectInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestAspectInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_complex_type_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_complex_type_instantiator.py index 7ad92de..a95566e 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_complex_type_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_complex_type_instantiator.py @@ -3,7 +3,7 @@ from unittest import mock from esmf_aspect_meta_model_python.loader.instantiator.complex_type_instantiator import ComplexTypeInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestComplexTypeInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_duration_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_duration_instantiator.py index ecc338f..40fa390 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_duration_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_duration_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.duration_instantiator import DurationInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestDatatypeInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_either_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_either_instantiator.py index 7f611fd..2e9f315 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_either_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_either_instantiator.py @@ -3,7 +3,7 @@ from unittest import mock from esmf_aspect_meta_model_python.loader.instantiator.either_instantiator import EitherInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestEitherInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_encoding_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_encoding_constraint_instantiator.py index 7b3eba8..98ba462 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_encoding_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_encoding_constraint_instantiator.py @@ -5,7 +5,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.encoding_constraint_instantiator import ( EncodingConstraintInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestEncodingConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_entity_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_entity_instantiator.py index b5eba1d..726bd2a 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_entity_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_entity_instantiator.py @@ -5,7 +5,7 @@ import pytest from esmf_aspect_meta_model_python.loader.instantiator.entity_instantiator import EntityInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestEntityInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_enumeration_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_enumeration_instantiator.py index 6a8d59a..041618d 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_enumeration_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_enumeration_instantiator.py @@ -7,8 +7,8 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.enumeration_instantiator import EnumerationInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestEnumerationInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_event_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_event_instantiator.py index e1d2105..66600d4 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_event_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_event_instantiator.py @@ -3,7 +3,7 @@ from unittest import mock from esmf_aspect_meta_model_python.loader.instantiator.event_instantiator import EventInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestEventInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_fixed_point_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_fixed_point_constraint_instantiator.py index 80b7a4d..f2565a5 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_fixed_point_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_fixed_point_constraint_instantiator.py @@ -5,7 +5,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.fixed_point_constraint_instantiator import ( FixedPointConstraintInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestFixedPointConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_language_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_language_constraint_instantiator.py index dbfa4e5..e7186d5 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_language_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_language_constraint_instantiator.py @@ -5,7 +5,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.language_constraint_instantiator import ( LanguageConstraintInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestLanguageConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_length_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_length_constraint_instantiator.py index 2d26eea..e7c2dbc 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_length_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_length_constraint_instantiator.py @@ -5,7 +5,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.length_constraint_instantiator import ( LengthConstraintInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestLengthConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_list_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_list_instantiator.py index 245d1fa..5730149 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_list_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_list_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.list_instantiator import ListInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestListInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_locale_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_locale_constraint_instantiator.py index 59dab8e..890ae62 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_locale_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_locale_constraint_instantiator.py @@ -5,7 +5,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.locale_constraint_instantiator import ( LocaleConstraintInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestLocaleConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_measurement_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_measurement_instantiator.py index 3114c6c..ba917d0 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_measurement_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_measurement_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.measurement_instantiator import MeasurementInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestMeasurementInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_operation_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_operation_instantiator.py index be8d760..c3ec07f 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_operation_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_operation_instantiator.py @@ -3,7 +3,7 @@ from unittest import mock from esmf_aspect_meta_model_python.loader.instantiator.operation_instantiator import OperationInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestOperationInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_property_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_property_instantiator.py index 48fc640..3a947fe 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_property_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_property_instantiator.py @@ -4,194 +4,196 @@ import pytest +from rdflib import BNode, URIRef + from esmf_aspect_meta_model_python.loader.instantiator.property_instantiator import PropertyInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestPropertyInstantiator: """PropertyInstantiator unit tests class.""" + @mock.patch( + "esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.PropertyInstantiator." + "_create_property_direct_reference" + ) @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.isinstance") - def test_create_instance_element_is_URIRef(self, isinstance_mock): + def test_create_instance_element_is_URIRef(self, isinstance_mock, create_property_direct_reference_mock): isinstance_mock.return_value = True - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") - base_class_mock._create_property_direct_reference.return_value = "instance" - result = PropertyInstantiator._create_instance(base_class_mock, "element_node") - - assert result == "instance" - base_class_mock._create_property_direct_reference.assert_called_once_with("element_node") - + create_property_direct_reference_mock.return_value = "property_instance" + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + element_node_mock = mock.MagicMock(name="element_node") + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + result = instantiator_cls._create_instance(element_node_mock) + + assert result == "property_instance" + isinstance_mock.assert_called_once_with(element_node_mock, URIRef) + create_property_direct_reference_mock.assert_called_once_with(element_node_mock) + + @mock.patch( + "esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.PropertyInstantiator." + "_create_property_blank_node" + ) @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.isinstance") - def test_create_instance_element_blank_node(self, isinstance_mock): + def test_create_instance_element_blank_node(self, isinstance_mock, create_property_blank_node_mock): isinstance_mock.side_effect = (False, True) - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") - aspect_graph_mock = mock.MagicMock(name="aspect_graph") - aspect_graph_mock.value.return_value = "value" - base_class_mock._aspect_graph = aspect_graph_mock - base_class_mock._create_property_blank_node.return_value = "instance" + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.value.return_value = "value" + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + element_node_mock = mock.MagicMock(name="element_node") samm_mock = mock.MagicMock(name="SAMM") - samm_mock.get_urn.return_value = "predicate" - base_class_mock._samm = samm_mock - result = PropertyInstantiator._create_instance(base_class_mock, "element_node") - - assert result == "instance" + samm_mock.get_urn.return_value = "urn" + create_property_blank_node_mock.return_value = "property_instance" + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + instantiator_cls._aspect_graph = graph_mock + instantiator_cls._samm = samm_mock + result = instantiator_cls._create_instance(element_node_mock) + + assert result == "property_instance" + isinstance_mock.assert_has_calls( + [ + mock.call(element_node_mock, URIRef), + mock.call(element_node_mock, BNode), + ] + ) samm_mock.get_urn.assert_called_once_with(SAMM.property) - aspect_graph_mock.value.assert_called_once_with(subject="element_node", predicate="predicate") - base_class_mock._create_property_blank_node.assert_called_once_with("element_node") + graph_mock.value.assert_called_once_with(subject=element_node_mock, predicate="urn") + create_property_blank_node_mock.assert_called_once_with(element_node_mock) + @mock.patch( + "esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.PropertyInstantiator." + "_create_property_with_extends" + ) @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.isinstance") - def test_create_instance_element_node_with_extends(self, isinstance_mock): + def test_create_instance_element_node_with_extends(self, isinstance_mock, create_property_with_extends_mock): isinstance_mock.side_effect = (False, True) - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") - aspect_graph_mock = mock.MagicMock(name="aspect_graph") - aspect_graph_mock.value.side_effect = (None, "value") - base_class_mock._aspect_graph = aspect_graph_mock - base_class_mock._create_property_with_extends.return_value = "instance" + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.value.side_effect = (None, "value") + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + element_node_mock = mock.MagicMock(name="element_node") samm_mock = mock.MagicMock(name="SAMM") - samm_mock.get_urn.return_value = "predicate" - base_class_mock._samm = samm_mock - result = PropertyInstantiator._create_instance(base_class_mock, "element_node") - - assert result == "instance" + samm_mock.get_urn.side_effect = ("urn1", "urn2") + create_property_with_extends_mock.return_value = "property_instance" + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + instantiator_cls._aspect_graph = graph_mock + instantiator_cls._samm = samm_mock + result = instantiator_cls._create_instance(element_node_mock) + + assert result == "property_instance" + isinstance_mock.assert_has_calls( + [ + mock.call(element_node_mock, URIRef), + mock.call(element_node_mock, BNode), + ] + ) samm_mock.get_urn.assert_has_calls( [ mock.call(SAMM.property), mock.call(SAMM.extends), ] ) - aspect_graph_mock.value.assert_has_calls([mock.call(subject="element_node", predicate="predicate")]) - assert aspect_graph_mock.value.call_count == 2 - base_class_mock._create_property_with_extends.assert_called_once_with("element_node") + graph_mock.value.assert_has_calls( + [ + mock.call(subject=element_node_mock, predicate="urn1"), + mock.call(subject=element_node_mock, predicate="urn2"), + ] + ) + create_property_with_extends_mock.assert_called_once_with(element_node_mock) @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.isinstance") def test_create_instance_element_raise_exception(self, isinstance_mock): isinstance_mock.side_effect = (False, False) - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + element_node_mock = mock.MagicMock(name="element_node") + instantiator_cls = PropertyInstantiator(model_element_factory_mock) with pytest.raises(ValueError) as error: - PropertyInstantiator._create_instance(base_class_mock, "element_node") + instantiator_cls._create_instance(element_node_mock) assert str(error.value) == "The syntax of the property is not allowed." @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.DefaultProperty") def test_create_property_direct_reference(self, default_property_mock): - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") - base_class_mock._get_base_attributes.return_value = "meta_model_base_attributes" - base_class_mock._get_child.return_value = "characteristic" - samm_mock = mock.MagicMock(name="SAMM") - samm_mock.get_urn.side_effect = ("urn", "predicate") - base_class_mock._samm = samm_mock - aspect_graph_mock = mock.MagicMock(name="aspect_graph") - aspect_graph_mock.value.return_value = "example_value" - base_class_mock._aspect_graph = aspect_graph_mock - default_property_mock.return_value = "instance" - result = PropertyInstantiator._create_property_direct_reference(base_class_mock, "element_node") - - assert result == "instance" - base_class_mock._get_base_attributes.assert_called_once_with("element_node") - base_class_mock._get_child.assert_called_once_with("element_node", "urn", required=True) - samm_mock.get_urn.assert_has_calls( - [ - mock.call(SAMM.characteristic), - mock.call(SAMM.example_value), - ] - ) - aspect_graph_mock.value.assert_called_once_with(subject="element_node", predicate="predicate") + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + element_node_mock = mock.MagicMock(name="element_node") + default_property_mock.return_value = "default_property" + get_base_attributes_mock = mock.MagicMock(name="_get_base_attributes") + get_base_attributes_mock.return_value = "base_attributes" + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + instantiator_cls._get_base_attributes = get_base_attributes_mock + instantiator_cls._model_element_factory = model_element_factory_mock + result = instantiator_cls._create_property_direct_reference(element_node_mock) + + assert result == "default_property" + get_base_attributes_mock.assert_called_once_with(element_node_mock) default_property_mock.assert_called_once_with( - "meta_model_base_attributes", - "characteristic", - "example_value", + meta_model_base_attributes="base_attributes", + elements_factory=model_element_factory_mock, + graph_node=element_node_mock, ) - @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.DefaultProperty") - def test_create_property_blank_node(self, default_property_mock): - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") - base_class_mock._get_base_attributes.return_value = "meta_model_base_attributes" - base_class_mock._get_child.return_value = "characteristic" + @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.DefaultBlankProperty") + def test_create_property_blank_node(self, default_blank_property_mock): + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.value.return_value = "property_node" samm_mock = mock.MagicMock(name="SAMM") - samm_mock.get_urn.side_effect = ( - "predicate", - "predicate", - "urn", - "predicate", - "urn", - "predicate", - ) - base_class_mock._samm = samm_mock - aspect_graph_mock = mock.MagicMock(name="aspect_graph") - aspect_graph_mock.value.side_effect = ("optional", "not_in_payload", "property_node", "example_value") - base_class_mock._aspect_graph = aspect_graph_mock - default_property_mock.return_value = "instance" - result = PropertyInstantiator._create_property_blank_node(base_class_mock, "element_node") - - assert result == "instance" - base_class_mock._get_base_attributes.assert_called_once_with("property_node") - base_class_mock._get_child.assert_has_calls( - [ - mock.call("element_node", "urn"), - mock.call("property_node", "urn", required=True), - ] - ) - samm_mock.get_urn.assert_has_calls( - [ - mock.call(SAMM.characteristic), - mock.call(SAMM.example_value), - ] - ) - aspect_graph_mock.value.assert_has_calls( - [ - mock.call(subject="element_node", predicate="predicate"), - mock.call(subject="property_node", predicate="predicate"), - ] - ) - default_property_mock.assert_called_once_with( - "meta_model_base_attributes", - "characteristic", - "example_value", - optional=True, - not_in_payload=True, - payload_name="characteristic", + samm_mock.get_urn.return_value = "urn" + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + element_node_mock = mock.MagicMock(name="element_node") + get_base_attributes_mock = mock.MagicMock(name="_get_base_attributes") + get_base_attributes_mock.return_value = "meta_model_base_attributes" + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + instantiator_cls._aspect_graph = graph_mock + instantiator_cls._samm = samm_mock + instantiator_cls._get_base_attributes = get_base_attributes_mock + instantiator_cls._model_element_factory = model_element_factory_mock + default_blank_property_mock.return_value = "default_blank_property" + result = instantiator_cls._create_property_blank_node(element_node_mock) + + assert result == "default_blank_property" + samm_mock.get_urn.assert_called_once_with(SAMM.property) + graph_mock.value.assert_called_once_with(subject=element_node_mock, predicate="urn") + get_base_attributes_mock.assert_called_once_with("property_node") + default_blank_property_mock.assert_called_once_with( + base_element_node=element_node_mock, + meta_model_base_attributes="meta_model_base_attributes", + elements_factory=model_element_factory_mock, + graph_node="property_node", ) - @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.DefaultProperty") - def test_create_property_with_extends(self, default_property_mock): - base_class_mock = mock.MagicMock(name="PropertyInstantiator_class") - base_class_mock._get_child.side_effect = ("payload_name", "extends", "characteristic") - base_class_mock._get_base_attributes.return_value = "meta_model_base_attributes" + def test_create_property_blank_node_raise_exception(self): + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.value.return_value = "" samm_mock = mock.MagicMock(name="SAMM") - samm_mock.get_urn.side_effect = ( - "urn", - "urn", - "urn", - "predicate", - ) - base_class_mock._samm = samm_mock - aspect_graph_mock = mock.MagicMock(name="aspect_graph") - aspect_graph_mock.value.return_value = "example_value" - base_class_mock._aspect_graph = aspect_graph_mock - default_property_mock.return_value = "instance" - result = PropertyInstantiator._create_property_with_extends(base_class_mock, "element_node") - - assert result == "instance" - base_class_mock._get_base_attributes.assert_called_once_with("element_node") - base_class_mock._get_child.assert_has_calls( - [ - mock.call("element_node", "urn"), - mock.call("element_node", "urn", required=True), - ] - ) - samm_mock.get_urn.assert_has_calls( - [ - mock.call(SAMM.payload_name), - mock.call(SAMM.extends), - mock.call(SAMM.characteristic), - mock.call(SAMM.example_value), - ] - ) - aspect_graph_mock.value.assert_called_once_with(subject="element_node", predicate="predicate") - default_property_mock.assert_called_once_with( - "meta_model_base_attributes", - "characteristic", - "example_value", - "extends", - payload_name="payload_name", + samm_mock.get_urn.return_value = "urn" + element_node_mock = mock.MagicMock(name="element_node") + element_node_mock.__str__.return_value = "element_node" + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + instantiator_cls._aspect_graph = graph_mock + instantiator_cls._samm = samm_mock + with pytest.raises(ValueError) as error: + instantiator_cls._create_property_blank_node(element_node_mock) + + assert str(error.value) == "Could not find property for the node element_node" + samm_mock.get_urn.assert_called_once_with(SAMM.property) + graph_mock.value.assert_called_once_with(subject=element_node_mock, predicate="urn") + + @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.property_instantiator.DefaultPropertyWithExtends") + def test_create_property_with_extends(self, default_property_with_extends_mock): + element_node_mock = mock.MagicMock(name="element_node") + model_element_factory_mock = mock.MagicMock(name="model_element_factory") + default_property_with_extends_mock.return_value = "default_property_with_extends" + get_base_attributes_mock = mock.MagicMock(name="_get_base_attributes") + get_base_attributes_mock.return_value = "base_attributes" + instantiator_cls = PropertyInstantiator(model_element_factory_mock) + instantiator_cls._get_base_attributes = get_base_attributes_mock + instantiator_cls._model_element_factory = model_element_factory_mock + result = instantiator_cls._create_property_with_extends(element_node_mock) + + assert result == "default_property_with_extends" + default_property_with_extends_mock.assert_called_once_with( + meta_model_base_attributes="base_attributes", + elements_factory=model_element_factory_mock, + graph_node=element_node_mock, ) + get_base_attributes_mock.assert_called_once_with(element_node_mock) diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_quantifiable_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_quantifiable_instantiator.py index ae5b63d..5c7555c 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_quantifiable_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_quantifiable_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.quantifiable_instantiator import QuantifiableInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestQuantifiableInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_range_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_range_constraint_instantiator.py index 0644b96..ab07766 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_range_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_range_constraint_instantiator.py @@ -4,7 +4,7 @@ from esmf_aspect_meta_model_python.base.bound_definition import BoundDefinition from esmf_aspect_meta_model_python.loader.instantiator.range_constraint_instantiator import RangeConstraintInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestRangeConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_regular_expression_constraint_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_regular_expression_constraint_instantiator.py index a2190bc..d71f9aa 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_regular_expression_constraint_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_regular_expression_constraint_instantiator.py @@ -5,7 +5,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.regular_expression_constraint_instantiator import ( RegularExpressionConstraintInstantiator, ) -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestRegularExpressionConstraintInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_set_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_set_instantiator.py index 521ad9d..65b5ab3 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_set_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_set_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.set_instantiator import SetInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestSetInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_sorted_set_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_sorted_set_instantiator.py index ba7d6dd..56a9f07 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_sorted_set_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_sorted_set_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.sorted_set_instantiator import SortedSetInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestSortedSetInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_structured_value_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_structured_value_instantiator.py index 2150df9..6b75b2a 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_structured_value_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_structured_value_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.structured_value_instantiator import StructuredValueInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestStructuredValueInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_time_series_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_time_series_instantiator.py index 8de8818..4b55bd4 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_time_series_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_time_series_instantiator.py @@ -6,7 +6,7 @@ from esmf_aspect_meta_model_python.loader.instantiator.constants import DATA_TYPE_ERROR_MSG from esmf_aspect_meta_model_python.loader.instantiator.time_series_instantiator import TimeSeriesInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestTimeSeriesInstantiator: diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_trait_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_trait_instantiator.py index 636c0e5..efcea9a 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_trait_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_trait_instantiator.py @@ -5,14 +5,16 @@ import pytest from esmf_aspect_meta_model_python.loader.instantiator.trait_instantiator import TraitInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMMC import SAMMC +from esmf_aspect_meta_model_python.vocabulary.sammc import SAMMC class TestTraitInstantiator: """TraitInstantiator unit tests class.""" + @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.trait_instantiator.Constraint") + @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.trait_instantiator.isinstance") @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.trait_instantiator.DefaultTrait") - def test_create_instance(self, default_trait_mock): + def test_create_instance(self, default_trait_mock, isinstance_mock, constraint_mock): base_class_mock = mock.MagicMock(name="TraitInstantiator_class") base_class_mock._get_base_attributes.return_value = "meta_model_base_attributes" base_class_mock._model_element_factory.create_element.return_value = "element" @@ -24,13 +26,13 @@ def test_create_instance(self, default_trait_mock): aspect_graph_mock.objects.return_value = ["constraint_subject"] base_class_mock._aspect_graph = aspect_graph_mock default_trait_mock.return_value = "instance" + isinstance_mock.return_value = True result = TraitInstantiator._create_instance(base_class_mock, "element_node") assert result == "instance" base_class_mock._get_base_attributes.assert_called_once_with("element_node") base_class_mock._get_child.assert_called_once_with("element_node", "urn", required=True) base_class_mock._model_element_factory.create_element.assert_called_once_with("constraint_subject") - base_class_mock._get_child.assert_called_once_with("element_node", "urn", required=True) aspect_graph_mock.objects.assert_called_once_with(subject="element_node", predicate="predicate") sammc_mock.get_urn.assert_has_calls( [ @@ -39,6 +41,30 @@ def test_create_instance(self, default_trait_mock): ] ) default_trait_mock.assert_called_once_with("meta_model_base_attributes", "base_characteristic", ["element"]) + isinstance_mock.assert_called_once_with("element", constraint_mock) + + @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.trait_instantiator.Constraint") + @mock.patch("esmf_aspect_meta_model_python.loader.instantiator.trait_instantiator.isinstance") + def test_create_instance_raise_exception_for_type(self, isinstance_mock, constraint_mock): + base_class_mock = mock.MagicMock(name="TraitInstantiator_class") + base_class_mock._get_base_attributes.return_value = "meta_model_base_attributes" + base_class_mock._model_element_factory.create_element.return_value = "element" + sammc_mock = mock.MagicMock(name="SAMMC") + sammc_mock.get_urn.side_effect = ("predicate", "urn") + base_class_mock._sammc = sammc_mock + aspect_graph_mock = mock.MagicMock(name="aspect_graph") + aspect_graph_mock.objects.return_value = ["constraint_subject"] + base_class_mock._aspect_graph = aspect_graph_mock + isinstance_mock.return_value = False + with pytest.raises(ValueError) as error: + TraitInstantiator._create_instance(base_class_mock, "element_node") + + assert str(error.value) == "Trait element_node has element element that is not a Constraint." + base_class_mock._get_base_attributes.assert_called_once_with("element_node") + base_class_mock._model_element_factory.create_element.assert_called_once_with("constraint_subject") + aspect_graph_mock.objects.assert_called_once_with(subject="element_node", predicate="predicate") + sammc_mock.get_urn.assert_called_once_with(SAMMC.constraint) + isinstance_mock.assert_called_once_with("element", constraint_mock) def test_create_instance_raise_exception(self): base_class_mock = mock.MagicMock(name="TraitInstantiator_class") diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_unit_instantiator.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_unit_instantiator.py index 48655af..e9cd9dc 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_unit_instantiator.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/instantiators/test_unit_instantiator.py @@ -3,7 +3,7 @@ from unittest import mock from esmf_aspect_meta_model_python.loader.instantiator.unit_instantiator import UnitInstantiator -from esmf_aspect_meta_model_python.vocabulary.SAMM import SAMM +from esmf_aspect_meta_model_python.vocabulary.samm import SAMM class TestUnitInstantiator: @@ -44,7 +44,7 @@ def test_create_instance(self, default_unit_mock): mock.call(SAMM.symbol), mock.call(SAMM.common_code), mock.call(SAMM.reference_unit), - mock.call(SAMM.numericConversionFactor), + mock.call(SAMM.numeric_conversion_factor), mock.call(SAMM.quantity_kind), ] ) diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/test_aspect_loader.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/test_aspect_loader.py deleted file mode 100644 index f2bf74e..0000000 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/test_aspect_loader.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Aspect model Loader test suite.""" - -from unittest import mock - -from esmf_aspect_meta_model_python.loader.aspect_loader import AspectLoader - - -class TestAspectLoader: - @mock.patch("esmf_aspect_meta_model_python.loader.aspect_loader.SAMMGraph") - def test_load_aspect_model(self, samm_graph_mock): - graph_mock = mock.MagicMock(name="graph") - graph_mock.to_python.return_value = "python_aspect_nodes" - samm_graph_mock.return_value = graph_mock - loader = AspectLoader() - result = loader.load_aspect_model("graph", "aspect_urn") - - assert result == "python_aspect_nodes" - samm_graph_mock.assert_called_once_with(graph="graph") - graph_mock.to_python.assert_called_once_with("aspect_urn") diff --git a/core/esmf-aspect-meta-model-python/tests/unit/loader/test_samm_graph.py b/core/esmf-aspect-meta-model-python/tests/unit/loader/test_samm_graph.py index 8b3ed2a..f55a79d 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/loader/test_samm_graph.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/loader/test_samm_graph.py @@ -4,173 +4,295 @@ import pytest -from rdflib import RDF - from esmf_aspect_meta_model_python.loader.samm_graph import SAMMGraph class TestSAMMGraph: """SAMM Graph test suite.""" - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_init(self, populate_with_meta_data_mock): - result = SAMMGraph("graph", "cache") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.DefaultElementCache") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.Graph") + def test_init(self, graph_mock, default_element_cache_mock): + graph_mock.side_effect = ("rdf_graph", "samm_graph") + default_element_cache_mock.return_value = "cache" + result = SAMMGraph() - assert result._graph == "graph" + assert result.rdf_graph == "rdf_graph" + assert result.samm_graph == "samm_graph" assert result._cache == "cache" - assert result._samm_version == "" - populate_with_meta_data_mock.assert_called_once() - - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_rdf_graph(self, _): - samm_graph = SAMMGraph("graph", "cache") - result = samm_graph.get_rdf_graph() - - assert result == "graph" - - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_samm_version_from_graph(self, _): - graph_mock = mock.MagicMock(name="graph") - namespace_manager_mock = mock.MagicMock(name="namespace_manager") - namespace_manager_mock.namespaces.return_value = [ - ("prefix", "some_link"), - ("", "namespace"), - ("samm", "namespace:path:to:model:3.2.1#"), + assert result.samm_version is None + assert result.aspect is None + assert result.model_elements is None + assert result._samm is None + assert result._reader is None + + def test_str(self): + samm_graph = SAMMGraph() + samm_graph.samm_version = "1.2.3" + result = str(samm_graph) + + assert result == "SAMMGraph v1.2.3" + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.id") + def test_repr(self, id_mock): + id_mock.return_value = "instance_id" + samm_graph = SAMMGraph() + result = repr(samm_graph) + + assert result == ( + ")>" + ) + id_mock.assert_called_once_with(samm_graph) + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.InputHandler") + def test_get_rdf_graph(self, input_handler_mock): + input_data = "test_data" + input_type = "data_type" + reader_mock = mock.MagicMock(name="reade") + input_handler_mock.return_value.get_reader.return_value = reader_mock + reader_mock.read.return_value = "rdf_graph" + samm_graph = SAMMGraph() + samm_graph._get_rdf_graph(input_data, input_type) + + assert samm_graph.rdf_graph == "rdf_graph" + input_handler_mock.assert_called_once_with(input_data, input_type) + input_handler_mock.return_value.get_reader.assert_called_once() + reader_mock.read.assert_called_once_with(input_data) + + def test_get_samm_version_from_rdf_graph(self): + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.namespace_manager.namespaces.return_value = [ + ("samm", "urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#"), ] - graph_mock.namespace_manager = namespace_manager_mock - samm_graph = SAMMGraph(graph_mock, "cache") - result = samm_graph._get_samm_version_from_graph() + samm_graph = SAMMGraph() + samm_graph.rdf_graph = graph_mock + result = samm_graph._get_samm_version_from_rdf_graph() + + assert result == "2.1.0" + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm_version_from_rdf_graph") + def test_get_samm_version(self, get_samm_version_from_rdf_graph_mock): + get_samm_version_from_rdf_graph_mock.return_value = "1.2.3" + samm_graph = SAMMGraph() + samm_graph._get_samm_version() - assert result == "3.2.1" + assert samm_graph.samm_version == "1.2.3" + get_samm_version_from_rdf_graph_mock.assert_called_once() + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm_version_from_rdf_graph") + def test_get_samm_version_raises_value_error(self, get_samm_version_from_rdf_graph_mock): + get_samm_version_from_rdf_graph_mock.return_value = "" + samm_graph = SAMMGraph() + samm_graph.rdf_graph = "rdf_graph" - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm_version_from_graph") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_samm_version_raise_error(self, _, get_samm_version_from_graph_mock): - get_samm_version_from_graph_mock.return_value = "" - samm_graph = SAMMGraph("graph", "cache") with pytest.raises(ValueError) as error: - samm_graph.get_samm_version() + samm_graph._get_samm_version() - assert str(error.value) == "SAMM version not found in the Graph." - get_samm_version_from_graph_mock.assert_called_once() + assert str(error.value) == ( + "SAMM version number was not found in graph. Could not process RDF graph rdf_graph." + ) + get_samm_version_from_rdf_graph_mock.assert_called_once() - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm_version_from_graph") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_samm_version(self, _, get_samm_version_from_graph_mock): - get_samm_version_from_graph_mock.return_value = "1.2.3" - samm_graph = SAMMGraph("graph", "cache") - result = samm_graph.get_samm_version() + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMM") + def test_get_samm(self, samm_mock): + samm_mock.return_value = "samm" + samm_graph = SAMMGraph() + samm_graph.samm_version = "1.2.3" + samm_graph._get_samm() - assert result is None - assert samm_graph._samm_version == "1.2.3" - get_samm_version_from_graph_mock.assert_called_once() + assert samm_graph._samm == "samm" + samm_mock.assert_called_once_with("1.2.3") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.AspectMetaModelResolver") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.get_samm_version") - def test_populate_with_meta_data(self, get_samm_version_mock, aspect_meta_model_resolver_mock): - meta_model_reader_mock = mock.MagicMock(name="meta_model_reader") - aspect_meta_model_resolver_mock.return_value = meta_model_reader_mock - _ = SAMMGraph("graph", "cache") + def test_get_samm_graph(self, aspect_meta_model_resolver_mock): + samm_graph = SAMMGraph() + samm_graph.samm_graph = "samm_graph" + samm_graph.samm_version = "1.2.3" + result = samm_graph._get_samm_graph() + assert result is None + aspect_meta_model_resolver_mock.return_value.parse.assert_called_once_with("samm_graph", "1.2.3") + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm_graph") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_samm_version") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_rdf_graph") + def test_parse(self, get_rdf_graph_mock, get_samm_version_mock, get_samm_mock, get_samm_graph_mock): + samm_graph = SAMMGraph() + result = samm_graph.parse("input_data", "input_type") + + assert result == samm_graph + get_rdf_graph_mock.assert_called_once_with("input_data", "input_type") get_samm_version_mock.assert_called_once() - aspect_meta_model_resolver_mock.assert_called_once() - meta_model_reader_mock.parse.assert_called_once_with("graph", "") + get_samm_mock.assert_called_once() + get_samm_graph_mock.assert_called_once() + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.RDF.type") + def test_get_aspect_urn(self, rdf_type_mock): + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.subjects.return_value = ["aspect_urn", "aspect_urn_2"] + samm_mock = mock.MagicMock(name="samm") + samm_mock.get_urn.return_value = "aspect_type_urn" + samm_mock.Aspect = "Aspect" + samm_graph = SAMMGraph() + samm_graph.rdf_graph = graph_mock + samm_graph._samm = samm_mock + result = samm_graph.get_aspect_urn() + + assert result == "aspect_urn" + graph_mock.subjects.assert_called_once_with(predicate=rdf_type_mock, object="aspect_type_urn") + samm_mock.get_urn.assert_called_once_with("Aspect") + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.RDF.type") + def test_get_aspect_urn_raise_error(self, rdf_type_mock): + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.subjects.return_value = [] + samm_mock = mock.MagicMock(name="samm") + samm_mock.get_urn.return_value = "aspect_type_urn" + samm_mock.Aspect = "Aspect" + samm_graph = SAMMGraph() + samm_graph.rdf_graph = graph_mock + samm_graph._samm = samm_mock + with pytest.raises(ValueError) as error: + samm_graph.get_aspect_urn() + + assert str(error.value) == "Could not found Aspect node in the RDF graph." + graph_mock.subjects.assert_called_once_with(predicate=rdf_type_mock, object="aspect_type_urn") + samm_mock.get_urn.assert_called_once_with("Aspect") + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.RDF.type") + def test_get_node_from_graph(self, rdf_type_mock): + graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.subjects.return_value = ["node1", "node2"] + samm_graph = SAMMGraph() + samm_graph.rdf_graph = graph_mock + node_mock = mock.MagicMock(name="node") + result = samm_graph._get_node_from_graph(node_mock) + + assert result == ["node1", "node2"] + graph_mock.subjects.assert_called_once_with(predicate=rdf_type_mock, object=node_mock) + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._get_node_from_graph") + def test_get_all_model_elements(self, get_node_from_graph_mock): + samm_mock = mock.MagicMock(name="samm") + samm_mock.meta_model_elements = ["element1", "element2"] + samm_mock.get_urn.side_effect = ("urn1", "urn2") + get_node_from_graph_mock.side_effect = (["node1"], ["node2"]) + samm_graph = SAMMGraph() + samm_graph._samm = samm_mock + result = samm_graph.get_all_model_elements() + + assert result == ["node1", "node2"] + samm_mock.get_urn.assert_has_calls( + [ + mock.call("element1"), + mock.call("element2"), + ] + ) + get_node_from_graph_mock.assert_has_calls( + [ + mock.call("urn1"), + mock.call("urn2"), + ] + ) + + def test_get_all_model_elements_raises_value_error(self): + samm_mock = mock.MagicMock(name="samm") + samm_mock.meta_model_elements = [] + samm_graph = SAMMGraph() + samm_graph._samm = samm_mock - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_prepare_graph(self, populate_with_meta_data_mock): - _ = SAMMGraph(cache="cache") + with pytest.raises(ValueError) as error: + samm_graph.get_all_model_elements() - populate_with_meta_data_mock.assert_called_once() + assert str(error.value) == "There are no SAMM elements in the RDF graph." - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMM") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.Graph") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_aspect_nodes_from_graph(self, _, rdf_graph_mock, samm_mock): - rdf_graph_mock.return_value = rdf_graph_mock - rdf_graph_mock.subjects.return_value = ["subject_1", "subject_2"] - samm_mock.return_value = samm_mock - samm_mock.aspect = "aspect" - samm_mock.get_urn.return_value = "aspect_urn" - samm_graph = SAMMGraph(cache="cache") - samm_graph._samm_version = "1.2.3" - result = samm_graph.get_aspect_nodes_from_graph() - - assert result == ["subject_1", "subject_2"] - samm_mock.assert_called_once_with("1.2.3") - samm_mock.get_urn.assert_called_once_with("aspect") - rdf_graph_mock.assert_called_once() - rdf_graph_mock.subjects.assert_called_once_with(predicate=RDF.type, object="aspect_urn") + def test_load_aspect_model(self): + samm_graph = SAMMGraph() + samm_graph.aspect = "aspect" + result = samm_graph.load_aspect_model() - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.URIRef") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.isinstance") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_base_nodes_with_aspect_urn(self, _, isinstance_mock, uri_ref_mock): - isinstance_mock.return_value = False - uri_ref_mock.return_value = "aspect_uri_ref" - samm_graph = SAMMGraph("graph", "cache") - result = samm_graph.get_base_nodes("aspect_urn") + assert result == "aspect" + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.ModelElementFactory") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.get_aspect_urn") + def test_load_aspect_model_create_element(self, get_aspect_urn_mock, model_element_factory_mock): + reader_mock = mock.MagicMock(name="reader") + cache_mock = mock.MagicMock(name="cache") + samm_graph = SAMMGraph() + samm_graph.rdf_graph = "rdf_graph" + samm_graph.samm_graph = "_samm_graph" + samm_graph._reader = reader_mock + samm_graph.samm_version = "1.2.3" + samm_graph._cache = cache_mock + samm_graph.aspect = None + get_aspect_urn_mock.return_value = "aspect_urn" + model_element_factory_mock.return_value = model_element_factory_mock + model_element_factory_mock.create_element.return_value = "aspect" + result = samm_graph.load_aspect_model() - assert result == ["aspect_uri_ref"] - isinstance_mock.assert_called_once_with("aspect_urn", uri_ref_mock) - uri_ref_mock.assert_called_once_with("aspect_urn") + assert result == "aspect" + get_aspect_urn_mock.assert_called_once() + reader_mock.prepare_aspect_model.assert_called_once_with("rdf_graph_samm_graph") + model_element_factory_mock.assert_called_once_with("1.2.3", "rdf_graph_samm_graph", cache_mock) + model_element_factory_mock.create_element.assert_called_once_with("aspect_urn") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.get_aspect_nodes_from_graph") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_get_base_nodes_no_aspect_urn(self, _, get_nodes_from_graph_mock): - get_nodes_from_graph_mock.return_value = ["base_node"] - samm_graph = SAMMGraph("graph", "cache") - result = samm_graph.get_base_nodes() + def test_load_model_elements(self): + samm_graph = SAMMGraph() + samm_graph.model_elements = "model_elements" + result = samm_graph.load_model_elements() - assert result == ["base_node"] - get_nodes_from_graph_mock.assert_called_once() + assert result == "model_elements" @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.ModelElementFactory") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.get_base_nodes") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_to_python(self, _, get_base_nodes_mock, model_element_factory_mock): - get_base_nodes_mock.return_value = "base_nodes" + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.get_all_model_elements") + def test_load_model_elements_create_elements(self, get_all_model_elements_mock, model_element_factory_mock): + reader_mock = mock.MagicMock(name="reader") + cache_mock = mock.MagicMock(name="cache") + samm_graph = SAMMGraph() + samm_graph.rdf_graph = "rdf_graph" + samm_graph.samm_graph = "_samm_graph" + samm_graph._reader = reader_mock + samm_graph.samm_version = "1.2.3" + samm_graph._cache = cache_mock + samm_graph.model_elements = None + get_all_model_elements_mock.return_value = "model_elements" model_element_factory_mock.return_value = model_element_factory_mock - model_element_factory_mock.create_all_graph_elements.return_value = ["aspect_elements"] - samm_graph = SAMMGraph("graph", "cache") - samm_graph._samm_version = "samm_version" - result = samm_graph.to_python("aspect_urn") + model_element_factory_mock.create_all_graph_elements.return_value = "model_elements" + result = samm_graph.load_model_elements() - assert result == ["aspect_elements"] - get_base_nodes_mock.assert_called_once_with("aspect_urn") - model_element_factory_mock.assert_called_once_with("samm_version", "graph", "cache") - model_element_factory_mock.create_all_graph_elements.assert_called_once_with("base_nodes") + assert result == "model_elements" + get_all_model_elements_mock.assert_called_once() + reader_mock.prepare_aspect_model.assert_called_once_with("rdf_graph_samm_graph") + model_element_factory_mock.assert_called_once_with("1.2.3", "rdf_graph_samm_graph", cache_mock) + model_element_factory_mock.create_all_graph_elements.assert_called_once_with("model_elements") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.DefaultElementCache") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_find_by_name(self, _, default_element_cache_mock): + def test_find_by_name(self): cache_mock = mock.MagicMock(name="cache") - cache_mock.get_by_name.return_value = "graph_node" - default_element_cache_mock.return_value = cache_mock - samm_graph = SAMMGraph("graph") + cache_mock.get_by_name.return_value = "node" + samm_graph = SAMMGraph() + samm_graph._cache = cache_mock result = samm_graph.find_by_name("element_name") - assert result == "graph_node" + assert result == "node" cache_mock.get_by_name.assert_called_once_with("element_name") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.DefaultElementCache") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_find_by_urn(self, _, default_element_cache_mock): + def test_find_by_urn(self): cache_mock = mock.MagicMock(name="cache") - cache_mock.get_by_urn.return_value = "graph_node" - default_element_cache_mock.return_value = cache_mock - samm_graph = SAMMGraph("graph") + cache_mock.get_by_urn.return_value = "node" + samm_graph = SAMMGraph() + samm_graph._cache = cache_mock result = samm_graph.find_by_urn("urn") - assert result == "graph_node" + assert result == "node" cache_mock.get_by_urn.assert_called_once_with("urn") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.determine_element_access_path") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.find_by_name") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_determine_access_path(self, _, find_by_name_mock, determine_element_access_path_mock): + def test_determine_access_path(self, find_by_name_mock, determine_element_access_path_mock): find_by_name_mock.side_effect = (["base_element"], []) determine_element_access_path_mock.return_value = ["access_path"] - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph.determine_access_path("base_element_name") assert result == ["access_path"] @@ -180,10 +302,27 @@ def test_determine_access_path(self, _, find_by_name_mock, determine_element_acc @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._SAMMGraph__determine_access_path") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.Property") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.isinstance") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") + def test_determine_element_access_path_no_property( + self, + isinstance_mock, + property_mock, + determine_access_path_mock, + ): + isinstance_mock.return_value = False + base_element_mock = mock.MagicMock(name="base_element") + determine_access_path_mock.return_value = "element_access_path" + samm_graph = SAMMGraph() + result = samm_graph.determine_element_access_path(base_element_mock) + + assert result == "element_access_path" + isinstance_mock.assert_called_once_with(base_element_mock, property_mock) + determine_access_path_mock.assert_called_once_with(base_element_mock, []) + + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._SAMMGraph__determine_access_path") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.Property") + @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.isinstance") def test_determine_element_access_path_with_payload_name( self, - _, isinstance_mock, property_mock, determine_access_path_mock, @@ -192,7 +331,7 @@ def test_determine_element_access_path_with_payload_name( base_element_mock = mock.MagicMock(name="base_element") base_element_mock.payload_name = "payload_name" determine_access_path_mock.return_value = "element_access_path" - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph.determine_element_access_path(base_element_mock) assert result == "element_access_path" @@ -202,10 +341,8 @@ def test_determine_element_access_path_with_payload_name( @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph._SAMMGraph__determine_access_path") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.Property") @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.isinstance") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") def test_determine_element_access_path_base_element_name( self, - _, isinstance_mock, property_mock, determine_access_path_mock, @@ -215,55 +352,50 @@ def test_determine_element_access_path_base_element_name( base_element_mock.name = "base_element_name" base_element_mock.payload_name = None determine_access_path_mock.return_value = "element_access_path" - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph.determine_element_access_path(base_element_mock) assert result == "element_access_path" isinstance_mock.assert_called_once_with(base_element_mock, property_mock) determine_access_path_mock.assert_called_once_with(base_element_mock, [["base_element_name"]]) - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_determine_access_path_base_element_is_none(self, _): - samm_graph = SAMMGraph("graph", "cache") + def test_determine_access_path_base_element_is_none(self): + samm_graph = SAMMGraph() result = samm_graph._SAMMGraph__determine_access_path(None, "path") assert result == "path" - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_determine_access_path_parent_element_is_none(self, _): + def test_determine_access_path_parent_element_is_none(self): base_element_mock = mock.MagicMock(name="base_element") base_element_mock.parent_elements = None - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph._SAMMGraph__determine_access_path(base_element_mock, "path") assert result == "path" - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_determine_access_path_parent_element_is_empty_list(self, _): + def test_determine_access_path_parent_element_is_empty_list(self): base_element_mock = mock.MagicMock(name="base_element") base_element_mock.parent_elements = [] - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph._SAMMGraph__determine_access_path(base_element_mock, "path") assert result == "path" @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.isinstance") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_private_determine_access_path_parent_payload_name(self, _, isinstance_mock): + def test_private_determine_access_path_parent_payload_name(self, isinstance_mock): parent_element_mock = mock.MagicMock(name="parent_element") parent_element_mock.parent_elements = [] parent_element_mock.payload_name = "payload_name" base_element_mock = mock.MagicMock(name="base_element") base_element_mock.parent_elements = [parent_element_mock] isinstance_mock.return_value = True - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph._SAMMGraph__determine_access_path(base_element_mock, [["path"]]) assert result == [["payload_name", "path"]] @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.isinstance") - @mock.patch("esmf_aspect_meta_model_python.loader.samm_graph.SAMMGraph.populate_with_meta_data") - def test_private_determine_access_path_parent_name(self, _, isinstance_mock): + def test_private_determine_access_path_parent_name(self, isinstance_mock): parent_element_mock = mock.MagicMock(name="parent_element") parent_element_mock.parent_elements = [] parent_element_mock.payload_name = None @@ -271,7 +403,7 @@ def test_private_determine_access_path_parent_name(self, _, isinstance_mock): base_element_mock = mock.MagicMock(name="base_element") base_element_mock.parent_elements = [parent_element_mock] isinstance_mock.return_value = True - samm_graph = SAMMGraph("graph", "cache") + samm_graph = SAMMGraph() result = samm_graph._SAMMGraph__determine_access_path(base_element_mock, [["path"]]) assert result == [["payload_element_name", "path"]] diff --git a/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_data_string.py b/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_data_string.py index a8229ae..2ebdf00 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_data_string.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_data_string.py @@ -2,8 +2,6 @@ from unittest import mock -from rdflib import RDF - from esmf_aspect_meta_model_python.resolver.data_string import DataStringResolver @@ -19,22 +17,3 @@ def test_read(self, rdf_graph_mock): assert result == graph_mock graph_mock.parse.assert_called_once_with(data="data_string") - - @mock.patch("esmf_aspect_meta_model_python.resolver.data_string.SAMM") - @mock.patch("esmf_aspect_meta_model_python.resolver.data_string.DataStringResolver.get_samm_version") - def test_get_aspect_urn(self, get_samm_version_mock, samm_class_mock): - samm_mock = mock.MagicMock(name="samm") - samm_mock.get_urn.return_value = "aspect_urn" - samm_class_mock.return_value = samm_mock - samm_class_mock.aspect = "aspect" - get_samm_version_mock.return_value = "1.2.3" - graph_mock = mock.MagicMock(mname="graph") - graph_mock.value.return_value = "aspect_urn" - resolver = DataStringResolver() - resolver.graph = graph_mock - result = resolver.get_aspect_urn() - - assert result == "aspect_urn" - get_samm_version_mock.assert_called_once() - samm_mock.get_urn.assert_called_once_with("aspect") - graph_mock.value.assert_called_once_with(predicate=RDF.type, object="aspect_urn", any=False) diff --git a/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_handler.py b/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_handler.py index 44359b1..4946d96 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_handler.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_handler.py @@ -41,20 +41,6 @@ def test_get_reader_raise_error(self): assert str(error.value) == "Unknown input type" - @mock.patch("esmf_aspect_meta_model_python.resolver.handler.InputHandler.get_reader") - def test_get_rdf_graph(self, get_reader_mock): - reader_mock = mock.MagicMock(name="reader") - reader_mock.read.return_value = "graph" - reader_mock.get_aspect_urn.return_value = "aspect_urn" - get_reader_mock.return_value = reader_mock - handler = InputHandler("input_data", "input_type") - result = handler.get_rdf_graph() - - assert len(result) == 2 - graph, aspect_urn = result - assert graph == "graph" - assert aspect_urn == "aspect_urn" - @mock.patch("esmf_aspect_meta_model_python.resolver.handler.os.path.isfile") @mock.patch("esmf_aspect_meta_model_python.resolver.handler.InputHandler.contains_newline") def test_guess_input_type_file_path(self, contains_newline_mock, isfile_mock): diff --git a/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_local_file.py b/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_local_file.py index 7c53167..2ad2341 100644 --- a/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_local_file.py +++ b/core/esmf-aspect-meta-model-python/tests/unit/resolver/test_local_file.py @@ -4,8 +4,6 @@ import pytest -from rdflib import RDF - from esmf_aspect_meta_model_python.resolver.local_file import LocalFileResolver @@ -36,6 +34,18 @@ def test_validate_file_raise_error(self, exists_mock): assert str(error.value) == "Could not find a file file_path" exists_mock.assert_called_once_with("file_path") + @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.Graph") + @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.LocalFileResolver.validate_file") + def test_read(self, validate_file_mock, graph_mock): + rdf_graph_mock = mock.MagicMock(name="rdf_graph") + graph_mock.return_value = rdf_graph_mock + resolver = LocalFileResolver() + result = resolver.read("file_path") + + assert result == rdf_graph_mock + validate_file_mock.assert_called_once_with("file_path") + rdf_graph_mock.parse.assert_called_once_with("file_path") + def test_parse_namespace_no_data(self): resolver = LocalFileResolver() result = resolver._parse_namespace("urn:samm:org.eclipse.esmf.samm:2.1.0#") @@ -148,54 +158,11 @@ def test_get_dependency_files(self, get_dependency_folders_mock, get_additional_ get_additional_files_from_dir_mock.assert_called_once_with("dependency_folder") @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.LocalFileResolver._get_dependency_files") - def test_parse_namespaces(self, get_dependency_files_mock): - resolver = LocalFileResolver() - result = resolver.parse_namespaces("aspect_file_path") - - assert result is None - get_dependency_files_mock.assert_called_once_with({}, {}, "aspect_file_path") - - @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.LocalFileResolver.parse_namespaces") - @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.LocalFileResolver._find_aspect_urn") - def test_read(self, find_aspect_urn_mock, parse_namespaces_mock): + def test_prepare_aspect_model(self, get_dependency_files_mock): graph_mock = mock.MagicMock(name="graph") resolver = LocalFileResolver() - resolver.graph = graph_mock - result = resolver.read("file_path") - - assert result == graph_mock - assert resolver.file_path == "file_path" - graph_mock.parse.assert_called_once_with("file_path") - find_aspect_urn_mock.assert_called_once() - parse_namespaces_mock.assert_called_once_with("file_path") - - @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.Path") - @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.SAMM") - @mock.patch("esmf_aspect_meta_model_python.resolver.local_file.LocalFileResolver.get_samm_version") - def test_find_aspect_urn(self, get_samm_version_mock, samm_class_mock, path_mock): - samm_mock = mock.MagicMock(name="samm") - samm_mock.get_urn.return_value = "aspect_urn" - samm_class_mock.return_value = samm_mock - samm_class_mock.aspect = "aspect" - get_samm_version_mock.return_value = "1.2.3" - graph_mock = mock.MagicMock(mname="graph") - graph_mock.subjects.return_value = ["path#aspect_name", "path#another_aspect_urn"] - path_mock.return_value = path_mock - path_mock.stem = "aspect" - resolver = LocalFileResolver() - resolver.file_path = "path/to/file/aspect_name.ttl" - resolver.graph = graph_mock - result = resolver._find_aspect_urn() + resolver.file_path = "aspect_file_path" + result = resolver.prepare_aspect_model(graph_mock) assert result is None - get_samm_version_mock.assert_called_once() - samm_mock.get_urn.assert_called_once_with("aspect") - graph_mock.subjects.assert_called_once_with(predicate=RDF.type, object="aspect_urn") - path_mock.assert_called_once_with("path/to/file/aspect_name.ttl") - - def test_get_aspect_urn(self): - resolver = LocalFileResolver() - resolver.aspect_urn = "aspect_urn" - result = resolver.get_aspect_urn() - - assert result == "aspect_urn" + get_dependency_files_mock.assert_called_once_with({}, {}, "aspect_file_path") diff --git a/core/esmf-aspect-meta-model-python/tests_invalid/test_constraints.py b/core/esmf-aspect-meta-model-python/tests_invalid/test_constraints.py index 163076c..b38d6f6 100644 --- a/core/esmf-aspect-meta-model-python/tests_invalid/test_constraints.py +++ b/core/esmf-aspect-meta-model-python/tests_invalid/test_constraints.py @@ -13,25 +13,22 @@ import pytest -from esmf_aspect_meta_model_python import AspectLoader -from esmf_aspect_meta_model_python.resolver.handler import InputHandler +from esmf_aspect_meta_model_python import SAMMGraph RESOURCE_PATH = Path("tests_invalid/resources") def test_trait_missing_base_characteristic(): file_path = RESOURCE_PATH / "trait_missing_base_characteristic.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() + samm_graph = SAMMGraph() + samm_graph.parse(file_path) with pytest.raises(ValueError): - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) + samm_graph.load_aspect_model() def test_trait_missing_constraint(): file_path = RESOURCE_PATH / "trait_missing_constraint.ttl" - handler = InputHandler(str(file_path), input_type=InputHandler.FILE_PATH_TYPE) - rdf_graph, aspect_urn = handler.get_rdf_graph() + samm_graph = SAMMGraph() + samm_graph.parse(file_path) with pytest.raises(ValueError): - loader = AspectLoader() - _ = loader.load_aspect_model(rdf_graph, aspect_urn) + samm_graph.load_aspect_model()