Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3addfe607204a2486ec75cb600cd2f121b065e3 (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
/*****************************************************************************
 * 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 org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.FCM.Port;
import org.eclipse.papyrus.FCM.util.IMappingRule;
import org.eclipse.papyrus.FCM.util.MapUtil;
import org.eclipse.papyrus.qompass.designer.core.Log;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.Type;

/**
 * Will generate a suitable called interface push consumer. The port is typed with a primitive type
 * or data type. The generated interface has a "push (data <Type>)" operation ).
 *
 * The interface is identical to that of a PushProducer (and will be shared).
 *
 * @author ansgar
 */
public class PushConsumer implements IMappingRule {

	public static String PUSH_I_PREFIX = "Push_"; //$NON-NLS-1$

	public static String PUSH_OP_PREFIX = "push"; //$NON-NLS-1$

	public static String PUSH_OP_PARNAME = "data"; //$NON-NLS-1$

	@Override
	public boolean needsUpdate(Port p) {
		Type type = p.getBase_Port().getType();

		if ((type instanceof PrimitiveType) || (type instanceof DataType) || (type instanceof Signal)) {

			Interface derivedInterface = MapUtil.getOrCreateDerivedInterfaceFP(p, PUSH_I_PREFIX, type, false);
			if (derivedInterface == null) {
				return true;
			}
			Operation derivedOperation = derivedInterface.getOperation(PUSH_OP_PREFIX, null, null);
			if (derivedOperation == null) {
				return true;
			}
			EList<Parameter> parameters = derivedOperation.getOwnedParameters();
			if (parameters.size() != 1) {
				return true;
			} else {
				Parameter parameter = parameters.get(0);
				if (!parameter.getName().equals(PUSH_OP_PARNAME)) {
					return true;
				}
				if (parameter.getType() != type) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public Interface getProvided(Port p, boolean update) {
		Log.log(IStatus.INFO, Log.CALC_PORTKIND,
				p.getKind().getBase_Class().getName() + " => GetProvided on " + p.getBase_Port().getName());
		Type type = p.getBase_Port().getType();

		if ((type instanceof PrimitiveType) || (type instanceof DataType) || (type instanceof Signal)) {

			Interface derivedInterface = MapUtil.getOrCreateDerivedInterfaceFP(p, PUSH_I_PREFIX, type, update);
			if (!update) {
				return derivedInterface;
			}

			if (derivedInterface == null) {
				// may happen, if within template (do not want creation of derived interfaces in template)
				return null;
			}

			// check whether operation already exists. Create, if not
			Operation derivedOperation = derivedInterface.getOperation(PUSH_OP_PREFIX, null, null);
			if (derivedOperation == null) {
				derivedOperation = derivedInterface.createOwnedOperation(PUSH_OP_PREFIX, null, null);
			}
			EList<Parameter> parameters = derivedOperation.getOwnedParameters();
			if (parameters.size() == 0) {
				derivedOperation.createOwnedParameter(PUSH_OP_PARNAME, type);
			} else {
				parameters.get(0).setName(PUSH_OP_PARNAME);
				parameters.get(0).setType(type);
			}
			return derivedInterface;
		} else {
			return null;
		}
	}

	@Override
	public Interface getRequired(Port p, boolean update) {
		return null;
	}
}

Back to the top