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