Skip to content

Commit a1f4859

Browse files
committed
Clean up and add comments
1 parent 9ed11e1 commit a1f4859

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

core/sds-aspect-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/AspectModelResolver.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Try<VersionedModel> resolveAspectModel( final ResolutionStrategy resolver
8484

8585
if ( mergedModel.isFailure() ) {
8686
if ( mergedModel.getCause() instanceof FileNotFoundException ) {
87-
return Try.failure( new ModelResolutionException( mergedModel.getCause() ) );
87+
return Try.failure( new ModelResolutionException( "While trying to resolve " + input + ": " + mergedModel.getCause() ) );
8888
}
8989
return Try.failure( mergedModel.getCause() );
9090
}
@@ -133,6 +133,14 @@ public static boolean containsDefinition( final Model model, final AspectModelUr
133133
return model.contains( model.createResource( urn.toString() ), RDF.type, (RDFNode) null );
134134
}
135135

136+
/**
137+
* The main model resolution method that takes an Aspect Model element URN and a resolution strategy as input.
138+
* The strategy is applied to the URN to load a model, and then repeated for all URNs in the loaded model that
139+
* have not yet been loaded.
140+
* @param urn the Aspect Model element URN
141+
* @param resolutionStrategy the resolution strategy that knowns how to turn a URN into a Model
142+
* @return the fully resolved model, or a failure if one of the transitively referenced elements can't be found
143+
*/
136144
private Try<Model> resolve( final String urn, final ResolutionStrategy resolutionStrategy ) {
137145
final Model result = ModelFactory.createDefaultModel();
138146
final Stack<String> unresolvedUrns = new Stack<>();
@@ -147,14 +155,17 @@ private Try<Model> resolve( final String urn, final ResolutionStrategy resolutio
147155
}
148156
final Model model = resolvedModel.get();
149157

150-
// Merge the resolved model into the target if it was not already merged before
151-
// (because the model contains more than one definition)
158+
// Merge the resolved model into the target if it was not already merged before.
159+
// It could have been merged before when the model contains another model definition that was already resolved
152160
if ( !mergedModels.contains( model ) ) {
153161
mergeModels( result, model );
154162
mergedModels.add( model );
155163
}
156164
for ( final String element : getAllUrnsInModel( model ) ) {
157165
if ( !result.contains( model.createResource( element ), RDF.type, (RDFNode) null )
166+
// Backwards compatibility with BAMM 1.0.0
167+
&& !result.contains( model.createResource( element ), model.createProperty( "urn:bamm:io.openmanufacturing:meta-model:1.0.0#refines" ),
168+
(RDFNode) null )
158169
&& !unresolvedUrns.contains( element ) ) {
159170
unresolvedUrns.push( element );
160171
}
@@ -166,6 +177,14 @@ private Try<Model> resolve( final String urn, final ResolutionStrategy resolutio
166177

167178
private final Model EMPTY_MODEL = ModelFactory.createDefaultModel();
168179

180+
/**
181+
* Applies a {@link ResolutionStrategy} to a URI to be resolved, but only if the URI is actually a valid {@link AspectModelUrn}.
182+
* For meta model elements or other URIs, an empty model is returned. This method returns only a failure, when the used resolution
183+
* strategy fails.
184+
* @param urn the URN to resolve
185+
* @param resolutionStrategy the resolution strategy to apply
186+
* @return the model containing the defintion of the given model element
187+
*/
169188
private Try<Model> getModelForUrn( final String urn, final ResolutionStrategy resolutionStrategy ) {
170189
if ( urn.startsWith( RDF.getURI() ) || urn.startsWith( XSD.getURI() ) ) {
171190
return Try.success( EMPTY_MODEL );
@@ -189,6 +208,15 @@ private Try<Model> getModelForUrn( final String urn, final ResolutionStrategy re
189208
}
190209
}
191210

211+
/**
212+
* Defensively merge a model into an existing target model. This means:
213+
* <ul>
214+
* <li>Prefixes are only added when they are not already present, i.e., a model won't overwrite the empty prefix of the target model</li>
215+
* <li>Statements that have a blank node (including RDF lists) as their object won't be added</li>
216+
* </ul>
217+
* @param target the model to merge into
218+
* @param other the model to be merged
219+
*/
192220
private void mergeModels( final Model target, final Model other ) {
193221
for ( final Map.Entry<String, String> prefixEntry : other.getNsPrefixMap().entrySet() ) {
194222
if ( !target.getNsPrefixMap().containsKey( prefixEntry.getKey() ) ) {

core/sds-aspect-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/ClasspathStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,6 @@ public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
173173
.getOrElse( false ) )
174174
.findFirst()
175175
.orElse( Try.failure( new FileNotFoundException(
176-
"The AspectModel: " + aspectModelUrn + " could not be found in directory: " + directory ) ) );
176+
"The model file " + aspectModelUrn + " could not be found in directory: " + directory ) ) );
177177
}
178178
}

core/sds-aspect-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/FileSystemStrategy.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
33
*
44
* See the AUTHORS file(s) distributed with this work for additional
5-
* information regarding authorship.
5+
* information regarding authorship.
66
*
77
* This Source Code Form is subject to the terms of the Mozilla Public
88
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -80,17 +80,16 @@ public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
8080
aspectModelUrn.getName(), directory );
8181

8282
return Arrays.stream( Optional.ofNullable( directory.toFile().listFiles() ).orElse( new File[] {} ) )
83-
.filter( File::isFile )
84-
.filter( file -> file.getName().endsWith( ".ttl" ) )
85-
.map( File::toURI )
86-
.sorted()
87-
.map( this::loadFromUri )
88-
.filter( tryModel -> tryModel
89-
.map( model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
90-
.getOrElse( false ) )
91-
.findFirst()
92-
.orElse( Try.failure( new FileNotFoundException(
93-
"The AspectModel: " + aspectModelUrn.toString() + " could not be found in directory: "
94-
+ directory ) ) );
83+
.filter( File::isFile )
84+
.filter( file -> file.getName().endsWith( ".ttl" ) )
85+
.map( File::toURI )
86+
.sorted()
87+
.map( this::loadFromUri )
88+
.filter( tryModel -> tryModel
89+
.map( model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
90+
.getOrElse( false ) )
91+
.findFirst()
92+
.orElse( Try.failure( new FileNotFoundException(
93+
"The model file " + aspectModelUrn.toString() + " could not be found in directory: " + directory ) ) );
9594
}
9695
}

0 commit comments

Comments
 (0)