1- # Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
1+ # Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
22#
33# See the AUTHORS file(s) distributed with this work for additional
44# information regarding authorship.
99#
1010# SPDX-License-Identifier: MPL-2.0
1111
12- from pathlib import Path
1312from typing import List , Optional , Union
1413
1514from rdflib import RDF , Graph , URIRef
1615from rdflib .graph import Node
1716
17+ from esmf_aspect_meta_model_python .base .aspect import Aspect
1818from esmf_aspect_meta_model_python .base .base import Base
1919from esmf_aspect_meta_model_python .base .property import Property
2020from esmf_aspect_meta_model_python .loader .default_element_cache import DefaultElementCache
2121from esmf_aspect_meta_model_python .loader .model_element_factory import ModelElementFactory
22- from esmf_aspect_meta_model_python .resolver .base import AspectModelResolver , BaseResolver
22+ from esmf_aspect_meta_model_python .resolver .meta_model import AspectMetaModelResolver
2323from esmf_aspect_meta_model_python .vocabulary .SAMM import SAMM
2424
2525
@@ -31,16 +31,13 @@ class SAMMGraph:
3131 def __init__ (
3232 self ,
3333 graph : Graph | None = None ,
34- resolver : BaseResolver | None = None ,
35- cache : DefaultElementCache | None = None ,
34+ cache : Union [DefaultElementCache , None ] = None ,
3635 ):
37- super ().__init__ ()
38-
3936 self ._graph = graph if graph else Graph ()
40- self ._resolver = resolver if resolver else AspectModelResolver ()
4137 self ._cache = cache if cache else DefaultElementCache ()
4238 self ._samm_version = ""
43- self ._file_path : str = ""
39+
40+ self .populate_with_meta_data ()
4441
4542 def __repr__ (self ) -> str :
4643 return repr (self ._graph )
@@ -52,7 +49,7 @@ def get_rdf_graph(self) -> Graph:
5249 """Get RDF graph."""
5350 return self ._graph
5451
55- def get_samm_version (self ) -> str :
52+ def _get_samm_version_from_graph (self ):
5653 """Get SAMM version from the graph."""
5754 version = ""
5855
@@ -63,78 +60,55 @@ def get_samm_version(self) -> str:
6360
6461 return version
6562
66- @staticmethod
67- def convert_file_path (file_path : Union [Path , str ]) -> str :
68- """Convert file_path to the string.
69-
70- :param file_path: path to model file
71- """
72- if isinstance (file_path , Path ):
73- file_path = str (file_path )
74-
75- return file_path
76-
77- def parse (self , file_path : Union [Path , str ]) -> Graph :
78- """Parse a file to the SAMM graph.
79-
80- :param file_path: Path to the *ttl file.
81- """
82- self ._file_path = self .convert_file_path (file_path )
83- self ._graph .parse (self ._file_path )
84- self ._samm_version = self .get_samm_version ()
85- self ._resolver .resolve (self ._graph , self ._file_path , self ._samm_version )
86-
87- return self ._graph
63+ def get_samm_version (self ):
64+ """Get SAMM version from the graph."""
65+ version = self ._get_samm_version_from_graph ()
8866
89- def _get_model_file_path (self , model_file_path : str = "" ) -> str :
90- """Get a model file path.
67+ if not version :
68+ raise ValueError ("SAMM version not found in the Graph." )
69+ else :
70+ self ._samm_version = version
9171
92- :param model_file_path: str with path to the model
93- :return: validated path rto the model fiel
94- """
95- model_file_path = model_file_path if model_file_path else self ._file_path
96- if not model_file_path :
97- raise ValueError ("Path to the model is empty" )
72+ def populate_with_meta_data (self ):
73+ """Populate RDF graph with SAMM data."""
74+ if not self ._samm_version :
75+ self .get_samm_version ()
9876
99- return model_file_path
77+ meta_model_reader = AspectMetaModelResolver ()
78+ meta_model_reader .parse (self ._graph , self ._samm_version )
10079
101- def get_nodes_from_graph (self , model_file_path : str = "" ) -> List [Node ]:
102- """Get a list of URIRef to nodes from the base model file ."""
80+ def get_aspect_nodes_from_graph (self ) -> List [Node ]:
81+ """Get a list of Aspect nodes from the graph ."""
10382 nodes = []
104- model_file_path = self ._get_model_file_path (model_file_path )
105- base_graph = Graph ().parse (model_file_path , format = "turtle" )
83+ samm = SAMM (self ._samm_version )
10684
10785 # Search for Aspect elements
108- samm = SAMM (self ._samm_version )
109- for subject in base_graph .subjects (predicate = RDF .type , object = samm .get_urn (SAMM .aspect )): # type: ignore
86+ for subject in self ._graph .subjects (predicate = RDF .type , object = samm .get_urn (SAMM .aspect )): # type: ignore
11087 nodes .append (subject )
11188
112- if not nodes :
113- for subject , object in base_graph .subject_objects (predicate = RDF .type , unique = True ):
114- prefix_data = str (object ).replace ("<" , "" ).split (":" )
115- if ":" .join (prefix_data [:3 ]) == self .samm_prefix :
116- nodes .append (subject )
117-
11889 return nodes
11990
12091 def get_base_nodes (self , aspect_urn : URIRef | str = "" ) -> List [Node ]:
12192 """Get a list of base graph elements.
12293
123- :param aspect_urn: URN of the Aspect node.
94+ :param model_pointer: pointer to the model
12495 :return: List of base graph elements.
12596 """
12697 base_elements : list [Node ] = []
12798
12899 if aspect_urn :
129100 base_elements += [aspect_urn if isinstance (aspect_urn , URIRef ) else URIRef (aspect_urn )]
130101 else :
131- base_elements += self .get_nodes_from_graph ()
102+ base_elements += self .get_aspect_nodes_from_graph ()
132103
133104 return base_elements
134105
135- def to_python (self , aspect_urn : URIRef | str = "" ) -> List [URIRef | None ]:
106+ def to_python (self , aspect_urn : URIRef | str = "" ) -> List [Aspect ]:
136107 """Convert SAMM graph to Python objects."""
137108 base_nodes = self .get_base_nodes (aspect_urn )
109+ if not base_nodes :
110+ raise ValueError (f"Could not found Aspect node by the URN { aspect_urn } ." )
111+
138112 model_element_factory = ModelElementFactory (self ._samm_version , self ._graph , self ._cache )
139113 aspect_elements = model_element_factory .create_all_graph_elements (base_nodes )
140114
0 commit comments