1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.mapping;
\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
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
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
43 * @author Tuukka Lehtonen
\r
45 public class ComponentTypeUpdater extends Trigger {
\r
47 private static final boolean DEBUG = false;
\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
55 protected Session session;
\r
57 private Resource configurationDiagram;
\r
58 private Resource configuration;
\r
59 private Resource componentType;
\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
68 this.session = g.getSession();
\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
80 public boolean equals(Object other) {
\r
83 if (!(other instanceof ComponentTypeUpdater))
\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
92 public int hashCode() {
\r
93 return ObjectUtils.hashCode(configurationDiagram)
\r
94 + 31 * (ObjectUtils.hashCode(configuration)
\r
95 + 31 * ObjectUtils.hashCode(componentType));
\r
99 public IModification perform(ReadGraph graph) throws DatabaseException {
\r
101 System.out.println(this + ": Find modification (configuration diagram=" + configurationDiagram
\r
102 + ", configuration=" + configuration + ", componentType=" + componentType + ")");
\r
104 if (componentType== null || configuration == null || configurationDiagram == null)
\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
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
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
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
130 public class LiftedConnectionPointFixer implements IModification {
\r
132 private Resource componentType;
\r
134 public LiftedConnectionPointFixer(Resource componentType) {
\r
135 this.componentType = componentType;
\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
145 System.out.println("test lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)
\r
147 String error = LiftFlag.isConnectionPointValid(graph, componentType, connectionPoint, lcp);
\r
151 // Fix connection point and symbol terminal type
\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