]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentTypeUpdater.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentTypeUpdater.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentTypeUpdater.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentTypeUpdater.java
new file mode 100644 (file)
index 0000000..b6672de
--- /dev/null
@@ -0,0 +1,165 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * 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 org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.triggers.IModification;\r
+import org.simantics.layer0.utils.triggers.Trigger;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.flags.LiftFlag;\r
+import org.simantics.modeling.flags.LiftFlag.LiftedConnectionPoint;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+/**\r
+ * This trigger is used for configuration diagrams of structural component types\r
+ * to keep them in sync with the rest of the component type configuration,\r
+ * external interface and symbol(s).\r
+ * \r
+ * <p>\r
+ * This trigger is needed because generic modifications made to component type\r
+ * configurations do not automatically reflect to the symbol and interface.\r
+ * Putting this logic into a trigger keeps it nicely decoupled from the normal\r
+ * diagram editing logic.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ComponentTypeUpdater extends Trigger {\r
+\r
+    private static final boolean  DEBUG = false;\r
+\r
+    protected Layer0              L0;\r
+    protected Layer0X             L0X;\r
+    protected DiagramResource     DIA;\r
+    protected ModelingResources   MOD;\r
+    protected StructuralResource2 STR;\r
+\r
+    protected Session             session;\r
+\r
+    private Resource              configurationDiagram;\r
+    private Resource              configuration;\r
+    private Resource              componentType;\r
+\r
+    public ComponentTypeUpdater(ReadGraph g, Resource mapping) throws DatabaseException {\r
+        L0 = Layer0.getInstance(g);\r
+        L0X = Layer0X.getInstance(g);\r
+        DIA = DiagramResource.getInstance(g);\r
+        MOD = ModelingResources.getInstance(g);\r
+        STR = StructuralResource2.getInstance(g);\r
+\r
+        this.session = g.getSession();\r
+\r
+        this.configurationDiagram = g.getPossibleObject(mapping, g.getInverse(L0X.HasTrigger));\r
+        if (configurationDiagram != null) {\r
+            this.configuration = g.getPossibleObject(configurationDiagram, MOD.DiagramToComposite);\r
+            if (configuration != null) {\r
+                this.componentType = g.getPossibleObject(configuration, STR.Defines);\r
+            }\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object other) {\r
+        if (this == other)\r
+            return true;\r
+        if (!(other instanceof ComponentTypeUpdater))\r
+            return false;\r
+        ComponentTypeUpdater map = (ComponentTypeUpdater)other;\r
+        return ObjectUtils.objectEquals(map.configurationDiagram, configurationDiagram)\r
+                && ObjectUtils.objectEquals(map.configuration, configuration)\r
+                && ObjectUtils.objectEquals(map.componentType, componentType);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return ObjectUtils.hashCode(configurationDiagram)\r
+                + 31 * (ObjectUtils.hashCode(configuration)\r
+                        + 31 * ObjectUtils.hashCode(componentType));\r
+    }\r
+\r
+    @Override\r
+    public IModification perform(ReadGraph graph) throws DatabaseException {\r
+        if (DEBUG)\r
+            System.out.println(this + ": Find modification (configuration diagram=" + configurationDiagram\r
+                    + ", configuration=" + configuration + ", componentType=" + componentType + ")");\r
+\r
+        if (componentType== null || configuration == null || configurationDiagram == null)\r
+            return null;\r
+\r
+        // Disabled because we fixed modeling rules to prevent making different\r
+        // types of (invalid) connections to the once-created interface terminal\r
+        // flag.\r
+\r
+//        for (Resource connectionPoint : graph.syncRequest(new ObjectsWithType(componentType, L0.DomainOf,\r
+//                STR.ConnectionRelation))) {\r
+//            for (Resource flag : graph.getObjects(connectionPoint, DIA.Lifts)) {\r
+//                LiftedConnectionPoint lcp = LiftFlag.calculateLiftedConnectionPointForFlag(graph, flag);\r
+//                if (DEBUG)\r
+//                    System.out.println("lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true) + "): " + lcp);\r
+//                if (LiftFlag.isConnectionPointValid(graph, flag, connectionPoint, lcp) != null)\r
+//                    return new LiftedConnectionPointFixer(componentType);\r
+//            }\r
+//        }\r
+\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * A modification that tries to fix all the connection point definitions of\r
+     * the specified component type to match against the current connectivity of\r
+     * the flags lifted as these connection points.\r
+     */\r
+    public class LiftedConnectionPointFixer implements IModification {\r
+\r
+        private Resource componentType;\r
+\r
+        public LiftedConnectionPointFixer(Resource componentType) {\r
+            this.componentType = componentType;\r
+        }\r
+\r
+        @Override\r
+        public void perform(WriteGraph graph) throws DatabaseException {\r
+            for (Resource connectionPoint : graph.syncRequest(new ObjectsWithType(componentType, L0.DomainOf,\r
+                    STR.ConnectionRelation))) {\r
+                for (Resource flag : graph.getObjects(connectionPoint, DIA.Lifts)) {\r
+                    LiftedConnectionPoint lcp = LiftFlag.calculateLiftedConnectionPointForFlag(graph, flag);\r
+                    if (DEBUG)\r
+                        System.out.println("test lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)\r
+                                + "): " + lcp);\r
+                    String error = LiftFlag.isConnectionPointValid(graph, componentType, connectionPoint, lcp);\r
+                    if (error == null)\r
+                        continue;\r
+\r
+                    // Fix connection point and symbol terminal type\r
+                    if (DEBUG)\r
+                        System.out.println("modi lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)\r
+                                + "): " + lcp + "\nerrors: " + error);\r
+                    error = LiftFlag.validateConnectionPoint(graph, componentType, connectionPoint, lcp);\r
+                    if (error != null) {\r
+                        System.out.println(getClass().getSimpleName() + " error: " + error);\r
+                    }\r
+                }\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+}\r