]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Enhancements to UC migration. 75/2775/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Thu, 14 Mar 2019 15:02:31 +0000 (17:02 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 14 Mar 2019 15:02:31 +0000 (17:02 +0200)
gitlab #271
gitlab #272

Change-Id: Idefd29805e5c9cc5f7c331ba6bdb2a541e6ab134

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ChildrenByIdentifier.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java

diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ChildrenByIdentifier.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ChildrenByIdentifier.java
new file mode 100644 (file)
index 0000000..8b3c076
--- /dev/null
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * 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;
+       }
+}
index eb0b80709b70094fcdfff1f5900a70f35d0dee2e..4e472ded4a5d5198544a2ba6f18189db3d469ad9 100644 (file)
  *******************************************************************************/
 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;
@@ -36,12 +38,18 @@ import org.simantics.structural.stubs.StructuralResource2;
 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;
@@ -74,6 +82,15 @@ public class MigrateModel {
                
                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);
@@ -110,6 +127,7 @@ public class MigrateModel {
                                                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)) {
@@ -167,7 +185,29 @@ public class MigrateModel {
                                        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);
                                }
                                
                        }