[1604] | 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 |
|
---|
| 15 | package de.ugoe.cs.autoquest.plugin.uml;
|
---|
| 16 |
|
---|
| 17 | import java.util.Collection;
|
---|
[1763] | 18 | import java.util.Collections;
|
---|
| 19 | import java.util.Comparator;
|
---|
[1604] | 20 | import java.util.HashMap;
|
---|
[1898] | 21 | import java.util.HashSet;
|
---|
[1604] | 22 | import java.util.Iterator;
|
---|
[1763] | 23 | import java.util.LinkedHashMap;
|
---|
[1604] | 24 | import java.util.LinkedList;
|
---|
| 25 | import java.util.List;
|
---|
| 26 | import java.util.Map;
|
---|
| 27 | import java.util.Map.Entry;
|
---|
[1898] | 28 | import java.util.logging.Level;
|
---|
| 29 | import java.util.Set;
|
---|
| 30 | import java.util.TreeSet;
|
---|
[1604] | 31 |
|
---|
| 32 | import org.eclipse.emf.common.util.EList;
|
---|
[1763] | 33 | import org.eclipse.uml2.uml.Activity;
|
---|
| 34 | import org.eclipse.uml2.uml.ActivityEdge;
|
---|
| 35 | import org.eclipse.uml2.uml.ActivityNode;
|
---|
[1898] | 36 | import org.eclipse.uml2.uml.CallConcurrencyKind;
|
---|
[1763] | 37 | import org.eclipse.uml2.uml.CallOperationAction;
|
---|
[1604] | 38 | import org.eclipse.uml2.uml.Comment;
|
---|
[1752] | 39 | import org.eclipse.uml2.uml.Component;
|
---|
| 40 | import org.eclipse.uml2.uml.Connector;
|
---|
| 41 | import org.eclipse.uml2.uml.ConnectorEnd;
|
---|
[1908] | 42 | import org.eclipse.uml2.uml.DataType;
|
---|
[1835] | 43 | import org.eclipse.uml2.uml.Element;
|
---|
[1908] | 44 | import org.eclipse.uml2.uml.Expression;
|
---|
| 45 | import org.eclipse.uml2.uml.InstanceSpecification;
|
---|
| 46 | import org.eclipse.uml2.uml.InstanceValue;
|
---|
[1624] | 47 | import org.eclipse.uml2.uml.Interaction;
|
---|
[1643] | 48 | import org.eclipse.uml2.uml.InteractionFragment;
|
---|
[1752] | 49 | import org.eclipse.uml2.uml.Interface;
|
---|
[1624] | 50 | import org.eclipse.uml2.uml.Lifeline;
|
---|
[1908] | 51 | import org.eclipse.uml2.uml.LiteralBoolean;
|
---|
| 52 | import org.eclipse.uml2.uml.LiteralInteger;
|
---|
| 53 | import org.eclipse.uml2.uml.LiteralReal;
|
---|
| 54 | import org.eclipse.uml2.uml.LiteralString;
|
---|
[1624] | 55 | import org.eclipse.uml2.uml.Message;
|
---|
| 56 | import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
|
---|
[1752] | 57 | import org.eclipse.uml2.uml.MessageSort;
|
---|
[1624] | 58 | import org.eclipse.uml2.uml.Model;
|
---|
| 59 | import org.eclipse.uml2.uml.Operation;
|
---|
[1896] | 60 | import org.eclipse.uml2.uml.Package;
|
---|
[1908] | 61 | import org.eclipse.uml2.uml.Parameter;
|
---|
| 62 | import org.eclipse.uml2.uml.ParameterDirectionKind;
|
---|
[1752] | 63 | import org.eclipse.uml2.uml.Port;
|
---|
[1908] | 64 | import org.eclipse.uml2.uml.PrimitiveType;
|
---|
[1759] | 65 | import org.eclipse.uml2.uml.Profile;
|
---|
[1761] | 66 | import org.eclipse.uml2.uml.Property;
|
---|
[1604] | 67 | import org.eclipse.uml2.uml.Region;
|
---|
[1908] | 68 | import org.eclipse.uml2.uml.Slot;
|
---|
[1604] | 69 | import org.eclipse.uml2.uml.StateMachine;
|
---|
[1759] | 70 | import org.eclipse.uml2.uml.Stereotype;
|
---|
[1604] | 71 | import org.eclipse.uml2.uml.Transition;
|
---|
[1624] | 72 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
[1604] | 73 | import org.eclipse.uml2.uml.Vertex;
|
---|
| 74 |
|
---|
| 75 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 76 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
[1643] | 77 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
|
---|
[1604] | 78 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
[1763] | 79 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
[1898] | 80 | import de.ugoe.cs.util.console.Console;
|
---|
[1604] | 81 |
|
---|
| 82 | /**
|
---|
| 83 | * <p>
|
---|
[1624] | 84 | * Utilities for working with UML.
|
---|
[1604] | 85 | * </p>
|
---|
| 86 | *
|
---|
| 87 | * @author Steffen Herbold
|
---|
| 88 | */
|
---|
| 89 | public class UMLUtils {
|
---|
| 90 |
|
---|
[1900] | 91 | /**
|
---|
| 92 | * <p>
|
---|
| 93 | * Method for checking if the information in a usage journal can be mapped to the SUT model. In
|
---|
| 94 | * case this is not possible, the violations are reported.
|
---|
| 95 | * </p>
|
---|
| 96 | *
|
---|
| 97 | * @param sequences
|
---|
| 98 | * sequences of the usage journal
|
---|
| 99 | * @param model
|
---|
| 100 | * SUT model that is validated
|
---|
| 101 | * @param testContextName
|
---|
| 102 | * name of the test context to be used; if null, the first test context found is used
|
---|
| 103 | * @return number of violations
|
---|
| 104 | */
|
---|
| 105 | public static int validateModelWithLog(Collection<List<Event>> sequences,
|
---|
| 106 | Model model,
|
---|
| 107 | String testContextName)
|
---|
| 108 | {
|
---|
[1898] | 109 | final Profile utpProfile = model.getAppliedProfile("utp");
|
---|
| 110 | final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent");
|
---|
| 111 | final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT");
|
---|
| 112 | final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
|
---|
| 113 |
|
---|
[1900] | 114 | int violationCount = 0;
|
---|
[1898] | 115 | Component testContext = fetchTestContext(model, utpTestContext, testContextName);
|
---|
| 116 | if (testContext == null) {
|
---|
[1900] | 117 | violationCount++;
|
---|
| 118 | if (testContextName == null) {
|
---|
[1898] | 119 | Console.traceln(Level.SEVERE, "Could not find any TestContext in the model.");
|
---|
[1900] | 120 |
|
---|
[1898] | 121 | }
|
---|
[1900] | 122 | else {
|
---|
| 123 | Console.traceln(Level.SEVERE, "Could not find TestContext in the model: " +
|
---|
| 124 | testContextName);
|
---|
| 125 | }
|
---|
| 126 | Console
|
---|
| 127 | .traceln(Level.SEVERE,
|
---|
| 128 | "Hint: Check if you have applied the TestContext stereotype correctly in the model.");
|
---|
| 129 | Console.traceln(Level.SEVERE, "Aborting");
|
---|
| 130 | return violationCount;
|
---|
[1898] | 131 | }
|
---|
[1900] | 132 |
|
---|
[1898] | 133 | // Create list of unique methods calls
|
---|
| 134 | HashMap<String, Set<String>> calledMethods = new HashMap<>();
|
---|
[1900] | 135 | for (List<Event> sequence : sequences) {
|
---|
| 136 | for (Event event : sequence) {
|
---|
[1916] | 137 | String serviceName = getServiceNameFromEvent(event);
|
---|
| 138 | String calledMethod = getCalledMethodFromEvent(event);
|
---|
| 139 | if( serviceName!=null ) {
|
---|
| 140 | Set<String> curCalledMethods = calledMethods.get(serviceName);
|
---|
[1900] | 141 | if (curCalledMethods == null) {
|
---|
[1898] | 142 | curCalledMethods = new TreeSet<>();
|
---|
[1916] | 143 | calledMethods.put(serviceName, curCalledMethods);
|
---|
[1898] | 144 | }
|
---|
[1916] | 145 | curCalledMethods.add(calledMethod);
|
---|
[1898] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[1900] | 149 |
|
---|
| 150 | Console.traceln(Level.INFO,
|
---|
| 151 | "Found the following services and operations in the usage data: ");
|
---|
| 152 | for (Entry<String, Set<String>> entry : calledMethods.entrySet()) {
|
---|
[1908] | 153 | Console.traceln(Level.INFO, "\tService \"" + entry.getKey() + "\": ");
|
---|
| 154 | for(String method : entry.getValue()) {
|
---|
| 155 | Console.traceln(Level.INFO, "\t\t"+method);
|
---|
[1898] | 156 | }
|
---|
| 157 | }
|
---|
[1900] | 158 |
|
---|
[1898] | 159 | // fetch all SUTs and TestComponents
|
---|
| 160 | HashMap<String, Property> properties = new HashMap<>();
|
---|
| 161 | for (Property property : testContext.getAllAttributes()) {
|
---|
| 162 | if (property.getAppliedStereotypes().contains(utpSUT)) {
|
---|
[1900] | 163 | properties.put(property.getName(), property);
|
---|
[1898] | 164 | }
|
---|
| 165 | else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
|
---|
[1900] | 166 | properties.put(property.getName(), property);
|
---|
[1898] | 167 | }
|
---|
| 168 | }
|
---|
| 169 | Console.traceln(Level.INFO, "Found the following services in the TestConfiguration:");
|
---|
[1900] | 170 | for (Entry<String, Property> entry : properties.entrySet()) {
|
---|
[1898] | 171 | Console.traceln(Level.INFO, "\t" + entry.getKey());
|
---|
| 172 | }
|
---|
[1900] | 173 |
|
---|
| 174 | for (Entry<String, Set<String>> entry : calledMethods.entrySet()) {
|
---|
[1898] | 175 | String serviceName = entry.getKey();
|
---|
| 176 | Console.traceln(Level.INFO, "Checking service: " + serviceName);
|
---|
| 177 | Set<String> methodNames = entry.getValue();
|
---|
| 178 | Property property = properties.get(serviceName);
|
---|
[1900] | 179 | if (property == null) {
|
---|
| 180 | violationCount++;
|
---|
| 181 | Console.traceln(Level.SEVERE, "\tCould not find property for service: " +
|
---|
| 182 | serviceName);
|
---|
| 183 | Console
|
---|
| 184 | .traceln(Level.SEVERE,
|
---|
| 185 | "\tHint: Check service name map and/or model if the service is present and spelled correctly.");
|
---|
| 186 | Console
|
---|
| 187 | .traceln(Level.SEVERE,
|
---|
| 188 | "\tHint: Check if the SUT/TestComponent stereotype has been applied correctly in this TestContext.");
|
---|
| 189 | }
|
---|
| 190 | else {
|
---|
[1898] | 191 | Set<Interface> interfaces = getRealizedInterfacesFromProperty(property);
|
---|
[1900] | 192 | if (interfaces.isEmpty()) {
|
---|
| 193 | violationCount++;
|
---|
| 194 | Console
|
---|
| 195 | .traceln(Level.SEVERE,
|
---|
| 196 | "\tCould not find any interfaces implementing the property for service: " +
|
---|
| 197 | serviceName);
|
---|
| 198 | Console
|
---|
| 199 | .traceln(Level.SEVERE,
|
---|
| 200 | "\tHint: Check if the property correctly realizes the interfaces in the model.");
|
---|
| 201 | }
|
---|
| 202 | else {
|
---|
| 203 | Console.traceln(Level.INFO,
|
---|
| 204 | "\tFound the following realized interfaces for the service \"" +
|
---|
| 205 | serviceName + "\": ");
|
---|
[1908] | 206 | for( Interface intface : interfaces ) {
|
---|
| 207 | Console.traceln(Level.INFO, "\t" + intface.getName());
|
---|
| 208 | for( Operation operation : intface.getAllOperations() ) {
|
---|
| 209 | Console.traceln(Level.INFO, "\t\t" + operation.getName());
|
---|
[1898] | 210 | }
|
---|
| 211 | }
|
---|
[1900] | 212 | for (String methodName : methodNames) {
|
---|
| 213 | boolean methodFound = false;
|
---|
| 214 | for (Interface intface : interfaces) {
|
---|
[1898] | 215 | if (getOperationFromName(intface.getOperations(), methodName) != null) {
|
---|
| 216 | // interface found
|
---|
[1900] | 217 | Console.traceln(Level.INFO, "\tMethod " + methodName +
|
---|
| 218 | " found in interface " + intface.getName());
|
---|
[1898] | 219 | methodFound = true;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
[1900] | 222 | if (!methodFound) {
|
---|
| 223 | violationCount++;
|
---|
| 224 | Console.traceln(Level.SEVERE, "\tCould not find operation: " +
|
---|
| 225 | methodName);
|
---|
[1898] | 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
[1900] | 231 | return violationCount;
|
---|
[1898] | 232 | }
|
---|
[1900] | 233 |
|
---|
[1624] | 234 | /**
|
---|
| 235 | * <p>
|
---|
| 236 | * Creates a sequence of events with {@link UMLTransitionType} as event type from a given
|
---|
| 237 | * sequence of events with the {@link SOAPEventType}, by matching the sequences to a state
|
---|
| 238 | * machine.
|
---|
| 239 | * </p>
|
---|
| 240 | *
|
---|
| 241 | * @param sequence
|
---|
| 242 | * SOAP sequences
|
---|
| 243 | * @param stateMachine
|
---|
| 244 | * the state machine
|
---|
| 245 | * @return create UML sequences
|
---|
| 246 | */
|
---|
| 247 | public static List<Event> createUMLTransitionSequence(List<Event> sequence,
|
---|
| 248 | StateMachine stateMachine)
|
---|
| 249 | {
|
---|
| 250 | List<List<Transition>> matchingSequences =
|
---|
| 251 | determineMatchingTransitionSequences(sequence, stateMachine);
|
---|
[1604] | 252 |
|
---|
| 253 | if (matchingSequences.size() != 1) {
|
---|
| 254 | throw new RuntimeException("no unique match found; " + matchingSequences.size() +
|
---|
| 255 | " matches");
|
---|
| 256 | }
|
---|
| 257 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
| 258 | for (Transition transition : matchingSequences.get(0)) {
|
---|
| 259 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
| 260 | }
|
---|
| 261 | return umlEventSequence;
|
---|
| 262 | }
|
---|
[1624] | 263 |
|
---|
| 264 | /**
|
---|
| 265 | * <p>
|
---|
| 266 | * Uses a sequences of events with the {@link UMLTransitionType} to determine the transition
|
---|
| 267 | * probabilities for the state machine.
|
---|
| 268 | * </p>
|
---|
| 269 | *
|
---|
| 270 | * @param sequences
|
---|
| 271 | * UML sequences
|
---|
| 272 | * @param stateMachine
|
---|
| 273 | * state machine to be converted to a usage profile
|
---|
| 274 | */
|
---|
| 275 | public static void convertStateMachineToUsageProfile(Collection<List<Event>> sequences,
|
---|
| 276 | StateMachine stateMachine)
|
---|
| 277 | {
|
---|
[1604] | 278 | // create state->outgoings hashmap
|
---|
[1624] | 279 | Map<Vertex, Map<Transition, Integer>> stateMap = new HashMap<>();
|
---|
| 280 | for (Region region : stateMachine.getRegions()) {
|
---|
| 281 | for (Vertex state : region.getSubvertices()) {
|
---|
| 282 | stateMap.put(state, new HashMap<Transition, Integer>());
|
---|
[1604] | 283 | }
|
---|
| 284 | }
|
---|
[1624] | 285 |
|
---|
[1604] | 286 | // create counters for each transition
|
---|
[1624] | 287 | for (List<Event> sequence : sequences) {
|
---|
| 288 | for (Event event : sequence) {
|
---|
| 289 | if (event.getType() instanceof UMLTransitionType) {
|
---|
[1604] | 290 | Transition transition = ((UMLTransitionType) event.getType()).getTransition();
|
---|
[1624] | 291 | Map<Transition, Integer> transitionMap = stateMap.get(transition.getSource());
|
---|
[1604] | 292 | Integer value = transitionMap.get(transition);
|
---|
[1624] | 293 | if (value == null) {
|
---|
[1604] | 294 | value = 0;
|
---|
| 295 | }
|
---|
[1624] | 296 | transitionMap.put(transition, value + 1);
|
---|
[1604] | 297 | }
|
---|
[1624] | 298 | else {
|
---|
| 299 | throw new RuntimeException(
|
---|
| 300 | "Wrong event type. Only UMLTransitionType supported but was: " +
|
---|
| 301 | event.getType().getClass().getName());
|
---|
| 302 | }
|
---|
[1604] | 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | // calculate probabilities
|
---|
[1624] | 307 | for (Region region : stateMachine.getRegions()) {
|
---|
| 308 | for (Vertex state : region.getSubvertices()) {
|
---|
| 309 | Map<Transition, Integer> transitionMap = stateMap.get(state);
|
---|
[1604] | 310 | int totalCount = 0;
|
---|
[1624] | 311 | for (Entry<Transition, Integer> entry : transitionMap.entrySet()) {
|
---|
[1604] | 312 | totalCount += entry.getValue();
|
---|
| 313 | }
|
---|
[1624] | 314 | if (totalCount != 0) {
|
---|
| 315 | for (Transition transition : state.getOutgoings()) {
|
---|
[1604] | 316 | double prob = 0.0d;
|
---|
[1624] | 317 | if (transitionMap.containsKey(transition)) {
|
---|
| 318 | prob = ((double) transitionMap.get(transition)) / totalCount;
|
---|
[1604] | 319 | }
|
---|
| 320 | Comment comment = transition.createOwnedComment();
|
---|
[1624] | 321 | comment.setBody("" + prob);
|
---|
[1604] | 322 | }
|
---|
[1624] | 323 | }
|
---|
| 324 | else {
|
---|
[1604] | 325 | // system has never been in this state, all transitions equally likely
|
---|
| 326 | int numOutgoings = state.getOutgoings().size();
|
---|
[1624] | 327 | for (Transition transition : state.getOutgoings()) {
|
---|
[1604] | 328 | Comment comment = transition.createOwnedComment();
|
---|
[1624] | 329 | comment.setBody("" + (1.0d / numOutgoings));
|
---|
[1604] | 330 | }
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
[1624] | 335 |
|
---|
| 336 | /**
|
---|
| 337 | * <p>
|
---|
| 338 | * Determines all matching {@link Transition} sequences in a state machine for a given sequence
|
---|
| 339 | * of SOAP events.
|
---|
| 340 | * </p>
|
---|
| 341 | *
|
---|
| 342 | * @param sequence
|
---|
| 343 | * SOAP sequence
|
---|
| 344 | * @param stateMachine
|
---|
| 345 | * the state machine
|
---|
| 346 | * @return all matching {@link Transition} sequences
|
---|
| 347 | */
|
---|
| 348 | public static List<List<Transition>> determineMatchingTransitionSequences(List<Event> sequence,
|
---|
| 349 | StateMachine stateMachine)
|
---|
| 350 | {
|
---|
[1604] | 351 | EList<Region> regions = stateMachine.getRegions();
|
---|
| 352 | EList<Vertex> states = null;
|
---|
| 353 | for (Region region : regions) {
|
---|
| 354 | if (states == null) {
|
---|
| 355 | states = region.getSubvertices();
|
---|
| 356 | }
|
---|
| 357 | else {
|
---|
| 358 | states.addAll(region.getSubvertices());
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | List<Transition> allTransitions = new LinkedList<>();
|
---|
| 362 | for (Vertex state : states) {
|
---|
| 363 | allTransitions.addAll(state.getOutgoings());
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | List<List<Transition>> matchingSequences = null;
|
---|
| 367 | List<Transition> currentTransitions = null;
|
---|
| 368 |
|
---|
| 369 | // first, we try to find a single unique transition that we can match using the method name
|
---|
| 370 | for (Iterator<Event> eventIterator = sequence.iterator(); eventIterator.hasNext();) {
|
---|
| 371 | Event event = eventIterator.next();
|
---|
| 372 | if (event.getType() instanceof SOAPEventType) {
|
---|
| 373 | SOAPEventType eventType = (SOAPEventType) event.getType();
|
---|
| 374 | if (matchingSequences == null) {
|
---|
| 375 | matchingSequences = new LinkedList<>();
|
---|
| 376 | List<Transition> initialMatches = matchTransitions(allTransitions, eventType);
|
---|
| 377 | for (Transition transition : initialMatches) {
|
---|
| 378 | List<Transition> candidate = new LinkedList<>();
|
---|
| 379 | candidate.add(transition);
|
---|
| 380 | matchingSequences.add(candidate);
|
---|
| 381 | }
|
---|
| 382 | currentTransitions = initialMatches;
|
---|
| 383 | }
|
---|
| 384 | else {
|
---|
| 385 | List<List<Transition>> nextMatchingSequences = new LinkedList<>();
|
---|
| 386 | List<Transition> nextCurrentTransitions = new LinkedList<>();
|
---|
| 387 | Iterator<Transition> currentTransitionIterator = currentTransitions.iterator();
|
---|
| 388 | Iterator<List<Transition>> currentMatchingSequencesIterator =
|
---|
| 389 | matchingSequences.iterator();
|
---|
| 390 | while (currentTransitionIterator.hasNext()) {
|
---|
| 391 | Transition currentTransition = currentTransitionIterator.next();
|
---|
| 392 | List<Transition> currentMatch = currentMatchingSequencesIterator.next();
|
---|
| 393 |
|
---|
| 394 | List<Transition> matches =
|
---|
| 395 | matchTransitions(currentTransition.getTarget().getOutgoings(),
|
---|
| 396 | eventType);
|
---|
| 397 | if (matches.isEmpty()) {
|
---|
| 398 | throw new RuntimeException("no matches found");
|
---|
| 399 | }
|
---|
| 400 | for (Transition matchingTransition : matches) {
|
---|
| 401 | List<Transition> candidate = new LinkedList<>(currentMatch);
|
---|
| 402 | candidate.add(matchingTransition);
|
---|
| 403 | nextMatchingSequences.add(candidate);
|
---|
| 404 | nextCurrentTransitions.add(matchingTransition);
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 | matchingSequences = nextMatchingSequences;
|
---|
| 408 | currentTransitions = nextCurrentTransitions;
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 | else {
|
---|
[1624] | 412 | throw new RuntimeException(
|
---|
| 413 | "Wrong event type. Only UMLTransitionType supported but was: " +
|
---|
| 414 | event.getType().getClass().getName());
|
---|
[1604] | 415 | }
|
---|
| 416 | }
|
---|
| 417 | return matchingSequences;
|
---|
| 418 | }
|
---|
[1624] | 419 |
|
---|
| 420 | /**
|
---|
| 421 | * <p>
|
---|
[1633] | 422 | * Extends a given model with an interaction that represents an observed sequence.
|
---|
[1624] | 423 | * </p>
|
---|
[1633] | 424 | *
|
---|
| 425 | * @param sequence
|
---|
| 426 | * sequence that is added as sequence diagram
|
---|
| 427 | * @param model
|
---|
| 428 | * UML model to which the interaction is added
|
---|
| 429 | * @param interactionName
|
---|
| 430 | * name of the interaction
|
---|
[1896] | 431 | * @param testContextName
|
---|
[1897] | 432 | * Name of the test context that should be used. If this value is null, the first
|
---|
| 433 | * test context found is used.
|
---|
[1624] | 434 | */
|
---|
[1633] | 435 | public static void createInteractionFromEventSequence(List<Event> sequence,
|
---|
| 436 | Model model,
|
---|
[1896] | 437 | String interactionName,
|
---|
| 438 | String testContextName)
|
---|
[1604] | 439 | {
|
---|
[1759] | 440 | final Profile utpProfile = model.getAppliedProfile("utp");
|
---|
| 441 | final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase");
|
---|
[1763] | 442 | final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent");
|
---|
| 443 | final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT");
|
---|
[1896] | 444 | final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
|
---|
| 445 |
|
---|
| 446 | Component testContext = fetchTestContext(model, utpTestContext, testContextName);
|
---|
[1897] | 447 | if (testContext == null) {
|
---|
[1896] | 448 | throw new RuntimeException("Could not find any test context in the model");
|
---|
| 449 | }
|
---|
[1897] | 450 |
|
---|
[1759] | 451 | Operation operation = testContext.createOwnedOperation(interactionName, null, null);
|
---|
| 452 | operation.applyStereotype(utpTestCase);
|
---|
| 453 |
|
---|
[1624] | 454 | Interaction interaction =
|
---|
[1759] | 455 | (Interaction) testContext.createPackagedElement(interactionName + "_Impl",
|
---|
| 456 | UMLPackage.Literals.INTERACTION);
|
---|
[1761] | 457 | operation.getMethods().add(interaction);
|
---|
[1835] | 458 |
|
---|
[1763] | 459 | // create lifelines
|
---|
| 460 | Lifeline userLifeline = null;
|
---|
[1900] | 461 |
|
---|
[1763] | 462 | for (Property property : testContext.getAllAttributes()) {
|
---|
| 463 | if (property.getAppliedStereotypes().contains(utpSUT)) {
|
---|
[1835] | 464 | String serviceName = property.getName();
|
---|
[1763] | 465 | Lifeline targetLifeline = interaction.createLifeline(serviceName);
|
---|
| 466 | targetLifeline.setRepresents(property);
|
---|
[1761] | 467 | }
|
---|
[1763] | 468 | else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
|
---|
[1900] | 469 | if (userLifeline != null) {
|
---|
| 470 | throw new RuntimeException(
|
---|
| 471 | "TestContext must only have one TestComponent for the application of usage-based testing.");
|
---|
[1897] | 472 | }
|
---|
[1763] | 473 | userLifeline = interaction.createLifeline(property.getName());
|
---|
| 474 | userLifeline.setRepresents(property);
|
---|
[1897] | 475 | }
|
---|
[1761] | 476 | }
|
---|
[1900] | 477 | if (userLifeline == null) {
|
---|
[1897] | 478 | throw new RuntimeException("No TestComponent found, could not create user lifeline.");
|
---|
| 479 | }
|
---|
[1900] | 480 | if (interaction.getLifelines().size() < 2) {
|
---|
[1897] | 481 | throw new RuntimeException("Fewer than two lifelines created. No SUT found.");
|
---|
| 482 | }
|
---|
[1900] | 483 |
|
---|
[1624] | 484 | int i = 0;
|
---|
| 485 | for (Event event : sequence) {
|
---|
[1633] | 486 | if (!(event.equals(Event.STARTEVENT) || event.equals(Event.ENDEVENT))) {
|
---|
[1643] | 487 | String serviceName = getServiceNameFromEvent(event);
|
---|
| 488 | String methodName = getCalledMethodFromEvent(event);
|
---|
[1913] | 489 | String clientName = getClientNameFromEvent(event);
|
---|
[1835] | 490 | // determine lifelines
|
---|
| 491 | Lifeline msgTargetLifeline;
|
---|
| 492 | Lifeline msgSourceLifeline;
|
---|
[1913] | 493 |
|
---|
| 494 | msgSourceLifeline = interaction.getLifeline(clientName);
|
---|
| 495 | msgTargetLifeline = interaction.getLifeline(serviceName);
|
---|
| 496 |
|
---|
[1900] | 497 | if (msgSourceLifeline == null) {
|
---|
| 498 | throw new RuntimeException(
|
---|
[1913] | 499 | "Error creating message: could not determine source lifeline for component: " + clientName);
|
---|
[1752] | 500 | }
|
---|
[1900] | 501 | if (msgTargetLifeline == null) {
|
---|
| 502 | throw new RuntimeException(
|
---|
[1913] | 503 | "Error creating message: could not determine target lifeline for component: " + serviceName);
|
---|
[1897] | 504 | }
|
---|
| 505 | // determine correct target interface
|
---|
[1898] | 506 | Set<Interface> targetInterfaces =
|
---|
[1897] | 507 | getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents());
|
---|
| 508 | if (targetInterfaces.isEmpty()) {
|
---|
| 509 | throw new RuntimeException("no interface associated with the property " +
|
---|
| 510 | msgTargetLifeline.getRepresents().getName());
|
---|
| 511 | }
|
---|
[1896] | 512 | Interface targetInterface = null;
|
---|
[1897] | 513 | for (Interface intface : targetInterfaces) {
|
---|
[1896] | 514 | if (getOperationFromName(intface.getOperations(), methodName) != null) {
|
---|
| 515 | // interface found
|
---|
| 516 | targetInterface = intface;
|
---|
| 517 | break;
|
---|
| 518 | }
|
---|
| 519 | }
|
---|
[1897] | 520 | if (targetInterface == null) {
|
---|
[1896] | 521 | StringBuilder errStrBuilder = new StringBuilder();
|
---|
[1900] | 522 | errStrBuilder
|
---|
| 523 | .append("Error creating message: operation not found in the implementing interfaces (");
|
---|
[1896] | 524 | Iterator<Interface> iter = targetInterfaces.iterator();
|
---|
[1897] | 525 | while (iter.hasNext()) {
|
---|
[1896] | 526 | String interfaceName = iter.next().getName();
|
---|
| 527 | errStrBuilder.append(interfaceName);
|
---|
[1897] | 528 | if (iter.hasNext()) {
|
---|
[1896] | 529 | errStrBuilder.append(",");
|
---|
[1897] | 530 | }
|
---|
| 531 | else {
|
---|
[1896] | 532 | errStrBuilder.append("): " + methodName);
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | throw new RuntimeException(errStrBuilder.toString());
|
---|
| 536 | }
|
---|
[1897] | 537 |
|
---|
| 538 | Operation calledOperation =
|
---|
| 539 | getOperationFromName(targetInterface.getOperations(), methodName);
|
---|
| 540 |
|
---|
| 541 | // setup for both SYNCH and ASYNCH calls
|
---|
| 542 | MessageOccurrenceSpecification callSendFragment =
|
---|
| 543 | (MessageOccurrenceSpecification) interaction
|
---|
| 544 | .createFragment(i + ":" + methodName + "_callSendFragment",
|
---|
| 545 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
| 546 | MessageOccurrenceSpecification callRecvFragment =
|
---|
| 547 | (MessageOccurrenceSpecification) interaction
|
---|
| 548 | .createFragment(i + ":" + methodName + "_callRecvFragment",
|
---|
| 549 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
| 550 |
|
---|
| 551 | callSendFragment.setCovered(msgSourceLifeline);
|
---|
| 552 | callRecvFragment.setCovered(msgTargetLifeline);
|
---|
| 553 |
|
---|
| 554 | // create call
|
---|
| 555 | Message callMessage = interaction.createMessage(methodName);
|
---|
[1908] | 556 | callMessage.setSignature(calledOperation);
|
---|
| 557 | setMessageParameters(callMessage, calledOperation, event);
|
---|
[1897] | 558 | callMessage.setConnector(inferConnector(msgSourceLifeline, msgTargetLifeline));
|
---|
| 559 | callMessage.setSendEvent(callSendFragment);
|
---|
| 560 | callMessage.setReceiveEvent(callRecvFragment);
|
---|
| 561 | callSendFragment.setMessage(callMessage);
|
---|
| 562 | callRecvFragment.setMessage(callMessage);
|
---|
| 563 |
|
---|
[1896] | 564 | boolean asynch = false;
|
---|
[1900] | 565 | if (calledOperation.getConcurrency() == CallConcurrencyKind.CONCURRENT_LITERAL) {
|
---|
[1898] | 566 | asynch = true;
|
---|
| 567 | }
|
---|
[1897] | 568 | if (asynch) {
|
---|
| 569 | // Create ASYNCH call
|
---|
| 570 | callMessage.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
|
---|
| 571 | }
|
---|
| 572 | else {
|
---|
[1896] | 573 | // SYNCH call
|
---|
[1897] | 574 | callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
|
---|
| 575 |
|
---|
| 576 | // setup reply and behavior execution specifications
|
---|
[1896] | 577 | MessageOccurrenceSpecification replySendFragment =
|
---|
| 578 | (MessageOccurrenceSpecification) interaction
|
---|
| 579 | .createFragment(i + ":" + methodName + "_replySendFragment",
|
---|
| 580 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
| 581 | MessageOccurrenceSpecification replyRecvFragment =
|
---|
| 582 | (MessageOccurrenceSpecification) interaction
|
---|
| 583 | .createFragment(i + ":" + methodName + "_replyRecvFragment",
|
---|
| 584 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
[1897] | 585 |
|
---|
| 586 | replySendFragment.setCovered(msgTargetLifeline);
|
---|
| 587 | replyRecvFragment.setCovered(msgSourceLifeline);
|
---|
| 588 |
|
---|
[1900] | 589 | /*
|
---|
| 590 | * BehaviorExecutionSpecification sourceBehaviorExecutionSpecification =
|
---|
| 591 | * (BehaviorExecutionSpecification) interaction .createFragment(":" + methodName
|
---|
| 592 | * + "_sourceBhvExecSpec",
|
---|
| 593 | * UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
|
---|
| 594 | * BehaviorExecutionSpecification targetBehaviorExecutionSpecification =
|
---|
| 595 | * (BehaviorExecutionSpecification) interaction .createFragment(":" + methodName
|
---|
| 596 | * + "_targetBhvExecSpec",
|
---|
| 597 | * UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
|
---|
| 598 | *
|
---|
| 599 | * sourceBehaviorExecutionSpecification.setStart(callSendFragment);
|
---|
| 600 | * sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment);
|
---|
| 601 | * targetBehaviorExecutionSpecification.setStart(callRecvFragment);
|
---|
| 602 | * targetBehaviorExecutionSpecification.setFinish(replySendFragment);
|
---|
| 603 | */
|
---|
[1897] | 604 |
|
---|
[1896] | 605 | // create reply
|
---|
| 606 | Message replyMessage = interaction.createMessage(methodName + "_reply");
|
---|
| 607 | replyMessage.setMessageSort(MessageSort.REPLY_LITERAL);
|
---|
| 608 | replyMessage.setSignature(calledOperation);
|
---|
| 609 | replyMessage.setSendEvent(replySendFragment);
|
---|
| 610 | replyMessage.setReceiveEvent(replyRecvFragment);
|
---|
| 611 | replySendFragment.setMessage(replyMessage);
|
---|
| 612 | replyRecvFragment.setMessage(replyMessage);
|
---|
[1753] | 613 | }
|
---|
[1643] | 614 |
|
---|
[1633] | 615 | i++;
|
---|
[1624] | 616 | }
|
---|
| 617 | }
|
---|
| 618 | }
|
---|
| 619 |
|
---|
[1908] | 620 |
|
---|
| 621 |
|
---|
[1624] | 622 | /**
|
---|
| 623 | * <p>
|
---|
[1643] | 624 | * Calculates the usage score of an interaction as the logsum of the event probabilities
|
---|
| 625 | * multiplied with the length of the interaction.
|
---|
| 626 | * </p>
|
---|
| 627 | *
|
---|
| 628 | * @param interaction
|
---|
| 629 | * interaction for which the score is calculated
|
---|
| 630 | * @param usageProfile
|
---|
| 631 | * usage profile used for the calculation
|
---|
| 632 | * @return calculated usage score
|
---|
| 633 | */
|
---|
[1763] | 634 | public static double calculateUsageScore(Interaction interaction,
|
---|
| 635 | IStochasticProcess usageProfile)
|
---|
| 636 | {
|
---|
[1643] | 637 | double usageScore = 0.0d;
|
---|
| 638 |
|
---|
| 639 | EList<InteractionFragment> interactionFragments = interaction.getFragments();
|
---|
| 640 | List<Event> eventSequence = new LinkedList<>();
|
---|
| 641 | eventSequence.add(Event.STARTEVENT);
|
---|
| 642 | for (InteractionFragment interactionFragment : interactionFragments) {
|
---|
| 643 | if (interactionFragment.getName() != null &&
|
---|
[1897] | 644 | interactionFragment.getName().endsWith("_recvFragment")) // TODO must be more
|
---|
| 645 | // generic
|
---|
[1643] | 646 | {
|
---|
| 647 | String serviceName =
|
---|
[1763] | 648 | interactionFragment.getCovereds().get(0).getRepresents().getName().split("_")[0];
|
---|
[1643] | 649 | String methodName = "UNKNOWN";
|
---|
| 650 | if (interactionFragment instanceof MessageOccurrenceSpecification) {
|
---|
| 651 | methodName =
|
---|
| 652 | ((MessageOccurrenceSpecification) interactionFragment).getMessage()
|
---|
| 653 | .getName();
|
---|
| 654 | }
|
---|
[1913] | 655 | eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName,""))); // TODO add client name
|
---|
[1643] | 656 | }
|
---|
| 657 | }
|
---|
[1763] | 658 | eventSequence.add(Event.ENDEVENT);
|
---|
[1643] | 659 | double prob = usageProfile.getLogSum(eventSequence);
|
---|
[1763] | 660 | usageScore = eventSequence.size() * prob;
|
---|
[1643] | 661 |
|
---|
| 662 | return usageScore;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /**
|
---|
| 666 | * <p>
|
---|
[1763] | 667 | * Extends the given model with an activity for usage-based scheduling of the test cases.
|
---|
| 668 | * </p>
|
---|
| 669 | *
|
---|
| 670 | * @param model
|
---|
| 671 | * model to be extended
|
---|
| 672 | * @param usageProfile
|
---|
[1897] | 673 | * usage profile used as foundation
|
---|
[1763] | 674 | */
|
---|
[1897] | 675 | public static void createScheduling(Model model,
|
---|
| 676 | IStochasticProcess usageProfile,
|
---|
| 677 | String testContextName)
|
---|
| 678 | {
|
---|
[1763] | 679 |
|
---|
| 680 | final Profile utpProfile = model.getAppliedProfile("utp");
|
---|
| 681 | final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase");
|
---|
[1896] | 682 | final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
|
---|
[1763] | 683 |
|
---|
[1896] | 684 | Component testContext = fetchTestContext(model, utpTestContext, testContextName);
|
---|
[1897] | 685 | if (testContext == null) {
|
---|
[1896] | 686 | throw new RuntimeException("Could not find any test context in the model");
|
---|
| 687 | }
|
---|
[1763] | 688 |
|
---|
| 689 | Map<Operation, Double> usageScoreMapUnsorted = new HashMap<>();
|
---|
| 690 |
|
---|
| 691 | // first, we determine all test cases and calculate their usage scores
|
---|
| 692 | for (Operation operation : testContext.getAllOperations()) {
|
---|
| 693 | if (operation.getAppliedStereotypes().contains(utpTestCase)) {
|
---|
| 694 | Interaction interaction = (Interaction) operation.getMethods().get(0);
|
---|
| 695 | usageScoreMapUnsorted
|
---|
| 696 | .put(operation, calculateUsageScore(interaction, usageProfile));
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
| 699 | Map<Operation, Double> usageScoreMapSorted = sortByValue(usageScoreMapUnsorted);
|
---|
| 700 |
|
---|
| 701 | // now we create the scheduling
|
---|
| 702 | Activity schedulingActivity =
|
---|
| 703 | (Activity) testContext.createOwnedBehavior("UsageBasedScheduling",
|
---|
| 704 | UMLPackage.Literals.ACTIVITY);
|
---|
| 705 | testContext.setClassifierBehavior(schedulingActivity);
|
---|
| 706 |
|
---|
| 707 | ActivityNode startNode =
|
---|
| 708 | schedulingActivity.createOwnedNode("final", UMLPackage.Literals.INITIAL_NODE);
|
---|
| 709 | ActivityNode finalNode =
|
---|
| 710 | schedulingActivity.createOwnedNode("final", UMLPackage.Literals.ACTIVITY_FINAL_NODE);
|
---|
| 711 |
|
---|
| 712 | ActivityNode currentOperationNode = startNode;
|
---|
| 713 |
|
---|
| 714 | for (Entry<Operation, Double> entry : usageScoreMapSorted.entrySet()) {
|
---|
| 715 | Operation operation = entry.getKey();
|
---|
| 716 | CallOperationAction nextOperationNode =
|
---|
| 717 | (CallOperationAction) schedulingActivity
|
---|
| 718 | .createOwnedNode(operation.getName(), UMLPackage.Literals.CALL_OPERATION_ACTION);
|
---|
| 719 | nextOperationNode.setOperation(operation);
|
---|
| 720 |
|
---|
| 721 | ActivityEdge edge =
|
---|
| 722 | schedulingActivity.createEdge(currentOperationNode.getName() + "_to_" +
|
---|
| 723 | nextOperationNode.getName(), UMLPackage.Literals.CONTROL_FLOW);
|
---|
| 724 | edge.setSource(currentOperationNode);
|
---|
| 725 | edge.setTarget(nextOperationNode);
|
---|
| 726 |
|
---|
| 727 | currentOperationNode = nextOperationNode;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | ActivityEdge edge =
|
---|
| 731 | schedulingActivity
|
---|
| 732 | .createEdge(currentOperationNode.getName() + "_to_" + finalNode.getName(),
|
---|
| 733 | UMLPackage.Literals.CONTROL_FLOW);
|
---|
| 734 | edge.setSource(currentOperationNode);
|
---|
| 735 | edge.setTarget(finalNode);
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 | /**
|
---|
| 739 | * From
|
---|
| 740 | * http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java
|
---|
| 741 | * and adapted to do an inverse sorting
|
---|
| 742 | */
|
---|
| 743 | public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
|
---|
| 744 | List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
|
---|
| 745 | Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
|
---|
| 746 | @Override
|
---|
| 747 | public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
|
---|
| 748 | return -1 * (o1.getValue()).compareTo(o2.getValue());
|
---|
| 749 | }
|
---|
| 750 | });
|
---|
| 751 |
|
---|
| 752 | Map<K, V> result = new LinkedHashMap<>();
|
---|
| 753 | for (Map.Entry<K, V> entry : list) {
|
---|
| 754 | result.put(entry.getKey(), entry.getValue());
|
---|
| 755 | }
|
---|
| 756 | return result;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | /**
|
---|
| 760 | * <p>
|
---|
[1643] | 761 | * Helper function to get the name of a service from a SOAP event.
|
---|
| 762 | * </p>
|
---|
| 763 | *
|
---|
| 764 | * @param event
|
---|
| 765 | * event for which the service name is retrieved
|
---|
| 766 | * @return service name
|
---|
| 767 | */
|
---|
[1835] | 768 | protected static String getServiceNameFromEvent(Event event) {
|
---|
[1643] | 769 | if (event.getType() instanceof SOAPEventType) {
|
---|
| 770 | return ((SOAPEventType) event.getType()).getServiceName();
|
---|
| 771 | }
|
---|
| 772 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
| 773 | return ((SimpleSOAPEventType) event.getType()).getServiceName();
|
---|
| 774 | }
|
---|
| 775 | else {
|
---|
| 776 | throw new RuntimeException(
|
---|
| 777 | "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +
|
---|
| 778 | event.getType().getClass().getName());
|
---|
| 779 | }
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | /**
|
---|
| 783 | *
|
---|
| 784 | * <p>
|
---|
| 785 | * Helper function to get the called method from a SOAP event
|
---|
| 786 | * </p>
|
---|
| 787 | *
|
---|
| 788 | * @param event
|
---|
| 789 | * event for which the called method is retrieved
|
---|
| 790 | * @return called method
|
---|
| 791 | */
|
---|
| 792 | private static String getCalledMethodFromEvent(Event event) {
|
---|
| 793 | if (event.getType() instanceof SOAPEventType) {
|
---|
| 794 | return ((SOAPEventType) event.getType()).getCalledMethod();
|
---|
| 795 | }
|
---|
| 796 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
| 797 | return ((SimpleSOAPEventType) event.getType()).getCalledMethod();
|
---|
| 798 | }
|
---|
| 799 | else {
|
---|
| 800 | throw new RuntimeException(
|
---|
| 801 | "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +
|
---|
| 802 | event.getType().getClass().getName());
|
---|
| 803 | }
|
---|
| 804 | }
|
---|
[1913] | 805 |
|
---|
| 806 | /**
|
---|
| 807 | * <p>
|
---|
| 808 | * Helper function to get the name of a client from a SOAP event.
|
---|
| 809 | * </p>
|
---|
| 810 | *
|
---|
| 811 | * @param event
|
---|
| 812 | * event for which the client name is retrieved
|
---|
| 813 | * @return service name
|
---|
| 814 | */
|
---|
| 815 | protected static String getClientNameFromEvent(Event event) {
|
---|
| 816 | if (event.getType() instanceof SOAPEventType) {
|
---|
| 817 | return ((SOAPEventType) event.getType()).getClientName();
|
---|
| 818 | }
|
---|
| 819 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
| 820 | return ((SimpleSOAPEventType) event.getType()).getClientName();
|
---|
| 821 | }
|
---|
| 822 | else {
|
---|
| 823 | throw new RuntimeException(
|
---|
| 824 | "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +
|
---|
| 825 | event.getType().getClass().getName());
|
---|
| 826 | }
|
---|
| 827 | }
|
---|
[1643] | 828 |
|
---|
| 829 | /**
|
---|
| 830 | * <p>
|
---|
[1624] | 831 | * Fetches an operation using only its name from a list of operations. Returns the first match
|
---|
| 832 | * found or null if no match is found.
|
---|
| 833 | * </p>
|
---|
| 834 | *
|
---|
| 835 | * @param operations
|
---|
| 836 | * list of operations
|
---|
| 837 | * @param name
|
---|
| 838 | * name of the operation
|
---|
| 839 | * @return first matching operation; null if no match is found
|
---|
| 840 | */
|
---|
| 841 | private static Operation getOperationFromName(EList<Operation> operations, String name) {
|
---|
| 842 | if (name == null) {
|
---|
| 843 | throw new IllegalArgumentException("name of the operation must not be null");
|
---|
| 844 | }
|
---|
| 845 | if (operations != null) {
|
---|
| 846 | for (Operation operation : operations) {
|
---|
| 847 | if (operation.getName() != null && operation.getName().equals(name)) {
|
---|
| 848 | return operation;
|
---|
| 849 | }
|
---|
| 850 | }
|
---|
| 851 | }
|
---|
| 852 | return null;
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | /**
|
---|
| 856 | * <p>
|
---|
| 857 | * Determines which transitions match a given {@link SOAPEventType}.
|
---|
| 858 | * </p>
|
---|
| 859 | *
|
---|
| 860 | * @param transitions
|
---|
| 861 | * the transitions
|
---|
| 862 | * @param eventType
|
---|
| 863 | * the SOAP event
|
---|
| 864 | * @return matching transitions
|
---|
| 865 | */
|
---|
| 866 | private static List<Transition> matchTransitions(List<Transition> transitions,
|
---|
| 867 | SOAPEventType eventType)
|
---|
| 868 | {
|
---|
[1604] | 869 | List<Transition> matching = new LinkedList<>();
|
---|
| 870 | for (Transition transition : transitions) {
|
---|
| 871 | // String serviceName = transition.getName().split("\\.")[0]; // TODO service name check
|
---|
| 872 | String methodName = transition.getName().split("\\.")[1];
|
---|
| 873 | if (methodName.equals(eventType.getCalledMethod())) {
|
---|
| 874 | matching.add(transition);
|
---|
| 875 | }
|
---|
| 876 | }
|
---|
| 877 | return matching;
|
---|
| 878 | }
|
---|
[1897] | 879 |
|
---|
[1898] | 880 | private static Set<Interface> getRealizedInterfacesFromProperty(Property property) {
|
---|
[1763] | 881 | return getRealizedInterfaceFromComponent((Component) property.getType());
|
---|
| 882 | }
|
---|
[1897] | 883 |
|
---|
[1898] | 884 | private static Set<Interface> getRealizedInterfaceFromComponent(Component comp) {
|
---|
| 885 | Set<Interface> interfaces = new HashSet<>();
|
---|
[1897] | 886 | // Interface myInterface = null;
|
---|
| 887 | for (Property property : comp.getAttributes()) {
|
---|
| 888 | if (property instanceof Port) {
|
---|
[1835] | 889 | Port port = (Port) property;
|
---|
[1897] | 890 | if (!port.isConjugated()) {
|
---|
[1900] | 891 | interfaces.addAll(port.getProvideds());
|
---|
[1835] | 892 | }
|
---|
| 893 | }
|
---|
| 894 | }
|
---|
[1896] | 895 | return interfaces;
|
---|
[1763] | 896 | }
|
---|
[1897] | 897 |
|
---|
| 898 | private static Component fetchTestContext(Package pkg,
|
---|
| 899 | Stereotype utpTestContext,
|
---|
| 900 | String testContextName)
|
---|
| 901 | {
|
---|
[1896] | 902 | List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext);
|
---|
[1897] | 903 | if (testContexts.isEmpty()) {
|
---|
[1896] | 904 | return null;
|
---|
| 905 | }
|
---|
[1897] | 906 | if (testContextName != null) {
|
---|
| 907 | for (Component testContext : testContexts) {
|
---|
| 908 | if (testContextName.equals(testContext.getName())) {
|
---|
[1896] | 909 | return testContext;
|
---|
| 910 | }
|
---|
[1835] | 911 | }
|
---|
[1896] | 912 | return null;
|
---|
[1897] | 913 | }
|
---|
| 914 | else {
|
---|
[1896] | 915 | return testContexts.get(0);
|
---|
[1835] | 916 | }
|
---|
| 917 | }
|
---|
[1897] | 918 |
|
---|
| 919 | private static List<Component> fetchTestContextRecursively(Package pkg,
|
---|
| 920 | Stereotype utpTestContext)
|
---|
| 921 | {
|
---|
[1896] | 922 | List<Component> testContexts = new LinkedList<>();
|
---|
[1897] | 923 | for (Element element : pkg.getOwnedElements()) {
|
---|
| 924 | if (element instanceof Package) {
|
---|
[1896] | 925 | testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext));
|
---|
| 926 | }
|
---|
[1897] | 927 | if (element instanceof Component &&
|
---|
| 928 | element.getAppliedStereotypes().contains(utpTestContext))
|
---|
| 929 | {
|
---|
[1896] | 930 | testContexts.add((Component) element);
|
---|
| 931 | }
|
---|
| 932 | }
|
---|
| 933 | return testContexts;
|
---|
| 934 | }
|
---|
[1897] | 935 |
|
---|
| 936 | /**
|
---|
| 937 | * <p>
|
---|
[1900] | 938 | * Infers connector between two lifelines. TODO: currently assumes only one connector between
|
---|
| 939 | * two lifelines possible. I need to make sure this assumption is valid.
|
---|
[1897] | 940 | * </p>
|
---|
| 941 | *
|
---|
| 942 | * @param userAttributes
|
---|
| 943 | * @param targetAttributes
|
---|
| 944 | */
|
---|
| 945 | private static Connector inferConnector(Lifeline msgSourceLifeline, Lifeline msgTargetLifeline)
|
---|
| 946 | {
|
---|
| 947 | EList<Property> userAttributes =
|
---|
| 948 | ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
|
---|
| 949 | EList<Property> targetAttributes =
|
---|
| 950 | ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
|
---|
| 951 | for (Property userAttribute : userAttributes) {
|
---|
| 952 | if (userAttribute instanceof Port) {
|
---|
| 953 | EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
|
---|
| 954 | for (ConnectorEnd userEnd : userEnds) {
|
---|
| 955 | Connector userConnector = (Connector) userEnd.eContainer();
|
---|
| 956 | for (Property targetAttribute : targetAttributes) {
|
---|
| 957 | if (targetAttribute instanceof Port) {
|
---|
| 958 | EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
|
---|
| 959 | for (ConnectorEnd targetEnd : targetEnds) {
|
---|
| 960 | Connector targetConnector = (Connector) targetEnd.eContainer();
|
---|
| 961 | if (targetConnector == userConnector) {
|
---|
| 962 | return targetConnector;
|
---|
| 963 | }
|
---|
| 964 | }
|
---|
| 965 | }
|
---|
| 966 | }
|
---|
| 967 | }
|
---|
| 968 | }
|
---|
| 969 | }
|
---|
| 970 | return null;
|
---|
| 971 | }
|
---|
[1908] | 972 |
|
---|
| 973 | private static void setMessageParameters(Message callMessage, Operation calledOperation, Event event) {
|
---|
| 974 | // Set parameters of operation
|
---|
| 975 | for( Parameter param : calledOperation.getOwnedParameters() ) {
|
---|
| 976 | Expression argument = (Expression) callMessage.createArgument(param.getName(), param.getType(), UMLPackage.Literals.EXPRESSION);
|
---|
| 977 |
|
---|
| 978 | if( param.getDirection()==ParameterDirectionKind.IN_LITERAL || param.getDirection()==ParameterDirectionKind.INOUT_LITERAL) {
|
---|
| 979 | if( param.getType() instanceof DataType ) {
|
---|
| 980 | InstanceSpecification instSpec = createInstanceSpecification((DataType) param.getType(), event);
|
---|
| 981 |
|
---|
| 982 | InstanceValue value = (InstanceValue) argument.createOperand(null, param.getType(), UMLPackage.Literals.INSTANCE_VALUE);
|
---|
| 983 | value.setInstance(instSpec);
|
---|
| 984 | }
|
---|
| 985 | else if( param.getType() instanceof PrimitiveType ) {
|
---|
| 986 | if( "String".equals(param.getType().getName()) ) {
|
---|
| 987 | LiteralString spec = (LiteralString) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_STRING);
|
---|
| 988 | spec.setValue("foobar"); // TODO needs to be real value
|
---|
| 989 | }
|
---|
| 990 | else if( "Integer".equals(param.getType().getName()) ) {
|
---|
| 991 | LiteralInteger spec = (LiteralInteger) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_INTEGER);
|
---|
| 992 | spec.setValue(42); // TODO needs to be real value
|
---|
| 993 | }
|
---|
| 994 | else if( "Boolean".equals(param.getType().getName()) ) {
|
---|
| 995 | LiteralBoolean spec = (LiteralBoolean) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_BOOLEAN);
|
---|
| 996 | spec.setValue(true); // TODO needs to be real value
|
---|
| 997 | }
|
---|
| 998 | else if( "Real".equals(param.getType().getName()) ) {
|
---|
| 999 | LiteralReal spec = (LiteralReal) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_REAL);
|
---|
| 1000 | spec.setValue(3.14); // TODO needs to be real value
|
---|
| 1001 | }
|
---|
| 1002 | }
|
---|
| 1003 | } else {
|
---|
| 1004 | // set literalNull for out and return parameters
|
---|
| 1005 | argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
|
---|
| 1006 | }
|
---|
| 1007 | }
|
---|
| 1008 | }
|
---|
| 1009 |
|
---|
| 1010 | public static InstanceSpecification createInstanceSpecification(DataType type, Event event) {
|
---|
| 1011 | String pkgUBTInstSpecs = "UBT_InstanceSpecifications";
|
---|
| 1012 | Model model = type.getModel();
|
---|
| 1013 | Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs);
|
---|
| 1014 | if( ubtInstSpecPkg==null ) {
|
---|
| 1015 | ubtInstSpecPkg = (Package) type.getModel().createPackagedElement(pkgUBTInstSpecs, UMLPackage.Literals.PACKAGE);
|
---|
| 1016 | }
|
---|
[1916] | 1017 | String serviceName = getServiceNameFromEvent(event);
|
---|
[1908] | 1018 | Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName);
|
---|
| 1019 | if( serviceInstSpecPkg==null ) {
|
---|
| 1020 | serviceInstSpecPkg = (Package) ubtInstSpecPkg.createPackagedElement(serviceName, UMLPackage.Literals.PACKAGE);
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
| 1023 | InstanceSpecification instSpec = (InstanceSpecification) serviceInstSpecPkg.createPackagedElement("instspec_"+type.getName(), UMLPackage.Literals.INSTANCE_SPECIFICATION);
|
---|
| 1024 | instSpec.getClassifiers().add(type);
|
---|
| 1025 | for( Property prop : type.getAllAttributes() ) {
|
---|
| 1026 | // TODO handle multiplicity
|
---|
| 1027 | //int lowerBound = prop.getLower();
|
---|
| 1028 | //int upperBound = prop.getUpper();
|
---|
| 1029 |
|
---|
| 1030 | Slot slot = instSpec.createSlot();
|
---|
| 1031 | slot.setDefiningFeature(prop);
|
---|
| 1032 | if( prop.getType() instanceof PrimitiveType ) {
|
---|
| 1033 | if( "String".equals(prop.getType().getName()) ) {
|
---|
| 1034 | LiteralString value = (LiteralString) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_STRING);
|
---|
| 1035 | value.setValue("foobar"); // TODO needs to be real value
|
---|
| 1036 | }
|
---|
| 1037 | else if( "Integer".equals(prop.getType().getName()) ) {
|
---|
| 1038 | LiteralInteger value = (LiteralInteger) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_INTEGER);
|
---|
| 1039 | value.setValue(42); // TODO needs to be real value
|
---|
| 1040 | }
|
---|
| 1041 | else if( "Boolean".equals(prop.getType().getName()) ) {
|
---|
| 1042 | LiteralBoolean value = (LiteralBoolean) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_BOOLEAN);
|
---|
| 1043 | value.setValue(true); // TODO needs to be real value
|
---|
| 1044 | }
|
---|
| 1045 | else if( "Real".equals(prop.getType().getName()) ) {
|
---|
| 1046 | LiteralReal value = (LiteralReal) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_REAL);
|
---|
| 1047 | value.setValue(3.14); // TODO needs to be real value
|
---|
| 1048 | }
|
---|
| 1049 | }
|
---|
| 1050 | else if( prop.getType() instanceof DataType ) {
|
---|
| 1051 | InstanceValue value = (InstanceValue) slot.createValue(prop.getName(), prop.getType(), UMLPackage.Literals.INSTANCE_VALUE);
|
---|
| 1052 | value.setInstance(createInstanceSpecification((DataType) prop.getType(), event));
|
---|
| 1053 | }
|
---|
| 1054 | }
|
---|
| 1055 | return instSpec;
|
---|
| 1056 | }
|
---|
[1604] | 1057 | }
|
---|