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

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