Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading BAMM-based Aspect Models without prefix declarations #479

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}

Expand Down Expand Up @@ -82,8 +88,15 @@ protected Optional<String> rewriteUri( final String oldUri, final Map<String, St
}

private boolean modelContainsBammPrefixes( final Model model ) {
return model.getNsPrefixMap().values().stream().anyMatch( uri ->
uri.startsWith( "urn:bamm:io.openmanufacturing:meta-model:" ) && uri.contains( bammVersion.versionString() ) );
final String bammPrefix = "urn:bamm:io.openmanufacturing:meta-model:";
final Predicate<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<VersionedModel> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
@prefix aux: <urn:bamm:io.openmanufacturing:aspect-model:aux#> .
@prefix ns: <urn:bamm:org.eclipse.esmf.test:1.0.0#BammAspectWithoutPrefixes> .
ns: a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Aspect> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Aspect for movement information"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"BammAspectWithoutPrefixes" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#operations>
() ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"BammAspectWithoutPrefixes"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#properties>
( <urn:bamm:org.eclipse.esmf.test:1.0.0#moving> <urn:bamm:org.eclipse.esmf.test:1.0.0#speedLimitWarning> <urn:bamm:org.eclipse.esmf.test:1.0.0#position> ) .
<urn:bamm:org.eclipse.esmf.test:1.0.0#moving>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Property> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#characteristic>
<urn:bamm:io.openmanufacturing:characteristic:1.0.0#Boolean> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Flag indicating if the position is changing"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"moving" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Moving"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#z>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Property> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#characteristic>
<urn:bamm:org.eclipse.esmf.test:1.0.0#Coordinate> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"z coordinate in space"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"z" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"z"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#y>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Property> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#characteristic>
<urn:bamm:org.eclipse.esmf.test:1.0.0#Coordinate> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"y coordinate in space"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"y" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"y"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#Coordinate>
a <urn:bamm:io.openmanufacturing:characteristic:1.0.0#Measurement> ;
<urn:bamm:io.openmanufacturing:characteristic:1.0.0#unit>
<urn:bamm:io.openmanufacturing:unit:1.0.0#metre> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#dataType>
<http://www.w3.org/2001/XMLSchema#float> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Represents a coordinate along an axis in space."@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"Coordinate" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Coordinate"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#speedLimitWarning>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Property> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#characteristic>
<urn:bamm:org.eclipse.esmf.test:1.0.0#WarningLevel> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Indicats if speed limit is adhered to."@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"speedLimitWarning" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Speed Limit Warning"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#SpatialPosition>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Entity> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Position in space, described along three axis, with the third axis optional, if all positions are in a plane."@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"SpatialPosition" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Spatial Position"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#properties>
( <urn:bamm:org.eclipse.esmf.test:1.0.0#x> <urn:bamm:org.eclipse.esmf.test:1.0.0#y>
[ <urn:bamm:io.openmanufacturing:meta-model:1.0.0#optional>
true ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#property>
<urn:bamm:org.eclipse.esmf.test:1.0.0#z>
]
) .
<urn:bamm:org.eclipse.esmf.test:1.0.0#x>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Property> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#characteristic>
<urn:bamm:org.eclipse.esmf.test:1.0.0#Coordinate> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"x coordinate in space"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"x" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"x"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#position>
a <urn:bamm:io.openmanufacturing:meta-model:1.0.0#Property> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#characteristic>
[ a <urn:bamm:io.openmanufacturing:characteristic:1.0.0#SingleEntity> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#dataType>
<urn:bamm:org.eclipse.esmf.test:1.0.0#SpatialPosition> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Represents a single location in space."@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"SpatialPositionCharacteristic" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Spatial Position Characteristic"@en
] ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Indicates a position"@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"position" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Position"@en .
<urn:bamm:org.eclipse.esmf.test:1.0.0#WarningLevel>
a <urn:bamm:io.openmanufacturing:characteristic:1.0.0#Enumeration> ;
<urn:bamm:io.openmanufacturing:characteristic:1.0.0#values>
( "green" "yellow" "red" ) ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#dataType>
<http://www.w3.org/2001/XMLSchema#string> ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#description>
"Represents if speed of position change is within specification (green), within tolerance (yellow), or outside specification (red)."@en ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#name>
"WarningLevel" ;
<urn:bamm:io.openmanufacturing:meta-model:1.0.0#preferredName>
"Warning Level"@en .