|
13 | 13 |
|
14 | 14 | package org.eclipse.esmf.aspectmodel.versionupdate.migrator;
|
15 | 15 |
|
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
16 | 18 | import java.util.Map;
|
17 | 19 | import java.util.Optional;
|
18 | 20 | import java.util.stream.Collectors;
|
19 | 21 |
|
20 | 22 | import org.apache.jena.rdf.model.Model;
|
| 23 | +import org.apache.jena.rdf.model.Statement; |
| 24 | +import org.eclipse.esmf.aspectmodel.vocabulary.Namespace; |
21 | 25 | import org.eclipse.esmf.samm.KnownVersion;
|
22 | 26 |
|
23 | 27 | /**
|
@@ -67,4 +71,24 @@ protected Optional<String> rewriteUri( final String oldUri, final Map<String, St
|
67 | 71 | result = result.replace( "urn:bamm:", "urn:samm:" );
|
68 | 72 | return Optional.of( result );
|
69 | 73 | }
|
| 74 | + |
| 75 | + @Override |
| 76 | + public Model migrate( final Model sourceModel ) { |
| 77 | + final Map<String, String> targetPrefixes = Namespace.createPrefixMap( getTargetKnownVersion() ); |
| 78 | + final Map<String, String> oldToNewNamespaces = buildReplacementPrefixMap( sourceModel, targetPrefixes ); |
| 79 | + |
| 80 | + final List<Statement> remappedStatements = new ArrayList<>(); |
| 81 | + |
| 82 | + // it is important to do the remapping "in situ" (in the same model), because otherwise the position information would be lost. |
| 83 | + sourceModel.listStatements().forEach( statement -> |
| 84 | + remappedStatements.add( sourceModel.createStatement( updateResource( statement.getSubject(), oldToNewNamespaces ), |
| 85 | + updateProperty( statement.getPredicate(), oldToNewNamespaces ), |
| 86 | + updateRdfNode( statement.getObject(), oldToNewNamespaces ) ) ) |
| 87 | + ); |
| 88 | + |
| 89 | + sourceModel.removeAll(); |
| 90 | + remappedStatements.forEach( sourceModel::add ); |
| 91 | + sourceModel.setNsPrefixes( buildPrefixMap( sourceModel, targetPrefixes, oldToNewNamespaces ) ); |
| 92 | + return sourceModel; |
| 93 | + } |
70 | 94 | }
|
0 commit comments