[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;
|
---|
[1926] | 37 | import org.eclipse.uml2.uml.CallEvent;
|
---|
[1763] | 38 | import org.eclipse.uml2.uml.CallOperationAction;
|
---|
[1604] | 39 | import org.eclipse.uml2.uml.Comment;
|
---|
[1752] | 40 | import org.eclipse.uml2.uml.Component;
|
---|
| 41 | import org.eclipse.uml2.uml.Connector;
|
---|
| 42 | import org.eclipse.uml2.uml.ConnectorEnd;
|
---|
[1908] | 43 | import org.eclipse.uml2.uml.DataType;
|
---|
[1835] | 44 | import org.eclipse.uml2.uml.Element;
|
---|
[1908] | 45 | import org.eclipse.uml2.uml.Expression;
|
---|
| 46 | import org.eclipse.uml2.uml.InstanceSpecification;
|
---|
| 47 | import org.eclipse.uml2.uml.InstanceValue;
|
---|
[1624] | 48 | import org.eclipse.uml2.uml.Interaction;
|
---|
[1643] | 49 | import org.eclipse.uml2.uml.InteractionFragment;
|
---|
[1752] | 50 | import org.eclipse.uml2.uml.Interface;
|
---|
[1624] | 51 | import org.eclipse.uml2.uml.Lifeline;
|
---|
[1908] | 52 | import org.eclipse.uml2.uml.LiteralBoolean;
|
---|
| 53 | import org.eclipse.uml2.uml.LiteralInteger;
|
---|
| 54 | import org.eclipse.uml2.uml.LiteralReal;
|
---|
| 55 | import org.eclipse.uml2.uml.LiteralString;
|
---|
[1624] | 56 | import org.eclipse.uml2.uml.Message;
|
---|
| 57 | import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
|
---|
[1752] | 58 | import org.eclipse.uml2.uml.MessageSort;
|
---|
[1624] | 59 | import org.eclipse.uml2.uml.Model;
|
---|
| 60 | import org.eclipse.uml2.uml.Operation;
|
---|
[1896] | 61 | import org.eclipse.uml2.uml.Package;
|
---|
[1908] | 62 | import org.eclipse.uml2.uml.Parameter;
|
---|
| 63 | import org.eclipse.uml2.uml.ParameterDirectionKind;
|
---|
[1752] | 64 | import org.eclipse.uml2.uml.Port;
|
---|
[1908] | 65 | import org.eclipse.uml2.uml.PrimitiveType;
|
---|
[1761] | 66 | import org.eclipse.uml2.uml.Property;
|
---|
[1604] | 67 | import org.eclipse.uml2.uml.Region;
|
---|
[1926] | 68 | import org.eclipse.uml2.uml.Relationship;
|
---|
[1908] | 69 | import org.eclipse.uml2.uml.Slot;
|
---|
[1604] | 70 | import org.eclipse.uml2.uml.StateMachine;
|
---|
[1759] | 71 | import org.eclipse.uml2.uml.Stereotype;
|
---|
[1604] | 72 | import org.eclipse.uml2.uml.Transition;
|
---|
[1926] | 73 | import org.eclipse.uml2.uml.Trigger;
|
---|
[1624] | 74 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
[1929] | 75 | import org.eclipse.uml2.uml.ValueSpecification;
|
---|
[1604] | 76 | import org.eclipse.uml2.uml.Vertex;
|
---|
| 77 |
|
---|
| 78 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
[1926] | 79 | import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
|
---|
[1604] | 80 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
[1643] | 81 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
|
---|
[1604] | 82 | import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
|
---|
[1763] | 83 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
[1926] | 84 | import de.ugoe.cs.util.StringTools;
|
---|
[1898] | 85 | import de.ugoe.cs.util.console.Console;
|
---|
[1604] | 86 |
|
---|
| 87 | /**
|
---|
| 88 | * <p>
|
---|
[1624] | 89 | * Utilities for working with UML.
|
---|
[1604] | 90 | * </p>
|
---|
| 91 | *
|
---|
| 92 | * @author Steffen Herbold
|
---|
| 93 | */
|
---|
| 94 | public class UMLUtils {
|
---|
| 95 |
|
---|
[1900] | 96 | /**
|
---|
[1926] | 97 | * In case a multiplicity is defined as *, this value defines the highest one that can be picked
|
---|
| 98 | */
|
---|
| 99 | final static int MAX_MULTIPLICITY = 10;
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
[1900] | 102 | * <p>
|
---|
| 103 | * Method for checking if the information in a usage journal can be mapped to the SUT model. In
|
---|
| 104 | * case this is not possible, the violations are reported.
|
---|
| 105 | * </p>
|
---|
| 106 | *
|
---|
| 107 | * @param sequences
|
---|
| 108 | * sequences of the usage journal
|
---|
| 109 | * @param model
|
---|
| 110 | * SUT model that is validated
|
---|
| 111 | * @param testContextName
|
---|
| 112 | * name of the test context to be used; if null, the first test context found is used
|
---|
| 113 | * @return number of violations
|
---|
| 114 | */
|
---|
| 115 | public static int validateModelWithLog(Collection<List<Event>> sequences,
|
---|
| 116 | Model model,
|
---|
| 117 | String testContextName)
|
---|
| 118 | {
|
---|
| 119 | int violationCount = 0;
|
---|
[1929] | 120 | Component testContext = fetchTestContext(model, testContextName);
|
---|
[1898] | 121 | if (testContext == null) {
|
---|
[1900] | 122 | violationCount++;
|
---|
| 123 | if (testContextName == null) {
|
---|
[1898] | 124 | Console.traceln(Level.SEVERE, "Could not find any TestContext in the model.");
|
---|
[1900] | 125 |
|
---|
[1898] | 126 | }
|
---|
[1900] | 127 | else {
|
---|
| 128 | Console.traceln(Level.SEVERE, "Could not find TestContext in the model: " +
|
---|
| 129 | testContextName);
|
---|
| 130 | }
|
---|
| 131 | Console
|
---|
| 132 | .traceln(Level.SEVERE,
|
---|
| 133 | "Hint: Check if you have applied the TestContext stereotype correctly in the model.");
|
---|
| 134 | Console.traceln(Level.SEVERE, "Aborting");
|
---|
| 135 | return violationCount;
|
---|
[1898] | 136 | }
|
---|
[1900] | 137 |
|
---|
[1898] | 138 | // Create list of unique methods calls
|
---|
| 139 | HashMap<String, Set<String>> calledMethods = new HashMap<>();
|
---|
[1900] | 140 | for (List<Event> sequence : sequences) {
|
---|
| 141 | for (Event event : sequence) {
|
---|
[1926] | 142 | String serviceName = SOAPUtils.getServiceNameFromEvent(event);
|
---|
| 143 | String calledMethod = SOAPUtils.getCalledMethodFromEvent(event);
|
---|
| 144 | if (serviceName != null) {
|
---|
[1916] | 145 | Set<String> curCalledMethods = calledMethods.get(serviceName);
|
---|
[1900] | 146 | if (curCalledMethods == null) {
|
---|
[1898] | 147 | curCalledMethods = new TreeSet<>();
|
---|
[1916] | 148 | calledMethods.put(serviceName, curCalledMethods);
|
---|
[1898] | 149 | }
|
---|
[1916] | 150 | curCalledMethods.add(calledMethod);
|
---|
[1898] | 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[1900] | 154 |
|
---|
| 155 | Console.traceln(Level.INFO,
|
---|
| 156 | "Found the following services and operations in the usage data: ");
|
---|
| 157 | for (Entry<String, Set<String>> entry : calledMethods.entrySet()) {
|
---|
[1908] | 158 | Console.traceln(Level.INFO, "\tService \"" + entry.getKey() + "\": ");
|
---|
[1926] | 159 | for (String method : entry.getValue()) {
|
---|
| 160 | Console.traceln(Level.INFO, "\t\t" + method);
|
---|
[1898] | 161 | }
|
---|
| 162 | }
|
---|
[1900] | 163 |
|
---|
[1898] | 164 | // fetch all SUTs and TestComponents
|
---|
| 165 | HashMap<String, Property> properties = new HashMap<>();
|
---|
[1929] | 166 | for (Property property : fetchAllSUTProperties(testContext)) {
|
---|
| 167 | properties.put(property.getName(), property);
|
---|
[1898] | 168 | }
|
---|
[1929] | 169 | for (Property property : fetchAllTestComponentProperties(testContext)) {
|
---|
| 170 | properties.put(property.getName(), property);
|
---|
| 171 | }
|
---|
[1898] | 172 | Console.traceln(Level.INFO, "Found the following services in the TestConfiguration:");
|
---|
[1900] | 173 | for (Entry<String, Property> entry : properties.entrySet()) {
|
---|
[1898] | 174 | Console.traceln(Level.INFO, "\t" + entry.getKey());
|
---|
| 175 | }
|
---|
[1900] | 176 |
|
---|
| 177 | for (Entry<String, Set<String>> entry : calledMethods.entrySet()) {
|
---|
[1898] | 178 | String serviceName = entry.getKey();
|
---|
| 179 | Console.traceln(Level.INFO, "Checking service: " + serviceName);
|
---|
| 180 | Set<String> methodNames = entry.getValue();
|
---|
| 181 | Property property = properties.get(serviceName);
|
---|
[1900] | 182 | if (property == null) {
|
---|
| 183 | violationCount++;
|
---|
| 184 | Console.traceln(Level.SEVERE, "\tCould not find property for service: " +
|
---|
| 185 | serviceName);
|
---|
| 186 | Console
|
---|
| 187 | .traceln(Level.SEVERE,
|
---|
| 188 | "\tHint: Check service name map and/or model if the service is present and spelled correctly.");
|
---|
| 189 | Console
|
---|
| 190 | .traceln(Level.SEVERE,
|
---|
| 191 | "\tHint: Check if the SUT/TestComponent stereotype has been applied correctly in this TestContext.");
|
---|
| 192 | }
|
---|
| 193 | else {
|
---|
[1898] | 194 | Set<Interface> interfaces = getRealizedInterfacesFromProperty(property);
|
---|
[1900] | 195 | if (interfaces.isEmpty()) {
|
---|
| 196 | violationCount++;
|
---|
| 197 | Console
|
---|
| 198 | .traceln(Level.SEVERE,
|
---|
| 199 | "\tCould not find any interfaces implementing the property for service: " +
|
---|
| 200 | serviceName);
|
---|
| 201 | Console
|
---|
| 202 | .traceln(Level.SEVERE,
|
---|
| 203 | "\tHint: Check if the property correctly realizes the interfaces in the model.");
|
---|
| 204 | }
|
---|
| 205 | else {
|
---|
| 206 | Console.traceln(Level.INFO,
|
---|
| 207 | "\tFound the following realized interfaces for the service \"" +
|
---|
| 208 | serviceName + "\": ");
|
---|
[1926] | 209 | for (Interface intface : interfaces) {
|
---|
[1908] | 210 | Console.traceln(Level.INFO, "\t" + intface.getName());
|
---|
[1926] | 211 | for (Operation operation : intface.getAllOperations()) {
|
---|
[1908] | 212 | Console.traceln(Level.INFO, "\t\t" + operation.getName());
|
---|
[1898] | 213 | }
|
---|
| 214 | }
|
---|
[1900] | 215 | for (String methodName : methodNames) {
|
---|
| 216 | boolean methodFound = false;
|
---|
| 217 | for (Interface intface : interfaces) {
|
---|
[1898] | 218 | if (getOperationFromName(intface.getOperations(), methodName) != null) {
|
---|
| 219 | // interface found
|
---|
[1900] | 220 | Console.traceln(Level.INFO, "\tMethod " + methodName +
|
---|
| 221 | " found in interface " + intface.getName());
|
---|
[1898] | 222 | methodFound = true;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
[1900] | 225 | if (!methodFound) {
|
---|
| 226 | violationCount++;
|
---|
| 227 | Console.traceln(Level.SEVERE, "\tCould not find operation: " +
|
---|
| 228 | methodName);
|
---|
[1898] | 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
[1900] | 234 | return violationCount;
|
---|
[1898] | 235 | }
|
---|
[1900] | 236 |
|
---|
[1624] | 237 | /**
|
---|
| 238 | * <p>
|
---|
| 239 | * Creates a sequence of events with {@link UMLTransitionType} as event type from a given
|
---|
[1926] | 240 | * sequence of events with the {@link SOAPEventType} or {@link SimpleSOAPEventType}, by matching
|
---|
| 241 | * the sequences to a state machine.
|
---|
[1624] | 242 | * </p>
|
---|
| 243 | *
|
---|
| 244 | * @param sequence
|
---|
| 245 | * SOAP sequences
|
---|
| 246 | * @param stateMachine
|
---|
| 247 | * the state machine
|
---|
| 248 | * @return create UML sequences
|
---|
| 249 | */
|
---|
| 250 | public static List<Event> createUMLTransitionSequence(List<Event> sequence,
|
---|
| 251 | StateMachine stateMachine)
|
---|
| 252 | {
|
---|
[1926] | 253 | System.out.println("foo");
|
---|
[1624] | 254 | List<List<Transition>> matchingSequences =
|
---|
| 255 | determineMatchingTransitionSequences(sequence, stateMachine);
|
---|
[1926] | 256 | System.out.println(matchingSequences.size());
|
---|
[1604] | 257 |
|
---|
| 258 | if (matchingSequences.size() != 1) {
|
---|
| 259 | throw new RuntimeException("no unique match found; " + matchingSequences.size() +
|
---|
| 260 | " matches");
|
---|
| 261 | }
|
---|
| 262 | List<Event> umlEventSequence = new LinkedList<>();
|
---|
| 263 | for (Transition transition : matchingSequences.get(0)) {
|
---|
| 264 | umlEventSequence.add(new Event(new UMLTransitionType(transition)));
|
---|
| 265 | }
|
---|
| 266 | return umlEventSequence;
|
---|
| 267 | }
|
---|
[1624] | 268 |
|
---|
| 269 | /**
|
---|
| 270 | * <p>
|
---|
| 271 | * Uses a sequences of events with the {@link UMLTransitionType} to determine the transition
|
---|
| 272 | * probabilities for the state machine.
|
---|
| 273 | * </p>
|
---|
| 274 | *
|
---|
| 275 | * @param sequences
|
---|
| 276 | * UML sequences
|
---|
| 277 | * @param stateMachine
|
---|
| 278 | * state machine to be converted to a usage profile
|
---|
| 279 | */
|
---|
| 280 | public static void convertStateMachineToUsageProfile(Collection<List<Event>> sequences,
|
---|
| 281 | StateMachine stateMachine)
|
---|
| 282 | {
|
---|
[1604] | 283 | // create state->outgoings hashmap
|
---|
[1624] | 284 | Map<Vertex, Map<Transition, Integer>> stateMap = new HashMap<>();
|
---|
| 285 | for (Region region : stateMachine.getRegions()) {
|
---|
| 286 | for (Vertex state : region.getSubvertices()) {
|
---|
| 287 | stateMap.put(state, new HashMap<Transition, Integer>());
|
---|
[1604] | 288 | }
|
---|
| 289 | }
|
---|
[1624] | 290 |
|
---|
[1604] | 291 | // create counters for each transition
|
---|
[1624] | 292 | for (List<Event> sequence : sequences) {
|
---|
| 293 | for (Event event : sequence) {
|
---|
| 294 | if (event.getType() instanceof UMLTransitionType) {
|
---|
[1604] | 295 | Transition transition = ((UMLTransitionType) event.getType()).getTransition();
|
---|
[1624] | 296 | Map<Transition, Integer> transitionMap = stateMap.get(transition.getSource());
|
---|
[1604] | 297 | Integer value = transitionMap.get(transition);
|
---|
[1624] | 298 | if (value == null) {
|
---|
[1604] | 299 | value = 0;
|
---|
| 300 | }
|
---|
[1624] | 301 | transitionMap.put(transition, value + 1);
|
---|
[1604] | 302 | }
|
---|
[1624] | 303 | else {
|
---|
| 304 | throw new RuntimeException(
|
---|
| 305 | "Wrong event type. Only UMLTransitionType supported but was: " +
|
---|
| 306 | event.getType().getClass().getName());
|
---|
| 307 | }
|
---|
[1604] | 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | // calculate probabilities
|
---|
[1624] | 312 | for (Region region : stateMachine.getRegions()) {
|
---|
| 313 | for (Vertex state : region.getSubvertices()) {
|
---|
| 314 | Map<Transition, Integer> transitionMap = stateMap.get(state);
|
---|
[1604] | 315 | int totalCount = 0;
|
---|
[1624] | 316 | for (Entry<Transition, Integer> entry : transitionMap.entrySet()) {
|
---|
[1604] | 317 | totalCount += entry.getValue();
|
---|
| 318 | }
|
---|
[1624] | 319 | if (totalCount != 0) {
|
---|
| 320 | for (Transition transition : state.getOutgoings()) {
|
---|
[1604] | 321 | double prob = 0.0d;
|
---|
[1624] | 322 | if (transitionMap.containsKey(transition)) {
|
---|
| 323 | prob = ((double) transitionMap.get(transition)) / totalCount;
|
---|
[1604] | 324 | }
|
---|
| 325 | Comment comment = transition.createOwnedComment();
|
---|
[1624] | 326 | comment.setBody("" + prob);
|
---|
[1604] | 327 | }
|
---|
[1624] | 328 | }
|
---|
| 329 | else {
|
---|
[1604] | 330 | // system has never been in this state, all transitions equally likely
|
---|
| 331 | int numOutgoings = state.getOutgoings().size();
|
---|
[1624] | 332 | for (Transition transition : state.getOutgoings()) {
|
---|
[1604] | 333 | Comment comment = transition.createOwnedComment();
|
---|
[1624] | 334 | comment.setBody("" + (1.0d / numOutgoings));
|
---|
[1604] | 335 | }
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
[1624] | 340 |
|
---|
| 341 | /**
|
---|
| 342 | * <p>
|
---|
| 343 | * Determines all matching {@link Transition} sequences in a state machine for a given sequence
|
---|
| 344 | * of SOAP events.
|
---|
| 345 | * </p>
|
---|
| 346 | *
|
---|
| 347 | * @param sequence
|
---|
| 348 | * SOAP sequence
|
---|
| 349 | * @param stateMachine
|
---|
| 350 | * the state machine
|
---|
| 351 | * @return all matching {@link Transition} sequences
|
---|
| 352 | */
|
---|
| 353 | public static List<List<Transition>> determineMatchingTransitionSequences(List<Event> sequence,
|
---|
| 354 | StateMachine stateMachine)
|
---|
| 355 | {
|
---|
[1604] | 356 | EList<Region> regions = stateMachine.getRegions();
|
---|
| 357 | EList<Vertex> states = null;
|
---|
| 358 | for (Region region : regions) {
|
---|
| 359 | if (states == null) {
|
---|
| 360 | states = region.getSubvertices();
|
---|
| 361 | }
|
---|
| 362 | else {
|
---|
| 363 | states.addAll(region.getSubvertices());
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 | List<Transition> allTransitions = new LinkedList<>();
|
---|
| 367 | for (Vertex state : states) {
|
---|
| 368 | allTransitions.addAll(state.getOutgoings());
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | List<List<Transition>> matchingSequences = null;
|
---|
| 372 | List<Transition> currentTransitions = null;
|
---|
| 373 |
|
---|
| 374 | // first, we try to find a single unique transition that we can match using the method name
|
---|
| 375 | for (Iterator<Event> eventIterator = sequence.iterator(); eventIterator.hasNext();) {
|
---|
| 376 | Event event = eventIterator.next();
|
---|
[1926] | 377 | System.out.println(event);
|
---|
| 378 | System.out.println(matchingSequences);
|
---|
| 379 | if (matchingSequences == null) {
|
---|
| 380 | matchingSequences = new LinkedList<>();
|
---|
| 381 | List<Transition> initialMatches = matchTransitions(allTransitions, event);
|
---|
| 382 | for (Transition transition : initialMatches) {
|
---|
| 383 | List<Transition> candidate = new LinkedList<>();
|
---|
| 384 | candidate.add(transition);
|
---|
| 385 | matchingSequences.add(candidate);
|
---|
[1604] | 386 | }
|
---|
[1926] | 387 | currentTransitions = initialMatches;
|
---|
| 388 | }
|
---|
| 389 | else {
|
---|
| 390 | List<List<Transition>> nextMatchingSequences = new LinkedList<>();
|
---|
| 391 | List<Transition> nextCurrentTransitions = new LinkedList<>();
|
---|
| 392 | Iterator<Transition> currentTransitionIterator = currentTransitions.iterator();
|
---|
| 393 | Iterator<List<Transition>> currentMatchingSequencesIterator =
|
---|
| 394 | matchingSequences.iterator();
|
---|
| 395 | while (currentTransitionIterator.hasNext()) {
|
---|
| 396 | Transition currentTransition = currentTransitionIterator.next();
|
---|
| 397 | List<Transition> currentMatch = currentMatchingSequencesIterator.next();
|
---|
[1604] | 398 |
|
---|
[1926] | 399 | List<Transition> matches =
|
---|
| 400 | matchTransitions(currentTransition.getTarget().getOutgoings(), event);
|
---|
| 401 | if (matches.isEmpty()) {
|
---|
| 402 | throw new RuntimeException("no matches found");
|
---|
[1604] | 403 | }
|
---|
[1926] | 404 | for (Transition matchingTransition : matches) {
|
---|
| 405 | List<Transition> candidate = new LinkedList<>(currentMatch);
|
---|
| 406 | candidate.add(matchingTransition);
|
---|
| 407 | nextMatchingSequences.add(candidate);
|
---|
| 408 | nextCurrentTransitions.add(matchingTransition);
|
---|
| 409 | }
|
---|
[1604] | 410 | }
|
---|
[1926] | 411 | matchingSequences = nextMatchingSequences;
|
---|
| 412 | currentTransitions = nextCurrentTransitions;
|
---|
[1604] | 413 | }
|
---|
| 414 | }
|
---|
| 415 | return matchingSequences;
|
---|
| 416 | }
|
---|
[1624] | 417 |
|
---|
| 418 | /**
|
---|
| 419 | * <p>
|
---|
[1633] | 420 | * Extends a given model with an interaction that represents an observed sequence.
|
---|
[1624] | 421 | * </p>
|
---|
[1633] | 422 | *
|
---|
| 423 | * @param sequence
|
---|
| 424 | * sequence that is added as sequence diagram
|
---|
| 425 | * @param model
|
---|
| 426 | * UML model to which the interaction is added
|
---|
| 427 | * @param interactionName
|
---|
| 428 | * name of the interaction
|
---|
[1896] | 429 | * @param testContextName
|
---|
[1897] | 430 | * Name of the test context that should be used. If this value is null, the first
|
---|
| 431 | * test context found is used.
|
---|
[1624] | 432 | */
|
---|
[1929] | 433 | public static Interaction createInteractionFromEventSequence(List<Event> sequence,
|
---|
| 434 | Model model,
|
---|
| 435 | String interactionName,
|
---|
| 436 | String testContextName)
|
---|
[1604] | 437 | {
|
---|
[1929] | 438 | final Component testContext = fetchTestContext(model, testContextName);
|
---|
[1897] | 439 | if (testContext == null) {
|
---|
[1896] | 440 | throw new RuntimeException("Could not find any test context in the model");
|
---|
| 441 | }
|
---|
[1897] | 442 |
|
---|
[1929] | 443 | final Operation operation = testContext.createOwnedOperation(interactionName, null, null);
|
---|
| 444 | operation.applyStereotype(UTPUtils.getTestCaseStereotype(model));
|
---|
[1759] | 445 |
|
---|
[1929] | 446 | final Interaction interaction =
|
---|
[1759] | 447 | (Interaction) testContext.createPackagedElement(interactionName + "_Impl",
|
---|
| 448 | UMLPackage.Literals.INTERACTION);
|
---|
[1761] | 449 | operation.getMethods().add(interaction);
|
---|
[1835] | 450 |
|
---|
[1763] | 451 | // create lifelines
|
---|
| 452 | Lifeline userLifeline = null;
|
---|
[1900] | 453 |
|
---|
[1929] | 454 | for (Property property : fetchAllSUTProperties(testContext)) {
|
---|
| 455 | String serviceName = property.getName();
|
---|
| 456 | Lifeline targetLifeline = interaction.createLifeline(serviceName);
|
---|
| 457 | targetLifeline.setRepresents(property);
|
---|
| 458 | }
|
---|
| 459 | for (Property property : fetchAllTestComponentProperties(testContext)) {
|
---|
| 460 | // TODO check if this is still required
|
---|
| 461 | if (userLifeline != null) {
|
---|
| 462 | throw new RuntimeException(
|
---|
| 463 | "TestContext must only have one TestComponent for the application of usage-based testing.");
|
---|
[1761] | 464 | }
|
---|
[1929] | 465 | userLifeline = interaction.createLifeline(property.getName());
|
---|
| 466 | userLifeline.setRepresents(property);
|
---|
[1761] | 467 | }
|
---|
[1929] | 468 |
|
---|
[1900] | 469 | if (userLifeline == null) {
|
---|
[1897] | 470 | throw new RuntimeException("No TestComponent found, could not create user lifeline.");
|
---|
| 471 | }
|
---|
[1900] | 472 | if (interaction.getLifelines().size() < 2) {
|
---|
[1897] | 473 | throw new RuntimeException("Fewer than two lifelines created. No SUT found.");
|
---|
| 474 | }
|
---|
[1900] | 475 |
|
---|
[1624] | 476 | int i = 0;
|
---|
| 477 | for (Event event : sequence) {
|
---|
[1633] | 478 | if (!(event.equals(Event.STARTEVENT) || event.equals(Event.ENDEVENT))) {
|
---|
[1926] | 479 | String serviceName = SOAPUtils.getServiceNameFromEvent(event);
|
---|
| 480 | String methodName = SOAPUtils.getCalledMethodFromEvent(event);
|
---|
| 481 | String clientName = SOAPUtils.getClientNameFromEvent(event);
|
---|
| 482 | String prefix = interactionName + ":" + i + ":" + methodName + "_";
|
---|
[1835] | 483 | // determine lifelines
|
---|
| 484 | Lifeline msgTargetLifeline;
|
---|
| 485 | Lifeline msgSourceLifeline;
|
---|
[1926] | 486 |
|
---|
[1913] | 487 | msgSourceLifeline = interaction.getLifeline(clientName);
|
---|
| 488 | msgTargetLifeline = interaction.getLifeline(serviceName);
|
---|
[1926] | 489 |
|
---|
[1900] | 490 | if (msgSourceLifeline == null) {
|
---|
| 491 | throw new RuntimeException(
|
---|
[1926] | 492 | "Error creating message: could not determine source lifeline for component: " +
|
---|
| 493 | clientName);
|
---|
[1752] | 494 | }
|
---|
[1900] | 495 | if (msgTargetLifeline == null) {
|
---|
| 496 | throw new RuntimeException(
|
---|
[1926] | 497 | "Error creating message: could not determine target lifeline for component: " +
|
---|
| 498 | serviceName);
|
---|
[1897] | 499 | }
|
---|
| 500 | // determine correct target interface
|
---|
[1898] | 501 | Set<Interface> targetInterfaces =
|
---|
[1897] | 502 | getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents());
|
---|
| 503 | if (targetInterfaces.isEmpty()) {
|
---|
| 504 | throw new RuntimeException("no interface associated with the property " +
|
---|
| 505 | msgTargetLifeline.getRepresents().getName());
|
---|
| 506 | }
|
---|
[1896] | 507 | Interface targetInterface = null;
|
---|
[1897] | 508 | for (Interface intface : targetInterfaces) {
|
---|
[1896] | 509 | if (getOperationFromName(intface.getOperations(), methodName) != null) {
|
---|
| 510 | // interface found
|
---|
| 511 | targetInterface = intface;
|
---|
| 512 | break;
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
[1897] | 515 | if (targetInterface == null) {
|
---|
[1896] | 516 | StringBuilder errStrBuilder = new StringBuilder();
|
---|
[1900] | 517 | errStrBuilder
|
---|
| 518 | .append("Error creating message: operation not found in the implementing interfaces (");
|
---|
[1896] | 519 | Iterator<Interface> iter = targetInterfaces.iterator();
|
---|
[1897] | 520 | while (iter.hasNext()) {
|
---|
[1896] | 521 | String interfaceName = iter.next().getName();
|
---|
| 522 | errStrBuilder.append(interfaceName);
|
---|
[1897] | 523 | if (iter.hasNext()) {
|
---|
[1896] | 524 | errStrBuilder.append(",");
|
---|
[1897] | 525 | }
|
---|
| 526 | else {
|
---|
[1896] | 527 | errStrBuilder.append("): " + methodName);
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 | throw new RuntimeException(errStrBuilder.toString());
|
---|
| 531 | }
|
---|
[1897] | 532 |
|
---|
| 533 | Operation calledOperation =
|
---|
| 534 | getOperationFromName(targetInterface.getOperations(), methodName);
|
---|
| 535 |
|
---|
| 536 | // setup for both SYNCH and ASYNCH calls
|
---|
| 537 | MessageOccurrenceSpecification callSendFragment =
|
---|
[1926] | 538 | (MessageOccurrenceSpecification) interaction.createFragment(prefix +
|
---|
| 539 | "callSendFragment", UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
[1897] | 540 | MessageOccurrenceSpecification callRecvFragment =
|
---|
[1926] | 541 | (MessageOccurrenceSpecification) interaction.createFragment(prefix +
|
---|
| 542 | "callRecvFragment", UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
[1897] | 543 |
|
---|
| 544 | callSendFragment.setCovered(msgSourceLifeline);
|
---|
| 545 | callRecvFragment.setCovered(msgTargetLifeline);
|
---|
| 546 |
|
---|
[1929] | 547 | // get connector
|
---|
| 548 | Connector connector = inferConnector(msgSourceLifeline, msgTargetLifeline);
|
---|
| 549 |
|
---|
[1897] | 550 | // create call
|
---|
[1929] | 551 | Message callMessage = interaction.createMessage(prefix + "call");
|
---|
[1926] | 552 | callMessage.setSignature(calledOperation);
|
---|
[1929] | 553 | setCallMessageParameters(callMessage, calledOperation, event, prefix);
|
---|
| 554 | callMessage.setConnector(connector);
|
---|
[1897] | 555 | callMessage.setSendEvent(callSendFragment);
|
---|
| 556 | callMessage.setReceiveEvent(callRecvFragment);
|
---|
| 557 | callSendFragment.setMessage(callMessage);
|
---|
| 558 | callRecvFragment.setMessage(callMessage);
|
---|
| 559 |
|
---|
[1896] | 560 | boolean asynch = false;
|
---|
[1900] | 561 | if (calledOperation.getConcurrency() == CallConcurrencyKind.CONCURRENT_LITERAL) {
|
---|
[1898] | 562 | asynch = true;
|
---|
| 563 | }
|
---|
[1897] | 564 | if (asynch) {
|
---|
| 565 | // Create ASYNCH call
|
---|
| 566 | callMessage.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
|
---|
| 567 | }
|
---|
| 568 | else {
|
---|
[1896] | 569 | // SYNCH call
|
---|
[1897] | 570 | callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
|
---|
| 571 |
|
---|
| 572 | // setup reply and behavior execution specifications
|
---|
[1896] | 573 | MessageOccurrenceSpecification replySendFragment =
|
---|
| 574 | (MessageOccurrenceSpecification) interaction
|
---|
[1926] | 575 | .createFragment(prefix + "replySendFragment",
|
---|
[1896] | 576 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
| 577 | MessageOccurrenceSpecification replyRecvFragment =
|
---|
| 578 | (MessageOccurrenceSpecification) interaction
|
---|
[1926] | 579 | .createFragment(prefix + "replyRecvFragment",
|
---|
[1896] | 580 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
[1897] | 581 |
|
---|
| 582 | replySendFragment.setCovered(msgTargetLifeline);
|
---|
| 583 | replyRecvFragment.setCovered(msgSourceLifeline);
|
---|
| 584 |
|
---|
[1900] | 585 | /*
|
---|
| 586 | * BehaviorExecutionSpecification sourceBehaviorExecutionSpecification =
|
---|
| 587 | * (BehaviorExecutionSpecification) interaction .createFragment(":" + methodName
|
---|
| 588 | * + "_sourceBhvExecSpec",
|
---|
| 589 | * UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
|
---|
| 590 | * BehaviorExecutionSpecification targetBehaviorExecutionSpecification =
|
---|
| 591 | * (BehaviorExecutionSpecification) interaction .createFragment(":" + methodName
|
---|
| 592 | * + "_targetBhvExecSpec",
|
---|
| 593 | * UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
|
---|
| 594 | *
|
---|
| 595 | * sourceBehaviorExecutionSpecification.setStart(callSendFragment);
|
---|
| 596 | * sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment);
|
---|
| 597 | * targetBehaviorExecutionSpecification.setStart(callRecvFragment);
|
---|
| 598 | * targetBehaviorExecutionSpecification.setFinish(replySendFragment);
|
---|
| 599 | */
|
---|
[1897] | 600 |
|
---|
[1896] | 601 | // create reply
|
---|
[1929] | 602 | Message replyMessage = interaction.createMessage(prefix + "_reply");
|
---|
[1896] | 603 | replyMessage.setMessageSort(MessageSort.REPLY_LITERAL);
|
---|
| 604 | replyMessage.setSignature(calledOperation);
|
---|
[1929] | 605 | setReplyMessageParameters(replyMessage, calledOperation);
|
---|
| 606 | replyMessage.setConnector(connector);
|
---|
[1896] | 607 | replyMessage.setSendEvent(replySendFragment);
|
---|
| 608 | replyMessage.setReceiveEvent(replyRecvFragment);
|
---|
| 609 | replySendFragment.setMessage(replyMessage);
|
---|
| 610 | replyRecvFragment.setMessage(replyMessage);
|
---|
[1753] | 611 | }
|
---|
[1643] | 612 |
|
---|
[1633] | 613 | i++;
|
---|
[1624] | 614 | }
|
---|
| 615 | }
|
---|
[1929] | 616 | return interaction;
|
---|
[1624] | 617 | }
|
---|
| 618 |
|
---|
| 619 | /**
|
---|
| 620 | * <p>
|
---|
[1643] | 621 | * Calculates the usage score of an interaction as the logsum of the event probabilities
|
---|
| 622 | * multiplied with the length of the interaction.
|
---|
| 623 | * </p>
|
---|
| 624 | *
|
---|
| 625 | * @param interaction
|
---|
| 626 | * interaction for which the score is calculated
|
---|
| 627 | * @param usageProfile
|
---|
| 628 | * usage profile used for the calculation
|
---|
| 629 | * @return calculated usage score
|
---|
| 630 | */
|
---|
[1763] | 631 | public static double calculateUsageScore(Interaction interaction,
|
---|
| 632 | IStochasticProcess usageProfile)
|
---|
| 633 | {
|
---|
[1643] | 634 | double usageScore = 0.0d;
|
---|
| 635 | EList<InteractionFragment> interactionFragments = interaction.getFragments();
|
---|
| 636 | List<Event> eventSequence = new LinkedList<>();
|
---|
| 637 | eventSequence.add(Event.STARTEVENT);
|
---|
| 638 | for (InteractionFragment interactionFragment : interactionFragments) {
|
---|
[1929] | 639 | if (interactionFragment instanceof MessageOccurrenceSpecification) {
|
---|
| 640 | Message message =
|
---|
| 641 | ((MessageOccurrenceSpecification) interactionFragment).getMessage();
|
---|
| 642 | if (message.getReceiveEvent().equals(interactionFragment) && isCallMessage(message))
|
---|
| 643 | {
|
---|
| 644 | String clientName =
|
---|
| 645 | ((MessageOccurrenceSpecification) message.getSendEvent()).getCovereds()
|
---|
| 646 | .get(0).getName();
|
---|
| 647 | String serviceName =
|
---|
| 648 | ((MessageOccurrenceSpecification) message.getReceiveEvent()).getCovereds()
|
---|
| 649 | .get(0).getName();
|
---|
| 650 | String methodName = message.getSignature().getName();
|
---|
| 651 | eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName,
|
---|
| 652 | clientName, null)));
|
---|
[1643] | 653 | }
|
---|
| 654 | }
|
---|
| 655 | }
|
---|
[1763] | 656 | eventSequence.add(Event.ENDEVENT);
|
---|
[1643] | 657 | double prob = usageProfile.getLogSum(eventSequence);
|
---|
[1763] | 658 | usageScore = eventSequence.size() * prob;
|
---|
[1643] | 659 |
|
---|
| 660 | return usageScore;
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | /**
|
---|
| 664 | * <p>
|
---|
[1763] | 665 | * Extends the given model with an activity for usage-based scheduling of the test cases.
|
---|
| 666 | * </p>
|
---|
| 667 | *
|
---|
| 668 | * @param model
|
---|
| 669 | * model to be extended
|
---|
| 670 | * @param usageProfile
|
---|
[1897] | 671 | * usage profile used as foundation
|
---|
[1763] | 672 | */
|
---|
[1897] | 673 | public static void createScheduling(Model model,
|
---|
| 674 | IStochasticProcess usageProfile,
|
---|
| 675 | String testContextName)
|
---|
| 676 | {
|
---|
[1929] | 677 | final Component testContext = fetchTestContext(model, testContextName);
|
---|
[1897] | 678 | if (testContext == null) {
|
---|
[1896] | 679 | throw new RuntimeException("Could not find any test context in the model");
|
---|
| 680 | }
|
---|
[1763] | 681 |
|
---|
| 682 | Map<Operation, Double> usageScoreMapUnsorted = new HashMap<>();
|
---|
| 683 |
|
---|
| 684 | // first, we determine all test cases and calculate their usage scores
|
---|
[1929] | 685 | final Stereotype utpTestCase = UTPUtils.getTestCaseStereotype(model);
|
---|
[1763] | 686 | for (Operation operation : testContext.getAllOperations()) {
|
---|
[1929] | 687 | if (operation.getAppliedStereotypes().contains(utpTestCase) &&
|
---|
| 688 | !operation.getMethods().isEmpty())
|
---|
| 689 | {
|
---|
[1763] | 690 | Interaction interaction = (Interaction) operation.getMethods().get(0);
|
---|
| 691 | usageScoreMapUnsorted
|
---|
| 692 | .put(operation, calculateUsageScore(interaction, usageProfile));
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 | Map<Operation, Double> usageScoreMapSorted = sortByValue(usageScoreMapUnsorted);
|
---|
| 696 |
|
---|
| 697 | // now we create the scheduling
|
---|
| 698 | Activity schedulingActivity =
|
---|
| 699 | (Activity) testContext.createOwnedBehavior("UsageBasedScheduling",
|
---|
| 700 | UMLPackage.Literals.ACTIVITY);
|
---|
| 701 | testContext.setClassifierBehavior(schedulingActivity);
|
---|
| 702 |
|
---|
| 703 | ActivityNode startNode =
|
---|
| 704 | schedulingActivity.createOwnedNode("final", UMLPackage.Literals.INITIAL_NODE);
|
---|
| 705 | ActivityNode finalNode =
|
---|
| 706 | schedulingActivity.createOwnedNode("final", UMLPackage.Literals.ACTIVITY_FINAL_NODE);
|
---|
| 707 |
|
---|
| 708 | ActivityNode currentOperationNode = startNode;
|
---|
| 709 |
|
---|
| 710 | for (Entry<Operation, Double> entry : usageScoreMapSorted.entrySet()) {
|
---|
| 711 | Operation operation = entry.getKey();
|
---|
| 712 | CallOperationAction nextOperationNode =
|
---|
| 713 | (CallOperationAction) schedulingActivity
|
---|
| 714 | .createOwnedNode(operation.getName(), UMLPackage.Literals.CALL_OPERATION_ACTION);
|
---|
| 715 | nextOperationNode.setOperation(operation);
|
---|
| 716 |
|
---|
| 717 | ActivityEdge edge =
|
---|
| 718 | schedulingActivity.createEdge(currentOperationNode.getName() + "_to_" +
|
---|
| 719 | nextOperationNode.getName(), UMLPackage.Literals.CONTROL_FLOW);
|
---|
| 720 | edge.setSource(currentOperationNode);
|
---|
| 721 | edge.setTarget(nextOperationNode);
|
---|
| 722 |
|
---|
| 723 | currentOperationNode = nextOperationNode;
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | ActivityEdge edge =
|
---|
| 727 | schedulingActivity
|
---|
| 728 | .createEdge(currentOperationNode.getName() + "_to_" + finalNode.getName(),
|
---|
| 729 | UMLPackage.Literals.CONTROL_FLOW);
|
---|
| 730 | edge.setSource(currentOperationNode);
|
---|
| 731 | edge.setTarget(finalNode);
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | /**
|
---|
| 735 | * <p>
|
---|
[1624] | 736 | * Fetches an operation using only its name from a list of operations. Returns the first match
|
---|
| 737 | * found or null if no match is found.
|
---|
| 738 | * </p>
|
---|
| 739 | *
|
---|
| 740 | * @param operations
|
---|
| 741 | * list of operations
|
---|
| 742 | * @param name
|
---|
| 743 | * name of the operation
|
---|
| 744 | * @return first matching operation; null if no match is found
|
---|
| 745 | */
|
---|
| 746 | private static Operation getOperationFromName(EList<Operation> operations, String name) {
|
---|
| 747 | if (name == null) {
|
---|
| 748 | throw new IllegalArgumentException("name of the operation must not be null");
|
---|
| 749 | }
|
---|
| 750 | if (operations != null) {
|
---|
| 751 | for (Operation operation : operations) {
|
---|
| 752 | if (operation.getName() != null && operation.getName().equals(name)) {
|
---|
| 753 | return operation;
|
---|
| 754 | }
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
| 757 | return null;
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | /**
|
---|
| 761 | * <p>
|
---|
| 762 | * Determines which transitions match a given {@link SOAPEventType}.
|
---|
| 763 | * </p>
|
---|
| 764 | *
|
---|
| 765 | * @param transitions
|
---|
| 766 | * the transitions
|
---|
| 767 | * @param eventType
|
---|
| 768 | * the SOAP event
|
---|
| 769 | * @return matching transitions
|
---|
| 770 | */
|
---|
[1926] | 771 | private static List<Transition> matchTransitions(List<Transition> transitions, Event event) {
|
---|
| 772 | String eventService = SOAPUtils.getServiceNameFromEvent(event);
|
---|
| 773 | String eventMethod = SOAPUtils.getCalledMethodFromEvent(event);
|
---|
| 774 |
|
---|
| 775 | Map<Interface, String> interfaceServiceMap =
|
---|
| 776 | createInterfaceServiceMap(transitions.get(0).getModel());
|
---|
| 777 |
|
---|
[1604] | 778 | List<Transition> matching = new LinkedList<>();
|
---|
| 779 | for (Transition transition : transitions) {
|
---|
[1926] | 780 | EList<Trigger> triggers = transition.getTriggers();
|
---|
| 781 | if (triggers.size() == 1) {
|
---|
| 782 | if (triggers.get(0).getEvent() instanceof CallEvent) {
|
---|
| 783 | CallEvent callEvent = (CallEvent) triggers.get(0).getEvent();
|
---|
| 784 | String transitionMethod = callEvent.getOperation().getName();
|
---|
| 785 |
|
---|
| 786 | Interface intface = callEvent.getOperation().getInterface();
|
---|
| 787 | for (Relationship relationship : intface.getRelationships()) {
|
---|
| 788 | for (Element element : relationship.getRelatedElements()) {
|
---|
| 789 | if (element instanceof Component) {
|
---|
| 790 |
|
---|
| 791 | }
|
---|
| 792 | }
|
---|
| 793 | }
|
---|
| 794 |
|
---|
| 795 | String transitionService =
|
---|
| 796 | interfaceServiceMap.get(callEvent.getOperation().getInterface());
|
---|
| 797 | if (eventMethod.equals(transitionMethod) &&
|
---|
| 798 | eventService.equals(transitionService))
|
---|
| 799 | {
|
---|
| 800 | matching.add(transition);
|
---|
| 801 | }
|
---|
| 802 | }
|
---|
[1604] | 803 | }
|
---|
[1926] | 804 |
|
---|
[1604] | 805 | }
|
---|
| 806 | return matching;
|
---|
| 807 | }
|
---|
[1897] | 808 |
|
---|
[1929] | 809 | /**
|
---|
| 810 | * <p>
|
---|
| 811 | * Fetches all realized interfaces from the type of a UML {@link Property} (i.e.,
|
---|
| 812 | * property.getType()). If no interfaces are realized, an empty set is returned.
|
---|
| 813 | * </p>
|
---|
| 814 | *
|
---|
| 815 | * @param property
|
---|
| 816 | * property, of the whose realized interfaces of the type are determined
|
---|
| 817 | * @return realized interfaces
|
---|
| 818 | */
|
---|
[1898] | 819 | private static Set<Interface> getRealizedInterfacesFromProperty(Property property) {
|
---|
[1763] | 820 | return getRealizedInterfaceFromComponent((Component) property.getType());
|
---|
| 821 | }
|
---|
[1897] | 822 |
|
---|
[1929] | 823 | /**
|
---|
| 824 | * <p>
|
---|
| 825 | * Fetches all realized interfaces from a UML {@link Component}. If no interfaces are realized,
|
---|
| 826 | * an empty set is returned.
|
---|
| 827 | * </p>
|
---|
| 828 | *
|
---|
| 829 | * @param comp
|
---|
| 830 | * component whose realized interfaces are determined
|
---|
| 831 | * @return realized interfaces
|
---|
| 832 | */
|
---|
| 833 | private static Set<Interface> getRealizedInterfaceFromComponent(Component component) {
|
---|
[1898] | 834 | Set<Interface> interfaces = new HashSet<>();
|
---|
[1897] | 835 | // Interface myInterface = null;
|
---|
[1929] | 836 | for (Property property : component.getAttributes()) {
|
---|
[1897] | 837 | if (property instanceof Port) {
|
---|
[1835] | 838 | Port port = (Port) property;
|
---|
[1897] | 839 | if (!port.isConjugated()) {
|
---|
[1900] | 840 | interfaces.addAll(port.getProvideds());
|
---|
[1835] | 841 | }
|
---|
| 842 | }
|
---|
| 843 | }
|
---|
[1896] | 844 | return interfaces;
|
---|
[1763] | 845 | }
|
---|
[1897] | 846 |
|
---|
[1929] | 847 | /**
|
---|
| 848 | * <p>
|
---|
| 849 | * Determines searches within a {@link Package} for a component to which the UTP TestContext
|
---|
| 850 | * stereotype is applied.
|
---|
| 851 | * <ul>
|
---|
| 852 | * <li>If no testContextName is provided, the first test context found is returned.</li>
|
---|
| 853 | * <li>In case no test context is found, null is returned.</li>
|
---|
| 854 | * </p>
|
---|
| 855 | *
|
---|
| 856 | * @param pkg
|
---|
| 857 | * package where the test context is being locked for
|
---|
| 858 | * @param testContextName
|
---|
| 859 | * name of the test context; in case no test name is specified, use null and not the
|
---|
| 860 | * empty String.
|
---|
| 861 | * @return {@link Component} to which the TestContext stereotype is applied
|
---|
| 862 | */
|
---|
| 863 | private static Component fetchTestContext(final Package pkg, final String testContextName) {
|
---|
| 864 | List<Component> testContexts = fetchAllTestContexts(pkg);
|
---|
[1897] | 865 | if (testContexts.isEmpty()) {
|
---|
[1896] | 866 | return null;
|
---|
| 867 | }
|
---|
[1897] | 868 | if (testContextName != null) {
|
---|
| 869 | for (Component testContext : testContexts) {
|
---|
| 870 | if (testContextName.equals(testContext.getName())) {
|
---|
[1896] | 871 | return testContext;
|
---|
| 872 | }
|
---|
[1835] | 873 | }
|
---|
[1896] | 874 | return null;
|
---|
[1897] | 875 | }
|
---|
| 876 | else {
|
---|
[1896] | 877 | return testContexts.get(0);
|
---|
[1835] | 878 | }
|
---|
| 879 | }
|
---|
[1897] | 880 |
|
---|
[1929] | 881 | /**
|
---|
| 882 | * <p>
|
---|
| 883 | * Retrieves all UML {@link Component}s to which the UTP TestContext stereotype is applied from
|
---|
| 884 | * a package. This method calls itself recursively to include all components contained in
|
---|
| 885 | * sub-packages.
|
---|
| 886 | * </p>
|
---|
| 887 | * <p>
|
---|
| 888 | * In case no test context is found, an empty list is returned.
|
---|
| 889 | * </p>
|
---|
| 890 | *
|
---|
| 891 | * @param pkg
|
---|
| 892 | * package from which the test contexts are retrieved
|
---|
| 893 | * @return {@link List} of test contexts
|
---|
| 894 | */
|
---|
| 895 | private static List<Component> fetchAllTestContexts(final Package pkg) {
|
---|
| 896 | final Stereotype utpTestContext = UTPUtils.getTestContextStereotype(pkg.getModel());
|
---|
| 897 | final List<Component> testContexts = new LinkedList<>();
|
---|
[1897] | 898 | for (Element element : pkg.getOwnedElements()) {
|
---|
| 899 | if (element instanceof Package) {
|
---|
[1929] | 900 | testContexts.addAll(fetchAllTestContexts((Package) element));
|
---|
[1896] | 901 | }
|
---|
[1897] | 902 | if (element instanceof Component &&
|
---|
| 903 | element.getAppliedStereotypes().contains(utpTestContext))
|
---|
| 904 | {
|
---|
[1896] | 905 | testContexts.add((Component) element);
|
---|
| 906 | }
|
---|
| 907 | }
|
---|
| 908 | return testContexts;
|
---|
| 909 | }
|
---|
[1897] | 910 |
|
---|
| 911 | /**
|
---|
| 912 | * <p>
|
---|
[1929] | 913 | * Retrieves all properties that represent a UTP TestComponent from a test context.
|
---|
[1897] | 914 | * </p>
|
---|
| 915 | *
|
---|
[1929] | 916 | * @param testContext
|
---|
| 917 | * test context from which the properties are retrieved
|
---|
| 918 | * @return properties that represent test components
|
---|
| 919 | */
|
---|
| 920 | private static Set<Property> fetchAllTestComponentProperties(final Component testContext) {
|
---|
| 921 | // fetch all SUTs and TestComponents
|
---|
| 922 | final Stereotype utpTestComponent =
|
---|
| 923 | UTPUtils.getTestComponentStereotype(testContext.getModel());
|
---|
| 924 | final Set<Property> properties = new HashSet<>();
|
---|
| 925 | for (Property property : testContext.getAllAttributes()) {
|
---|
| 926 | if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
|
---|
| 927 | properties.add(property);
|
---|
| 928 | }
|
---|
| 929 | }
|
---|
| 930 | return properties;
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 | /**
|
---|
| 934 | * <p>
|
---|
| 935 | * Retrieves all properties that represent a UTP SUT from a test context.
|
---|
| 936 | * </p>
|
---|
| 937 | *
|
---|
| 938 | * @param testContext
|
---|
| 939 | * test context from which the properties are retrieved
|
---|
| 940 | * @return properties that represent the SUTs
|
---|
| 941 | */
|
---|
| 942 | private static Set<Property> fetchAllSUTProperties(final Component testContext) {
|
---|
| 943 | // fetch all SUTs and TestComponents
|
---|
| 944 | final Stereotype utpSUT = UTPUtils.getSUTStereotype(testContext.getModel());
|
---|
| 945 | final Set<Property> properties = new HashSet<>();
|
---|
| 946 | for (Property property : testContext.getAllAttributes()) {
|
---|
| 947 | if (property.getAppliedStereotypes().contains(utpSUT)) {
|
---|
| 948 | properties.add(property);
|
---|
| 949 | }
|
---|
| 950 | }
|
---|
| 951 | return properties;
|
---|
| 952 | }
|
---|
| 953 |
|
---|
| 954 | /**
|
---|
| 955 | * <p>
|
---|
| 956 | * Infers connector between two lifelines.
|
---|
| 957 | * </p>
|
---|
| 958 | * <p>
|
---|
| 959 | * TODO: Currently assumes only one connector between two lifelines possible. This assumption is
|
---|
| 960 | * invalid as soon as there are two ports that connect the same two properties.
|
---|
| 961 | * </p>
|
---|
| 962 | *
|
---|
| 963 | * @param msgSourceLifeline
|
---|
| 964 | * source lifeline of the message
|
---|
[1897] | 965 | * @param targetAttributes
|
---|
[1929] | 966 | * target lifeline of the message
|
---|
[1897] | 967 | */
|
---|
| 968 | private static Connector inferConnector(Lifeline msgSourceLifeline, Lifeline msgTargetLifeline)
|
---|
| 969 | {
|
---|
| 970 | EList<Property> userAttributes =
|
---|
| 971 | ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
|
---|
| 972 | EList<Property> targetAttributes =
|
---|
| 973 | ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
|
---|
| 974 | for (Property userAttribute : userAttributes) {
|
---|
| 975 | if (userAttribute instanceof Port) {
|
---|
| 976 | EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
|
---|
| 977 | for (ConnectorEnd userEnd : userEnds) {
|
---|
| 978 | Connector userConnector = (Connector) userEnd.eContainer();
|
---|
| 979 | for (Property targetAttribute : targetAttributes) {
|
---|
| 980 | if (targetAttribute instanceof Port) {
|
---|
| 981 | EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
|
---|
| 982 | for (ConnectorEnd targetEnd : targetEnds) {
|
---|
| 983 | Connector targetConnector = (Connector) targetEnd.eContainer();
|
---|
| 984 | if (targetConnector == userConnector) {
|
---|
| 985 | return targetConnector;
|
---|
| 986 | }
|
---|
| 987 | }
|
---|
| 988 | }
|
---|
| 989 | }
|
---|
| 990 | }
|
---|
| 991 | }
|
---|
| 992 | }
|
---|
| 993 | return null;
|
---|
| 994 | }
|
---|
[1926] | 995 |
|
---|
[1929] | 996 | /**
|
---|
| 997 | * <p>
|
---|
| 998 | * Creates a map that maps the interfaces to the properties, i.e., services that they are
|
---|
| 999 | * represented by.
|
---|
| 1000 | * </p>
|
---|
| 1001 | * <p>
|
---|
| 1002 | * TODO: currently assumes that each interfaces is only realized by one property
|
---|
| 1003 | * </p>
|
---|
| 1004 | *
|
---|
| 1005 | * @param model
|
---|
| 1006 | * model for which the interface->service map is created
|
---|
| 1007 | * @return the map
|
---|
| 1008 | */
|
---|
[1926] | 1009 | private static Map<Interface, String> createInterfaceServiceMap(Model model) {
|
---|
| 1010 | Map<Interface, String> interfaceServiceMap = new HashMap<>();
|
---|
[1929] | 1011 | List<Component> testContexts = fetchAllTestContexts(model.getModel());
|
---|
[1926] | 1012 | for (Component testContext : testContexts) {
|
---|
[1929] | 1013 | for (Property property : fetchAllSUTProperties(testContext)) {
|
---|
| 1014 | for (Interface intface : getRealizedInterfacesFromProperty(property)) {
|
---|
| 1015 | interfaceServiceMap.put(intface, property.getName());
|
---|
[1926] | 1016 | }
|
---|
| 1017 | }
|
---|
[1929] | 1018 | for (Property property : fetchAllTestComponentProperties(testContext)) {
|
---|
| 1019 | for (Interface intface : getRealizedInterfacesFromProperty(property)) {
|
---|
| 1020 | interfaceServiceMap.put(intface, property.getName());
|
---|
| 1021 | }
|
---|
| 1022 | }
|
---|
[1926] | 1023 | }
|
---|
| 1024 | return interfaceServiceMap;
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
[1929] | 1027 | /**
|
---|
| 1028 | * <p>
|
---|
| 1029 | * Sets values for the parameters of a call message. The values are, if possible, inferred from
|
---|
| 1030 | * the event that is provided.
|
---|
| 1031 | * </p>
|
---|
| 1032 | *
|
---|
| 1033 | * @param callMessage
|
---|
| 1034 | * call message for which the parameters are set
|
---|
| 1035 | * @param calledOperation
|
---|
| 1036 | * operation that is called by the message
|
---|
| 1037 | * @param event
|
---|
| 1038 | * event that provides the parameters; in case of null, default values are assumed
|
---|
| 1039 | * @param prefix
|
---|
| 1040 | * prefix of the call message; used to create good warnings and debugging information
|
---|
| 1041 | */
|
---|
| 1042 | private static void setCallMessageParameters(Message callMessage,
|
---|
| 1043 | Operation calledOperation,
|
---|
| 1044 | Event event,
|
---|
| 1045 | String prefix)
|
---|
[1926] | 1046 | {
|
---|
| 1047 | org.w3c.dom.Element requestBody = SOAPUtils.getSoapRequestBodyFromEvent(event);
|
---|
[1929] | 1048 | Package instSpecPkg = null;
|
---|
[1926] | 1049 |
|
---|
[1908] | 1050 | // Set parameters of operation
|
---|
[1926] | 1051 | for (Parameter param : calledOperation.getOwnedParameters()) {
|
---|
[1929] | 1052 | if (instSpecPkg == null) {
|
---|
| 1053 | instSpecPkg = getOrCreateInstanceSpecificationPackage(param.getModel(), event);
|
---|
| 1054 | }
|
---|
| 1055 |
|
---|
| 1056 | String path = calledOperation.getName() + ":" + param.getName();
|
---|
[1926] | 1057 | Expression argument =
|
---|
| 1058 | (Expression) callMessage.createArgument(param.getName(), param.getType(),
|
---|
| 1059 | UMLPackage.Literals.EXPRESSION);
|
---|
| 1060 |
|
---|
[1929] | 1061 | if (isInParameter(param)) {
|
---|
[1926] | 1062 | if (param.getType() instanceof DataType) {
|
---|
| 1063 | List<org.w3c.dom.Element> paramNodes =
|
---|
[1929] | 1064 | SOAPUtils.getMatchingChildNode(param.getType().getName(), requestBody);
|
---|
| 1065 | int multiplicityChosen = paramNodes.size();
|
---|
| 1066 |
|
---|
| 1067 | if (multiplicityChosen == 0 && param.getLower() > 0) {
|
---|
| 1068 | Console.traceln(Level.WARNING,
|
---|
| 1069 | "required attribute not found in SOAP message: " + path);
|
---|
| 1070 | Console
|
---|
| 1071 | .traceln(Level.WARNING,
|
---|
| 1072 | "setting default values for this attribute and all its children");
|
---|
| 1073 | Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
|
---|
| 1074 | SOAPUtils.getSerialization(requestBody));
|
---|
| 1075 | multiplicityChosen = param.getLower();
|
---|
| 1076 | }
|
---|
| 1077 | for (int i = 0; i < multiplicityChosen; i++) {
|
---|
| 1078 | org.w3c.dom.Element paramNode = null;
|
---|
| 1079 | if (!paramNodes.isEmpty()) {
|
---|
| 1080 | paramNode = paramNodes.get(i);
|
---|
| 1081 | }
|
---|
[1926] | 1082 | InstanceSpecification instSpec =
|
---|
[1929] | 1083 | createInstanceSpecification((DataType) param.getType(), instSpecPkg,
|
---|
| 1084 | prefix, paramNode, path);
|
---|
[1926] | 1085 |
|
---|
| 1086 | InstanceValue value =
|
---|
| 1087 | (InstanceValue) argument
|
---|
| 1088 | .createOperand(null, param.getType(),
|
---|
| 1089 | UMLPackage.Literals.INSTANCE_VALUE);
|
---|
| 1090 | value.setInstance(instSpec);
|
---|
[1908] | 1091 | }
|
---|
| 1092 | }
|
---|
[1926] | 1093 | else if (param.getType() instanceof PrimitiveType) {
|
---|
[1929] | 1094 | createOperandPrimitiveType(param, argument, requestBody, path);
|
---|
[1926] | 1095 | }
|
---|
| 1096 | }
|
---|
| 1097 | else {
|
---|
[1908] | 1098 | // set literalNull for out and return parameters
|
---|
| 1099 | argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
|
---|
| 1100 | }
|
---|
| 1101 | }
|
---|
| 1102 | }
|
---|
[1926] | 1103 |
|
---|
[1929] | 1104 | /**
|
---|
| 1105 | * <p>
|
---|
| 1106 | * Creates an {@link InstanceSpecification} for a data type in the given package. The values are
|
---|
| 1107 | * inferred, if possible, from the DOM node. The prefix and the path are used for naming the
|
---|
| 1108 | * instance specification and to provide good warnings and debug information in case of
|
---|
| 1109 | * problems.
|
---|
| 1110 | * </p>
|
---|
| 1111 | *
|
---|
| 1112 | * @param type
|
---|
| 1113 | * DataType for which the {@link InstanceSpecification} is created
|
---|
| 1114 | * @param pkg
|
---|
| 1115 | * package in which the {@link InstanceSpecification} is created
|
---|
| 1116 | * @param prefix
|
---|
| 1117 | * prefix used for naming the {@link InstanceSpecification}
|
---|
| 1118 | * @param currentNode
|
---|
| 1119 | * node of a DOM from which values are inferred
|
---|
| 1120 | * @param path
|
---|
| 1121 | * used for warnings and debug information
|
---|
| 1122 | * @return {@link InstanceSpecification} for the given type
|
---|
| 1123 | */
|
---|
[1926] | 1124 | private static InstanceSpecification createInstanceSpecification(DataType type,
|
---|
[1929] | 1125 | Package pkg,
|
---|
[1926] | 1126 | String prefix,
|
---|
| 1127 | org.w3c.dom.Element currentNode,
|
---|
| 1128 | String path)
|
---|
| 1129 | {
|
---|
| 1130 | if ("".equals(path)) {
|
---|
| 1131 | path = type.getName();
|
---|
| 1132 | }
|
---|
| 1133 |
|
---|
| 1134 | InstanceSpecification instSpec =
|
---|
[1929] | 1135 | (InstanceSpecification) pkg
|
---|
| 1136 | .createPackagedElement(prefix + "instspec_" + type.getName(),
|
---|
| 1137 | UMLPackage.Literals.INSTANCE_SPECIFICATION);
|
---|
[1908] | 1138 | instSpec.getClassifiers().add(type);
|
---|
[1926] | 1139 | for (Property prop : type.getAllAttributes()) {
|
---|
| 1140 | if (prop.getType() instanceof PrimitiveType) {
|
---|
| 1141 | createSlotPrimitiveType(instSpec, prop, currentNode, path);
|
---|
| 1142 | }
|
---|
| 1143 | else if (prop.getType() instanceof DataType) {
|
---|
| 1144 | List<org.w3c.dom.Element> attributeNodes = null;
|
---|
| 1145 | int multiplicityChosen = 0;
|
---|
| 1146 | if (currentNode != null) {
|
---|
[1929] | 1147 | attributeNodes = SOAPUtils.getMatchingChildNode(prop.getName(), currentNode);
|
---|
[1926] | 1148 | multiplicityChosen = attributeNodes.size();
|
---|
| 1149 | }
|
---|
| 1150 |
|
---|
| 1151 | if (multiplicityChosen == 0 && prop.getLower() > 0) {
|
---|
| 1152 | if (currentNode != null) {
|
---|
| 1153 | Console.traceln(Level.WARNING,
|
---|
| 1154 | "required attribute not found in SOAP message: " + path +
|
---|
| 1155 | "." + prop.getName());
|
---|
| 1156 | Console
|
---|
| 1157 | .traceln(Level.WARNING,
|
---|
| 1158 | "setting default values for this attribute and all its children");
|
---|
| 1159 | Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
|
---|
| 1160 | SOAPUtils.getSerialization(currentNode));
|
---|
| 1161 | }
|
---|
| 1162 | multiplicityChosen = prop.getLower();
|
---|
| 1163 | }
|
---|
| 1164 | for (int i = 0; i < multiplicityChosen; i++) {
|
---|
| 1165 | org.w3c.dom.Element attributeNode = null;
|
---|
| 1166 | if (attributeNodes != null && !attributeNodes.isEmpty()) {
|
---|
| 1167 | attributeNode = attributeNodes.get(i);
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
| 1170 | Slot slot = instSpec.createSlot();
|
---|
| 1171 | slot.setDefiningFeature(prop);
|
---|
| 1172 |
|
---|
| 1173 | InstanceValue value =
|
---|
| 1174 | (InstanceValue) slot.createValue(prop.getName() + "_" + i, prop.getType(),
|
---|
| 1175 | UMLPackage.Literals.INSTANCE_VALUE);
|
---|
[1929] | 1176 | value.setInstance(createInstanceSpecification((DataType) prop.getType(), pkg,
|
---|
[1926] | 1177 | prefix, attributeNode, path +
|
---|
| 1178 | "." + prop.getName()));
|
---|
| 1179 | }
|
---|
| 1180 | }
|
---|
| 1181 | else {
|
---|
| 1182 | Console.traceln(Level.SEVERE, "property neither DataType nor PrimitiveType: " +
|
---|
| 1183 | prop.getType());
|
---|
| 1184 | // TODO abort?
|
---|
| 1185 | }
|
---|
| 1186 | }
|
---|
| 1187 | return instSpec;
|
---|
| 1188 | }
|
---|
| 1189 |
|
---|
[1929] | 1190 | /**
|
---|
| 1191 | * <p>
|
---|
| 1192 | * Gets or creates a {@link Package} for {@link InstanceSpecification} created by the
|
---|
| 1193 | * usage-based testing. Each service gets its own sub-package within a package called
|
---|
| 1194 | * UBT_InstanceSpecifications. "
|
---|
| 1195 | * </p>
|
---|
| 1196 | *
|
---|
| 1197 | * @param model
|
---|
| 1198 | * model in which the package is generated
|
---|
| 1199 | * @param event
|
---|
| 1200 | * event from which the service name is inferred
|
---|
| 1201 | * @return package for the {@link InstanceSpecification}s
|
---|
| 1202 | */
|
---|
| 1203 | private static Package getOrCreateInstanceSpecificationPackage(Model model, Event event) {
|
---|
| 1204 | String pkgUBTInstSpecs = "UBT_InstanceSpecifications";
|
---|
| 1205 | Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs);
|
---|
| 1206 | if (ubtInstSpecPkg == null) {
|
---|
| 1207 | ubtInstSpecPkg =
|
---|
| 1208 | (Package) model.createPackagedElement(pkgUBTInstSpecs, UMLPackage.Literals.PACKAGE);
|
---|
| 1209 | }
|
---|
| 1210 | String serviceName = SOAPUtils.getServiceNameFromEvent(event);
|
---|
| 1211 | Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName);
|
---|
| 1212 | if (serviceInstSpecPkg == null) {
|
---|
| 1213 | serviceInstSpecPkg =
|
---|
| 1214 | (Package) ubtInstSpecPkg.createPackagedElement(serviceName,
|
---|
| 1215 | UMLPackage.Literals.PACKAGE);
|
---|
| 1216 | }
|
---|
| 1217 | return serviceInstSpecPkg;
|
---|
| 1218 | }
|
---|
| 1219 |
|
---|
| 1220 | /**
|
---|
| 1221 | * <p>
|
---|
| 1222 | * Creates an operand that defines a {@link PrimitiveType}.
|
---|
| 1223 | * </p>
|
---|
| 1224 | * <p>
|
---|
| 1225 | * TODO: Currently does nothing in case of multiplicity 0. I am not sure if, in that case, one
|
---|
| 1226 | * has to define LiteralNull instead.
|
---|
| 1227 | * </p>
|
---|
| 1228 | *
|
---|
| 1229 | * @param param
|
---|
| 1230 | * parameter for which the operand is created
|
---|
| 1231 | * @param argument
|
---|
| 1232 | * argument to which the operand is added
|
---|
| 1233 | * @param currentNode
|
---|
| 1234 | * DOM node from which is value for the operand is inferred
|
---|
| 1235 | * @param path
|
---|
| 1236 | * used for warnings and debug information
|
---|
| 1237 | */
|
---|
[1926] | 1238 | private static void createOperandPrimitiveType(Parameter param,
|
---|
| 1239 | Expression argument,
|
---|
[1929] | 1240 | org.w3c.dom.Element currentNode,
|
---|
| 1241 | String path)
|
---|
[1926] | 1242 | {
|
---|
[1929] | 1243 | List<String> attributeValues = SOAPUtils.getValuesFromElement(param.getName(), currentNode);
|
---|
| 1244 |
|
---|
| 1245 | if (attributeValues.isEmpty()) {
|
---|
| 1246 | if (param.getLower() == 0) {
|
---|
| 1247 | // ignoring optional attribute
|
---|
| 1248 | return;
|
---|
| 1249 | }
|
---|
| 1250 | else {
|
---|
| 1251 | if (currentNode != null) {
|
---|
| 1252 | Console.traceln(Level.WARNING,
|
---|
| 1253 | "required attribute not found in SOAP message: " + path + "." +
|
---|
| 1254 | param.getName());
|
---|
| 1255 | Console.traceln(Level.WARNING, "setting default values for this attribute");
|
---|
| 1256 | Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
|
---|
| 1257 | SOAPUtils.getSerialization(currentNode));
|
---|
| 1258 | }
|
---|
| 1259 | attributeValues.add(null);
|
---|
| 1260 | }
|
---|
[1926] | 1261 | }
|
---|
[1929] | 1262 | for (String attributeValue : attributeValues) {
|
---|
| 1263 | if ("String".equals(param.getType().getName())) {
|
---|
| 1264 | LiteralString spec =
|
---|
| 1265 | (LiteralString) argument.createOperand(param.getName(), null,
|
---|
| 1266 | UMLPackage.Literals.LITERAL_STRING);
|
---|
| 1267 | if (attributeValue != null) {
|
---|
| 1268 | spec.setValue(attributeValue);
|
---|
| 1269 | }
|
---|
| 1270 | else {
|
---|
| 1271 | spec.setValue("foobar");
|
---|
| 1272 | }
|
---|
| 1273 | }
|
---|
| 1274 | else if ("Integer".equals(param.getType().getName())) {
|
---|
| 1275 | LiteralInteger spec =
|
---|
| 1276 | (LiteralInteger) argument.createOperand(param.getName(), null,
|
---|
| 1277 | UMLPackage.Literals.LITERAL_INTEGER);
|
---|
| 1278 | if (attributeValue != null) {
|
---|
| 1279 | spec.setValue(Integer.parseInt(attributeValue));
|
---|
| 1280 | }
|
---|
| 1281 | else {
|
---|
| 1282 | spec.setValue(42);
|
---|
| 1283 | }
|
---|
| 1284 | }
|
---|
| 1285 | else if ("Boolean".equals(param.getType().getName())) {
|
---|
| 1286 | LiteralBoolean spec =
|
---|
| 1287 | (LiteralBoolean) argument.createOperand(param.getName(), null,
|
---|
| 1288 | UMLPackage.Literals.LITERAL_BOOLEAN);
|
---|
| 1289 | if (attributeValue != null) {
|
---|
| 1290 | spec.setValue(Boolean.parseBoolean(attributeValue));
|
---|
| 1291 | }
|
---|
| 1292 | else {
|
---|
| 1293 | spec.setValue(true);
|
---|
| 1294 | }
|
---|
| 1295 | }
|
---|
| 1296 | else if ("Real".equals(param.getType().getName())) {
|
---|
| 1297 | LiteralReal spec =
|
---|
| 1298 | (LiteralReal) argument.createOperand(param.getName(), null,
|
---|
| 1299 | UMLPackage.Literals.LITERAL_REAL);
|
---|
| 1300 | if (attributeValue != null) {
|
---|
| 1301 | spec.setValue(Double.parseDouble(attributeValue));
|
---|
| 1302 | }
|
---|
| 1303 | else {
|
---|
| 1304 | spec.setValue(3.14);
|
---|
| 1305 | }
|
---|
| 1306 | }
|
---|
[1926] | 1307 | }
|
---|
| 1308 | }
|
---|
| 1309 |
|
---|
[1929] | 1310 | /**
|
---|
| 1311 | * <p>
|
---|
| 1312 | * Creates a {@link Slot} in an {@link InstanceSpecification} for a primitive type.
|
---|
| 1313 | * </p>
|
---|
| 1314 | *
|
---|
| 1315 | * @param instSpec
|
---|
| 1316 | * instance specification to which the slot is added
|
---|
| 1317 | * @param prop
|
---|
| 1318 | * property that describes the slot
|
---|
| 1319 | * @param currentNode
|
---|
| 1320 | * DOM node from which is value for the slot is inferred
|
---|
| 1321 | * @param path
|
---|
| 1322 | * used for warnings and debug information
|
---|
| 1323 | */
|
---|
[1926] | 1324 | private static void createSlotPrimitiveType(InstanceSpecification instSpec,
|
---|
| 1325 | Property prop,
|
---|
| 1326 | org.w3c.dom.Element currentNode,
|
---|
| 1327 | String path)
|
---|
| 1328 | {
|
---|
[1929] | 1329 | List<String> attributeValues = SOAPUtils.getValuesFromElement(prop.getName(), currentNode);
|
---|
[1926] | 1330 |
|
---|
| 1331 | if (attributeValues.isEmpty()) {
|
---|
| 1332 | if (prop.getLower() == 0) {
|
---|
| 1333 | // ignoring optional attribute
|
---|
| 1334 | return;
|
---|
| 1335 | }
|
---|
| 1336 | else {
|
---|
| 1337 | if (currentNode != null) {
|
---|
| 1338 | Console.traceln(Level.WARNING,
|
---|
| 1339 | "required attribute not found in SOAP message: " + path + "." +
|
---|
| 1340 | prop.getName());
|
---|
| 1341 | Console.traceln(Level.WARNING, "setting default values for this attribute");
|
---|
[1929] | 1342 | Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
|
---|
| 1343 | SOAPUtils.getSerialization(currentNode));
|
---|
[1926] | 1344 | }
|
---|
| 1345 | attributeValues.add(null);
|
---|
| 1346 | }
|
---|
| 1347 | }
|
---|
| 1348 | for (String attributeValue : attributeValues) {
|
---|
[1908] | 1349 | Slot slot = instSpec.createSlot();
|
---|
| 1350 | slot.setDefiningFeature(prop);
|
---|
[1926] | 1351 | if ("String".equals(prop.getType().getName())) {
|
---|
| 1352 | LiteralString value =
|
---|
| 1353 | (LiteralString) slot.createValue(prop.getName(), null,
|
---|
| 1354 | UMLPackage.Literals.LITERAL_STRING);
|
---|
| 1355 | if (attributeValue != null) {
|
---|
| 1356 | value.setValue(attributeValue);
|
---|
[1908] | 1357 | }
|
---|
[1926] | 1358 | else {
|
---|
| 1359 | value.setValue("foobar");
|
---|
[1908] | 1360 | }
|
---|
[1926] | 1361 | }
|
---|
| 1362 | else if ("Integer".equals(prop.getType().getName())) {
|
---|
| 1363 | LiteralInteger value =
|
---|
| 1364 | (LiteralInteger) slot.createValue(prop.getName(), null,
|
---|
| 1365 | UMLPackage.Literals.LITERAL_INTEGER);
|
---|
| 1366 | if (attributeValue != null) {
|
---|
| 1367 | value.setValue(Integer.parseInt(attributeValue));
|
---|
[1908] | 1368 | }
|
---|
[1926] | 1369 | else {
|
---|
| 1370 | value.setValue(42);
|
---|
| 1371 | }
|
---|
| 1372 | }
|
---|
| 1373 | else if ("Boolean".equals(prop.getType().getName())) {
|
---|
| 1374 | LiteralBoolean value =
|
---|
| 1375 | (LiteralBoolean) slot.createValue(prop.getName(), null,
|
---|
| 1376 | UMLPackage.Literals.LITERAL_BOOLEAN);
|
---|
| 1377 | if (attributeValue != null) {
|
---|
| 1378 | value.setValue(Boolean.parseBoolean(attributeValue));
|
---|
| 1379 | }
|
---|
| 1380 | else {
|
---|
| 1381 | value.setValue(true);
|
---|
| 1382 | }
|
---|
| 1383 | }
|
---|
| 1384 | else if ("Real".equals(prop.getType().getName())) {
|
---|
| 1385 | LiteralReal value =
|
---|
| 1386 | (LiteralReal) slot.createValue(prop.getName(), null,
|
---|
| 1387 | UMLPackage.Literals.LITERAL_REAL);
|
---|
| 1388 | if (attributeValue != null) {
|
---|
| 1389 | value.setValue(Double.parseDouble(attributeValue));
|
---|
| 1390 | }
|
---|
| 1391 | else {
|
---|
[1929] | 1392 | value.setValue(3.14);
|
---|
[1908] | 1393 | }
|
---|
| 1394 | }
|
---|
[1926] | 1395 | else {
|
---|
| 1396 | Console.traceln(Level.SEVERE, "could not create literal for primitive type: " +
|
---|
| 1397 | prop.getType().getName());
|
---|
| 1398 | // TODO abort?
|
---|
[1908] | 1399 | }
|
---|
| 1400 | }
|
---|
| 1401 | }
|
---|
[1926] | 1402 |
|
---|
[1929] | 1403 | /**
|
---|
| 1404 | * <p>
|
---|
| 1405 | * Sets values for the parameters of a reply message. The values are, all LiterealNull and to
|
---|
| 1406 | * the INOUT, OUT and REPLY parameters, the UTP stereotype LiteralAny is applied.
|
---|
| 1407 | * </p>
|
---|
| 1408 | *
|
---|
| 1409 | * @param replyMessage
|
---|
| 1410 | * reply message for which the parameters are set
|
---|
| 1411 | * @param calledOperation
|
---|
| 1412 | * operation that is replied for by the message
|
---|
| 1413 | */
|
---|
| 1414 | private static void setReplyMessageParameters(Message replyMessage, Operation calledOperation) {
|
---|
| 1415 | for (Parameter param : calledOperation.getOwnedParameters()) {
|
---|
| 1416 | Expression argument =
|
---|
| 1417 | (Expression) replyMessage.createArgument(param.getName(), param.getType(),
|
---|
| 1418 | UMLPackage.Literals.EXPRESSION);
|
---|
| 1419 | if (isOutParameter(param)) {
|
---|
| 1420 | ValueSpecification operand =
|
---|
| 1421 | argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
|
---|
| 1422 | operand.applyStereotype(UTPUtils.getLiteralAnyStereotype(param.getModel()));
|
---|
| 1423 | }
|
---|
| 1424 | else {
|
---|
| 1425 | argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
|
---|
| 1426 | }
|
---|
| 1427 | }
|
---|
[1926] | 1428 | }
|
---|
| 1429 |
|
---|
[1929] | 1430 | /**
|
---|
| 1431 | * <p>
|
---|
| 1432 | * Checks if a parameter has the direction IN or INOUT
|
---|
| 1433 | * </p>
|
---|
| 1434 | *
|
---|
| 1435 | * @param parameter
|
---|
| 1436 | * parameter that is checked
|
---|
| 1437 | * @return true if the direction is IN or INOUT; false otherwise
|
---|
| 1438 | */
|
---|
| 1439 | private static boolean isInParameter(Parameter parameter) {
|
---|
| 1440 | return parameter.getDirection() == ParameterDirectionKind.IN_LITERAL ||
|
---|
| 1441 | parameter.getDirection() == ParameterDirectionKind.INOUT_LITERAL;
|
---|
[1926] | 1442 | }
|
---|
| 1443 |
|
---|
[1929] | 1444 | /**
|
---|
| 1445 | * <p>
|
---|
| 1446 | * Checks if a parameter has the direction RETURN, OUT or INOUT
|
---|
| 1447 | * </p>
|
---|
| 1448 | *
|
---|
| 1449 | * @param parameter
|
---|
| 1450 | * parameter that is checked
|
---|
| 1451 | * @return true if the direction is RETURN, OUT, or INOUT; false otherwise
|
---|
| 1452 | */
|
---|
| 1453 | private static boolean isOutParameter(Parameter parameter) {
|
---|
| 1454 | return parameter.getDirection() == ParameterDirectionKind.RETURN_LITERAL ||
|
---|
| 1455 | parameter.getDirection() == ParameterDirectionKind.OUT_LITERAL ||
|
---|
| 1456 | parameter.getDirection() == ParameterDirectionKind.INOUT_LITERAL;
|
---|
| 1457 | }
|
---|
| 1458 |
|
---|
| 1459 | /**
|
---|
| 1460 | * <p>
|
---|
| 1461 | * Checks if the {@link MessageSort} of a message is a call message, i.e., ASYNCH_CALL or
|
---|
| 1462 | * SYNCH_CALL.
|
---|
| 1463 | * </p>
|
---|
| 1464 | *
|
---|
| 1465 | * @param message
|
---|
| 1466 | * message that is checked
|
---|
| 1467 | * @return true if the message is a call message; false otherwise
|
---|
| 1468 | */
|
---|
| 1469 | private static boolean isCallMessage(Message message) {
|
---|
| 1470 | if (message == null) {
|
---|
| 1471 | return false;
|
---|
[1926] | 1472 | }
|
---|
[1929] | 1473 | MessageSort msgSort = message.getMessageSort();
|
---|
| 1474 | return msgSort == MessageSort.ASYNCH_CALL_LITERAL ||
|
---|
| 1475 | msgSort == MessageSort.SYNCH_CALL_LITERAL;
|
---|
[1926] | 1476 | }
|
---|
| 1477 |
|
---|
[1929] | 1478 | /**
|
---|
| 1479 | * <p>
|
---|
| 1480 | * inverse-sorts the values of a map. Has been adapted from <a href=
|
---|
| 1481 | * "http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java"
|
---|
| 1482 | * >this</a> StackOverflow post.
|
---|
| 1483 | * </p>
|
---|
| 1484 | *
|
---|
| 1485 | * @param map
|
---|
| 1486 | * map whose values are sorted
|
---|
| 1487 | * @return sorted version of the map
|
---|
| 1488 | */
|
---|
| 1489 | private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
|
---|
| 1490 | // TODO possibly move to another class
|
---|
| 1491 | List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
|
---|
| 1492 | Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
|
---|
| 1493 | @Override
|
---|
| 1494 | public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
|
---|
| 1495 | return -1 * (o1.getValue()).compareTo(o2.getValue());
|
---|
| 1496 | }
|
---|
| 1497 | });
|
---|
[1926] | 1498 |
|
---|
[1929] | 1499 | Map<K, V> result = new LinkedHashMap<>();
|
---|
| 1500 | for (Map.Entry<K, V> entry : list) {
|
---|
| 1501 | result.put(entry.getKey(), entry.getValue());
|
---|
[1926] | 1502 | }
|
---|
[1929] | 1503 | return result;
|
---|
[1926] | 1504 | }
|
---|
[1604] | 1505 | }
|
---|