1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.mapping;
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;
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).
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.
43 * @author Tuukka Lehtonen
45 public class ComponentTypeUpdater extends Trigger {
47 private static final boolean DEBUG = false;
50 protected Layer0X L0X;
51 protected DiagramResource DIA;
52 protected ModelingResources MOD;
53 protected StructuralResource2 STR;
55 protected Session session;
57 private Resource configurationDiagram;
58 private Resource configuration;
59 private Resource componentType;
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);
68 this.session = g.getSession();
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);
80 public boolean equals(Object other) {
83 if (!(other instanceof ComponentTypeUpdater))
85 ComponentTypeUpdater map = (ComponentTypeUpdater)other;
86 return ObjectUtils.objectEquals(map.configurationDiagram, configurationDiagram)
87 && ObjectUtils.objectEquals(map.configuration, configuration)
88 && ObjectUtils.objectEquals(map.componentType, componentType);
92 public int hashCode() {
93 return ObjectUtils.hashCode(configurationDiagram)
94 + 31 * (ObjectUtils.hashCode(configuration)
95 + 31 * ObjectUtils.hashCode(componentType));
99 public IModification perform(ReadGraph graph) throws DatabaseException {
101 System.out.println(this + ": Find modification (configuration diagram=" + configurationDiagram
102 + ", configuration=" + configuration + ", componentType=" + componentType + ")");
104 if (componentType== null || configuration == null || configurationDiagram == null)
107 // Disabled because we fixed modeling rules to prevent making different
108 // types of (invalid) connections to the once-created interface terminal
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);
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);
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.
130 public class LiftedConnectionPointFixer implements IModification {
132 private Resource componentType;
134 public LiftedConnectionPointFixer(Resource componentType) {
135 this.componentType = componentType;
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);
145 System.out.println("test lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)
147 String error = LiftFlag.isConnectionPointValid(graph, componentType, connectionPoint, lcp);
151 // Fix connection point and symbol terminal type
153 System.out.println("modi lifted cp(" + NameUtils.getSafeName(graph, connectionPoint, true)
154 + "): " + lcp + "\nerrors: " + error);
155 error = LiftFlag.validateConnectionPoint(graph, componentType, connectionPoint, lcp);
157 System.out.println(getClass().getSimpleName() + " error: " + error);