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