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