-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.mapping;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-\r
-import java.util.Map;\r
-\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.datatypes.literal.GUID;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.synchronization.ISynchronizationContext;\r
-import org.simantics.diagram.synchronization.SynchronizationException;\r
-import org.simantics.diagram.synchronization.SynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.BasicResources;\r
-import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;\r
-import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;\r
-import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ComponentUtils;\r
-import org.simantics.modeling.services.ComponentNamingUtil;\r
-import org.simantics.modeling.services.NamingException;\r
-import org.simantics.project.IProject;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class ComponentCopyAdvisor extends GraphCopyAdvisor {\r
-\r
- @Override\r
- public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source,\r
- Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
- BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);\r
- return (graph.isInstanceOf(source, br.STR.Component) || graph.isInstanceOf(source, br.STR.Connection))\r
- ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;\r
- }\r
-\r
- @Override\r
- public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
- Resource targetContainer) throws DatabaseException {\r
- return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());\r
- }\r
-\r
- @Override\r
- public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
- Resource targetContainer, Map<Object, Object> map) throws DatabaseException {\r
- StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
- Resource copy = null;\r
- if (graph.isInstanceOf(source, STR.Connection)) {\r
- // Configuration connections are not named, can't use TG copy for\r
- // them at the moment.\r
- copy = CopyAdvisorUtil.copy2(graph, source, null, map);\r
- } else {\r
-// Resource model = graph.syncRequest(new PossibleModel(targetContainer));\r
-// copy = CopyAdvisorUtil.copy4(graph, source, model);\r
- copy = CopyAdvisorUtil.copy2(graph, source, null, map);\r
- }\r
-\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))\r
- graph.claim(targetContainer, L0.ConsistsOf, copy);\r
-\r
- graph.claimLiteral(copy, L0.identifier, L0.identifier_Inverse, L0.GUID, GUID.random(), GUID.BINDING);\r
- if (context.get(SynchronizationHints.NO_RENAME) == null)\r
- renameComponent(context, graph, source, copy, sourceContainer, targetContainer);\r
- return copy;\r
- }\r
-\r
- public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,\r
- Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
- return renameComponent(context, graph, source, copy, sourceContainer, targetContainer,\r
- Layer0.getInstance(graph).HasName);\r
- }\r
-\r
- public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,\r
- Resource copy, Resource sourceContainer, Resource targetContainer, Resource nameProperty)\r
- throws DatabaseException {\r
- BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);\r
-\r
- Resource sourceComponentType = graph.getPossibleType(source, br.STR.Component);\r
- if (sourceComponentType == null)\r
- return null;\r
-\r
- // Try to obtain a ComponentNamingStrategy for renaming the copied object.\r
- IProject project = context.get(SynchronizationHints.PROJECT);\r
- if (project == null)\r
- project = Simantics.getProject();\r
-\r
- // Try to give a valid or at least unique name for the new component.\r
- //Resource configurationRoot = ComponentUtils.getComponentConfigurationRoot(graph, copy);\r
- //Resource composite = ComponentUtils.tryGetComponentContainer(graph, copy);\r
- Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);\r
- Resource composite = targetContainer;\r
-\r
- if (configurationRoot != null) {\r
- try {\r
- String name = ComponentNamingUtil.findFreshInstanceName(graph, project, configurationRoot, composite, sourceComponentType);\r
- graph.claimLiteral(copy, nameProperty, name, Bindings.STRING);\r
- return name;\r
- } catch (NamingException e) {\r
- // This will produce duplicate names in the model,\r
- // should not happen if we don't run out of names.\r
- throw new SynchronizationException(e);\r
- }\r
- }\r
- return null;\r
- }\r
-\r
- @Override\r
- public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,\r
- Resource targetContainer) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
-\r
- if (sourceContainer.equals(targetContainer))\r
- return null;\r
-\r
- // This handles e.g. connections, which are not part of container\r
- if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {\r
- graph.deny(sourceContainer, L0.ConsistsOf, source);\r
- graph.claim(targetContainer, L0.ConsistsOf, source);\r
- }\r
-\r
- return source;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * 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.modeling.mapping;
+
+import gnu.trove.map.hash.THashMap;
+
+import java.util.Map;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.diagram.synchronization.ISynchronizationContext;
+import org.simantics.diagram.synchronization.SynchronizationException;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.BasicResources;
+import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
+import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
+import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ComponentUtils;
+import org.simantics.modeling.services.ComponentNamingUtil;
+import org.simantics.modeling.services.NamingException;
+import org.simantics.project.IProject;
+import org.simantics.structural.stubs.StructuralResource2;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class ComponentCopyAdvisor extends GraphCopyAdvisor {
+
+ @Override
+ public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source,
+ Resource sourceContainer, Resource targetContainer) throws DatabaseException {
+ BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
+ return (graph.isInstanceOf(source, br.STR.Component) || graph.isInstanceOf(source, br.STR.Connection))
+ ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
+ }
+
+ @Override
+ public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
+ Resource targetContainer) throws DatabaseException {
+ return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
+ }
+
+ @Override
+ public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
+ Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+ Resource copy = null;
+ if (graph.isInstanceOf(source, STR.Connection)) {
+ // Configuration connections are not named, can't use TG copy for
+ // them at the moment.
+ copy = CopyAdvisorUtil.copy2(graph, source, null, map);
+ } else {
+// Resource model = graph.syncRequest(new PossibleModel(targetContainer));
+// copy = CopyAdvisorUtil.copy4(graph, source, model);
+ copy = CopyAdvisorUtil.copy2(graph, source, null, map);
+ }
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
+ graph.claim(targetContainer, L0.ConsistsOf, copy);
+
+ Layer0Utils.claimNewIdentifier(graph, copy, false);
+ if (context.get(SynchronizationHints.NO_RENAME) == null)
+ renameComponent(context, graph, source, copy, sourceContainer, targetContainer);
+ return copy;
+ }
+
+ public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,
+ Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
+ return renameComponent(context, graph, source, copy, sourceContainer, targetContainer,
+ Layer0.getInstance(graph).HasName);
+ }
+
+ public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,
+ Resource copy, Resource sourceContainer, Resource targetContainer, Resource nameProperty)
+ throws DatabaseException {
+ BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
+
+ Resource sourceComponentType = graph.getPossibleType(source, br.STR.Component);
+ if (sourceComponentType == null)
+ return null;
+
+ // Try to obtain a ComponentNamingStrategy for renaming the copied object.
+ IProject project = context.get(SynchronizationHints.PROJECT);
+ if (project == null)
+ project = Simantics.getProject();
+
+ // Try to give a valid or at least unique name for the new component.
+ //Resource configurationRoot = ComponentUtils.getComponentConfigurationRoot(graph, copy);
+ //Resource composite = ComponentUtils.tryGetComponentContainer(graph, copy);
+ Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);
+ Resource composite = targetContainer;
+
+ if (configurationRoot != null) {
+ try {
+ String name = ComponentNamingUtil.findFreshInstanceName(graph, project, configurationRoot, composite, sourceComponentType);
+ graph.claimLiteral(copy, nameProperty, name, Bindings.STRING);
+ return name;
+ } catch (NamingException e) {
+ // This will produce duplicate names in the model,
+ // should not happen if we don't run out of names.
+ throw new SynchronizationException(e);
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
+ Resource targetContainer) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+
+ if (sourceContainer.equals(targetContainer))
+ return null;
+
+ // This handles e.g. connections, which are not part of container
+ if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
+ graph.deny(sourceContainer, L0.ConsistsOf, source);
+ graph.claim(targetContainer, L0.ConsistsOf, source);
+ }
+
+ return source;
+ }
+
+}