Changeset 1624 for trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs
- Timestamp:
- 07/29/14 14:30:16 (10 years ago)
- Location:
- trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java
r1604 r1624 24 24 25 25 import org.eclipse.emf.common.util.EList; 26 import org.eclipse.uml2.uml.Association; 27 import org.eclipse.uml2.uml.Class; 26 28 import org.eclipse.uml2.uml.Comment; 29 import org.eclipse.uml2.uml.Element; 30 import org.eclipse.uml2.uml.Interaction; 31 import org.eclipse.uml2.uml.Lifeline; 32 import org.eclipse.uml2.uml.Message; 33 import org.eclipse.uml2.uml.MessageOccurrenceSpecification; 34 import org.eclipse.uml2.uml.Model; 35 import org.eclipse.uml2.uml.Operation; 27 36 import org.eclipse.uml2.uml.Region; 28 37 import org.eclipse.uml2.uml.StateMachine; 29 38 import org.eclipse.uml2.uml.Transition; 39 import org.eclipse.uml2.uml.UMLPackage; 30 40 import org.eclipse.uml2.uml.Vertex; 31 41 … … 36 46 /** 37 47 * <p> 38 * TODO comment48 * Utilities for working with UML. 39 49 * </p> 40 50 * … … 43 53 public class UMLUtils { 44 54 45 public static List<Event> createUMLTransitionSequence(List<Event> sequence, StateMachine stateMachine) { 46 List<List<Transition>> matchingSequences = determineMatchingTransitionSequences(sequence, stateMachine); 55 /** 56 * <p> 57 * Creates a sequence of events with {@link UMLTransitionType} as event type from a given 58 * sequence of events with the {@link SOAPEventType}, by matching the sequences to a state 59 * machine. 60 * </p> 61 * 62 * @param sequence 63 * SOAP sequences 64 * @param stateMachine 65 * the state machine 66 * @return create UML sequences 67 */ 68 public static List<Event> createUMLTransitionSequence(List<Event> sequence, 69 StateMachine stateMachine) 70 { 71 List<List<Transition>> matchingSequences = 72 determineMatchingTransitionSequences(sequence, stateMachine); 47 73 48 74 if (matchingSequences.size() != 1) { … … 56 82 return umlEventSequence; 57 83 } 58 59 public static void convertStateMachineToUsageProfile(Collection<List<Event>> sequences, StateMachine stateMachine) { 84 85 /** 86 * <p> 87 * Uses a sequences of events with the {@link UMLTransitionType} to determine the transition 88 * probabilities for the state machine. 89 * </p> 90 * 91 * @param sequences 92 * UML sequences 93 * @param stateMachine 94 * state machine to be converted to a usage profile 95 */ 96 public static void convertStateMachineToUsageProfile(Collection<List<Event>> sequences, 97 StateMachine stateMachine) 98 { 60 99 // create state->outgoings hashmap 61 Map<Vertex, Map<Transition, Integer>> stateMap = new HashMap<>();62 for ( Region region : stateMachine.getRegions()) {63 for ( Vertex state : region.getSubvertices()) {64 stateMap.put(state, new HashMap<Transition, Integer>());65 } 66 } 67 100 Map<Vertex, Map<Transition, Integer>> stateMap = new HashMap<>(); 101 for (Region region : stateMachine.getRegions()) { 102 for (Vertex state : region.getSubvertices()) { 103 stateMap.put(state, new HashMap<Transition, Integer>()); 104 } 105 } 106 68 107 // create counters for each transition 69 for (List<Event> sequence : sequences) {70 for ( Event event : sequence) {71 if ( event.getType() instanceof UMLTransitionType) {108 for (List<Event> sequence : sequences) { 109 for (Event event : sequence) { 110 if (event.getType() instanceof UMLTransitionType) { 72 111 Transition transition = ((UMLTransitionType) event.getType()).getTransition(); 73 Map<Transition, Integer> transitionMap = stateMap.get(transition.getSource());112 Map<Transition, Integer> transitionMap = stateMap.get(transition.getSource()); 74 113 Integer value = transitionMap.get(transition); 75 if ( value==null) {114 if (value == null) { 76 115 value = 0; 77 116 } 78 transitionMap.put(transition, value+1); 79 } else { 80 throw new RuntimeException("Wrong event type. Only UMLTransitionType supported but was: " + 81 event.getType().getClass().getName()); 117 transitionMap.put(transition, value + 1); 118 } 119 else { 120 throw new RuntimeException( 121 "Wrong event type. Only UMLTransitionType supported but was: " + 122 event.getType().getClass().getName()); 82 123 } 83 124 } … … 85 126 86 127 // calculate probabilities 87 for ( Region region : stateMachine.getRegions()) {88 for ( Vertex state : region.getSubvertices()) {89 Map<Transition, Integer> transitionMap = stateMap.get(state);128 for (Region region : stateMachine.getRegions()) { 129 for (Vertex state : region.getSubvertices()) { 130 Map<Transition, Integer> transitionMap = stateMap.get(state); 90 131 int totalCount = 0; 91 for ( Entry<Transition,Integer> entry : transitionMap.entrySet()) {132 for (Entry<Transition, Integer> entry : transitionMap.entrySet()) { 92 133 totalCount += entry.getValue(); 93 134 } 94 if ( totalCount!=0) {95 for ( Transition transition : state.getOutgoings()) {135 if (totalCount != 0) { 136 for (Transition transition : state.getOutgoings()) { 96 137 double prob = 0.0d; 97 if (transitionMap.containsKey(transition)) {98 prob = ((double) transitionMap.get(transition)) /totalCount;138 if (transitionMap.containsKey(transition)) { 139 prob = ((double) transitionMap.get(transition)) / totalCount; 99 140 } 100 141 Comment comment = transition.createOwnedComment(); 101 comment.setBody("" + prob ); 102 } 103 } else { 142 comment.setBody("" + prob); 143 } 144 } 145 else { 104 146 // system has never been in this state, all transitions equally likely 105 147 int numOutgoings = state.getOutgoings().size(); 106 for ( Transition transition : state.getOutgoings()) {148 for (Transition transition : state.getOutgoings()) { 107 149 Comment comment = transition.createOwnedComment(); 108 comment.setBody("" + (1.0d/numOutgoings) ); 109 } 110 } 111 } 112 } 113 } 114 115 public static List<List<Transition>> determineMatchingTransitionSequences(List<Event> sequence, StateMachine stateMachine) { 150 comment.setBody("" + (1.0d / numOutgoings)); 151 } 152 } 153 } 154 } 155 } 156 157 /** 158 * <p> 159 * Determines all matching {@link Transition} sequences in a state machine for a given sequence 160 * of SOAP events. 161 * </p> 162 * 163 * @param sequence 164 * SOAP sequence 165 * @param stateMachine 166 * the state machine 167 * @return all matching {@link Transition} sequences 168 */ 169 public static List<List<Transition>> determineMatchingTransitionSequences(List<Event> sequence, 170 StateMachine stateMachine) 171 { 116 172 EList<Region> regions = stateMachine.getRegions(); 117 173 EList<Vertex> states = null; … … 175 231 } 176 232 else { 177 throw new RuntimeException("Wrong event type. Only UMLTransitionType supported but was: " + 178 event.getType().getClass().getName()); 233 throw new RuntimeException( 234 "Wrong event type. Only UMLTransitionType supported but was: " + 235 event.getType().getClass().getName()); 179 236 } 180 237 } 181 238 return matchingSequences; 182 239 } 183 184 private static List<Transition> matchTransitions(List<Transition> transitions, SOAPEventType eventType) 240 241 /** 242 * <p> 243 * Extends a given model with an interaction that represents an observed sequence. 244 * </p> 245 * 246 * @param sequence sequence that is added as sequence diagram 247 * @param model UML model to which the interaction is added 248 * @param interactionName name of the interaction 249 */ 250 public static void createInteractionFromEventSequence(List<Event> sequence, Model model, String interactionName) 251 { 252 Map<String, Class> classMap = new HashMap<>(); 253 254 EList<Element> elements = model.getOwnedElements(); 255 for (Element element : elements) { 256 if (element instanceof Class) { 257 classMap.put(((Class) element).getName(), (Class) element); 258 } 259 } 260 261 Interaction interaction = 262 (Interaction) model 263 .createPackagedElement(interactionName, UMLPackage.Literals.INTERACTION); 264 265 Lifeline userLifeline = interaction.createLifeline("user"); 266 267 int i = 0; 268 for (Event event : sequence) { 269 if (event.getType() instanceof SOAPEventType) { 270 SOAPEventType eventType = (SOAPEventType) event.getType(); 271 String serviceName = eventType.getServiceName(); 272 String methodName = eventType.getCalledMethod(); 273 Class targetClass = classMap.get(serviceName); 274 if (targetClass == null) { 275 throw new RuntimeException( 276 "Could not find class in the UML model that belong to the service: " + 277 serviceName); 278 } 279 280 Lifeline targetLifeline = interaction.getLifeline(serviceName); 281 if (targetLifeline == null) { 282 targetLifeline = interaction.createLifeline(serviceName); 283 Association association = 284 (Association) model.getPackagedElement("user_" + serviceName, true, 285 UMLPackage.Literals.ASSOCIATION, 286 true); 287 targetLifeline 288 .setRepresents(association.getMemberEnd(serviceName, 289 classMap.get(serviceName))); 290 } 291 MessageOccurrenceSpecification sendFragment = 292 (MessageOccurrenceSpecification) interaction 293 .createFragment(i + ":" + methodName + "_sendFragment", 294 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 295 MessageOccurrenceSpecification recvFragment = 296 (MessageOccurrenceSpecification) interaction 297 .createFragment(i + ":" + methodName + "_recvFragment", 298 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 299 300 sendFragment.setCovered(userLifeline); 301 recvFragment.setCovered(targetLifeline); 302 303 Message message = interaction.createMessage(methodName); 304 message.setSignature(getOperationFromName(targetClass.getOperations(), methodName)); 305 message.setSendEvent(sendFragment); 306 message.setReceiveEvent(recvFragment); 307 308 sendFragment.setMessage(message); 309 recvFragment.setMessage(message); 310 } 311 else { 312 throw new RuntimeException( 313 "Wrong event type. Only SOAPEventType supported but was: " + 314 event.getType().getClass().getName()); 315 } 316 i++; 317 } 318 } 319 320 /** 321 * <p> 322 * Fetches an operation using only its name from a list of operations. Returns the first match 323 * found or null if no match is found. 324 * </p> 325 * 326 * @param operations 327 * list of operations 328 * @param name 329 * name of the operation 330 * @return first matching operation; null if no match is found 331 */ 332 private static Operation getOperationFromName(EList<Operation> operations, String name) { 333 if (name == null) { 334 throw new IllegalArgumentException("name of the operation must not be null"); 335 } 336 if (operations != null) { 337 for (Operation operation : operations) { 338 if (operation.getName() != null && operation.getName().equals(name)) { 339 return operation; 340 } 341 } 342 } 343 return null; 344 } 345 346 /** 347 * <p> 348 * Determines which transitions match a given {@link SOAPEventType}. 349 * </p> 350 * 351 * @param transitions 352 * the transitions 353 * @param eventType 354 * the SOAP event 355 * @return matching transitions 356 */ 357 private static List<Transition> matchTransitions(List<Transition> transitions, 358 SOAPEventType eventType) 185 359 { 186 360 List<Transition> matching = new LinkedList<>(); … … 194 368 return matching; 195 369 } 196 370 197 371 } -
trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/eventcore/UMLTransitionType.java
r1604 r1624 1 // Copyright 2012 Georg-August-Universität Göttingen, Germany 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 1 15 package de.ugoe.cs.autoquest.plugin.uml.eventcore; 2 16 … … 5 19 import de.ugoe.cs.autoquest.eventcore.IEventType; 6 20 7 // TODO comment 21 /** 22 * <p> 23 * Event type for transitions in UML sequence diagrams 24 * </p> 25 * 26 * @author Steffen Herbold 27 */ 8 28 public class UMLTransitionType implements IEventType { 9 29 10 /** */ 30 /** 31 * Id for object serialization. 32 */ 11 33 private static final long serialVersionUID = 1L; 12 34 35 /** 36 * associated UML transition 37 */ 13 38 private final Transition transition; 14 39 40 /** 41 * <p> 42 * Constructor. Creates a new instance. 43 * </p> 44 * 45 * @param transition 46 * the instance 47 */ 15 48 public UMLTransitionType(Transition transition) { 49 if (transition == null) { 50 throw new IllegalArgumentException("Transition must not be null"); 51 } 16 52 this.transition = transition; 17 53 } 18 54 55 /** 56 * <p> 57 * Returns the associated UML element 58 * </p> 59 * 60 * @return 61 */ 19 62 public Transition getTransition() { 20 63 return transition; 21 64 } 22 65 66 /* 67 * (non-Javadoc) 68 * 69 * @see java.lang.Object#equals(java.lang.Object) 70 */ 23 71 @Override 24 72 public boolean equals(Object other) { 25 if ( other==this) {73 if (other == this) { 26 74 return true; 27 75 } 28 if ( other instanceof UMLTransitionType) {76 if (other instanceof UMLTransitionType) { 29 77 return ((UMLTransitionType) other).transition.equals(transition); 30 78 } 31 79 return false; 32 80 }; 33 81 82 /* 83 * (non-Javadoc) 84 * 85 * @see java.lang.Object#hashCode() 86 */ 34 87 @Override 35 88 public int hashCode() { 36 89 return transition.hashCode(); 37 90 } 38 91 92 /* 93 * (non-Javadoc) 94 * 95 * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName() 96 */ 39 97 @Override 40 98 public String getName() { 41 // TODO Auto-generated method stub 42 System.out.println("TODO: implement UMLTransactionType.getName "); 43 return null; 99 return transition.getName(); 44 100 } 45 101
Note: See TracChangeset
for help on using the changeset viewer.