Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cee9f055d017ef3a61be83544fa9ecdf8beb098d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Ansgar Radermacher  ansgar.radermacher@cea.fr
 *
 *****************************************************************************/

package org.eclipse.papyrus.qompass.modellibs.core.mappingrules;

import java.util.Iterator;

import org.eclipse.papyrus.FCM.Port;
import org.eclipse.papyrus.FCM.util.IMappingRule;
import org.eclipse.papyrus.FCM.util.MapUtil;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterDirectionKind;
import org.eclipse.uml2.uml.Type;

/**
 * Use a conjugated interface (!= conjugated port), i.e. an interface in which the roles "in" and "out"
 * of each parameter of an operation are inversed. This transformation is useful in the context of transforming
 * a "push" interface into a "pull" interface, i.e. instead of calling and providing values values to the
 * called component via "in", the called components queries us and obtains these values as out parameters.
 * TODO: This rules is currently not used, since data flow operation are currently either based on a datatype
 * or a MARTE FlowPort
 *
 * @author ansgar
 *
 */
public class UseConjIntf implements IMappingRule {

	private static final String CONJ_INTF_TYPE = "ConjIntfType_"; //$NON-NLS-1$
	private static final String CONJ_INTF = "ConjIntf_"; //$NON-NLS-1$

	@Override
	public Type calcDerivedType(Port p, boolean update) {
		Type type = p.getBase_Port().getType();
		if (!(type instanceof Interface)) {
			return null;
		}

		Interface typingInterface = (Interface) type;
		Interface derivedInterface = MapUtil.getDerivedInterface(p, CONJ_INTF, update);
		Class derivedType = MapUtil.getDerivedClass(p, CONJ_INTF_TYPE, update);
		if (!update) {
			return derivedType;
		}
		if (derivedInterface == null) {
			return null;
		}
		MapUtil.addUsage(derivedType, derivedInterface);
		for (Operation operation : typingInterface.getOwnedOperations()) {
			String name = operation.getName();

			// check whether operation already exists. Create, if not
			Operation derivedOperation = derivedInterface.getOperation(name, null, null);
			if (derivedOperation == null) {
				derivedOperation = derivedInterface.createOwnedOperation(name, null, null);
			}

			// TODO: move to Copy (factor code, ensure that these values are handled in case of model copies ...)
			derivedOperation.setIsAbstract(operation.isAbstract());
			derivedOperation.setIsStatic(operation.isStatic()); // (does not make sense for an interface, if true)
			derivedOperation.setIsUnique(operation.isUnique());
			derivedOperation.setIsQuery(operation.isQuery());

			for (Parameter parameter : operation.getOwnedParameters()) {
				String paramName = parameter.getName();
				Type paramType = parameter.getType();
				if (derivedOperation.getOwnedParameter(paramName, paramType) == null) {
					Parameter newParameter =
							derivedOperation.createOwnedParameter(parameter.getName(), parameter.getType());
					ParameterDirectionKind direction = parameter.getDirection();
					if (direction == ParameterDirectionKind.IN_LITERAL) {
						newParameter.setDirection(ParameterDirectionKind.OUT_LITERAL);
					}
					else if (direction == ParameterDirectionKind.OUT_LITERAL) {
						newParameter.setDirection(ParameterDirectionKind.IN_LITERAL);
					}
					else {
						newParameter.setDirection(direction);
					}
					newParameter.setLower(parameter.getLower());
					newParameter.setUpper(parameter.getUpper());
				}
			}
			// remove those parameters that exist in derived, but not original interface.
			Iterator<Parameter> derivedParameters = derivedOperation.getOwnedParameters().iterator();
			while (derivedParameters.hasNext()) {
				Parameter parameter = derivedParameters.next();
				String paramName = parameter.getName();
				Type paramType = parameter.getType();
				if (operation.getOwnedParameter(paramName, paramType) == null) {
					// not on in original interface, remove from derived as well
					derivedParameters.remove();
				}
			}
		}

		// check whether operations in derived interface exist in original interface
		// (remove, if not)
		Iterator<Operation> derivedOperations = derivedInterface.getOwnedOperations().iterator();
		while (derivedOperations.hasNext()) {
			Operation derivedOperation = derivedOperations.next();
			String name = derivedOperation.getName();
			if (typingInterface.getOperation(name, null, null) == null) {
				// not in typing interface, remove
				if (derivedInterface.getOperations().remove(derivedOperation)) {
					derivedOperations = derivedInterface.getOwnedOperations().iterator();
				}
			}
		}
		return derivedType;
	}

	@Override
	public boolean needsUpdate(Port p) {
		Type type = p.getType();
		if (!(type instanceof Interface)) {
			return false;
		}

		Interface typingInterface = (Interface) type;
		Interface derivedInterface = MapUtil.getOrCreateDerivedInterface(p, CONJ_INTF);
		Class derivedType = MapUtil.getOrCreateDerivedClass(p, CONJ_INTF_TYPE);
		if ((derivedInterface == null) || (derivedType == null)) {
			return true;
		}
		for (Operation operation : typingInterface.getOwnedOperations()) {
			String name = operation.getName();

			// check whether operation already exists. Create, if not
			Operation derivedOperation = derivedInterface.getOperation(name, null, null);
			if (derivedOperation == null) {
				return true;
			}

			// TODO: move to Copy (factor code, ensure that these values are handled in case of model copies ...)
			derivedOperation.setIsAbstract(operation.isAbstract());
			derivedOperation.setIsStatic(operation.isStatic()); // (does not make sense for an interface, if true)
			derivedOperation.setIsUnique(operation.isUnique());
			derivedOperation.setIsQuery(operation.isQuery());

			for (Parameter parameter : operation.getOwnedParameters()) {
				String paramName = parameter.getName();
				Type paramType = parameter.getType();
				if (derivedOperation.getOwnedParameter(paramName, paramType) == null) {
					return true;
				}
			}
			// remove those parameters that exist in derived, but not original interface.
			Iterator<Parameter> derivedParameters = derivedOperation.getOwnedParameters().iterator();
			while (derivedParameters.hasNext()) {
				Parameter parameter = derivedParameters.next();
				String paramName = parameter.getName();
				Type paramType = parameter.getType();
				if (operation.getOwnedParameter(paramName, paramType) == null) {
					// not on in original operation
					return true;
				}
			}
		}

		// check whether operations in derived interface exist in original interface
		// (remove, if not)
		Iterator<Operation> derivedOperations = derivedInterface.getOwnedOperations().iterator();
		while (derivedOperations.hasNext()) {
			Operation derivedOperation = derivedOperations.next();
			String name = derivedOperation.getName();
			if (typingInterface.getOperation(name, null, null) == null) {
				// not in typing interface
				return true;
			}
		}
		return false;
	}
}

Back to the top