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 gnu.trove.set.hash.THashSet;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
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;
40 * @author Antti Villberg
42 public class MigrateModel {
44 public static class MigrationOperation {
46 public NamedResource instanceToMigrate;
47 public NamedResource targetType;
48 public NamedResource targetSymbol;
50 public MigrationOperation(NamedResource nr, NamedResource targetType, NamedResource targetSymbol) {
51 this.instanceToMigrate = nr;
52 this.targetType = targetType;
53 this.targetSymbol = targetSymbol;
57 public String toString() {
58 return instanceToMigrate.getName();
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);
69 String targetURI = graph.getURI(targetType.getResource());
70 return URIStringUtils.unescape(sourceURI) + " into " + URIStringUtils.unescape(targetURI);
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;
87 public String replace(WriteGraph graph, Resource instanceToMigrate, Resource type) throws DatabaseException {
89 Layer0 L0 = Layer0.getInstance(graph);
90 StructuralResource2 STR = StructuralResource2.getInstance(graph);
92 Collection<Resource> currentTypes = graph.getPrincipalTypes(instanceToMigrate);
93 if(currentTypes.size() == 1 && currentTypes.contains(type)) return null;
95 StringBuilder problems = new StringBuilder();
98 for(Statement stm : graph.getStatements(instanceToMigrate, L0.IsWeaklyRelatedTo)) {
100 Resource predicate = stm.getPredicate();
101 if(stm.isAsserted(instanceToMigrate)) continue;
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");
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");
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");
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");
134 Collection<Resource> sourceConnetionTypes = graph.syncRequest(new AllowedConnectionTypes(predicate));
135 Collection<Resource> replacementConnectionTypes = graph.syncRequest(new AllowedConnectionTypes(replacement));
137 Set<Resource> sourceSet = new THashSet<>(sourceConnetionTypes);
138 Set<Resource> replacementSet = new THashSet<>(replacementConnectionTypes);
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");
150 if(problems.length() > 0) {
151 return problems.toString();
155 for(Statement stm : graph.getStatements(instanceToMigrate, L0.IsWeaklyRelatedTo)) {
157 Resource predicate = stm.getPredicate();
158 if(stm.isAsserted(instanceToMigrate)) continue;
160 if(L0.InstanceOf.equals(predicate)) {
162 graph.claim(instanceToMigrate, L0.InstanceOf, type);
165 if(graph.isInstanceOf(predicate, STR.Property) || graph.isInstanceOf(predicate, STR.ConnectionRelation)) {
166 Resource replacement = getPossibleReplacement(graph, type, predicate);
168 if(replacement == null) continue;
169 graph.claim(stm.getSubject(), replacement, stm.getObject());
178 public String perform(WriteGraph graph) throws DatabaseException {
180 Resource instance = instanceToMigrate.getResource();
181 Resource type = targetType.getResource();
182 Resource symbol = targetSymbol != null ? targetSymbol.getResource() : null;
184 String result = replace(graph, instance, type);
185 if(result != null) return result;
187 ModelingResources MOD = ModelingResources.getInstance(graph);
188 Resource element = graph.getPossibleObject(instance, MOD.ComponentToElement);
189 if(element == null) return null;
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";
208 return replace(graph, element, targetSymbol);
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();