13
13
14
14
package org .eclipse .esmf .aspectmodel .versionupdate .migrator ;
15
15
16
- import java .util .AbstractMap ;
17
16
import java .util .HashMap ;
18
17
import java .util .Map ;
19
18
import java .util .Optional ;
25
24
import org .apache .jena .rdf .model .RDFNode ;
26
25
import org .apache .jena .rdf .model .Resource ;
27
26
import org .apache .jena .rdf .model .ResourceFactory ;
27
+ import org .eclipse .esmf .aspectmodel .vocabulary .Namespace ;
28
+ import org .eclipse .esmf .samm .KnownVersion ;
28
29
29
30
import com .google .common .collect .Streams ;
30
31
31
- import org .eclipse .esmf .samm .KnownVersion ;
32
- import org .eclipse .esmf .aspectmodel .vocabulary .Namespace ;
33
-
34
32
/**
35
33
* Abstract migration function that is used to apply a change to all URIs in a model
36
34
*/
@@ -39,6 +37,12 @@ protected AbstractUriRewriter( final KnownVersion sourceVersion, final KnownVers
39
37
super ( sourceVersion , targetVersion , 100 );
40
38
}
41
39
40
+ /**
41
+ * The URI rewriter implementation decides whether a URI needs to be rewritten, given the map of old to new namespaces
42
+ * @param oldUri the URI to rewrite
43
+ * @param oldToNewNamespaces the map of old to new namespaces
44
+ * @return empty if the URI should be kept as-is, the replacement URI otherwise
45
+ */
42
46
protected abstract Optional <String > rewriteUri ( String oldUri , Map <String , String > oldToNewNamespaces );
43
47
44
48
protected Resource updateResource ( final Resource resource , final Map <String , String > oldToNewNamespaces ) {
@@ -60,38 +64,51 @@ protected RDFNode updateRdfNode( final RDFNode rdfNode, final Map<String, String
60
64
return rdfNode ;
61
65
}
62
66
63
- @ Override
64
- public Model migrate ( final Model sourceModel ) {
65
- final Model targetModel = ModelFactory .createDefaultModel ();
66
-
67
- final Map <String , String > targetPrefixes = Namespace .createPrefixMap ( getTargetKnownVersion () );
68
-
67
+ protected Map <String , String > buildReplacementPrefixMap ( final Model sourceModel , final Map <String , String > targetPrefixes ) {
69
68
final Map <String , String > sourcePrefixes = Namespace .createPrefixMap ( getSourceKnownVersion () );
70
69
final Map <String , String > oldToNewNamespaces = new HashMap <>();
71
70
for ( final Map .Entry <String , String > targetEntry : targetPrefixes .entrySet () ) {
72
71
final String prefix = targetEntry .getKey ();
73
72
if ( prefix != null ) {
74
73
final String sourceUri = sourcePrefixes .get ( prefix );
75
- if ( sourceUri != null && !sourceUri .equals ( targetEntry .getValue () )) {
74
+ if ( sourceUri != null && !sourceUri .equals ( targetEntry .getValue () ) ) {
76
75
oldToNewNamespaces .put ( sourceUri , targetEntry .getValue () );
77
76
}
78
77
}
79
78
}
80
79
80
+ return oldToNewNamespaces ;
81
+ }
82
+
83
+ /**
84
+ * Builds the map of RDF prefixes to set in the migrated model, e.g. "xsd" -> XSD.NS
85
+ *
86
+ * @param sourceModel the source model
87
+ * @param targetPrefixes the target prefix map, containing e.g. "samm" -> samm.getNamespace()
88
+ * @param oldToNewNamespaces the map of old RDF namespaces to their new counterparts
89
+ * @return the prefix map
90
+ */
91
+ protected Map <String , String > buildPrefixMap ( final Model sourceModel , final Map <String , String > targetPrefixes , final Map <String , String > oldToNewNamespaces ) {
92
+ return sourceModel .getNsPrefixMap ().keySet ().stream ()
93
+ .map ( prefix -> Map .<String , String > entry ( prefix , targetPrefixes .getOrDefault ( prefix , sourceModel .getNsPrefixURI ( prefix ) ) ) )
94
+ .collect ( Collectors .toMap ( Map .Entry ::getKey , Map .Entry ::getValue ) );
95
+ }
96
+
97
+ @ Override
98
+ public Model migrate ( final Model sourceModel ) {
99
+ final Model targetModel = ModelFactory .createDefaultModel ();
100
+
101
+ final Map <String , String > targetPrefixes = Namespace .createPrefixMap ( getTargetKnownVersion () );
102
+ final Map <String , String > oldToNewNamespaces = buildReplacementPrefixMap ( sourceModel , targetPrefixes );
103
+
81
104
Streams .stream ( sourceModel .listStatements () ).map ( statement -> targetModel
82
105
.createStatement (
83
106
updateResource ( statement .getSubject (), oldToNewNamespaces ),
84
107
updateProperty ( statement .getPredicate (), oldToNewNamespaces ),
85
108
updateRdfNode ( statement .getObject (), oldToNewNamespaces )
86
109
) ).forEach ( targetModel ::add );
87
110
88
- final Map <String , String > newPrefixMap =
89
- sourceModel .getNsPrefixMap ().keySet ().stream ()
90
- .map ( prefix -> new AbstractMap .SimpleEntry <>( prefix ,
91
- Optional .ofNullable ( targetPrefixes .get ( prefix ) ).orElse ( sourceModel .getNsPrefixURI ( prefix ) ) ) )
92
- .collect ( Collectors .toMap ( AbstractMap .SimpleEntry ::getKey , AbstractMap .SimpleEntry ::getValue ) );
93
- targetModel .setNsPrefixes ( newPrefixMap );
94
-
111
+ targetModel .setNsPrefixes ( buildPrefixMap ( sourceModel , targetPrefixes , oldToNewNamespaces ) );
95
112
return targetModel ;
96
113
}
97
114
}
0 commit comments