Skip to content

Commit 5ba4bd2

Browse files
authored
Merge pull request #235 from bci-oss/bugfix/OMP-SDK-234-fix-model-resolution
Fix model resolution to only load referenced models once.
2 parents 0762a98 + 34ae6c0 commit 5ba4bd2

File tree

6 files changed

+141
-9
lines changed

6 files changed

+141
-9
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,23 +175,28 @@ private Try<Model> resolve( final String urn, final ResolutionStrategy resolutio
175175

176176
// Merge the resolved model into the target if it was not already merged before.
177177
// It could have been merged before when the model contains another model definition that was already resolved
178-
if ( !mergedModels.contains( model ) ) {
178+
if ( !modelAlreadyResolved( model, mergedModels ) ) {
179179
mergeModels( result, model );
180180
mergedModels.add( model );
181-
}
182-
for ( final String element : getAllUrnsInModel( model ) ) {
183-
if ( !result.contains( model.createResource( element ), RDF.type, (RDFNode) null )
184-
// Backwards compatibility with BAMM 1.0.0
185-
&& !result.contains( model.createResource( element ), refines, (RDFNode) null )
186-
&& !unresolvedUrns.contains( element ) ) {
187-
unresolvedUrns.push( element );
181+
182+
for ( final String element : getAllUrnsInModel( model ) ) {
183+
if ( !result.contains( model.createResource( element ), RDF.type, (RDFNode) null )
184+
// Backwards compatibility with BAMM 1.0.0
185+
&& !result.contains( model.createResource( element ), refines, (RDFNode) null )
186+
&& !unresolvedUrns.contains( element ) ) {
187+
unresolvedUrns.push( element );
188+
}
188189
}
189190
}
190191
}
191192

192193
return Try.success( result );
193194
}
194195

196+
private boolean modelAlreadyResolved( final Model model, final Set<Model> resolvedModels ) {
197+
return resolvedModels.stream().anyMatch( model::isIsomorphicWith );
198+
}
199+
195200
private final Model EMPTY_MODEL = ModelFactory.createDefaultModel();
196201

197202
/**

core/sds-aspect-model-resolver/src/test/java/io/openmanufacturing/sds/aspectmodel/resolver/AspectModelResolverTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import static org.assertj.vavr.api.VavrAssertions.assertThat;
1717

1818
import java.io.File;
19-
import java.io.FileNotFoundException;
2019
import java.net.URISyntaxException;
2120
import java.util.List;
2221

@@ -372,4 +371,27 @@ public void testMergingModelsWithBlankNodeValues( final KnownVersion metaModelVe
372371
final List<Statement> propertiesAssertions = model.listStatements( primaryAspect, bamm.properties(), (RDFNode) null ).toList();
373372
Assertions.assertThat( propertiesAssertions.size() ).isEqualTo( 1 );
374373
}
374+
375+
@ParameterizedTest
376+
@MethodSource( value = "allVersions" )
377+
public void testMultiReferenceSameSource( final KnownVersion metaModelVersion ) throws URISyntaxException {
378+
final File aspectModelsRootDirectory = new File(
379+
AspectModelResolverTest.class.getClassLoader()
380+
.getResource( metaModelVersion.toString().toLowerCase() )
381+
.toURI()
382+
.getPath() );
383+
384+
final AspectModelUrn testUrn = AspectModelUrn.fromUrn( "urn:bamm:io.openmanufacturing.test:1.0.0#VehicleInstance" );
385+
386+
final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() );
387+
final Try<VersionedModel> result = resolver.resolveAspectModel( urnStrategy, testUrn );
388+
assertThat( result ).isSuccess();
389+
390+
// make sure the source file for the definitions of ModelYear and ModelCode (ModelDef.ttl) is only loaded once
391+
final Model model = result.get().getModel();
392+
final Resource entity = model.createResource( "urn:bamm:io.openmanufacturing.test:1.0.0#SomeOtherNonRelatedEntity" );
393+
final BAMM bamm = new BAMM( metaModelVersion );
394+
final List<Statement> properties = model.listStatements( entity, bamm.properties(), (RDFNode) null ).toList();
395+
Assertions.assertThat( properties.size() ).isEqualTo( 1 );
396+
}
375397
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:1.0.0#> .
13+
@prefix bamm-c: <urn:bamm:io.openmanufacturing:characteristic:1.0.0#> .
14+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
15+
@prefix : <urn:bamm:io.openmanufacturing.test:1.0.0#> .
16+
17+
:ModelCode a bamm-c:Code ;
18+
bamm:name "ModelCode" ;
19+
bamm:dataType xsd:string .
20+
21+
:ModelYear a bamm-c:Code ;
22+
bamm:name "ModelYear" ;
23+
bamm:dataType xsd:nonNegativeInteger .
24+
25+
:SomeOtherNonRelatedEntity a bamm:Entity ;
26+
bamm:name "SomeOtherNonRelatedEntity";
27+
bamm:properties ( :name ) .
28+
29+
:name a bamm:Property ;
30+
bamm:name "name";
31+
bamm:characteristic bamm-c:Text .
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:1.0.0#> .
13+
@prefix : <urn:bamm:io.openmanufacturing.test:1.0.0#> .
14+
15+
:VehicleInstance a bamm:Entity ;
16+
bamm:name "VehicleInstance" ;
17+
bamm:properties ( :modelYear :model ) .
18+
19+
:modelYear a bamm:Property ;
20+
bamm:name "modelYear" ;
21+
bamm:characteristic :ModelYear .
22+
23+
:model a bamm:Property ;
24+
bamm:name "model" ;
25+
bamm:characteristic :ModelCode .
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:2.0.0#> .
13+
@prefix bamm-c: <urn:bamm:io.openmanufacturing:characteristic:2.0.0#> .
14+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
15+
@prefix : <urn:bamm:io.openmanufacturing.test:1.0.0#> .
16+
17+
:ModelCode a bamm-c:Code ;
18+
bamm:dataType xsd:string .
19+
20+
:ModelYear a bamm-c:Code ;
21+
bamm:dataType xsd:nonNegativeInteger .
22+
23+
:SomeOtherNonRelatedEntity a bamm:Entity ;
24+
bamm:properties ( :name ) .
25+
26+
:name a bamm:Property ;
27+
bamm:characteristic bamm-c:Text .
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:2.0.0#> .
13+
@prefix : <urn:bamm:io.openmanufacturing.test:1.0.0#> .
14+
15+
:VehicleInstance a bamm:Entity ;
16+
bamm:properties ( :modelYear :model ) .
17+
18+
:modelYear a bamm:Property ;
19+
bamm:characteristic :ModelYear .
20+
21+
:model a bamm:Property ;
22+
bamm:characteristic :ModelCode .

0 commit comments

Comments
 (0)