Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b3743a61a01e4aa79dbcc05863a0fa9801005a5 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***************************************************************************
 * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte,
 * Generalitat de la Comunitat Valenciana . 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: Gabriel Merin Cubero (Prodevelop) – Initial API implementation
 * 				 Franciso Javier Cano Muñoz (Prodevelop) - Improvements in the first msg search
 *
 ******************************************************************************/

package org.eclipse.papyrus.diagram.sequence.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.eclipse.gmf.runtime.common.core.util.HashUtil;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.OccurrenceSpecification;

public class DiagramInitializationUtil {

	public static List<List<Message>> getGlobalMsgOrdering(
			Interaction interaction) {
		HashMap<Lifeline, List<Message>> lifelinesOrderedList = new HashMap<Lifeline, List<Message>>();

		// For each lifeline, obtains it's ordered messages
		for (Lifeline lifeline : interaction.getLifelines()) {
			lifelinesOrderedList.put(lifeline,
					getLifelineOrderdedMsgs(lifeline));
		}

		// Merge the information of all the lifelines in a global message
		// ordered list
		List<List<Message>> globalOrderedList = mergeOrderedMsgsLists(lifelinesOrderedList);

		// Return the list
		return globalOrderedList;
	}

	public static Message getLifelineFirstMessage(Lifeline lifeline) {
		// Obtain the Interaction in which the Lifeline is contained
		Interaction interaction = lifeline.getInteraction();

		// If there is no interaction, we cannot continue
		if (interaction == null)
			return null;

		// Loop through the list of messages
		for (Message msg : interaction.getMessages()) {

			MessageOccurrenceSpecification sourceEnd = MessageCommonUtil
					.getMessageSrcMOS(msg);
			MessageOccurrenceSpecification targetEnd = MessageCommonUtil
					.getMessageDstMOS(msg);

			// This should not happen but just in case
			if (sourceEnd == null || targetEnd == null)
				continue;

			// This should not happen either
			if (sourceEnd.getGeneralOrderings().size() == 0
					|| targetEnd.getGeneralOrderings().size() == 0)
				continue;

			// Check if the message belongs to this Lifeline
			if (MessageCommonUtil.getMessageSrcLifeline(msg) == lifeline) {
				// Check if it is the first message
				if (sourceEnd.getGeneralOrderings().get(0).getBefore() == sourceEnd)
					return msg;
			}
			if (MessageCommonUtil.getMessageDstLifeline(msg) == lifeline) {
				// Check if it is the first message
				if (targetEnd.getGeneralOrderings().get(0).getBefore() == targetEnd)
					return msg;
			}
		}

		// If we get here it means the lifeline has no messages
		return null;
	}

	public static List<Message> getLifelineOrderdedMsgs(Lifeline lifeline) {
		// Get it's first message
		Message msg = getLifelineFirstMessage(lifeline);

		// Create the empty list
		ArrayList<Message> orderedList = new ArrayList<Message>();

		// If no message found then nothing is left to do
		if (msg == null)
			return orderedList;

		// Add the first element
		orderedList.add(msg);

		// Get the initial OS
		OccurrenceSpecification auxOS;
		if (MessageCommonUtil.getMessageSrcLifeline(msg) == lifeline) {
			auxOS = MessageCommonUtil.getMessageSrcMOS(msg);
		} else {
			auxOS = MessageCommonUtil.getMessageDstMOS(msg);
		}
		// Add the rest of elements
		while (!isLastOS(auxOS)) {
			auxOS = auxOS.getGeneralOrderings().get(0).getAfter();
			if (auxOS instanceof MessageOccurrenceSpecification
					&& ((MessageOccurrenceSpecification) auxOS).getMessage() != null) {
				orderedList.add(((MessageOccurrenceSpecification) auxOS)
						.getMessage());
			}
		}

		// Return the ordered list
		return orderedList;
	}

	public static List<List<Message>> mergeOrderedMsgsLists(
			HashMap<Lifeline, List<Message>> orderedLists) {

		ArrayList<List<Message>> globalList = new ArrayList<List<Message>>();

		// We need one or more lists to order
		if (orderedLists.size() >= 1) {
			// VisitedLists
			List<List<Message>> visitedLists = new ArrayList<List<Message>>();
			// HashMap
			HashMap<Message, Integer> hashMap = new HashMap<Message, Integer>();

			for (List<Message> partialList : orderedLists.values()) {
				// Case the hashMap is empty
				if (hashMap.size() == 0) {
					for (int i = 0; i < partialList.size(); i++) {
						hashMap.put(partialList.get(i), i);
					}
					visitedLists.add(partialList);
				}
				// Otherwise
				else {
					int offset = 0;
					Integer existingPos;
					for (int i = 0; i < partialList.size(); i++) {
						Message msg = partialList.get(i);
						existingPos = hashMap.get(msg);
						// If not found add it
						if (existingPos == null) {
							// Add it to it's position
							hashMap.put(msg, i + offset);
						}
						// Found
						else {
							if (existingPos > (i + offset)) {
								// The global list rules
								offset = existingPos - i;
							} else if (existingPos < (i + offset)) {
								// The partial list rules

								// Current message (and the ones that go after
								// him) need to be moved to the new position
								List<Message> followersList = getFollowersList(
										msg, visitedLists);

								// Note: The followers list also includes msg

								int shift = (i + offset) - existingPos;
								for (Message follower : followersList) {
									// Update it's position
									hashMap.put(follower, hashMap.get(follower)
											+ shift); 
								}

							} else {
								// If existingPos == (i + offset), then do
								// nothing
							}
						}
					} // end for (partialList elements)
					visitedLists.add(partialList);
				}
			} // end for (orderedLists elements)

			// If the hashMap is not empty, add the ordered elements to the
			// globalList
			if (!hashMap.isEmpty()) {
				// Obtain the list of values ordered and without repeated elements
				Set<Integer> valuesSet = new HashSet<Integer>();
				valuesSet.addAll(hashMap.values());
				Object[] valuesSortedArray = valuesSet.toArray();
				Arrays.sort(valuesSortedArray);
				
				// Initialize the globalList with the maximum size possible
				for (int i = 0; i < valuesSortedArray.length; i++)
					globalList.add(new ArrayList<Message>());

				
				// Add the ordered elements
				for (Entry<Message, Integer> entry : hashMap.entrySet()) {
					int pos = Arrays.binarySearch(valuesSortedArray, entry.getValue());
					globalList.get(pos).add(entry.getKey());
				}
			}
		}

		// Return the list
		return globalList;
	}

	public static List<Message> getFollowersList(Message msg,
			List<List<Message>> visitedLists) {
		List<Message> followersList = new ArrayList<Message>();
		HashMap<List<Message>, List<Message>> followersFoundPerList = new HashMap<List<Message>, List<Message>>();
		Set<Message> addSet = new HashSet<Message>();
		Set<Message> partialAddSet = new HashSet<Message>();

		// Foreach visited list obtain it's followersList
		for (List<Message> visitedList : visitedLists) {
			followersFoundPerList.put(visitedList, followersList(msg,
					visitedList));
		}

		// Build a set with the all followersLists
		for (Entry<List<Message>, List<Message>> entry : followersFoundPerList
				.entrySet()) {
			addSet.addAll(entry.getValue());
		}

		boolean modifiedFlag;
		do {
			modifiedFlag = false;

			// Check if any of the visited lists has an empty followersList
			for (Entry<List<Message>, List<Message>> entry : followersFoundPerList
					.entrySet()) {
				// If empty, obtain again it's list with the addSet
				if (entry.getValue().isEmpty()) {
					// Loop through the addSet
					for (Message auxMsg : addSet) {
						partialAddSet.addAll(followersList(auxMsg, entry
								.getKey()));
					}
					if (!partialAddSet.isEmpty()) {
						entry.getValue().addAll(partialAddSet);
						addSet.addAll(partialAddSet);
						partialAddSet.clear();
						modifiedFlag = true;
					}
				}
			}
		} while (modifiedFlag);

		// Add the msg
		addSet.add(msg);

		// Add the set to the list
		followersList.addAll(addSet);

		return followersList;
	}

	public static List<Message> followersList(Message msg, List<Message> msgList) {
		if (msgList.contains(msg)) {
			List<Message> returnList = new ArrayList<Message>();
			returnList.addAll(msgList.subList(msgList.indexOf(msg) + 1, msgList
					.size()));
			return returnList;
		} else
			return new ArrayList<Message>();
	}

	public static boolean isLastOS(OccurrenceSpecification mos) {
		// If there is no ordering, we will consider as the last MOS
		if (mos.getGeneralOrderings().size() == 0)
			return true;

		// If the After attribute matches with itself, then it is the last MOS
		if (mos.getGeneralOrderings().get(0).getAfter() == mos)
			return true;
		else
			return false;
	}

}

Back to the top