Index: trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java
===================================================================
--- trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java	(revision 1835)
+++ trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java	(revision 1896)
@@ -30,4 +30,5 @@
 import org.eclipse.uml2.uml.ActivityEdge;
 import org.eclipse.uml2.uml.ActivityNode;
+import org.eclipse.uml2.uml.BehaviorExecutionSpecification;
 import org.eclipse.uml2.uml.CallOperationAction;
 import org.eclipse.uml2.uml.Comment;
@@ -45,4 +46,5 @@
 import org.eclipse.uml2.uml.Model;
 import org.eclipse.uml2.uml.Operation;
+import org.eclipse.uml2.uml.Package;
 import org.eclipse.uml2.uml.Port;
 import org.eclipse.uml2.uml.Profile;
@@ -267,16 +269,22 @@
      * @param interactionName
      *            name of the interaction
+     * @param testContextName
+     *            Name of the test context that should be used. If this value is null, the first test context found is used.
      */
     public static void createInteractionFromEventSequence(List<Event> sequence,
                                                           Model model,
-                                                          String interactionName)
+                                                          String interactionName,
+                                                          String testContextName)
     {
-
-        Component testContext = fetchTestContext(model);
-
         final Profile utpProfile = model.getAppliedProfile("utp");
         final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase");
         final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent");
         final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT");
+        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
+
+        Component testContext = fetchTestContext(model, utpTestContext, testContextName);
+        if( testContext==null ) {
+            throw new RuntimeException("Could not find any test context in the model");
+        }
         
         Operation operation = testContext.createOwnedOperation(interactionName, null, null);
@@ -310,5 +318,4 @@
                 String serviceName = getServiceNameFromEvent(event);
                 String methodName = getCalledMethodFromEvent(event);
-                
                 // determine lifelines
                 Lifeline msgTargetLifeline;
@@ -332,47 +339,79 @@
                     msgTargetLifeline = interaction.getLifeline(serviceName);
                 }
-
-                // determine target interface
-                Interface targetInterface = getRealizedInterfaceFromProperty((Property) msgTargetLifeline.getRepresents());
                 
-                // create message
-                MessageOccurrenceSpecification sendFragment =
-                    (MessageOccurrenceSpecification) interaction
-                        .createFragment(i + ":" + methodName + "_sendFragment",
-                                        UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-                MessageOccurrenceSpecification recvFragment =
-                    (MessageOccurrenceSpecification) interaction
-                        .createFragment(i + ":" + methodName + "_recvFragment",
-                                        UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-
-                sendFragment.setCovered(msgSourceLifeline);
-                recvFragment.setCovered(msgTargetLifeline);
-
-                Message message = interaction.createMessage(methodName);
-                if (getOperationFromName(targetInterface.getOperations(), methodName) == null) {
-                    System.out.println("operation not found in the " + targetInterface.getName() + " interface: " + methodName);
-                }
-                message.setSignature(getOperationFromName(targetInterface.getOperations(),
-                                                          methodName));
-                message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
-                message.setSendEvent(sendFragment);
-                message.setReceiveEvent(recvFragment);
-
-                // now the connector needs to be determined
-                EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
-                EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
+                // TODO null checks for lifelines
+
+                // determine target interface and be able to deal with multiple interfaces
+                List<Interface> targetInterfaces = getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents());
+                if( targetInterfaces.isEmpty() ) {
+                    throw new RuntimeException("no interface associated with the property " + msgTargetLifeline.getRepresents().getName());
+                }
+                Interface targetInterface = null;
+                for( Interface intface : targetInterfaces ) {
+                    System.out.println(intface.getOperations());
+                    if (getOperationFromName(intface.getOperations(), methodName) != null) {
+                        // interface found
+                        targetInterface = intface;
+                        break;
+                    }
+                }
+                if( targetInterface == null ) {
+                    StringBuilder errStrBuilder = new StringBuilder();
+                    errStrBuilder.append("operation not found in the implementing interfaces (");
+                    Iterator<Interface> iter = targetInterfaces.iterator();
+                    while( iter.hasNext() ) {
+                        String interfaceName = iter.next().getName();
+                        errStrBuilder.append(interfaceName);
+                        if( iter.hasNext() ) {
+                            errStrBuilder.append(",");
+                        } else {
+                            errStrBuilder.append("): " + methodName);
+                        }
+                    }
+                    throw new RuntimeException(errStrBuilder.toString());
+                }
                 
-                for( Property userAttribute : userAttributes ) {
-                    if( userAttribute instanceof Port ) {
-                        EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
-                        for( ConnectorEnd userEnd : userEnds ) {
-                            Connector userConnector = (Connector) userEnd.eContainer();
-                            for( Property targetAttribute : targetAttributes ) {
-                                if( targetAttribute instanceof Port ) {
-                                    EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
-                                    for( ConnectorEnd targetEnd : targetEnds ) {
-                                        Connector targetConnector = (Connector) targetEnd.eContainer();
-                                        if( targetConnector==userConnector ) {
-                                            message.setConnector(targetConnector);
+                // create message ASYNCH
+                boolean asynch = false;
+                if( asynch ) {
+                    MessageOccurrenceSpecification sendFragment =
+                        (MessageOccurrenceSpecification) interaction
+                            .createFragment(i + ":" + methodName + "_sendFragment",
+                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+                    MessageOccurrenceSpecification recvFragment =
+                        (MessageOccurrenceSpecification) interaction
+                            .createFragment(i + ":" + methodName + "_recvFragment",
+                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+    
+                    sendFragment.setCovered(msgSourceLifeline);
+                    recvFragment.setCovered(msgTargetLifeline);
+    
+                    Message message = interaction.createMessage(methodName);
+                    if (getOperationFromName(targetInterface.getOperations(), methodName) == null) {
+                        System.out.println("operation not found in the " + targetInterface.getName() + " interface: " + methodName);
+                    }
+                    message.setSignature(getOperationFromName(targetInterface.getOperations(),
+                                                              methodName));
+                    message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
+                    message.setSendEvent(sendFragment);
+                    message.setReceiveEvent(recvFragment);
+    
+                    // now the connector needs to be determined
+                    EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
+                    EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
+                    
+                    for( Property userAttribute : userAttributes ) {
+                        if( userAttribute instanceof Port ) {
+                            EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
+                            for( ConnectorEnd userEnd : userEnds ) {
+                                Connector userConnector = (Connector) userEnd.eContainer();
+                                for( Property targetAttribute : targetAttributes ) {
+                                    if( targetAttribute instanceof Port ) {
+                                        EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
+                                        for( ConnectorEnd targetEnd : targetEnds ) {
+                                            Connector targetConnector = (Connector) targetEnd.eContainer();
+                                            if( targetConnector==userConnector ) {
+                                                message.setConnector(targetConnector);
+                                            }
                                         }
                                     }
@@ -381,8 +420,91 @@
                         }
                     }
+                    sendFragment.setMessage(message);
+                    recvFragment.setMessage(message);
+                } else {
+                    // SYNCH call
+                    MessageOccurrenceSpecification callSendFragment =
+                        (MessageOccurrenceSpecification) interaction
+                            .createFragment(i + ":" + methodName + "_callSendFragment",
+                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+                    MessageOccurrenceSpecification callRecvFragment =
+                        (MessageOccurrenceSpecification) interaction
+                            .createFragment(i + ":" + methodName + "_callRecvFragment",
+                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+                    
+                    MessageOccurrenceSpecification replySendFragment =
+                        (MessageOccurrenceSpecification) interaction
+                            .createFragment(i + ":" + methodName + "_replySendFragment",
+                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+                    MessageOccurrenceSpecification replyRecvFragment =
+                        (MessageOccurrenceSpecification) interaction
+                            .createFragment(i + ":" + methodName + "_replyRecvFragment",
+                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+    
+                    BehaviorExecutionSpecification sourceBehaviorExecutionSpecification = 
+                        (BehaviorExecutionSpecification) interaction
+                            .createFragment(":" + methodName + "_sourceBhvExecSpec",
+                                            UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
+                    BehaviorExecutionSpecification targetBehaviorExecutionSpecification = 
+                        (BehaviorExecutionSpecification) interaction
+                            .createFragment(":" + methodName + "_targetBhvExecSpec",
+                                            UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
+                    
+                    
+                    callSendFragment.setCovered(msgSourceLifeline);
+                    callRecvFragment.setCovered(msgTargetLifeline);
+                    replySendFragment.setCovered(msgTargetLifeline);
+                    replyRecvFragment.setCovered(msgSourceLifeline);
+                    
+                    sourceBehaviorExecutionSpecification.setStart(callSendFragment);
+                    sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment);
+                    targetBehaviorExecutionSpecification.setStart(callRecvFragment);
+                    targetBehaviorExecutionSpecification.setFinish(replySendFragment);
+    
+                    Operation calledOperation = getOperationFromName(targetInterface.getOperations(), methodName);
+                    
+                    // create call
+                    Message callMessage = interaction.createMessage(methodName);
+                    callMessage.setSignature(calledOperation);
+                    callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
+                    callMessage.setSendEvent(callSendFragment);
+                    callMessage.setReceiveEvent(callRecvFragment);
+                    callSendFragment.setMessage(callMessage);
+                    callRecvFragment.setMessage(callMessage);
+    
+                    // now the connector needs to be determined
+                    EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
+                    EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
+                    
+                    for( Property userAttribute : userAttributes ) {
+                        if( userAttribute instanceof Port ) {
+                            EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
+                            for( ConnectorEnd userEnd : userEnds ) {
+                                Connector userConnector = (Connector) userEnd.eContainer();
+                                for( Property targetAttribute : targetAttributes ) {
+                                    if( targetAttribute instanceof Port ) {
+                                        EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
+                                        for( ConnectorEnd targetEnd : targetEnds ) {
+                                            Connector targetConnector = (Connector) targetEnd.eContainer();
+                                            if( targetConnector==userConnector ) {
+                                                callMessage.setConnector(targetConnector);
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                    
+                    // create reply
+                    Message replyMessage = interaction.createMessage(methodName + "_reply");
+                    replyMessage.setMessageSort(MessageSort.REPLY_LITERAL);
+                    replyMessage.setSignature(calledOperation);
+                    replyMessage.setSendEvent(replySendFragment);
+                    replyMessage.setReceiveEvent(replyRecvFragment);
+                    replySendFragment.setMessage(replyMessage);
+                    replyRecvFragment.setMessage(replyMessage);
                 }
                 
-                sendFragment.setMessage(message);
-                recvFragment.setMessage(message);
 
                 i++;
@@ -441,12 +563,16 @@
      *            model to be extended
      * @param usageProfile
-     *            usage profile used as foundation
-     */
-    public static void createScheduling(Model model, IStochasticProcess usageProfile) {
+     *            usage profile used as foundation 
+     */
+    public static void createScheduling(Model model, IStochasticProcess usageProfile, String testContextName) {
 
         final Profile utpProfile = model.getAppliedProfile("utp");
         final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase");
-
-        Component testContext = fetchTestContext(model);
+        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
+
+        Component testContext = fetchTestContext(model, utpTestContext, testContextName);
+        if( testContext==null ) {
+            throw new RuntimeException("Could not find any test context in the model");
+        }
 
         Map<Operation, Double> usageScoreMapUnsorted = new HashMap<>();
@@ -618,14 +744,17 @@
     }
     
-    private static Interface getRealizedInterfaceFromProperty(Property property) {
+    private static List<Interface> getRealizedInterfacesFromProperty(Property property) {
         return getRealizedInterfaceFromComponent((Component) property.getType());
     }
     
-    private static Interface getRealizedInterfaceFromComponent(Component comp) {
-        Interface myInterface = null;
+    private static List<Interface> getRealizedInterfaceFromComponent(Component comp) {
+        List<Interface> interfaces = new LinkedList<>();
+        //Interface myInterface = null;
         for( Property property : comp.getAttributes() ) {
             if( property instanceof Port ) {
                 Port port = (Port) property;
                 if( !port.isConjugated() ) {
+                    interfaces.addAll(port.getProvideds());
+                    /*
                     if( myInterface==null ) {
                         myInterface = port.getProvideds().get(0);
@@ -634,8 +763,10 @@
                         System.err.println("multiple different interfaces found");
                     }
-                }
-            }
-        }
-        return myInterface;
+                    */
+                }
+            }
+        }
+        return interfaces;
+        //return myInterface;
         //return ((Port) comp.getAttributes().get(0)).getInterface();
         //Realization realization = (Realization) comp.getNestedClassifiers().get(0).getRelationships(UMLPackage.Literals.REALIZATION).get(0);
@@ -643,14 +774,32 @@
     }
     
-    private static Component fetchTestContext(Model model) {
-        final Profile utpProfile = model.getAppliedProfile("utp");
-        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
-        
-        for( Element element : model.getOwnedElements() ) {
-            if( element instanceof Component && element.getApplicableStereotypes().contains(utpTestContext) ) {
-                return (Component) element;
-            }
-        }
-        return null;
+    private static Component fetchTestContext(Package pkg, Stereotype utpTestContext, String testContextName) {
+        List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext);
+        if( testContexts.isEmpty() ) {
+            return null;
+        }
+        if( testContextName!=null ) {
+            for( Component testContext : testContexts ) {
+                if( testContextName.equals(testContext.getName()) ) {
+                    return testContext;
+                }
+            }
+            return null;
+        } else {
+            return testContexts.get(0);
+        }
+    }
+    
+    private static List<Component> fetchTestContextRecursively(Package pkg, Stereotype utpTestContext) {
+        List<Component> testContexts = new LinkedList<>();
+        for( Element element : pkg.getOwnedElements() ) {
+            if( element instanceof Package ) {
+                testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext));
+            }
+            if( element instanceof Component && element.getAppliedStereotypes().contains(utpTestContext) ) {
+                testContexts.add((Component) element);
+            }
+        }
+        return testContexts;
     }
 }
