--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.layer0.request;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.simantics.datatypes.literal.GUID;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+
+/**
+ * Get a map from GUIDs to the children of the given resource.
+ */
+public final class ChildrenByIdentifier extends ResourceRead<Map<GUID, Resource>> {
+ public ChildrenByIdentifier(Resource resource) {
+ super(resource);
+ }
+
+ @Override
+ public Map<GUID, Resource> perform(ReadGraph graph) throws DatabaseException {
+ Map<GUID, Resource> result = new HashMap<>();
+ Layer0 L0 = Layer0.getInstance(graph);
+ Collection<Resource> children = graph.getObjects(resource, L0.ConsistsOf);
+ for (Resource child : children) {
+ GUID guid = graph.getPossibleRelatedValue(child, L0.identifier, GUID.BINDING);
+ if (guid != null)
+ result.put(guid, child);
+ }
+
+ return result;
+ }
+}
*******************************************************************************/
package org.simantics.modeling;
-import gnu.trove.set.hash.THashSet;
-
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.simantics.databoard.Bindings;
import org.simantics.databoard.util.URIStringUtils;
+import org.simantics.datatypes.literal.GUID;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.Statement;
import org.simantics.db.WriteGraph;
import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.adapter.Instances;
+import org.simantics.db.layer0.request.ChildrenByIdentifier;
import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.diagram.stubs.DiagramResource;
import org.simantics.layer0.Layer0;
import org.simantics.structural2.modelingRules.AllowedConnectionTypes;
import org.simantics.utils.ObjectUtils;
import org.simantics.utils.datastructures.Triple;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.set.hash.THashSet;
/**
* @author Antti Villberg
*/
public class MigrateModel {
+ public static final Logger LOGGER = LoggerFactory.getLogger(MigrateModel.class);
+
public static class MigrationOperation {
public NamedResource instanceToMigrate;
private Resource getPossibleReplacement(ReadGraph graph, Resource type, Resource predicate) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(graph);
+
+ // Try to find a relation with the same GUID
+ GUID guid = graph.getPossibleRelatedValue(predicate, L0.identifier, GUID.BINDING);
+ Map<GUID, Resource> children = graph.syncRequest(new ChildrenByIdentifier(type), TransientCacheListener.instance());
+ Resource replacement = children.get(guid);
+ if (replacement != null)
+ return replacement;
+
+ // Fall back to using relation name
String name = graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);
if(name == null) return null;
Resource child = Layer0Utils.getPossibleChild(graph, type, name);
problems.append(" " + name + " was a property in the source type\n");
continue;
}
+
String sourceValueType = graph.getPossibleRelatedValue(predicate, L0.RequiresValueType, Bindings.STRING);
String replacementValueType = graph.getPossibleRelatedValue(replacement, L0.RequiresValueType, Bindings.STRING);
if(!ObjectUtils.objectEquals(sourceValueType, replacementValueType)) {
Resource replacement = getPossibleReplacement(graph, type, predicate);
graph.deny(stm);
if(replacement == null) continue;
- graph.claim(stm.getSubject(), replacement, stm.getObject());
+
+ // Filter out incompatible literal types.
+ // TODO: Show warning in UI
+ Resource object = stm.getObject();
+ Resource replacementRange = graph.getPossibleObject(replacement, L0.HasRange);
+ if (replacementRange != null && !graph.isInstanceOf(object, replacementRange)) {
+ String instanceName = graph.getPossibleRelatedValue(instanceToMigrate, L0.HasName);
+ if (instanceName == null) instanceName = "<unknown>";
+
+ String rangeName = graph.getPossibleRelatedValue(replacementRange, L0.HasName);
+ if (rangeName == null) rangeName = "<unknown>";
+
+ Resource literalType= graph.getPossibleType(object, L0.Value);
+ String literalTypeName = literalType != null ? graph.getPossibleRelatedValue(literalType, L0.HasName) : null;
+ if (literalTypeName == null) literalTypeName = "<unknown>";
+
+ String replacementName = graph.getRelatedValue(replacement, L0.HasName);
+ LOGGER.warn("{}: Ignored incompatible value of type {} for predicate {} with range {}", instanceName, literalTypeName, replacementName, rangeName);
+
+ continue;
+ }
+
+ graph.claim(stm.getSubject(), replacement, object);
}
}