Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 95182dc0a8f928bc7cbbb15b6e0715557667f3a9 (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
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.toolsmiths.profilemigration.internal.data.structure;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.tree.TreeNode;
import org.eclipse.papyrus.toolsmiths.profilemigration.internal.utils.ProfileUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Stereotype;

/**
 * Registry of each StereotypeApplicationDescriptor in the model
 */
public class StereotypeApplicationRegistry {

	/**
	 * This list contain all stereotype application of the profiled model before the migration
	 */
	public static List<StereotypeApplicationDescriptor> stereotypeApplicationList;

	private static Package model;

	/**
	 * Constructor, initialize the list of stereotype application
	 *
	 * @param treeNode
	 * @param profiledModel
	 */
	public StereotypeApplicationRegistry(TreeNode treeNode, Package profiledModel) {
		initList(treeNode, profiledModel);
		StereotypeApplicationRegistry.model = profiledModel;
	}

	/**
	 * Get the list of StereotypeApplicationDescriptor corresponding to the stereotype
	 *
	 * @param stereotype
	 *            a stereotype from the model
	 * @return the list of StereotypeApplicationDescriptor corresponding to the stereotype
	 */
	public static List<StereotypeApplicationDescriptor> getStereotypeApplicationDescriptors(Stereotype stereotype) {
		List<StereotypeApplicationDescriptor> result = new ArrayList<>();
		for (StereotypeApplicationDescriptor descriptor : stereotypeApplicationList) {
			if (descriptor.getStereotype().equals(stereotype)) {
				result.add(descriptor);
			}
		}
		return result;
	}

	/**
	 * Get the list of StereotypeApplicationDescriptor corresponding to the stereotype and sub stereotypes
	 * Warning: this method give only stereotype find is applied profiles
	 *
	 * @param stereotype
	 *            a stereotype from the model
	 * @return the list of StereotypeApplicationDescriptor corresponding to the modelStereotype and sub stereotypes
	 */
	public static List<StereotypeApplicationDescriptor> getAllStereotypeApplicationDescriptors(Stereotype stereotype) {
		List<StereotypeApplicationDescriptor> result = new ArrayList<>();
		for (StereotypeApplicationDescriptor descriptor : stereotypeApplicationList) {
			for (Stereotype subStereotype : ProfileUtil.findAllSubStereotypes(stereotype, model, false)) {
				if (descriptor.getStereotype().equals(subStereotype)) {
					result.add(descriptor);
				}
			}
		}
		return result;
	}

	/**
	 * Get the list of StereotypeApplicationDescriptor corresponding to stereotype application on the element
	 *
	 * @param element
	 *
	 * @return the list of StereotypeApplicationDescriptor corresponding to stereotype application on the element
	 */
	public static List<StereotypeApplicationDescriptor> getAllStereotypeApplicationDescriptorsFromOwner(Element element) {
		List<StereotypeApplicationDescriptor> result = new ArrayList<>();
		for (StereotypeApplicationDescriptor descriptor : stereotypeApplicationList) {
			if (descriptor.getOwner().equals(element)) {
				result.add(descriptor);
			}
		}
		return result;
	}

	/**
	 * Get the StereotypeApplicationDescriptor corresponding to the application of the stereotype on the owner
	 *
	 * @param owner
	 *            the owner of the stereotype
	 * @param stereotype
	 *            a stereotype from the model
	 * @return the StereotypeApplicationDescriptor corresponding to the application of the stereotype on the owner
	 */
	public static StereotypeApplicationDescriptor getStereotypeApplicationDescriptors(Element owner, Stereotype stereotype) {
		StereotypeApplicationDescriptor result = null;
		for (StereotypeApplicationDescriptor descriptor : stereotypeApplicationList) {
			if (descriptor.getOwner().equals(owner) && descriptor.getStereotype().equals(stereotype)) {
				result = descriptor;
			}
		}
		return result;
	}

	/**
	 * Initialize the list of stereotype application in the specified model
	 *
	 * @param treeNode
	 * @param profiledModel
	 */
	private void initList(TreeNode treeNode, Package profiledModel) {
		// get all stereotypes in the treeNode
		Set<Stereotype> treeNodeStereotypes = new HashSet<>();
		getStereotypes(treeNode, treeNodeStereotypes);

		// fill the list
		stereotypeApplicationList = new ArrayList<>();
		TreeIterator<EObject> iter = profiledModel.eAllContents();
		while (iter.hasNext()) {
			EObject object = iter.next();
			if (object instanceof Element) {
				Element element = (Element) object;
				for (Stereotype stereotype : element.getAppliedStereotypes()) {
					stereotypeApplicationList.add(new StereotypeApplicationDescriptor(stereotype, element));
				}
			}
		}
	}

	/**
	 * Get all stereotype find in the treeNode
	 *
	 * @param treeNode
	 * @param result
	 *            the set of finding stereotype
	 */
	private void getStereotypes(TreeNode treeNode, Set<Stereotype> result) {
		if (treeNode.getData() instanceof Match) {
			if (((Match) treeNode.getData()).getRight() instanceof EAnnotation) {
				return; // not interesting, this is the record of different version of the profile
			}
			if (((Match) treeNode.getData()).getLeft() instanceof Stereotype) {
				result.add((Stereotype) ((Match) treeNode.getData()).getLeft());
			} else if (((Match) treeNode.getData()).getRight() instanceof Stereotype) {
				result.add((Stereotype) ((Match) treeNode.getData()).getRight());
			}
		}
		for (TreeNode child : treeNode.getChildren()) {
			getStereotypes(child, result);
		}
	}
}

Back to the top