From aabbe89ad0f3e0b2448fc05dc5a1633d51d8f73b Mon Sep 17 00:00:00 2001 From: Yuriy Shevtsov Date: Tue, 29 Aug 2023 12:17:08 +0200 Subject: [PATCH] Fixed migration for Literal Datatypes #382 --- .../versionupdate/MigratorService.java | 29 +++++++------------ .../migrator/AbstractUriRewriter.java | 26 ++++++++++++----- .../migrator/BammUriRewriter.java | 17 ----------- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/MigratorService.java b/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/MigratorService.java index 15f1c52db..e21065bfb 100644 --- a/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/MigratorService.java +++ b/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/MigratorService.java @@ -15,19 +15,16 @@ import java.util.Comparator; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import org.apache.jena.rdf.model.Model; import org.eclipse.esmf.aspectmodel.VersionNumber; +import org.eclipse.esmf.aspectmodel.resolver.exceptions.InvalidVersionException; import org.eclipse.esmf.aspectmodel.resolver.services.VersionedModel; import org.eclipse.esmf.aspectmodel.versionupdate.migrator.Migrator; +import org.eclipse.esmf.samm.KnownVersion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.eclipse.esmf.samm.KnownVersion; - -import org.eclipse.esmf.aspectmodel.resolver.exceptions.InvalidVersionException; - import io.vavr.control.Try; /** @@ -72,7 +69,7 @@ private Model execute( final Migrator migrator, final Model sourceModel ) { * @return the resulting {@link VersionedModel} that corresponds to the input Aspect model, but with the new meta model version */ public Try updateMetaModelVersion( final VersionedModel versionedModel ) { - final VersionNumber targetVersion = VersionNumber.parse( KnownVersion.getLatest().toVersionString() ); + final VersionNumber latestKnownVersion = VersionNumber.parse( KnownVersion.getLatest().toVersionString() ); VersionNumber sourceVersion = versionedModel.getMetaModelVersion(); Model migrationModel = versionedModel.getRawModel(); @@ -81,27 +78,23 @@ public Try updateMetaModelVersion( final VersionedModel versione sourceVersion = VersionNumber.parse( KnownVersion.SAMM_1_0_0.toVersionString() ); } - if ( sourceVersion.equals( targetVersion ) ) { - return getSdsMigratorFactory().createAspectMetaModelResourceResolver().mergeMetaModelIntoRawModel( migrationModel, targetVersion ); + if ( sourceVersion.greaterThan( latestKnownVersion ) ) { + // looks like unreachable + return Try.failure( new InvalidVersionException( + String.format( "Model version %s can not be updated to version %s", sourceVersion, latestKnownVersion ) ) ); } - if ( sourceVersion.greaterThan( targetVersion ) ) { - return Try.failure( new InvalidVersionException( - String.format( "Model version %s can not be updated to version %s", sourceVersion, targetVersion ) ) ); + if ( !sourceVersion.equals( latestKnownVersion ) ) { + migrationModel = migrate( sammMigratorFactory.createMigrators(), sourceVersion, latestKnownVersion, migrationModel ); } - return migration( sourceVersion, targetVersion, migrationModel ); + return getSdsMigratorFactory().createAspectMetaModelResourceResolver().mergeMetaModelIntoRawModel( migrationModel, latestKnownVersion ); } private Model customMigration( final MigratorFactory migratorFactory, final VersionNumber sourceVersion, final VersionedModel versionedModel ) { return migrate( migratorFactory.createMigrators(), sourceVersion, migratorFactory.getLatestVersion(), versionedModel.getRawModel() ); } - private Try migration( final VersionNumber sourceVersion, final VersionNumber targetVersion, final Model targetModel ) { - final Model model = migrate( sammMigratorFactory.createMigrators(), sourceVersion, targetVersion, targetModel ); - return getSdsMigratorFactory().createAspectMetaModelResourceResolver().mergeMetaModelIntoRawModel( model, targetVersion ); - } - private Model migrate( final List migrators, final VersionNumber sourceVersion, final VersionNumber targetVersion, final Model targetModel ) { if ( migrators.isEmpty() ) { return targetModel; @@ -112,7 +105,7 @@ private Model migrate( final List migrators, final VersionNumber sourc .sorted( comparator.thenComparing( Migrator::order ) ) .dropWhile( migrator -> !migrator.sourceVersion().equals( sourceVersion ) ) .takeWhile( migrator -> !migrator.targetVersion().greaterThan( targetVersion ) ) - .collect( Collectors.toList() ); + .toList(); Model migratorTargetModel = targetModel; for ( final Migrator migrator : migratorSet ) { diff --git a/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/AbstractUriRewriter.java b/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/AbstractUriRewriter.java index 771934e14..c78420376 100644 --- a/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/AbstractUriRewriter.java +++ b/core/esmf-aspect-meta-model-version-migrator/src/main/java/org/eclipse/esmf/aspectmodel/versionupdate/migrator/AbstractUriRewriter.java @@ -18,6 +18,8 @@ import java.util.Optional; import java.util.stream.Collectors; +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.ModelFactory; import org.apache.jena.rdf.model.Property; @@ -39,7 +41,8 @@ protected AbstractUriRewriter( final KnownVersion sourceVersion, final KnownVers /** * The URI rewriter implementation decides whether a URI needs to be rewritten, given the map of old to new namespaces - * @param oldUri the URI to rewrite + * + * @param oldUri the URI to rewrite * @param oldToNewNamespaces the map of old to new namespaces * @return empty if the URI should be kept as-is, the replacement URI otherwise */ @@ -58,10 +61,18 @@ protected Property updateProperty( final Property property, final Map oldToNewNamespaces ) { - if ( rdfNode.isResource() ) { - return updateResource( rdfNode.asResource(), oldToNewNamespaces ); + RDFNode result = rdfNode; + if ( result.isResource() ) { + result = updateResource( rdfNode.asResource(), oldToNewNamespaces ); + } + if ( result instanceof Literal literal ) { + result = Optional.ofNullable( literal.getDatatypeURI() ) + .flatMap( type -> rewriteUri( type, oldToNewNamespaces ) ) + .map( NodeFactory::getType ) + . map( type -> ResourceFactory.createTypedLiteral( literal.getString(), type ) ) + .orElse( result ); } - return rdfNode; + return result; } protected Map buildReplacementPrefixMap( final Model sourceModel, final Map targetPrefixes ) { @@ -83,12 +94,13 @@ protected Map buildReplacementPrefixMap( final Model sourceModel /** * Builds the map of RDF prefixes to set in the migrated model, e.g. "xsd" -> XSD.NS * - * @param sourceModel the source model - * @param targetPrefixes the target prefix map, containing e.g. "samm" -> samm.getNamespace() + * @param sourceModel the source model + * @param targetPrefixes the target prefix map, containing e.g. "samm" -> samm.getNamespace() * @param oldToNewNamespaces the map of old RDF namespaces to their new counterparts * @return the prefix map */ - protected Map buildPrefixMap( final Model sourceModel, final Map targetPrefixes, final Map oldToNewNamespaces ) { + protected Map buildPrefixMap( final Model sourceModel, final Map targetPrefixes, + final Map oldToNewNamespaces ) { return sourceModel.getNsPrefixMap().keySet().stream() .map( prefix -> Map. entry( prefix, targetPrefixes.getOrDefault( prefix, sourceModel.getNsPrefixURI( prefix ) ) ) ) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ) ); 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 319a1c7cf..757938fd6 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 @@ -19,11 +19,7 @@ import java.util.Optional; import java.util.stream.Collectors; -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; @@ -82,19 +78,6 @@ protected Optional rewriteUri( final String oldUri, final Map oldToNewNamespaces ) { - RDFNode result = super.updateRdfNode( rdfNode, oldToNewNamespaces ); - if ( result instanceof Literal literal ) { - result = Optional.ofNullable( literal.getDatatypeURI() ) - .flatMap( type -> rewriteUri( type, oldToNewNamespaces ) ) - .map( NodeFactory::getType ) - . map( type -> ResourceFactory.createTypedLiteral( literal.getString(), type ) ) - .orElse( result ); - } - return result; - } - @Override public Model migrate( final Model sourceModel ) { final Map targetPrefixes = Namespace.createPrefixMap( getTargetKnownVersion() );