Skip to content

Commit

Permalink
Extend migrator to also consider the "curie" literals. (eclipse-esmf#417
Browse files Browse the repository at this point in the history
)

Co-authored-by: Michele Santoro <[email protected]>
  • Loading branch information
RaMisess and michelu89 authored Aug 31, 2023
1 parent f7aef28 commit cf223f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.jena.datatypes.BaseDatatype;
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;
Expand Down Expand Up @@ -57,9 +59,17 @@ protected Property updateProperty( final Property property, final Map<String, St
return rewriteUri( property.getURI(), oldToNewNamespaces ).map( ResourceFactory::createProperty ).orElse( property );
}

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 ) ) )
.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
return updateLiteral( rdfNode.asLiteral(), oldToNewNamespaces );
}
return rdfNode;
}
Expand Down Expand Up @@ -88,7 +98,8 @@ 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 @@ -22,23 +22,23 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.vocabulary.RDF;
import org.assertj.core.api.Assertions;
import org.eclipse.esmf.aspectmodel.VersionNumber;
import org.eclipse.esmf.aspectmodel.resolver.services.VersionedModel;
import org.eclipse.esmf.aspectmodel.vocabulary.SAMM;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.test.MetaModelVersions;
import org.eclipse.esmf.test.TestAspect;

import com.google.common.collect.Streams;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.vocabulary.RDF;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.collect.Streams;

public class MigratorTest extends MetaModelVersions {

private final MigratorService migratorService = new MigratorService();
Expand Down Expand Up @@ -118,6 +118,16 @@ public void testRemoveSammName( final KnownVersion metaModelVersion ) {
assertThat( sammNameStatements ).isEmpty();
}

@ParameterizedTest
@MethodSource( "allVersions" )
public void testCurieMigration( final KnownVersion metaModelVersion ) {
final VersionedModel versionedModel = TestResources.getModelWithoutResolution( TestAspect.ASPECT_WITH_CURIE, metaModelVersion );
final VersionedModel rewrittenModel = migratorService.updateMetaModelVersion( versionedModel ).get();
final SAMM latestSamm = new SAMM( KnownVersion.getLatest() );
assertThat( rewrittenModel.getRawModel().listStatements( null, latestSamm.exampleValue(), (RDFNode) null ).nextStatement().getObject().asLiteral()
.getDatatypeURI() ).isEqualTo( latestSamm.curie().getURI() );
}

private Set<String> getAllUris( final Model model ) {
return Streams.stream( model.listStatements() ).flatMap( statement -> {
final Stream<String> subjectUri = Stream.of( statement.getSubject().getURI() );
Expand Down

0 comments on commit cf223f7

Please sign in to comment.