From: Reino Ruusu Date: Thu, 14 Mar 2019 15:02:31 +0000 (+0200) Subject: Enhancements to UC migration. X-Git-Tag: v1.43.0~136^2~179 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5ef2cc0bbaf6e69c23bd1f26ea2a5ff09b6473ce;p=simantics%2Fplatform.git Enhancements to UC migration. gitlab #271 gitlab #272 Change-Id: Idefd29805e5c9cc5f7c331ba6bdb2a541e6ab134 (cherry picked from commit 40b90f72c9598beb7e21a07b280c43a68adc34f2) --- 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 index 000000000..8b3c0768b --- /dev/null +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ChildrenByIdentifier.java @@ -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> { + public ChildrenByIdentifier(Resource resource) { + super(resource); + } + + @Override + public Map perform(ReadGraph graph) throws DatabaseException { + Map result = new HashMap<>(); + Layer0 L0 = Layer0.getInstance(graph); + Collection 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; + } +} diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java index eb0b80709..4e472ded4 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java @@ -11,24 +11,26 @@ *******************************************************************************/ 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 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 = ""; + + String rangeName = graph.getPossibleRelatedValue(replacementRange, L0.HasName); + if (rangeName == null) rangeName = ""; + + Resource literalType= graph.getPossibleType(object, L0.Value); + String literalTypeName = literalType != null ? graph.getPossibleRelatedValue(literalType, L0.HasName) : null; + if (literalTypeName == null) literalTypeName = ""; + + 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); } }