Skip to content

Commit

Permalink
eclipse-esmf#43 state initiator implementation v2 (#1)
Browse files Browse the repository at this point in the history
* StateInstantiator modified for EnumerationInstantiator inheritance
  • Loading branch information
thec0dewriter authored Oct 16, 2024
1 parent e372dd8 commit a85adaf
Showing 1 changed file with 7 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# SPDX-License-Identifier: MPL-2.0

import typing
from typing import Any, Dict, Optional

import rdflib # type: ignore

Expand All @@ -18,13 +18,13 @@
from esmf_aspect_meta_model_python.base.characteristics.state import State
from esmf_aspect_meta_model_python.impl.characteristics.default_state import DefaultState
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.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


class StateInstantiator(InstantiatorBase[State]):
class StateInstantiator(EnumerationInstantiator):
def _create_instance(self, element_node: Node) -> State:
data_type = self._get_data_type(element_node)
if data_type is None:
Expand All @@ -46,9 +46,9 @@ def _create_instance(self, element_node: Node) -> State:

return DefaultState(meta_model_base_attributes, data_type, values, default)

def __to_state_node_value(self, value_node: Node) -> typing.Dict:
def __to_state_node_value(self, value_node: Optional[Node]) -> Dict:
"""
This method instantiates one possible value of an state.
This method instantiates one possible value of a state.
:param value_node: Node of the Graph that represents one state value.
The Argument can either be a Literal or a URIRef.
- If value_node is a Literal it will represent e.g. a string or an integer value
Expand All @@ -66,9 +66,9 @@ def __to_state_node_value(self, value_node: Node) -> typing.Dict:
for property_urn, property_value in value_node_properties:
if property_urn != rdflib.RDF.type and isinstance(property_urn, str):
property_name = property_urn.split("#")[1]
actual_value: typing.Optional[typing.Any]
actual_value: Optional[Any]
if self.__is_collection_value(property_urn):
actual_value = self.__instantiate_state_collection(property_value)
actual_value = self.__instantiate_enum_collection(property_value)
else:
actual_value = self.__to_state_node_value(property_value)
value[property_name] = actual_value
Expand All @@ -84,20 +84,3 @@ def __to_state_node_value(self, value_node: Node) -> typing.Dict:
f"Every value of an state must either be a Literal (string, int, etc.) or "
f"a URI reference to a ComplexType. Values of type {type(value_node).__name__} are not allowed"
)

def __is_collection_value(self, property_subject: str) -> bool:
characteristic = self._aspect_graph.value( # type: ignore
subject=property_subject,
predicate=self._samm.get_urn(SAMM.characteristic),
)
characteristic_type = self._aspect_graph.value(subject=characteristic, predicate=rdflib.RDF.type)
return characteristic_type in self._sammc.collections_urns()

def __instantiate_state_collection(self, value_list) -> typing.List[typing.Dict]:
"""creates a collection as a child for state characteristics"""
value_node_list = RdfHelper.get_rdf_list_values(value_list, self._aspect_graph)
values = []
for value_node in value_node_list:
value = self.__to_state_node_value(value_node)
values.append(value)
return values

0 comments on commit a85adaf

Please sign in to comment.