Sync git svn branch with SVN repository r33366.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / UserComponentMigration.java
1 /*******************************************************************************\r
2  * Copyright (c) 2014, 2015 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling;\r
13 \r
14 import gnu.trove.set.hash.THashSet;\r
15 \r
16 import java.util.ArrayList;\r
17 import java.util.Collection;\r
18 import java.util.Collections;\r
19 import java.util.Comparator;\r
20 import java.util.Set;\r
21 \r
22 import org.eclipse.core.runtime.IProgressMonitor;\r
23 import org.eclipse.core.runtime.NullProgressMonitor;\r
24 import org.simantics.Simantics;\r
25 import org.simantics.databoard.Bindings;\r
26 import org.simantics.databoard.util.URIStringUtils;\r
27 import org.simantics.db.ReadGraph;\r
28 import org.simantics.db.Resource;\r
29 import org.simantics.db.WriteGraph;\r
30 import org.simantics.db.common.NamedResource;\r
31 import org.simantics.db.common.request.ObjectsWithType;\r
32 import org.simantics.db.common.request.PossibleIndexRoot;\r
33 import org.simantics.db.common.utils.NameUtils;\r
34 import org.simantics.db.common.utils.VersionMap;\r
35 import org.simantics.db.common.utils.Versions;\r
36 import org.simantics.db.exception.CancelTransactionException;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.layer0.adapter.Instances;\r
39 import org.simantics.db.layer0.request.ActiveModels;\r
40 import org.simantics.db.layer0.util.Layer0Utils;\r
41 import org.simantics.diagram.stubs.DiagramResource;\r
42 import org.simantics.layer0.Layer0;\r
43 import org.simantics.modeling.MigrateModel.MigrationOperation;\r
44 import org.simantics.modeling.migration.UserComponentPostMigrationAction;\r
45 import org.simantics.simulation.ontology.SimulationResource;\r
46 import org.simantics.structural.stubs.StructuralResource2;\r
47 import org.simantics.utils.datastructures.MapList;\r
48 import org.simantics.utils.datastructures.Pair;\r
49 import org.simantics.utils.datastructures.Triple;\r
50 import org.simantics.utils.strings.AlphanumComparator;\r
51 \r
52 /**\r
53  * @author Antti Villberg\r
54  * @author Tuukka Lehtonen\r
55  */\r
56 public class UserComponentMigration {\r
57 \r
58     public static void migrateUserComponents(WriteGraph graph, Resource source, Resource target, Collection<Resource> components) throws DatabaseException {\r
59         MigrateModel model = getComponentTypeModel(graph, source, target, null);\r
60         if (model.instances.isEmpty())\r
61             return;\r
62         Triple<String, NamedResource, Collection<MigrationOperation>> instances = model.instances.get(0);\r
63         ArrayList<MigrationOperation> result = new ArrayList<>();\r
64         \r
65         for (MigrationOperation instance : instances.third) {\r
66             if (components.contains(instance.instanceToMigrate.getResource())) {\r
67                result.add(instance); \r
68             }\r
69         }\r
70         if (!result.isEmpty())\r
71             doMigration(new NullProgressMonitor(), graph, result);\r
72     }\r
73 \r
74     public static String doMigration(IProgressMonitor monitor, WriteGraph graph, final ArrayList<MigrationOperation> operations) throws DatabaseException {\r
75         graph.markUndoPoint();\r
76         StringBuilder b = new StringBuilder();\r
77         int success = 0;\r
78         int problem = 0;\r
79         int no = 1;\r
80         int count = operations.size();\r
81         monitor.setTaskName("Migrating " + count + " User Components");\r
82         for(MigrationOperation op : operations) {\r
83             if (monitor.isCanceled())\r
84                 throw new CancelTransactionException();\r
85             monitor.subTask("(" + (no++) + "/" + count + ") " + op.toString());\r
86             String problems = op.perform(graph);\r
87             if(problems != null) {\r
88                 b.insert(0, problems);\r
89                 b.insert(0, op.getDescription(graph) + "\n");\r
90                 problem++;\r
91             } else {\r
92                 b.append(op.getDescription(graph) + "\n");\r
93                 b.append(" success\n");\r
94                 success++;\r
95             }\r
96         }\r
97         int total = success + problem;\r
98         b.insert(0, "---------------------\n");\r
99         b.insert(0, "Details:\n");\r
100         b.insert(0, "\n");\r
101         b.insert(0, "  OK: " + success + "\n"); \r
102         b.insert(0, "  Failure: " + problem + "\n"); \r
103         b.insert(0, "---------------------\n");\r
104         b.insert(0, "Performed migration for " + total + " instances:\n");\r
105 \r
106         Layer0Utils.addCommentMetadata(graph, "Migrated " + total + " instances");\r
107         return b.toString();\r
108     }\r
109 \r
110     public static void doPostMigration(IProgressMonitor monitor, WriteGraph graph, ArrayList<MigrationOperation> result) throws DatabaseException {\r
111         THashSet<Resource> roots = new THashSet<>();\r
112         for(MigrationOperation op : result) {\r
113             Resource root = graph.syncRequest(new PossibleIndexRoot(op.instanceToMigrate.getResource()));\r
114             if(root != null)\r
115                 roots.add(root);\r
116         }\r
117         if (monitor.isCanceled())\r
118             throw new CancelTransactionException();\r
119         for(Resource root : roots) {\r
120             UserComponentPostMigrationAction action = graph.getPossibleAdapter(root, UserComponentPostMigrationAction.class);\r
121             if(action != null)\r
122                 action.perform(monitor, graph);\r
123         }\r
124     }\r
125 \r
126     public static MigrateModel getComponentTypeModel(ReadGraph graph, Resource source, Resource target, Resource symbol) throws DatabaseException {\r
127         MigrateModel model = newMigrateModel(graph);\r
128 \r
129         MapList<NamedResource, MigrationOperation> list = new MapList<>(); \r
130 \r
131         Layer0 L0 = Layer0.getInstance(graph);\r
132         DiagramResource DIA = DiagramResource.getInstance(graph);\r
133         Instances query = graph.adapt(source, Instances.class);\r
134         Set<Resource> instances = new THashSet<>();\r
135         for(NamedResource nr : getLocations(graph, Simantics.getProjectResource())) {\r
136             Collection<Resource> found = query.find(graph, nr.getResource());\r
137             instances.addAll(found);\r
138         }\r
139 \r
140         model.instanceCount = instances.size();\r
141         for(Resource instance : instances) {\r
142 \r
143             String uri = graph.getPossibleURI(instance);\r
144 \r
145             Resource element = ModelingUtils.getPossibleElement(graph, instance);\r
146             if(element == null) {\r
147                 MigrationOperation op = new MigrationOperation(new NamedResource(uri, instance), new NamedResource("", target), null);\r
148                 addInstance(list, graph, instance, op);\r
149                 continue;\r
150             }\r
151 \r
152             Resource instanceSymbol = graph.getPossibleType(element, DIA.Element);\r
153             if(instanceSymbol == null) continue;\r
154 \r
155             if(symbol != null) {\r
156                 if(!symbol.equals(instanceSymbol)) {\r
157                     MigrationOperation op = new MigrationOperation(new NamedResource(uri, instance), new NamedResource("", target), new NamedResource("", symbol));\r
158                     addInstance(list, graph, instance, op);\r
159                 }\r
160             } else {\r
161                 String instanceSymbolName = graph.getRelatedValue(instanceSymbol, L0.HasName, Bindings.STRING);\r
162                 Resource targetSymbol = Layer0Utils.getPossibleChild(graph, target, DIA.ElementClass, instanceSymbolName); \r
163                 if(targetSymbol != null && !targetSymbol.equals(instanceSymbol)) {\r
164                     MigrationOperation op = new MigrationOperation(new NamedResource(uri, instance), new NamedResource("", target), new NamedResource("", targetSymbol));\r
165                     addInstance(list, graph, instance, op);\r
166                 }\r
167             }\r
168 \r
169         }\r
170 \r
171         sortInstances(model, list);\r
172 \r
173         return model;\r
174     }\r
175 \r
176     public static MigrateModel getSharedOntologyModel(ReadGraph graph, Resource sourceOntology, Resource targetOntology) throws DatabaseException {\r
177         MigrateModel model = newMigrateModel(graph);\r
178 \r
179         MapList<NamedResource, MigrationOperation> list = new MapList<>(); \r
180 \r
181         Layer0 L0 = Layer0.getInstance(graph);\r
182         DiagramResource DIA = DiagramResource.getInstance(graph);\r
183         StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
184         Instances query = graph.adapt(STR.ComponentType, Instances.class);\r
185 \r
186         Set<Resource> types = new THashSet<>();\r
187         for(Resource type : query.find(graph, sourceOntology)) {\r
188             // TODO: haxx\r
189             if(graph.isInheritedFrom(type, DIA.Element)) continue;\r
190             Resource root = graph.syncRequest(new PossibleIndexRoot(type));\r
191             if(sourceOntology.equals(root)) types.add(type);\r
192         }\r
193 \r
194         Set<Resource> instances = new THashSet<>();\r
195         Collection<NamedResource> locations = getLocations(graph, Simantics.getProjectResource()); \r
196 \r
197         for(Resource type : types) {\r
198             Instances query2 = graph.adapt(type, Instances.class);\r
199             for(NamedResource nr : locations) {\r
200                 Collection<Resource> found = query2.find(graph, nr.getResource());\r
201                 instances.addAll(found);\r
202             }\r
203         }\r
204 \r
205         model.instanceCount = instances.size();\r
206         for(Resource instance : instances) {\r
207             Resource type = graph.getPossibleType(instance, STR.Component);\r
208             String uri = graph.getPossibleURI(instance);\r
209             if (type == null || uri == null) {\r
210                 System.err.println("CANNOT MIGRATE INSTANCE DUE TO TYPING PROBLEM: " + NameUtils.getURIOrSafeNameInternal(graph, instance));\r
211                 continue;\r
212             }\r
213             NamedResource best = matchBest(graph, type, targetOntology);\r
214             if(best != null) {\r
215 \r
216                 Resource element = ModelingUtils.getPossibleElement(graph, instance);\r
217                 if(element == null) {\r
218                     MigrationOperation op = new MigrationOperation(new NamedResource(uri, instance), best, null);\r
219                     addInstance(list, graph, instance, op);\r
220                     continue;\r
221                 }\r
222 \r
223                 Resource instanceSymbol = graph.getPossibleType(element, DIA.Element);\r
224                 if(instanceSymbol == null) continue;\r
225 \r
226                 String instanceSymbolName = graph.getRelatedValue(instanceSymbol, L0.HasName, Bindings.STRING);\r
227                 Resource targetSymbol = Layer0Utils.getPossibleChild(graph, best.getResource(), DIA.ElementClass, instanceSymbolName); \r
228                 if(targetSymbol != null && !targetSymbol.equals(instanceSymbol)) {\r
229                     MigrationOperation op = new MigrationOperation(new NamedResource(uri, instance), best, new NamedResource("", targetSymbol));\r
230                     addInstance(list, graph, instance, op);\r
231                 }\r
232 \r
233             }\r
234         }\r
235 \r
236         sortInstances(model, list);\r
237 \r
238         return model;\r
239     }\r
240 \r
241     private static Collection<NamedResource> getLocations(ReadGraph graph, final Resource project) throws DatabaseException {\r
242         Layer0 L0 = Layer0.getInstance(graph);\r
243         SimulationResource SIMU = SimulationResource.getInstance(graph);\r
244         Collection<NamedResource> libraries = new ArrayList<>();\r
245         for (Resource r : graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, SIMU.Model))) {\r
246             String name = Versions.getStandardNameString(graph, r);\r
247             libraries.add(new NamedResource(name, r));\r
248         }\r
249         Collection<Resource> ontologies = Simantics.applySCL("Simantics/SharedOntologies", "traverseSharedOntologies", graph, graph.getRootLibrary());\r
250         for (Resource r : ontologies) {\r
251             String name = Versions.getStandardNameString(graph, r);\r
252             libraries.add(new NamedResource(name, r));\r
253         }\r
254         return libraries;\r
255     }\r
256 \r
257     private static final Comparator<NamedResource> NAMED_RESOURCE_COMPARATOR = new Comparator<NamedResource>() {\r
258         @Override\r
259         public int compare(NamedResource o1, NamedResource o2) {\r
260             return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());\r
261         }\r
262     };\r
263 \r
264     private static void sortInstances(MigrateModel model, MapList<NamedResource, MigrationOperation> list) {\r
265         ArrayList<NamedResource> keys = new ArrayList<>(list.getKeys());\r
266         Collections.sort(keys, NAMED_RESOURCE_COMPARATOR);\r
267         for(NamedResource key : keys) {\r
268             Collection<MigrationOperation> ops = list.getValuesSnapshot(key);\r
269             String[] parts = key.getName().split("#");\r
270             //System.out.println("Parts: " + Arrays.toString(parts));\r
271             model.instances.add(Triple.make(parts[0], new NamedResource(URIStringUtils.unescape(parts[1]), key.getResource()), ops));\r
272         }\r
273     }\r
274 \r
275     private static Pair<String,Integer> addInstance(MapList<NamedResource, MigrationOperation> list, ReadGraph graph, Resource instance, MigrationOperation op) throws DatabaseException {\r
276         Layer0 L0 = Layer0.getInstance(graph);\r
277         if(graph.isInstanceOf(instance, L0.IndexRoot)) return Pair.make("", 0);\r
278         instance = graph.getPossibleObject(instance, L0.PartOf);\r
279         if(instance == null) return Pair.make("", 0);\r
280         String name = Versions.getStandardNameString(graph, instance);\r
281         String escapedName = URIStringUtils.escape(name);\r
282         Pair<String,Integer> parent = addInstance(list, graph, instance, op);\r
283         StringBuilder code = new StringBuilder(parent.first).append('/').append(escapedName).append('#');\r
284         for(int i=0;i<parent.second;i++) code.append(' ');\r
285         code.append(escapedName);\r
286         list.add(new NamedResource(code.toString(), instance), op);\r
287         return Pair.make(parent.first + "/" + escapedName, parent.second + 4);\r
288     }\r
289 \r
290     private static NamedResource matchBest(ReadGraph graph, Resource type, Resource newOntology) throws DatabaseException {\r
291         VersionMap versions = Versions.match(graph, type, newOntology);\r
292         return versions.getNewest(graph, Versions.getBaseName(graph, type));\r
293     }\r
294 \r
295     private static MigrateModel newMigrateModel(ReadGraph graph) throws DatabaseException {\r
296         MigrateModel model = new MigrateModel();\r
297         model.activeModels.addAll( graph.syncRequest(new ActiveModels(Simantics.getProjectResource())) );\r
298         return model;\r
299     }\r
300 \r
301 }\r