source: trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java @ 2001

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