Skip to content

Commit

Permalink
Created from a patch from the original PR to resolve organisational i…
Browse files Browse the repository at this point in the history
…ssues. (eclipse-esmf#420)
  • Loading branch information
RaMisess authored Aug 31, 2023
1 parent 9a764b3 commit f7aef28
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 273 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public abstract class AbstractSammMigrator extends AbstractMigrator {
private final KnownVersion targetKnownVersion;

protected AbstractSammMigrator( final KnownVersion sourceKnownVersion, final KnownVersion targetKnownVersion, final int order ) {
super( VersionNumber.parse( sourceKnownVersion.toVersionString() ), VersionNumber.parse( targetKnownVersion.toVersionString() ), order );
super( VersionNumber.parse( sourceKnownVersion.toVersionString() ), VersionNumber.parse( targetKnownVersion.toVersionString() ),
order );
this.sourceKnownVersion = sourceKnownVersion;
this.targetKnownVersion = targetKnownVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.esmf.aspectmodel.resolver.exceptions.InvalidVersionException;
import org.eclipse.esmf.aspectmodel.vocabulary.Namespace;
import org.eclipse.esmf.samm.KnownVersion;

import org.apache.jena.graph.NodeFactory;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.eclipse.esmf.aspectmodel.vocabulary.Namespace;
import org.eclipse.esmf.samm.KnownVersion;

/**
* A {@link AbstractUriRewriter} that replaces all references to the legacy BAMM Aspect Meta Model to their corresponding SAMM counterparts
*/
public class BammUriRewriter extends AbstractUriRewriter {
public BammUriRewriter() {
super( KnownVersion.getLatest(), KnownVersion.getLatest() );
private final BAMM_VERSION bammVersion;

private static final String SAMM_C_PREFIX = "samm-c";
private static final String SAMM_E_PREFIX = "samm-e";

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() );
this.bammVersion = bammVersion;
}

@Override
Expand All @@ -42,8 +52,8 @@ protected Map<String, String> buildPrefixMap( final Model sourceModel, final Map
return sourceModel.getNsPrefixMap().keySet().stream()
.map( prefix -> switch ( prefix ) {
case "bamm" -> Map.entry( "samm", targetPrefixes.get( "samm" ) );
case "bamm-c" -> Map.entry( "samm-c", targetPrefixes.get( "samm-c" ) );
case "bamm-e" -> Map.entry( "samm-e", targetPrefixes.get( "samm-e" ) );
case "bamm-c" -> Map.entry( SAMM_C_PREFIX, targetPrefixes.get( SAMM_C_PREFIX ) );
case "bamm-e" -> Map.entry( SAMM_E_PREFIX, targetPrefixes.get( SAMM_E_PREFIX ) );
case "unit" -> Map.entry( "unit", targetPrefixes.get( "unit" ) );
default -> Map.entry( prefix, rewriteUri( sourceModel.getNsPrefixURI( prefix ), oldToNewNamespaces )
.orElse( sourceModel.getNsPrefixURI( prefix ) ) );
Expand All @@ -53,18 +63,12 @@ protected Map<String, String> buildPrefixMap( final Model sourceModel, final Map

@Override
protected Map<String, String> buildReplacementPrefixMap( final Model sourceModel, final Map<String, String> targetPrefixes ) {
final Map<String, String> oldSamm = Namespace.createPrefixMap( KnownVersion.SAMM_1_0_0 );
// The mapping of the URNs of the legacy BAMM Aspect Meta model to their corresponding SAMM counterparts
return Map.of(
"urn:bamm:io.openmanufacturing:meta-model:2.0.0#", targetPrefixes.get( "samm" ),
"urn:bamm:io.openmanufacturing:characteristic:2.0.0#", targetPrefixes.get( "samm-c" ),
"urn:bamm:io.openmanufacturing:entity:2.0.0#", targetPrefixes.get( "samm-e" ),
"urn:bamm:io.openmanufacturing:unit:2.0.0#", targetPrefixes.get( "unit" ),

"urn:bamm:io.openmanufacturing:meta-model:1.0.0#", oldSamm.get( "samm" ),
"urn:bamm:io.openmanufacturing:characteristic:1.0.0#", oldSamm.get( "samm-c" ),
"urn:bamm:io.openmanufacturing:entity:1.0.0#", oldSamm.get( "samm-e" ),
"urn:bamm:io.openmanufacturing:unit:1.0.0#", oldSamm.get( "unit" )
"urn:bamm:io.openmanufacturing:meta-model:" + bammVersion.versionString() + "#", targetPrefixes.get( "samm" ),
"urn:bamm:io.openmanufacturing:characteristic: " + bammVersion.versionString() + "#", targetPrefixes.get( SAMM_C_PREFIX ),
"urn:bamm:io.openmanufacturing:entity:" + bammVersion.versionString() + "#", targetPrefixes.get( SAMM_E_PREFIX ),
"urn:bamm:io.openmanufacturing:unit:" + bammVersion.versionString() + "#", targetPrefixes.get( "unit" )
);
}

Expand All @@ -85,7 +89,7 @@ protected Optional<String> rewriteUri( final String oldUri, final Map<String, St
@Override
protected RDFNode updateRdfNode( final RDFNode rdfNode, final Map<String, String> oldToNewNamespaces ) {
RDFNode result = super.updateRdfNode( rdfNode, oldToNewNamespaces );
if ( result instanceof Literal literal ) {
if ( result instanceof final Literal literal ) {
result = Optional.ofNullable( literal.getDatatypeURI() )
.flatMap( type -> rewriteUri( type, oldToNewNamespaces ) )
.map( NodeFactory::getType )
Expand All @@ -95,8 +99,17 @@ protected RDFNode updateRdfNode( final RDFNode rdfNode, final Map<String, String
return result;
}

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() ) );
}

@Override
public Model migrate( final Model sourceModel ) {
if ( !modelContainsBammPrefixes( sourceModel ) ) {
return sourceModel;
}

final Map<String, String> targetPrefixes = Namespace.createPrefixMap( getTargetKnownVersion() );
final Map<String, String> oldToNewNamespaces = buildReplacementPrefixMap( sourceModel, targetPrefixes );

Expand All @@ -114,4 +127,13 @@ public Model migrate( final Model sourceModel ) {
sourceModel.setNsPrefixes( buildPrefixMap( sourceModel, targetPrefixes, oldToNewNamespaces ) );
return sourceModel;
}

public enum BAMM_VERSION {
BAMM_1_0_0,
BAMM_2_0_0;

public String versionString() {
return toString().replaceFirst( "BAMM_(\\d+)_(\\d+)_(\\d+)", "$1.$2.$3" );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ public abstract class AbstractConstraint2BoxModelTest extends MetaModelVersions

final String sparqlQueryFileName = "constraint2boxmodel.sparql";

protected String boxSelectorStatement( final KnownVersion metaModelVersion, final String constraintIdentifier,
protected String boxSelectorStatement( final String constraintIdentifier,
final boolean isConstraintAnonymous ) {
final String ns = isConstraintAnonymous ? ":" : "test:";
if ( metaModelVersion.isNewerThan( KnownVersion.SAMM_1_0_0 ) ) {
return String.format( "%s a :Box", constraintIdentifier.equals( "*" ) ? "*" : ns + constraintIdentifier );
}
return ns + "TestConstraintConstraint a :Box";
return String.format( "%s a :Box", constraintIdentifier.equals( "*" ) ? "*" : ns + constraintIdentifier );
}

protected String entriesSelectorStatement( final KnownVersion metaModelVersion, final String constraintIdentifier,
protected String entriesSelectorStatement( final String constraintIdentifier,
final boolean isConstraintAnonymous ) {
final String ns = isConstraintAnonymous ? ":" : "test:";
if ( metaModelVersion.isNewerThan( KnownVersion.SAMM_1_0_0 ) ) {
return String.format( "%s :entries *", constraintIdentifier.equals( "*" ) ? "*" : ns + constraintIdentifier );
}
return ns + "TestConstraintConstraint :entries *";
return String.format( "%s :entries *", constraintIdentifier.equals( "*" ) ? "*" : ns + constraintIdentifier );
}

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.test.TestAspect;

public class Constraint2BoxModelTest extends AbstractConstraint2BoxModelTest {
private final String expectedValueEntryTitle = "value";
private final String expectedSeeEntryTitle = "see";
class Constraint2BoxModelTest extends AbstractConstraint2BoxModelTest {
private final static String EXPECTED_VALUE_ENTRY_TITLE = "value";
private final static String EXPECTED_SEE_ENTRY_TITLE = "see";

@ParameterizedTest
@MethodSource( value = "allVersions" )
public void testOnlyUsedConstraintsAreProcessedExpectSuccess( final KnownVersion metaModelVersion ) {
void testOnlyUsedConstraintsAreProcessedExpectSuccess( final KnownVersion metaModelVersion ) {
final TestContext context = new TestContext( TestAspect.ASPECT_WITH_USED_AND_UNUSED_CONSTRAINT, metaModelVersion );

final Model queryResult = context.executeQuery( sparqlQueryFileName );
Expand All @@ -39,54 +39,54 @@ public void testOnlyUsedConstraintsAreProcessedExpectSuccess( final KnownVersion

@ParameterizedTest
@MethodSource( value = "allVersions" )
public void testSeeAttributesArePresentExpectSuccess( final KnownVersion metaModelVersion ) {
void testSeeAttributesArePresentExpectSuccess( final KnownVersion metaModelVersion ) {
final String constraintIdentifier = "*";
final TestContext context = new TestContext( TestAspect.ASPECT_WITH_CONSTRAINT_WITH_MULTIPLE_SEE_ATTRIBUTES, metaModelVersion );
context.executeAttributeIsPresentTest(
sparqlQueryFileName, boxSelectorStatement( metaModelVersion, constraintIdentifier, true ),
entriesSelectorStatement( metaModelVersion, constraintIdentifier, true ),
sparqlQueryFileName, boxSelectorStatement( constraintIdentifier, true ),
entriesSelectorStatement( constraintIdentifier, true ),
totalNumberOfExpectedEntriesPerMetaModelVersion.get( metaModelVersion ),
5,
expectedSeeEntryTitle,
EXPECTED_SEE_ENTRY_TITLE,
"http://example.com/, http://example.com/me" );
}

@ParameterizedTest
@MethodSource( value = "allVersions" )
public void testSeeAttributeIsNotPresentExpectSuccess( final KnownVersion metaModelVersion ) {
void testSeeAttributeIsNotPresentExpectSuccess( final KnownVersion metaModelVersion ) {
final String constraintIdentifier = "*";
final TestContext context = new TestContext( TestAspect.ASPECT_WITH_CONSTRAINT_WITHOUT_SEE_ATTRIBUTE, metaModelVersion );
context.executeAttributeIsNotPresentTest( sparqlQueryFileName, boxSelectorStatement( metaModelVersion, constraintIdentifier, true ),
entriesSelectorStatement( metaModelVersion, constraintIdentifier, true ),
context.executeAttributeIsNotPresentTest( sparqlQueryFileName, boxSelectorStatement( constraintIdentifier, true ),
entriesSelectorStatement( constraintIdentifier, true ),
totalNumberOfExpectedEntriesPerMetaModelVersion.get( metaModelVersion ), 5 );
}

@ParameterizedTest
@MethodSource( value = "allVersions" )
public void testRegularExpressionConstraintExpectSuccess( final KnownVersion metaModelVersion ) {
void testRegularExpressionConstraintExpectSuccess( final KnownVersion metaModelVersion ) {
final String constraintIdentifier = "*";
final TestContext context = new TestContext( TestAspect.ASPECT_WITH_NUMERIC_REGULAR_EXPRESSION_CONSTRAINT, metaModelVersion );
context.executeAttributeIsPresentTest(
sparqlQueryFileName, boxSelectorStatement( metaModelVersion, constraintIdentifier, true ),
entriesSelectorStatement( metaModelVersion, constraintIdentifier, true ),
sparqlQueryFileName, boxSelectorStatement( constraintIdentifier, true ),
entriesSelectorStatement( constraintIdentifier, true ),
totalNumberOfExpectedEntriesPerMetaModelVersion.get( metaModelVersion ),
4,
expectedValueEntryTitle, "\\\\d*\\|x" );
EXPECTED_VALUE_ENTRY_TITLE, "\\\\d*\\|x" );
}

@ParameterizedTest
@MethodSource( value = "allVersions" )
public void testFixedPointConstraintExpectSuccess( final KnownVersion metaModelVersion ) {
void testFixedPointConstraintExpectSuccess( final KnownVersion metaModelVersion ) {
final String constraintIdentifier = "*";
final TestContext context = new TestContext( TestAspect.ASPECT_WITH_FIXED_POINT_CONSTRAINT, metaModelVersion );
context.executeAttributeIsPresentTest(
sparqlQueryFileName, boxSelectorStatement( metaModelVersion, constraintIdentifier, true ),
entriesSelectorStatement( metaModelVersion, constraintIdentifier, true ),
sparqlQueryFileName, boxSelectorStatement( constraintIdentifier, true ),
entriesSelectorStatement( constraintIdentifier, true ),
totalNumberOfExpectedEntriesPerMetaModelVersion.get( metaModelVersion ),
8, "scale", "5" );
context.executeAttributeIsPresentTest(
sparqlQueryFileName, boxSelectorStatement( metaModelVersion, constraintIdentifier, true ),
entriesSelectorStatement( metaModelVersion, constraintIdentifier, true ),
sparqlQueryFileName, boxSelectorStatement( constraintIdentifier, true ),
entriesSelectorStatement( constraintIdentifier, true ),
totalNumberOfExpectedEntriesPerMetaModelVersion.get( metaModelVersion ),
9, "integer", "3" );
}
Expand Down
Loading

0 comments on commit f7aef28

Please sign in to comment.