3232import org .apache .jena .riot .RiotException ;
3333import org .apache .jena .vocabulary .RDF ;
3434import org .eclipse .esmf .ame .exceptions .FileReadException ;
35+ import org .eclipse .esmf .ame .exceptions .InvalidAspectModelException ;
3536import org .eclipse .esmf .ame .exceptions .UrnNotFoundException ;
37+ import org .eclipse .esmf .ame .model .repository .AspectModelInformation ;
3638import org .eclipse .esmf .aspectmodel .resolver .AbstractResolutionStrategy ;
3739import org .eclipse .esmf .aspectmodel .resolver .services .TurtleLoader ;
3840import org .eclipse .esmf .aspectmodel .urn .AspectModelUrn ;
4547
4648import io .vavr .NotImplementedError ;
4749import io .vavr .control .Try ;
50+ import lombok .Getter ;
4851
52+ /**
53+ * Represents a resolution strategy for handling aspect models.
54+ *
55+ * <p>This abstract class provides common methods and behaviors to resolve and load aspect models, and should be
56+ * extended to create specific resolution strategies.</p>
57+ */
58+ @ Getter
4959public abstract class ResolutionStrategy extends AbstractResolutionStrategy {
5060 private static final Logger LOG = LoggerFactory .getLogger ( ResolutionStrategy .class );
5161
52- public final Path processingRootPath ;
53- public final Model aspectModel ;
62+ private final Path processingRootPath ;
63+ private final Model currentAspectModel ;
64+
65+ private final String currentFileName ;
5466
67+ /**
68+ * Constructs a new ResolutionStrategy with the given parameters.
69+ *
70+ * @param aspectModel The aspect model file content as string.
71+ * @param processingRootPath The root path where processing should occur.
72+ */
5573 public ResolutionStrategy ( final String aspectModel , final Path processingRootPath ) {
5674 this .processingRootPath = processingRootPath ;
57- this .aspectModel = loadTurtleFromString ( aspectModel );
75+ this .currentFileName = "NEW LOADED FILE" ;
76+ this .currentAspectModel = loadTurtleFromString ( aspectModel );
77+ }
78+
79+ /**
80+ * Constructs a new ResolutionStrategy with the given parameters.
81+ *
82+ * @param aspectModelInformation The information about the aspect model.
83+ * @param processingRootPath The root path where processing should occur.
84+ */
85+ public ResolutionStrategy ( final AspectModelInformation aspectModelInformation , final Path processingRootPath ) {
86+ this .processingRootPath = processingRootPath ;
87+ this .currentFileName = aspectModelInformation .getFileName ();
88+ this .currentAspectModel = loadTurtleFromString ( aspectModelInformation .getAspectModel () );
5889 }
5990
91+ /**
92+ * Attempts to apply the given aspect model URN.
93+ * <p>The method will resolve the model from the filesystem or fallback to other strategies based on the URN.</p>
94+ *
95+ * @param aspectModelUrn The aspect model URN to be processed.
96+ * @return A try containing the resolved model, or a failure if the model cannot be resolved.
97+ */
6098 @ Override
6199 public Try <Model > apply ( final AspectModelUrn aspectModelUrn ) {
62100 if ( aspectModelUrn == null ) {
@@ -75,19 +113,19 @@ private Try<Model> tryOnSuccess( final AspectModelUrn aspectModelUrn, final Try<
75113 return modelFromFileSystem ;
76114 }
77115
78- return Try .success ( aspectModel );
116+ return Try .success ( currentAspectModel );
79117 }
80118
81119 private Try <Model > tryOnFailure ( final AspectModelUrn aspectModelUrn ) {
82- final StmtIterator stmtIterator = aspectModel .listStatements (
120+ final StmtIterator stmtIterator = currentAspectModel .listStatements (
83121 ResourceFactory .createResource ( aspectModelUrn .toString () ), null , (RDFNode ) null );
84122
85123 if ( stmtIterator .hasNext () ) {
86- return Try .success ( aspectModel );
124+ return Try .success ( currentAspectModel );
87125 }
88126
89- return Try .failure ( new UrnNotFoundException (
90- String . format ( "%s cannot be resolved correctly." , aspectModelUrn ), aspectModelUrn ) );
127+ return Try .failure ( new UrnNotFoundException ( String . format ( "%s cannot be resolved correctly." , aspectModelUrn ),
128+ aspectModelUrn ) );
91129 }
92130
93131 protected abstract Try <Model > getModelFromFileSystem ( AspectModelUrn aspectModelUrn , Path rootPath );
@@ -118,25 +156,45 @@ protected Try<Model> loadTurtleFromFile( final File aspectModel ) {
118156 }
119157 }
120158
159+ /**
160+ * Retrieves the aspect model URN for the current aspect model.
161+ *
162+ * @return The aspect model URN.
163+ */
121164 public AspectModelUrn getAspectModelUrn () {
122- return AspectModelUrn .fromUrn (
123- getEsmfStatements ( aspectModel ).orElseThrow (
124- () -> new NotImplementedError ( "AspectModelUrn cannot be found." ) ).next ().getSubject ().getURI () );
165+ return AspectModelUrn .fromUrn ( getEsmfStatements ( currentAspectModel ).orElseThrow (
166+ () -> new InvalidAspectModelException ( String .format (
167+ "Unable to find a recognized version of SAMM (with the identifier 'urn:samm:org.eclipse.esmf.samm') in the file: %s." ,
168+ this .currentFileName ) ) ).next ().getSubject ().getURI () );
125169 }
126170
171+ /**
172+ * Retrieves the ESMF statements for the given aspect model.
173+ *
174+ * @param aspectModel The aspect model to get statements from.
175+ * @return An optional containing the statement iterator if available, or empty otherwise.
176+ */
127177 public static Optional <StmtIterator > getEsmfStatements ( final Model aspectModel ) {
128178 final List <StmtIterator > stmtIterators = new ArrayList <>();
129-
130179 KnownVersion .getVersions ()
131- .forEach ( version -> stmtIterators .addAll ( getListOfAllSAMMElements ( version )
132- .stream ()
133- .filter ( resource -> aspectModel .listStatements ( null , RDF .type , resource ).hasNext () )
134- .map ( resource -> aspectModel .listStatements ( null , RDF .type , resource ) )
135- .toList () ) );
136-
180+ .forEach ( version -> addAllStatementsFromVersion ( aspectModel , stmtIterators , version ) );
137181 return stmtIterators .isEmpty () ? Optional .empty () : stmtIterators .stream ().findFirst ();
138182 }
139183
184+ private static void addAllStatementsFromVersion ( final Model aspectModel , final List <StmtIterator > stmtIterators ,
185+ final KnownVersion version ) {
186+ stmtIterators .addAll ( getListOfAllSAMMElements ( version ).stream ().filter (
187+ resource -> aspectModel .listStatements ( null , RDF .type , resource ).hasNext () )
188+ .map ( resource -> aspectModel .listStatements ( null ,
189+ RDF .type , resource ) ).toList () );
190+ }
191+
192+ /**
193+ * Retrieves a list of all SAMM elements for the given known version.
194+ *
195+ * @param version The known version to get SAMM elements from.
196+ * @return A list of resources representing the SAMM elements.
197+ */
140198 private static List <Resource > getListOfAllSAMMElements ( final KnownVersion version ) {
141199 final SAMM samm = new SAMM ( version );
142200 final SAMMC sammc = new SAMMC ( version );
0 commit comments