diff --git a/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/BammUriRewriter.java b/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/BammUriRewriter.java index 99b36e6f3..67a430d40 100644 --- a/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/BammUriRewriter.java +++ b/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/BammUriRewriter.java @@ -17,14 +17,19 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.Stream; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Statement; import org.eclipse.esmf.aspectmodel.resolver.exceptions.InvalidVersionException; import org.eclipse.esmf.aspectmodel.vocabulary.Namespace; import org.eclipse.esmf.samm.KnownVersion; +import com.google.common.collect.Streams; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.Statement; +import org.apache.jena.vocabulary.RDF; + /** * A {@link AbstractUriRewriter} that replaces all references to the legacy BAMM Aspect Meta Model to their corresponding SAMM counterparts */ @@ -37,7 +42,8 @@ public class BammUriRewriter extends AbstractUriRewriter { public BammUriRewriter( final BAMM_VERSION bammVersion ) { // Translating versions will only fail if there are no SAMM versions (i.e., KnownVersion) for the versions in BAMM_VERSION super( KnownVersion.fromVersionString( bammVersion.versionString() ).orElseThrow( () -> - new InvalidVersionException( "BAMM version " + bammVersion.versionString() + " can not be translated to SAMM" ) ), KnownVersion.getLatest() ); + new InvalidVersionException( "BAMM version " + bammVersion.versionString() + " can not be translated to SAMM" ) ), + KnownVersion.getLatest() ); this.bammVersion = bammVersion; } @@ -82,8 +88,15 @@ protected Optional rewriteUri( final String oldUri, final Map - uri.startsWith( "urn:bamm:io.openmanufacturing:meta-model:" ) && uri.contains( bammVersion.versionString() ) ); + final String bammPrefix = "urn:bamm:io.openmanufacturing:meta-model:"; + final Predicate isBammRelated = uri -> + uri.startsWith( bammPrefix ) && uri.contains( bammVersion.versionString() ); + return // BAMM prefix is present + model.getNsPrefixMap().values().stream().anyMatch( isBammRelated ) || + // Or any referred resource uses a BAMM URN + Streams.stream( model.listObjectsOfProperty( RDF.type ) ) + .flatMap( object -> object.isURIResource() ? Stream.of( object.asResource().getURI() ) : Stream.empty() ) + .anyMatch( isBammRelated ); } @Override diff --git a/core/esmf-aspect-model-resolver/src/test/java/org/eclipse/esmf/aspectmodel/resolver/AspectModelResolverTest.java b/core/esmf-aspect-model-resolver/src/test/java/org/eclipse/esmf/aspectmodel/resolver/AspectModelResolverTest.java index 5d4279237..3cf7991ff 100644 --- a/core/esmf-aspect-model-resolver/src/test/java/org/eclipse/esmf/aspectmodel/resolver/AspectModelResolverTest.java +++ b/core/esmf-aspect-model-resolver/src/test/java/org/eclipse/esmf/aspectmodel/resolver/AspectModelResolverTest.java @@ -21,23 +21,23 @@ import java.nio.file.Paths; import java.util.List; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.Statement; -import org.apache.jena.vocabulary.RDF; import org.eclipse.esmf.aspectmodel.resolver.services.TurtleLoader; import org.eclipse.esmf.aspectmodel.resolver.services.VersionedModel; import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn; import org.eclipse.esmf.aspectmodel.vocabulary.SAMM; import org.eclipse.esmf.samm.KnownVersion; import org.eclipse.esmf.test.MetaModelVersions; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; import com.google.common.collect.Streams; - import io.vavr.control.Try; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.RDFNode; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.rdf.model.Statement; +import org.apache.jena.vocabulary.RDF; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; public class AspectModelResolverTest extends MetaModelVersions { private final AspectModelResolver resolver = new AspectModelResolver(); @@ -63,6 +63,25 @@ public void testLoadDataModelExpectSuccess( final KnownVersion metaModelVersion assertThat( result.get().getModel().listStatements( aspect, RDF.type, sammAspect ).nextOptional() ).isNotEmpty(); } + @Test + public void testLoadLegacyBammModelWithoutPrefixesExpectSuccess() throws URISyntaxException { + final KnownVersion metaModelVersion = KnownVersion.getLatest(); + final File aspectModelsRootDirectory = new File( + AspectModelResolverTest.class.getClassLoader() + .getResource( metaModelVersion.toString().toLowerCase() ) + .toURI() + .getPath() ); + + final AspectModelUrn testUrn = AspectModelUrn.fromUrn( TEST_NAMESPACE + "BammAspectWithoutPrefixes" ); + + final ResolutionStrategy urnStrategy = new FileSystemStrategy( aspectModelsRootDirectory.toPath() ); + final Try result = resolver.resolveAspectModel( urnStrategy, testUrn ); + assertThat( result.isSuccess() ).isTrue(); + + final SAMM samm = new SAMM( KnownVersion.getLatest() ); + assertThat( result.get().getModel().listStatements( null, RDF.type, samm.Aspect() ).nextOptional() ).isNotEmpty(); + } + @ParameterizedTest @MethodSource( value = "versionsUpToIncluding2_0_0" ) public void testLoadLegacyBammModelExpectSuccess( final KnownVersion metaModelVersion ) throws URISyntaxException { diff --git a/core/esmf-aspect-model-resolver/src/test/resources/samm_2_1_0/org.eclipse.esmf.test/1.0.0/BammAspectWithoutPrefixes.ttl b/core/esmf-aspect-model-resolver/src/test/resources/samm_2_1_0/org.eclipse.esmf.test/1.0.0/BammAspectWithoutPrefixes.ttl new file mode 100644 index 000000000..3a8f741fe --- /dev/null +++ b/core/esmf-aspect-model-resolver/src/test/resources/samm_2_1_0/org.eclipse.esmf.test/1.0.0/BammAspectWithoutPrefixes.ttl @@ -0,0 +1,122 @@ +@prefix aux: . +@prefix ns: . +ns: a ; + + "Aspect for movement information"@en ; + + "BammAspectWithoutPrefixes" ; + + () ; + + "BammAspectWithoutPrefixes"@en ; + + ( ) . + + a ; + + ; + + "Flag indicating if the position is changing"@en ; + + "moving" ; + + "Moving"@en . + + a ; + + ; + + "z coordinate in space"@en ; + + "z" ; + + "z"@en . + + a ; + + ; + + "y coordinate in space"@en ; + + "y" ; + + "y"@en . + + a ; + + ; + + ; + + "Represents a coordinate along an axis in space."@en ; + + "Coordinate" ; + + "Coordinate"@en . + + a ; + + ; + + "Indicats if speed limit is adhered to."@en ; + + "speedLimitWarning" ; + + "Speed Limit Warning"@en . + + a ; + + "Position in space, described along three axis, with the third axis optional, if all positions are in a plane."@en ; + + "SpatialPosition" ; + + "Spatial Position"@en ; + + ( + [ + true ; + + + ] + ) . + + a ; + + ; + + "x coordinate in space"@en ; + + "x" ; + + "x"@en . + + a ; + + [ a ; + + ; + + "Represents a single location in space."@en ; + + "SpatialPositionCharacteristic" ; + + "Spatial Position Characteristic"@en + ] ; + + "Indicates a position"@en ; + + "position" ; + + "Position"@en . + + a ; + + ( "green" "yellow" "red" ) ; + + ; + + "Represents if speed of position change is within specification (green), within tolerance (yellow), or outside specification (red)."@en ; + + "WarningLevel" ; + + "Warning Level"@en . \ No newline at end of file