]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/mapping/ComponentTypeUpdater.java
Sync git svn branch with SVN repository r33158.
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / mapping / ComponentTypeUpdater.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.mapping;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.Session;\r
17 import org.simantics.db.WriteGraph;\r
18 import org.simantics.db.common.request.ObjectsWithType;\r
19 import org.simantics.db.common.utils.NameUtils;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.diagram.stubs.DiagramResource;\r
22 import org.simantics.layer0.Layer0;\r
23 import org.simantics.layer0.utils.triggers.IModification;\r
24 import org.simantics.layer0.utils.triggers.Trigger;\r
25 import org.simantics.modeling.ModelingResources;\r
26 import org.simantics.modeling.flags.LiftFlag;\r
27 import org.simantics.modeling.flags.LiftFlag.LiftedConnectionPoint;\r
28 import org.simantics.operation.Layer0X;\r
29 import org.simantics.structural.stubs.StructuralResource2;\r
30 import org.simantics.utils.ObjectUtils;\r
31 \r
32 /**\r
33  * This trigger is used for configuration diagrams of structural component types\r
34  * to keep them in sync with the rest of the component type configuration,\r
35  * external interface and symbol(s).\r
36  * \r
37  * <p>\r
38  * This trigger is needed because generic modifications made to component type\r
39  * configurations do not automatically reflect to the symbol and interface.\r
40  * Putting this logic into a trigger keeps it nicely decoupled from the normal\r
41  * diagram editing logic.\r
42  * \r
43  * @author Tuukka Lehtonen\r
44  */\r
45 public class ComponentTypeUpdater extends Trigger {\r
46 \r
47     private static final boolean  DEBUG = false;\r
48 \r
49     protected Layer0              L0;\r
50     protected Layer0X             L0X;\r
51     protected DiagramResource     DIA;\r
52     protected ModelingResources   MOD;\r
53     protected StructuralResource2 STR;\r
54 \r
55     protected Session             session;\r
56 \r
57     private Resource              configurationDiagram;\r
58     private Resource              configuration;\r
59     private Resource              componentType;\r
60 \r
61     public ComponentTypeUpdater(ReadGraph g, Resource mapping) throws DatabaseException {\r
62         L0 = Layer0.getInstance(g);\r
63         L0X = Layer0X.getInstance(g);\r
64         DIA = DiagramResource.getInstance(g);\r
65         MOD = ModelingResources.getInstance(g);\r
66         STR = StructuralResource2.getInstance(g);\r
67 \r
68         this.session = g.getSession();\r
69 \r
70         this.configurationDiagram = g.getPossibleObject(mapping, g.getInverse(L0X.HasTrigger));\r
71         if (configurationDiagram != null) {\r
72             this.configuration = g.getPossibleObject(configurationDiagram, MOD.DiagramToComposite);\r
73             if (configuration != null) {\r
74                 this.componentType = g.getPossibleObject(configuration, STR.Defines);\r
75             }\r
76         }\r
77     }\r
78 \r
79     @Override\r
80     public boolean equals(Object other) {\r
81         if (this == other)\r
82             return true;\r
83         if (!(other instanceof ComponentTypeUpdater))\r
84             return false;\r
85         ComponentTypeUpdater map = (ComponentTypeUpdater)other;\r
86         return ObjectUtils.objectEquals(map.configurationDiagram, configurationDiagram)\r
87                 && ObjectUtils.objectEquals(map.configuration, configuration)\r
88                 && ObjectUtils.objectEquals(map.componentType, componentType);\r
89     }\r
90 \r
91     @Override\r
92     public int hashCode() {\r
93         return ObjectUtils.hashCode(configurationDiagram)\r
94                 + 31 * (ObjectUtils.hashCode(configuration)\r
95                         + 31 * ObjectUtils.hashCode(componentType));\r
96     }\r
97 \r
98     @Override\r
99     public IModification perform(ReadGraph graph) throws DatabaseException {\r
100         if (DEBUG)\r
101             System.out.println(this + ": Find modification (configuration diagram=" + configurationDiagram\r
102                     + ", configuration=" + configuration + ", componentType=" + componentType + ")");\r
103 \r
104         if (componentType== null || configuration == null || configurationDiagram == null)\r
105             return null;\r
106 \r
107         // Disabled because we fixed modeling rules to prevent making different\r
108         // types of (invalid) connections to the once-created interface terminal\r
109         // flag.\r
110 \r
111 //        for (Resource connectionPoint : graph.syncRequest(new ObjectsWithType(componentType, L0.DomainOf,\r
112 //                STR.ConnectionRelation))) {\r
113 //            for (Resource flag : graph.getObjects(connectionPoint, DIA.Lifts)) {\r
114 //                LiftedConnectionPoint lcp = LiftFlag.calculateLiftedConnectionPointForFlag(graph, flag);\r
115 //                if (DEBUG)\r
116 //                    System.out.println("lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true) + "): " + lcp);\r
117 //                if (LiftFlag.isConnectionPointValid(graph, flag, connectionPoint, lcp) != null)\r
118 //                    return new LiftedConnectionPointFixer(componentType);\r
119 //            }\r
120 //        }\r
121 \r
122         return null;\r
123     }\r
124 \r
125     /**\r
126      * A modification that tries to fix all the connection point definitions of\r
127      * the specified component type to match against the current connectivity of\r
128      * the flags lifted as these connection points.\r
129      */\r
130     public class LiftedConnectionPointFixer implements IModification {\r
131 \r
132         private Resource componentType;\r
133 \r
134         public LiftedConnectionPointFixer(Resource componentType) {\r
135             this.componentType = componentType;\r
136         }\r
137 \r
138         @Override\r
139         public void perform(WriteGraph graph) throws DatabaseException {\r
140             for (Resource connectionPoint : graph.syncRequest(new ObjectsWithType(componentType, L0.DomainOf,\r
141                     STR.ConnectionRelation))) {\r
142                 for (Resource flag : graph.getObjects(connectionPoint, DIA.Lifts)) {\r
143                     LiftedConnectionPoint lcp = LiftFlag.calculateLiftedConnectionPointForFlag(graph, flag);\r
144                     if (DEBUG)\r
145                         System.out.println("test lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)\r
146                                 + "): " + lcp);\r
147                     String error = LiftFlag.isConnectionPointValid(graph, componentType, connectionPoint, lcp);\r
148                     if (error == null)\r
149                         continue;\r
150 \r
151                     // Fix connection point and symbol terminal type\r
152                     if (DEBUG)\r
153                         System.out.println("modi lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)\r
154                                 + "): " + lcp + "\nerrors: " + error);\r
155                     error = LiftFlag.validateConnectionPoint(graph, componentType, connectionPoint, lcp);\r
156                     if (error != null) {\r
157                         System.out.println(getClass().getSimpleName() + " error: " + error);\r
158                     }\r
159                 }\r
160             }\r
161         }\r
162 \r
163     }\r
164 \r
165 }\r