]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java
Layer0Utils.addL0Identifier to prevent possible differentiation of code
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / MigrateModel.java
1 /*******************************************************************************
2  * Copyright (c) 2014, 2015 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling;
13
14 import gnu.trove.set.hash.THashSet;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.simantics.databoard.Bindings;
23 import org.simantics.databoard.util.URIStringUtils;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Statement;
27 import org.simantics.db.WriteGraph;
28 import org.simantics.db.common.NamedResource;
29 import org.simantics.db.common.utils.NameUtils;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.util.Layer0Utils;
32 import org.simantics.diagram.stubs.DiagramResource;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.structural.stubs.StructuralResource2;
35 import org.simantics.structural2.modelingRules.AllowedConnectionTypes;
36 import org.simantics.utils.ObjectUtils;
37 import org.simantics.utils.datastructures.Triple;
38
39 /**
40  * @author Antti Villberg
41  */
42 public class MigrateModel {
43
44         public static class MigrationOperation {
45                 
46                 public NamedResource instanceToMigrate;
47                 public NamedResource targetType;
48                 public NamedResource targetSymbol;
49                 
50                 public MigrationOperation(NamedResource nr, NamedResource targetType, NamedResource targetSymbol) {
51                         this.instanceToMigrate = nr;
52                         this.targetType = targetType;
53                         this.targetSymbol = targetSymbol;
54                 }
55                 
56                 @Override
57                 public String toString() {
58                         return instanceToMigrate.getName();
59                 }
60                 
61                 public String getDescription(ReadGraph graph) throws DatabaseException {
62                         String sourceURI = graph.getPossibleURI(instanceToMigrate.getResource());
63                         if(sourceURI == null) sourceURI = NameUtils.getSafeName(graph, instanceToMigrate.getResource());
64                         sourceURI = sourceURI.replace("http://Projects/Development%20Project/", "");
65                         if(targetSymbol != null) {
66                                 String targetURI = graph.getURI(targetSymbol.getResource());
67                                 return URIStringUtils.unescape(sourceURI) + " into " + URIStringUtils.unescape(targetURI);
68                         } else {
69                                 String targetURI = graph.getURI(targetType.getResource());
70                                 return URIStringUtils.unescape(sourceURI) + " into " + URIStringUtils.unescape(targetURI);
71                         }
72                 }
73                 
74                 private Resource getPossibleReplacement(ReadGraph graph, Resource type, Resource predicate) throws DatabaseException {
75                         Layer0 L0 = Layer0.getInstance(graph);
76                         String name = graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);
77                         if(name == null) return null;
78                         Resource child = Layer0Utils.getPossibleChild(graph, type, name);
79                         if(child != null) return child;
80                         for(Resource r : graph.getObjects(type, L0.DomainOf)) {
81                                 String rName = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
82                                 if(name.equals(rName)) return r;
83                         }
84                         return null; 
85                 }
86                 
87                 public String replace(WriteGraph graph, Resource instanceToMigrate, Resource type) throws DatabaseException {
88
89                         Layer0 L0 = Layer0.getInstance(graph);
90                         StructuralResource2 STR = StructuralResource2.getInstance(graph);
91
92                         Collection<Resource> currentTypes = graph.getPrincipalTypes(instanceToMigrate);
93                         if(currentTypes.size() == 1 && currentTypes.contains(type)) return null;
94                         
95                         StringBuilder problems = new StringBuilder();
96                         
97                         // Validate operation
98                         for(Statement stm : graph.getStatements(instanceToMigrate, L0.IsWeaklyRelatedTo)) {
99
100                                 Resource predicate = stm.getPredicate();
101                                 if(stm.isAsserted(instanceToMigrate)) continue;
102                                 
103                                 if(graph.isInstanceOf(predicate, STR.Property)) {
104                                         Resource replacement = getPossibleReplacement(graph, type, predicate);
105                                         // OK for a property to disappear
106                                         if(replacement == null) continue;
107                                         if(!graph.isInstanceOf(replacement, STR.Property)) {
108                                                 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
109                                                 problems.append(" " + name + " was a property in the source type\n");
110                                                 continue;
111                                         }
112                                         String sourceValueType = graph.getPossibleRelatedValue(predicate, L0.RequiresValueType, Bindings.STRING);
113                                         String replacementValueType = graph.getPossibleRelatedValue(replacement, L0.RequiresValueType, Bindings.STRING);
114                                         if(!ObjectUtils.objectEquals(sourceValueType, replacementValueType)) {
115                                                 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
116                                                 problems.append(" value types for property " + name + " differ (" + sourceValueType + " vs. " + replacementValueType + ")\n");
117                                                 continue;
118                                         }
119                                 }
120
121                                 if(graph.isInstanceOf(predicate, STR.ConnectionRelation)) {
122                                         Resource replacement = getPossibleReplacement(graph, type, predicate);
123                                         if(replacement == null) {
124                                                 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
125                                                 problems.append(" used connection point " + name + " has been removed from target type\n");
126                                                 continue;
127                                         }
128                                         if(!graph.isInstanceOf(replacement, STR.ConnectionRelation)) {
129                                                 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
130                                                 problems.append(" " + name + " was a connection point in the source type\n");
131                                                 continue;
132                                         }
133
134                                         Collection<Resource> sourceConnetionTypes = graph.syncRequest(new AllowedConnectionTypes(predicate));
135                                         Collection<Resource> replacementConnectionTypes = graph.syncRequest(new AllowedConnectionTypes(replacement));
136                                         
137                                         Set<Resource> sourceSet = new THashSet<>(sourceConnetionTypes);
138                                         Set<Resource> replacementSet = new THashSet<>(replacementConnectionTypes);
139                                         
140                                         if(!sourceSet.equals(replacementSet)) {
141                                                 String name = graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING);
142                                                 problems.append(" allowed connection types for connection point " + name + " differ (" + NameUtils.getSafeName(graph, sourceSet) + " vs. " + NameUtils.getSafeName(graph, replacementSet) + ")\n");
143                                                 continue;
144                                         }
145
146                                 }
147                                 
148                         }
149                         
150                         if(problems.length() > 0) {
151                                 return problems.toString();
152                         }
153
154                         // Perform operation
155                         for(Statement stm : graph.getStatements(instanceToMigrate, L0.IsWeaklyRelatedTo)) {
156
157                                 Resource predicate = stm.getPredicate();
158                                 if(stm.isAsserted(instanceToMigrate)) continue;
159                                                 
160                                 if(L0.InstanceOf.equals(predicate)) {
161                                         graph.deny(stm);
162                                         graph.claim(instanceToMigrate, L0.InstanceOf, type);
163                                 }
164                                 
165                                 if(graph.isInstanceOf(predicate, STR.Property) || graph.isInstanceOf(predicate, STR.ConnectionRelation)) {
166                                         Resource replacement = getPossibleReplacement(graph, type, predicate);
167                                         graph.deny(stm);
168                                         if(replacement == null) continue;
169                                         graph.claim(stm.getSubject(), replacement, stm.getObject());
170                                 }
171                                 
172                         }
173                         
174                         return null;
175
176                 }
177                 
178                 public String perform(WriteGraph graph) throws DatabaseException {
179                         
180                         Resource instance = instanceToMigrate.getResource();
181                         Resource type = targetType.getResource();
182                         Resource symbol = targetSymbol != null ? targetSymbol.getResource() : null;
183                         
184                         String result = replace(graph, instance, type);
185                         if(result != null) return result;
186                         
187                         ModelingResources MOD = ModelingResources.getInstance(graph);
188                         Resource element = graph.getPossibleObject(instance, MOD.ComponentToElement);
189                         if(element == null) return null;
190                         
191                         Resource targetSymbol = symbol;
192                         if(targetSymbol == null) {
193                                 Layer0 L0 = Layer0.getInstance(graph);
194                                 DiagramResource DIA = DiagramResource.getInstance(graph);
195                                 Resource currentSymbol = graph.getPossibleType(element, DIA.Element);
196                                 if(currentSymbol != null) {
197                                         String currentSymbolName = graph.getRelatedValue(currentSymbol, L0.HasName, Bindings.STRING);
198                                         targetSymbol = Layer0Utils.getPossibleChild(graph, type, DIA.ElementClass, currentSymbolName);
199                                         if(targetSymbol == null)
200                                                 return "Did not find symbol '" + currentSymbolName + "' from target type.\n";
201                                 } else {
202                                         // No symbol - fine 
203                                         return null;
204                                 }
205
206                         }
207                         
208                         return replace(graph, element, targetSymbol);
209                         
210                 }
211                 
212         }
213
214         public int instanceCount = 0;
215         public Set<Resource> activeModels = new THashSet<>();
216         public List<Triple<String, NamedResource, Collection<MigrationOperation>>> instances = new ArrayList<>();
217         public List<MigrationOperation> sortedShownInstances = Collections.emptyList();
218
219 }