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