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

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