|
Line 0
Link Here
|
|
|
1 |
/***************************************************************************** |
| 2 |
* Copyright (c) 2009 CEA |
| 3 |
* |
| 4 |
* |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* http://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: |
| 11 |
* Soyatec - Initial API and implementation |
| 12 |
* |
| 13 |
*****************************************************************************/ |
| 14 |
package org.eclipse.papyrus.uml.diagram.sequence.util; |
| 15 |
|
| 16 |
import org.eclipse.emf.common.util.EList; |
| 17 |
import org.eclipse.uml2.uml.Element; |
| 18 |
import org.eclipse.uml2.uml.ExecutionSpecification; |
| 19 |
import org.eclipse.uml2.uml.Gate; |
| 20 |
import org.eclipse.uml2.uml.Lifeline; |
| 21 |
import org.eclipse.uml2.uml.Message; |
| 22 |
import org.eclipse.uml2.uml.MessageEnd; |
| 23 |
import org.eclipse.uml2.uml.MessageSort; |
| 24 |
import org.eclipse.uml2.uml.NamedElement; |
| 25 |
import org.eclipse.uml2.uml.OccurrenceSpecification; |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Helper class for determine the message connections. Both for connecting and reconnecting. |
| 30 |
* |
| 31 |
* @author Jin Liu (jin.liu@soyatec.com) |
| 32 |
*/ |
| 33 |
public class MessageConnectionHelper { |
| 34 |
|
| 35 |
public static boolean debug = false; |
| 36 |
|
| 37 |
private MessageConnectionHelper() { |
| 38 |
} |
| 39 |
|
| 40 |
public static boolean canReorientSource(Message message, Element newSource) { |
| 41 |
if(message == null || newSource == null) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
Element target = null; |
| 45 |
MessageEnd receiveEvent = message.getReceiveEvent(); |
| 46 |
if(receiveEvent instanceof OccurrenceSpecification) { |
| 47 |
EList<Lifeline> covereds = ((OccurrenceSpecification)receiveEvent).getCovereds(); |
| 48 |
if(!covereds.isEmpty()) { |
| 49 |
target = covereds.get(0); |
| 50 |
} |
| 51 |
} else if(receiveEvent instanceof Gate) { |
| 52 |
target = ((Gate)receiveEvent).getOwner(); |
| 53 |
} |
| 54 |
return canExist(message.getMessageSort(), newSource, target); |
| 55 |
} |
| 56 |
|
| 57 |
public static boolean canReorientTarget(Message message, Element newTarget) { |
| 58 |
if(message == null || newTarget == null) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
Element source = null; |
| 62 |
MessageEnd sendEvent = message.getSendEvent(); |
| 63 |
if(sendEvent instanceof OccurrenceSpecification) { |
| 64 |
EList<Lifeline> covereds = ((OccurrenceSpecification)sendEvent).getCovereds(); |
| 65 |
if(!covereds.isEmpty()) { |
| 66 |
source = covereds.get(0); |
| 67 |
} |
| 68 |
} else if(sendEvent instanceof Gate) { |
| 69 |
source = ((Gate)sendEvent).getOwner(); |
| 70 |
} |
| 71 |
return canExist(message.getMessageSort(), source, newTarget); |
| 72 |
} |
| 73 |
|
| 74 |
public static boolean canExist(MessageSort messageSort, Element source, Element target) { |
| 75 |
if(debug) { |
| 76 |
print(messageSort, source, target); |
| 77 |
} |
| 78 |
if(MessageSort.ASYNCH_CALL_LITERAL == messageSort) { |
| 79 |
return canExistAsynchMessage(source, target); |
| 80 |
} else if(MessageSort.ASYNCH_SIGNAL_LITERAL == messageSort) { |
| 81 |
if(source == null) { |
| 82 |
return canExistFoundMessage(target); |
| 83 |
} |
| 84 |
if(target == null) { |
| 85 |
return canExistLostMessage(source); |
| 86 |
} |
| 87 |
} else if(MessageSort.SYNCH_CALL_LITERAL == messageSort) { |
| 88 |
return canExistSynchMessage(source, target); |
| 89 |
} else if(MessageSort.CREATE_MESSAGE_LITERAL == messageSort) { |
| 90 |
return canExistCreateMessage(source, target); |
| 91 |
} else if(MessageSort.DELETE_MESSAGE_LITERAL == messageSort) { |
| 92 |
return canExistDeleteMessage(source, target); |
| 93 |
} else if(MessageSort.REPLY_LITERAL == messageSort) { |
| 94 |
return canExistReplyMessage(source, target); |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
private static void print(MessageSort messageSort, Element source, Element target) { |
| 100 |
StringBuffer buf = new StringBuffer(); |
| 101 |
if(messageSort != null) { |
| 102 |
buf.append(messageSort.getName()); |
| 103 |
buf.append("["); |
| 104 |
} |
| 105 |
buf.append("Source: "); |
| 106 |
if(source != null) { |
| 107 |
buf.append(source.eClass().getName()); |
| 108 |
if(source instanceof NamedElement) { |
| 109 |
buf.append("("); |
| 110 |
buf.append(((NamedElement)source).getName()); |
| 111 |
buf.append(")"); |
| 112 |
} |
| 113 |
} else { |
| 114 |
buf.append("null"); |
| 115 |
} |
| 116 |
buf.append(", Target: "); |
| 117 |
if(target != null) { |
| 118 |
buf.append(target.eClass().getName()); |
| 119 |
if(target instanceof NamedElement) { |
| 120 |
buf.append("("); |
| 121 |
buf.append(((NamedElement)target).getName()); |
| 122 |
buf.append(")"); |
| 123 |
} |
| 124 |
} else { |
| 125 |
buf.append("null"); |
| 126 |
} |
| 127 |
buf.append("]"); |
| 128 |
System.out.println(new String(buf)); |
| 129 |
} |
| 130 |
|
| 131 |
public static boolean canExistReplyMessage(Element source, Element target) { |
| 132 |
if(target instanceof Message) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
if(source instanceof ExecutionSpecification && target instanceof Lifeline) { |
| 136 |
if(((ExecutionSpecification)source).getCovereds().contains(target)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
public static boolean canExistDeleteMessage(Element source, Element target) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
public static boolean canExistCreateMessage(Element source, Element target) { |
| 148 |
if(target != null) { |
| 149 |
if(false == target instanceof Lifeline) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
if(target == source) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
} |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
public static boolean canExistSynchMessage(Element source, Element target) { |
| 160 |
if(source != null && false == source instanceof ExecutionSpecification) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
if(target != null && !(target instanceof ExecutionSpecification || target instanceof Lifeline)) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
public static boolean canExistLostMessage(Element source) { |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
public static boolean canExistFoundMessage(Element target) { |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
public static boolean canExistAsynchMessage(Element source, Element target) { |
| 178 |
if(target instanceof Message) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
} |