1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
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;
51 import gnu.trove.set.hash.THashSet;
54 * @author Antti Villberg
55 * @author Tuukka Lehtonen
57 public class UserComponentMigration {
59 private static final Logger LOGGER = LoggerFactory.getLogger(UserComponentMigration.class);
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())
65 Triple<String, NamedResource, Collection<MigrationOperation>> instances = model.instances.get(0);
66 ArrayList<MigrationOperation> result = new ArrayList<>();
68 for (MigrationOperation instance : instances.third) {
69 if (components.contains(instance.instanceToMigrate.getResource())) {
73 if (!result.isEmpty())
74 doMigration(new NullProgressMonitor(), graph, result);
77 public static String doMigration(IProgressMonitor monitor, WriteGraph graph, final ArrayList<MigrationOperation> operations) throws DatabaseException {
78 graph.markUndoPoint();
79 StringBuilder b = new StringBuilder();
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");
95 b.append(op.getDescription(graph) + "\n");
96 b.append(" success\n");
100 int total = success + problem;
101 b.insert(0, "---------------------\n");
102 b.insert(0, "Details:\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");
109 Layer0Utils.addCommentMetadata(graph, "Migrated " + total + " instances");
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()));
120 if (monitor.isCanceled())
121 throw new CancelTransactionException();
122 for(Resource root : roots) {
123 UserComponentPostMigrationAction action = graph.getPossibleAdapter(root, UserComponentPostMigrationAction.class);
125 action.perform(monitor, graph);
129 public static MigrateModel getComponentTypeModel(ReadGraph graph, Resource source, Resource target, Resource symbol) throws DatabaseException {
130 MigrateModel model = newMigrateModel(graph);
132 MapList<NamedResource, MigrationOperation> list = new MapList<>();
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);
143 model.instanceCount = instances.size();
144 for(Resource instance : instances) {
146 String uri = graph.getPossibleURI(instance);
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);
155 Resource instanceSymbol = graph.getPossibleType(element, DIA.Element);
156 if(instanceSymbol == null) continue;
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);
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);
174 sortInstances(model, list);
179 public static MigrateModel getSharedOntologyModel(ReadGraph graph, Resource sourceOntology, Resource targetOntology) throws DatabaseException {
180 MigrateModel model = newMigrateModel(graph);
182 MapList<NamedResource, MigrationOperation> list = new MapList<>();
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);
189 Set<Resource> types = new THashSet<>();
190 for(Resource type : query.find(graph, sourceOntology)) {
192 if(graph.isInheritedFrom(type, DIA.Element)) continue;
193 Resource root = graph.syncRequest(new PossibleIndexRoot(type));
194 if(sourceOntology.equals(root)) types.add(type);
197 Set<Resource> instances = new THashSet<>();
198 Collection<NamedResource> locations = getLocations(graph, Simantics.getProjectResource());
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);
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));
216 NamedResource best = matchBest(graph, type, targetOntology);
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);
226 Resource instanceSymbol = graph.getPossibleType(element, DIA.Element);
227 if(instanceSymbol == null) continue;
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);
239 sortInstances(model, list);
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));
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));
258 private static final Comparator<NamedResource> NAMED_RESOURCE_COMPARATOR = new Comparator<NamedResource>() {
260 public int compare(NamedResource o1, NamedResource o2) {
261 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());
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));
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);
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));
296 private static MigrateModel newMigrateModel(ReadGraph graph) throws DatabaseException {
297 MigrateModel model = new MigrateModel();
298 model.activeModels.addAll( graph.syncRequest(new ActiveModels(Simantics.getProjectResource())) );