Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8891de9d1ad1113d1ef60026ae90b9fe04627d9c (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
/*****************************************************************************
 * 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.Status;
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.papyrus.qompass.designer.core.Utils;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
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 callable interface pulling consumer. The port is typed with a primitive type
 * or data type. The generated interface has a "<Type> pull as well as a "boolean hasData ()" operation).
 * 
 * @author ansgar
 */
public class PullConsumer implements IMappingRule {

	public Interface getProvided(Port p, InstanceSpecification config, boolean update) {
		return null;
	}

	public static PullConsumer getInstance() {
		if(instance == null) {
			instance = new PullConsumer();
		}
		return instance;
	}

	public Interface getRequired(Port p, InstanceSpecification config, boolean update) {
		org.eclipse.uml2.uml.Port umlPort = p.getBase_Port();
		Element owner = umlPort.getOwner();
		String ownerStr = "";
		if(owner instanceof NamedElement) {
			ownerStr = " of class " + ((NamedElement)owner).getQualifiedName();
		}
		Log.log(Status.INFO, Log.CALC_PORTKIND,
			p.getKind().getBase_Class().getName() + " => GetRequired on " + umlPort.getName() + ownerStr);
		Type type = umlPort.getType();

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

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

			// check whether operation already exists. Create, if not
			Operation derivedOperationPull = derivedInterface.getOperation("pull", null, null);
			if(derivedOperationPull == null) {
				derivedOperationPull = derivedInterface.createOwnedOperation("pull", null, null, type);
			}
			EList<Parameter> parameters = derivedOperationPull.getOwnedParameters();
			if(parameters.size() > 0) {
				Parameter parameter = parameters.get(0);
				if((parameter.getName() == null) || (!parameter.getName().equals("ret"))) {
					parameter.setName("ret");
				}
				if(parameter.getType() != type) {
					parameter.setType(type);
				}
			}
			Package model = Utils.getTop(umlPort);
			Element element = Utils.getQualifiedElement(model, "corba::Boolean");
			Type booleanType = null;
			if(element instanceof Type) {
				booleanType = (Type)element;
			}

			// check whether operation already exists. Create, if not
			Operation derivedOperationHasData = derivedInterface.getOperation("hasData", null, null);
			if(derivedOperationHasData == null) {
				derivedOperationHasData = derivedInterface.createOwnedOperation("hasData", null, null, booleanType);
			}

			parameters = derivedOperationHasData.getOwnedParameters();
			if(parameters.size() > 0) {
				Parameter parameter = parameters.get(0);
				if((parameter.getName() == null) || (!parameter.getName().equals("ret"))) {
					parameter.setName("ret");
				}
				if((booleanType != null) && (parameter.getType() != booleanType)) {
					// added != null check
					parameter.setType(booleanType);
				}
			}

			return derivedInterface;
		} else {
			return null;
		}
	}

	protected static PullConsumer instance;
}

Back to the top