Skip to content

Commit

Permalink
Fixed migration for Literal Datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ysrbo committed Sep 1, 2023
1 parent cf223f7 commit 4a57ac5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<VersionedModel> 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();

Expand All @@ -81,27 +78,23 @@ public Try<VersionedModel> 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<VersionedModel> 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<Migrator> migrators, final VersionNumber sourceVersion, final VersionNumber targetVersion, final Model targetModel ) {
if ( migrators.isEmpty() ) {
return targetModel;
Expand All @@ -112,7 +105,7 @@ private Model migrate( final List<Migrator> 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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.jena.datatypes.BaseDatatype;
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;
Expand Down Expand Up @@ -60,15 +60,17 @@ protected Property updateProperty( final Property property, final Map<String, St
}

protected Literal updateLiteral( final Literal literal, final Map<String, String> oldToNewNamespaces ) {
return rewriteUri( literal.getDatatypeURI(), oldToNewNamespaces )
.map( uri -> ResourceFactory.createTypedLiteral( literal.getLexicalForm(), new BaseDatatype( uri ) ) )
return Optional.ofNullable( literal.getDatatypeURI() )
.flatMap( uri -> rewriteUri( uri, oldToNewNamespaces ) )
.map( uri -> ResourceFactory.createTypedLiteral( literal.getLexicalForm(), NodeFactory.getType( uri ) ) )
.orElse( literal );
}

protected RDFNode updateRdfNode( final RDFNode rdfNode, final Map<String, String> oldToNewNamespaces ) {
if ( rdfNode.isResource() ) {
return updateResource( rdfNode.asResource(), oldToNewNamespaces );
} else if ( rdfNode.isLiteral() ) { // needed especially for "curie" literals, which are versioned: "unit:day"^^samm:curie
} else if ( rdfNode.isLiteral() ) {
// needed especially for "curie" literals, which are versioned: "unit:day"^^samm:curie
return updateLiteral( rdfNode.asLiteral(), oldToNewNamespaces );
}
return rdfNode;
Expand Down Expand Up @@ -98,8 +100,7 @@ protected Map<String, String> buildReplacementPrefixMap( final Model sourceModel
* @param oldToNewNamespaces the map of old RDF namespaces to their new counterparts
* @return the prefix map
*/
protected Map<String, String> buildPrefixMap( final Model sourceModel, final Map<String, String> targetPrefixes,
final Map<String, String> oldToNewNamespaces ) {
protected Map<String, String> buildPrefixMap( final Model sourceModel, final Map<String, String> targetPrefixes, final Map<String, String> oldToNewNamespaces ) {
return sourceModel.getNsPrefixMap().keySet().stream()
.map( prefix -> Map.<String, String> entry( prefix, targetPrefixes.getOrDefault( prefix, sourceModel.getNsPrefixURI( prefix ) ) ) )
.collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
import java.util.Optional;
import java.util.stream.Collectors;

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 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;

/**
* A {@link AbstractUriRewriter} that replaces all references to the legacy BAMM Aspect Meta Model to their corresponding SAMM counterparts
*/
Expand Down Expand Up @@ -86,19 +81,6 @@ protected Optional<String> rewriteUri( final String oldUri, final Map<String, St
return Optional.of( result );
}

@Override
protected RDFNode updateRdfNode( final RDFNode rdfNode, final Map<String, String> oldToNewNamespaces ) {
RDFNode result = super.updateRdfNode( rdfNode, oldToNewNamespaces );
if ( result instanceof final Literal literal ) {
result = Optional.ofNullable( literal.getDatatypeURI() )
.flatMap( type -> rewriteUri( type, oldToNewNamespaces ) )
.map( NodeFactory::getType )
.<RDFNode> map( type -> ResourceFactory.createTypedLiteral( literal.getString(), type ) )
.orElse( result );
}
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() ) );
Expand Down

0 comments on commit 4a57ac5

Please sign in to comment.