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

Last change on this file since 1929 was 1929, checked in by sherbold, 9 years ago
  • refactored and commented UMLUtils
  • created UTPUtils
  • moved DOM related methods from UMLUtils to SOAPUtils
  • Property svn:mime-type set to text/plain
File size: 66.2 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.logging.Level;
29import java.util.Set;
30import java.util.TreeSet;
31
32import org.eclipse.emf.common.util.EList;
33import org.eclipse.uml2.uml.Activity;
34import org.eclipse.uml2.uml.ActivityEdge;
35import org.eclipse.uml2.uml.ActivityNode;
36import org.eclipse.uml2.uml.CallConcurrencyKind;
37import org.eclipse.uml2.uml.CallEvent;
38import org.eclipse.uml2.uml.CallOperationAction;
39import org.eclipse.uml2.uml.Comment;
40import org.eclipse.uml2.uml.Component;
41import org.eclipse.uml2.uml.Connector;
42import org.eclipse.uml2.uml.ConnectorEnd;
43import org.eclipse.uml2.uml.DataType;
44import org.eclipse.uml2.uml.Element;
45import org.eclipse.uml2.uml.Expression;
46import org.eclipse.uml2.uml.InstanceSpecification;
47import org.eclipse.uml2.uml.InstanceValue;
48import org.eclipse.uml2.uml.Interaction;
49import org.eclipse.uml2.uml.InteractionFragment;
50import org.eclipse.uml2.uml.Interface;
51import org.eclipse.uml2.uml.Lifeline;
52import org.eclipse.uml2.uml.LiteralBoolean;
53import org.eclipse.uml2.uml.LiteralInteger;
54import org.eclipse.uml2.uml.LiteralReal;
55import org.eclipse.uml2.uml.LiteralString;
56import org.eclipse.uml2.uml.Message;
57import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
58import org.eclipse.uml2.uml.MessageSort;
59import org.eclipse.uml2.uml.Model;
60import org.eclipse.uml2.uml.Operation;
61import org.eclipse.uml2.uml.Package;
62import org.eclipse.uml2.uml.Parameter;
63import org.eclipse.uml2.uml.ParameterDirectionKind;
64import org.eclipse.uml2.uml.Port;
65import org.eclipse.uml2.uml.PrimitiveType;
66import org.eclipse.uml2.uml.Property;
67import org.eclipse.uml2.uml.Region;
68import org.eclipse.uml2.uml.Relationship;
69import org.eclipse.uml2.uml.Slot;
70import org.eclipse.uml2.uml.StateMachine;
71import org.eclipse.uml2.uml.Stereotype;
72import org.eclipse.uml2.uml.Transition;
73import org.eclipse.uml2.uml.Trigger;
74import org.eclipse.uml2.uml.UMLPackage;
75import org.eclipse.uml2.uml.ValueSpecification;
76import org.eclipse.uml2.uml.Vertex;
77
78import de.ugoe.cs.autoquest.eventcore.Event;
79import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
80import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
81import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
82import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType;
83import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
84import de.ugoe.cs.util.StringTools;
85import de.ugoe.cs.util.console.Console;
86
87/**
88 * <p>
89 * Utilities for working with UML.
90 * </p>
91 *
92 * @author Steffen Herbold
93 */
94public class UMLUtils {
95
96    /**
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    /**
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;
120        Component testContext = fetchTestContext(model, testContextName);
121        if (testContext == null) {
122            violationCount++;
123            if (testContextName == null) {
124                Console.traceln(Level.SEVERE, "Could not find any TestContext in the model.");
125
126            }
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;
136        }
137
138        // Create list of unique methods calls
139        HashMap<String, Set<String>> calledMethods = new HashMap<>();
140        for (List<Event> sequence : sequences) {
141            for (Event event : sequence) {
142                String serviceName = SOAPUtils.getServiceNameFromEvent(event);
143                String calledMethod = SOAPUtils.getCalledMethodFromEvent(event);
144                if (serviceName != null) {
145                    Set<String> curCalledMethods = calledMethods.get(serviceName);
146                    if (curCalledMethods == null) {
147                        curCalledMethods = new TreeSet<>();
148                        calledMethods.put(serviceName, curCalledMethods);
149                    }
150                    curCalledMethods.add(calledMethod);
151                }
152            }
153        }
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()) {
158            Console.traceln(Level.INFO, "\tService \"" + entry.getKey() + "\": ");
159            for (String method : entry.getValue()) {
160                Console.traceln(Level.INFO, "\t\t" + method);
161            }
162        }
163
164        // fetch all SUTs and TestComponents
165        HashMap<String, Property> properties = new HashMap<>();
166        for (Property property : fetchAllSUTProperties(testContext)) {
167            properties.put(property.getName(), property);
168        }
169        for (Property property : fetchAllTestComponentProperties(testContext)) {
170            properties.put(property.getName(), property);
171        }
172        Console.traceln(Level.INFO, "Found the following services in the TestConfiguration:");
173        for (Entry<String, Property> entry : properties.entrySet()) {
174            Console.traceln(Level.INFO, "\t" + entry.getKey());
175        }
176
177        for (Entry<String, Set<String>> entry : calledMethods.entrySet()) {
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);
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 {
194                Set<Interface> interfaces = getRealizedInterfacesFromProperty(property);
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 + "\": ");
209                    for (Interface intface : interfaces) {
210                        Console.traceln(Level.INFO, "\t" + intface.getName());
211                        for (Operation operation : intface.getAllOperations()) {
212                            Console.traceln(Level.INFO, "\t\t" + operation.getName());
213                        }
214                    }
215                    for (String methodName : methodNames) {
216                        boolean methodFound = false;
217                        for (Interface intface : interfaces) {
218                            if (getOperationFromName(intface.getOperations(), methodName) != null) {
219                                // interface found
220                                Console.traceln(Level.INFO, "\tMethod " + methodName +
221                                    " found in interface " + intface.getName());
222                                methodFound = true;
223                            }
224                        }
225                        if (!methodFound) {
226                            violationCount++;
227                            Console.traceln(Level.SEVERE, "\tCould not find operation: " +
228                                methodName);
229                        }
230                    }
231                }
232            }
233        }
234        return violationCount;
235    }
236
237    /**
238     * <p>
239     * Creates a sequence of events with {@link UMLTransitionType} as event type from a given
240     * sequence of events with the {@link SOAPEventType} or {@link SimpleSOAPEventType}, by matching
241     * the sequences to a state machine.
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    {
253        System.out.println("foo");
254        List<List<Transition>> matchingSequences =
255            determineMatchingTransitionSequences(sequence, stateMachine);
256        System.out.println(matchingSequences.size());
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            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);
386                }
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();
398
399                    List<Transition> matches =
400                        matchTransitions(currentTransition.getTarget().getOutgoings(), event);
401                    if (matches.isEmpty()) {
402                        throw new RuntimeException("no matches found");
403                    }
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                    }
410                }
411                matchingSequences = nextMatchingSequences;
412                currentTransitions = nextCurrentTransitions;
413            }
414        }
415        return matchingSequences;
416    }
417
418    /**
419     * <p>
420     * Extends a given model with an interaction that represents an observed sequence.
421     * </p>
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
429     * @param testContextName
430     *            Name of the test context that should be used. If this value is null, the first
431     *            test context found is used.
432     */
433    public static Interaction createInteractionFromEventSequence(List<Event> sequence,
434                                                                 Model model,
435                                                                 String interactionName,
436                                                                 String testContextName)
437    {
438        final Component testContext = fetchTestContext(model, testContextName);
439        if (testContext == null) {
440            throw new RuntimeException("Could not find any test context in the model");
441        }
442
443        final Operation operation = testContext.createOwnedOperation(interactionName, null, null);
444        operation.applyStereotype(UTPUtils.getTestCaseStereotype(model));
445
446        final Interaction interaction =
447            (Interaction) testContext.createPackagedElement(interactionName + "_Impl",
448                                                            UMLPackage.Literals.INTERACTION);
449        operation.getMethods().add(interaction);
450
451        // create lifelines
452        Lifeline userLifeline = null;
453
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.");
464            }
465            userLifeline = interaction.createLifeline(property.getName());
466            userLifeline.setRepresents(property);
467        }
468
469        if (userLifeline == null) {
470            throw new RuntimeException("No TestComponent found, could not create user lifeline.");
471        }
472        if (interaction.getLifelines().size() < 2) {
473            throw new RuntimeException("Fewer than two lifelines created. No SUT found.");
474        }
475
476        int i = 0;
477        for (Event event : sequence) {
478            if (!(event.equals(Event.STARTEVENT) || event.equals(Event.ENDEVENT))) {
479                String serviceName = SOAPUtils.getServiceNameFromEvent(event);
480                String methodName = SOAPUtils.getCalledMethodFromEvent(event);
481                String clientName = SOAPUtils.getClientNameFromEvent(event);
482                String prefix = interactionName + ":" + i + ":" + methodName + "_";
483                // determine lifelines
484                Lifeline msgTargetLifeline;
485                Lifeline msgSourceLifeline;
486
487                msgSourceLifeline = interaction.getLifeline(clientName);
488                msgTargetLifeline = interaction.getLifeline(serviceName);
489
490                if (msgSourceLifeline == null) {
491                    throw new RuntimeException(
492                                               "Error creating message: could not determine source lifeline for component: " +
493                                                   clientName);
494                }
495                if (msgTargetLifeline == null) {
496                    throw new RuntimeException(
497                                               "Error creating message: could not determine target lifeline for component: " +
498                                                   serviceName);
499                }
500                // determine correct target interface
501                Set<Interface> targetInterfaces =
502                    getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents());
503                if (targetInterfaces.isEmpty()) {
504                    throw new RuntimeException("no interface associated with the property " +
505                        msgTargetLifeline.getRepresents().getName());
506                }
507                Interface targetInterface = null;
508                for (Interface intface : targetInterfaces) {
509                    if (getOperationFromName(intface.getOperations(), methodName) != null) {
510                        // interface found
511                        targetInterface = intface;
512                        break;
513                    }
514                }
515                if (targetInterface == null) {
516                    StringBuilder errStrBuilder = new StringBuilder();
517                    errStrBuilder
518                        .append("Error creating message: operation not found in the implementing interfaces (");
519                    Iterator<Interface> iter = targetInterfaces.iterator();
520                    while (iter.hasNext()) {
521                        String interfaceName = iter.next().getName();
522                        errStrBuilder.append(interfaceName);
523                        if (iter.hasNext()) {
524                            errStrBuilder.append(",");
525                        }
526                        else {
527                            errStrBuilder.append("): " + methodName);
528                        }
529                    }
530                    throw new RuntimeException(errStrBuilder.toString());
531                }
532
533                Operation calledOperation =
534                    getOperationFromName(targetInterface.getOperations(), methodName);
535
536                // setup for both SYNCH and ASYNCH calls
537                MessageOccurrenceSpecification callSendFragment =
538                    (MessageOccurrenceSpecification) interaction.createFragment(prefix +
539                        "callSendFragment", UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
540                MessageOccurrenceSpecification callRecvFragment =
541                    (MessageOccurrenceSpecification) interaction.createFragment(prefix +
542                        "callRecvFragment", UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
543
544                callSendFragment.setCovered(msgSourceLifeline);
545                callRecvFragment.setCovered(msgTargetLifeline);
546
547                // get connector
548                Connector connector = inferConnector(msgSourceLifeline, msgTargetLifeline);
549
550                // create call
551                Message callMessage = interaction.createMessage(prefix + "call");
552                callMessage.setSignature(calledOperation);
553                setCallMessageParameters(callMessage, calledOperation, event, prefix);
554                callMessage.setConnector(connector);
555                callMessage.setSendEvent(callSendFragment);
556                callMessage.setReceiveEvent(callRecvFragment);
557                callSendFragment.setMessage(callMessage);
558                callRecvFragment.setMessage(callMessage);
559
560                boolean asynch = false;
561                if (calledOperation.getConcurrency() == CallConcurrencyKind.CONCURRENT_LITERAL) {
562                    asynch = true;
563                }
564                if (asynch) {
565                    // Create ASYNCH call
566                    callMessage.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
567                }
568                else {
569                    // SYNCH call
570                    callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
571
572                    // setup reply and behavior execution specifications
573                    MessageOccurrenceSpecification replySendFragment =
574                        (MessageOccurrenceSpecification) interaction
575                            .createFragment(prefix + "replySendFragment",
576                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
577                    MessageOccurrenceSpecification replyRecvFragment =
578                        (MessageOccurrenceSpecification) interaction
579                            .createFragment(prefix + "replyRecvFragment",
580                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
581
582                    replySendFragment.setCovered(msgTargetLifeline);
583                    replyRecvFragment.setCovered(msgSourceLifeline);
584
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                     */
600
601                    // create reply
602                    Message replyMessage = interaction.createMessage(prefix + "_reply");
603                    replyMessage.setMessageSort(MessageSort.REPLY_LITERAL);
604                    replyMessage.setSignature(calledOperation);
605                    setReplyMessageParameters(replyMessage, calledOperation);
606                    replyMessage.setConnector(connector);
607                    replyMessage.setSendEvent(replySendFragment);
608                    replyMessage.setReceiveEvent(replyRecvFragment);
609                    replySendFragment.setMessage(replyMessage);
610                    replyRecvFragment.setMessage(replyMessage);
611                }
612
613                i++;
614            }
615        }
616        return interaction;
617    }
618
619    /**
620     * <p>
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     */
631    public static double calculateUsageScore(Interaction interaction,
632                                             IStochasticProcess usageProfile)
633    {
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) {
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)));
653                }
654            }
655        }
656        eventSequence.add(Event.ENDEVENT);
657        double prob = usageProfile.getLogSum(eventSequence);
658        usageScore = eventSequence.size() * prob;
659
660        return usageScore;
661    }
662
663    /**
664     * <p>
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
671     *            usage profile used as foundation
672     */
673    public static void createScheduling(Model model,
674                                        IStochasticProcess usageProfile,
675                                        String testContextName)
676    {
677        final Component testContext = fetchTestContext(model, testContextName);
678        if (testContext == null) {
679            throw new RuntimeException("Could not find any test context in the model");
680        }
681
682        Map<Operation, Double> usageScoreMapUnsorted = new HashMap<>();
683
684        // first, we determine all test cases and calculate their usage scores
685        final Stereotype utpTestCase = UTPUtils.getTestCaseStereotype(model);
686        for (Operation operation : testContext.getAllOperations()) {
687            if (operation.getAppliedStereotypes().contains(utpTestCase) &&
688                !operation.getMethods().isEmpty())
689            {
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>
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     */
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
778        List<Transition> matching = new LinkedList<>();
779        for (Transition transition : transitions) {
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                }
803            }
804
805        }
806        return matching;
807    }
808
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     */
819    private static Set<Interface> getRealizedInterfacesFromProperty(Property property) {
820        return getRealizedInterfaceFromComponent((Component) property.getType());
821    }
822
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) {
834        Set<Interface> interfaces = new HashSet<>();
835        // Interface myInterface = null;
836        for (Property property : component.getAttributes()) {
837            if (property instanceof Port) {
838                Port port = (Port) property;
839                if (!port.isConjugated()) {
840                    interfaces.addAll(port.getProvideds());
841                }
842            }
843        }
844        return interfaces;
845    }
846
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);
865        if (testContexts.isEmpty()) {
866            return null;
867        }
868        if (testContextName != null) {
869            for (Component testContext : testContexts) {
870                if (testContextName.equals(testContext.getName())) {
871                    return testContext;
872                }
873            }
874            return null;
875        }
876        else {
877            return testContexts.get(0);
878        }
879    }
880
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<>();
898        for (Element element : pkg.getOwnedElements()) {
899            if (element instanceof Package) {
900                testContexts.addAll(fetchAllTestContexts((Package) element));
901            }
902            if (element instanceof Component &&
903                element.getAppliedStereotypes().contains(utpTestContext))
904            {
905                testContexts.add((Component) element);
906            }
907        }
908        return testContexts;
909    }
910
911    /**
912     * <p>
913     * Retrieves all properties that represent a UTP TestComponent from a test context.
914     * </p>
915     *
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
965     * @param targetAttributes
966     *            target lifeline of the message
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    }
995
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     */
1009    private static Map<Interface, String> createInterfaceServiceMap(Model model) {
1010        Map<Interface, String> interfaceServiceMap = new HashMap<>();
1011        List<Component> testContexts = fetchAllTestContexts(model.getModel());
1012        for (Component testContext : testContexts) {
1013            for (Property property : fetchAllSUTProperties(testContext)) {
1014                for (Interface intface : getRealizedInterfacesFromProperty(property)) {
1015                    interfaceServiceMap.put(intface, property.getName());
1016                }
1017            }
1018            for (Property property : fetchAllTestComponentProperties(testContext)) {
1019                for (Interface intface : getRealizedInterfacesFromProperty(property)) {
1020                    interfaceServiceMap.put(intface, property.getName());
1021                }
1022            }
1023        }
1024        return interfaceServiceMap;
1025    }
1026
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)
1046    {
1047        org.w3c.dom.Element requestBody = SOAPUtils.getSoapRequestBodyFromEvent(event);
1048        Package instSpecPkg = null;
1049
1050        // Set parameters of operation
1051        for (Parameter param : calledOperation.getOwnedParameters()) {
1052            if (instSpecPkg == null) {
1053                instSpecPkg = getOrCreateInstanceSpecificationPackage(param.getModel(), event);
1054            }
1055
1056            String path = calledOperation.getName() + ":" + param.getName();
1057            Expression argument =
1058                (Expression) callMessage.createArgument(param.getName(), param.getType(),
1059                                                        UMLPackage.Literals.EXPRESSION);
1060
1061            if (isInParameter(param)) {
1062                if (param.getType() instanceof DataType) {
1063                    List<org.w3c.dom.Element> paramNodes =
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                        }
1082                        InstanceSpecification instSpec =
1083                            createInstanceSpecification((DataType) param.getType(), instSpecPkg,
1084                                                        prefix, paramNode, path);
1085
1086                        InstanceValue value =
1087                            (InstanceValue) argument
1088                                .createOperand(null, param.getType(),
1089                                               UMLPackage.Literals.INSTANCE_VALUE);
1090                        value.setInstance(instSpec);
1091                    }
1092                }
1093                else if (param.getType() instanceof PrimitiveType) {
1094                    createOperandPrimitiveType(param, argument, requestBody, path);
1095                }
1096            }
1097            else {
1098                // set literalNull for out and return parameters
1099                argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
1100            }
1101        }
1102    }
1103
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     */
1124    private static InstanceSpecification createInstanceSpecification(DataType type,
1125                                                                     Package pkg,
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 =
1135            (InstanceSpecification) pkg
1136                .createPackagedElement(prefix + "instspec_" + type.getName(),
1137                                       UMLPackage.Literals.INSTANCE_SPECIFICATION);
1138        instSpec.getClassifiers().add(type);
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) {
1147                    attributeNodes = SOAPUtils.getMatchingChildNode(prop.getName(), currentNode);
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);
1176                    value.setInstance(createInstanceSpecification((DataType) prop.getType(), pkg,
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
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     */
1238    private static void createOperandPrimitiveType(Parameter param,
1239                                                   Expression argument,
1240                                                   org.w3c.dom.Element currentNode,
1241                                                   String path)
1242    {
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            }
1261        }
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            }
1307        }
1308    }
1309
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     */
1324    private static void createSlotPrimitiveType(InstanceSpecification instSpec,
1325                                                Property prop,
1326                                                org.w3c.dom.Element currentNode,
1327                                                String path)
1328    {
1329        List<String> attributeValues = SOAPUtils.getValuesFromElement(prop.getName(), currentNode);
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");
1342                    Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
1343                        SOAPUtils.getSerialization(currentNode));
1344                }
1345                attributeValues.add(null);
1346            }
1347        }
1348        for (String attributeValue : attributeValues) {
1349            Slot slot = instSpec.createSlot();
1350            slot.setDefiningFeature(prop);
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);
1357                }
1358                else {
1359                    value.setValue("foobar");
1360                }
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));
1368                }
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 {
1392                    value.setValue(3.14);
1393                }
1394            }
1395            else {
1396                Console.traceln(Level.SEVERE, "could not create literal for primitive type: " +
1397                    prop.getType().getName());
1398                // TODO abort?
1399            }
1400        }
1401    }
1402
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        }
1428    }
1429
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;
1442    }
1443
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;
1472        }
1473        MessageSort msgSort = message.getMessageSort();
1474        return msgSort == MessageSort.ASYNCH_CALL_LITERAL ||
1475            msgSort == MessageSort.SYNCH_CALL_LITERAL;
1476    }
1477
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        });
1498
1499        Map<K, V> result = new LinkedHashMap<>();
1500        for (Map.Entry<K, V> entry : list) {
1501            result.put(entry.getKey(), entry.getValue());
1502        }
1503        return result;
1504    }
1505}
Note: See TracBrowser for help on using the repository browser.