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 1926)
+++ trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java	(revision 1929)
@@ -15,6 +15,4 @@
 package de.ugoe.cs.autoquest.plugin.uml;
 
-import java.io.StringWriter;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -32,13 +30,5 @@
 import java.util.TreeSet;
 
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.TransformerFactoryConfigurationError;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
 import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.common.util.URI;
 import org.eclipse.uml2.uml.Activity;
 import org.eclipse.uml2.uml.ActivityEdge;
@@ -74,5 +64,4 @@
 import org.eclipse.uml2.uml.Port;
 import org.eclipse.uml2.uml.PrimitiveType;
-import org.eclipse.uml2.uml.Profile;
 import org.eclipse.uml2.uml.Property;
 import org.eclipse.uml2.uml.Region;
@@ -83,10 +72,7 @@
 import org.eclipse.uml2.uml.Transition;
 import org.eclipse.uml2.uml.Trigger;
-import org.eclipse.uml2.uml.Type;
 import org.eclipse.uml2.uml.UMLPackage;
+import org.eclipse.uml2.uml.ValueSpecification;
 import org.eclipse.uml2.uml.Vertex;
-import org.w3c.dom.Attr;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 
 import de.ugoe.cs.autoquest.eventcore.Event;
@@ -113,7 +99,4 @@
     final static int MAX_MULTIPLICITY = 10;
 
-    final public static URI UML_PRIMITIVE_TYPES_URI = URI
-        .createURI("pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml", true);
-
     /**
      * <p>
@@ -134,11 +117,6 @@
                                            String testContextName)
     {
-        final Profile utpProfile = model.getAppliedProfile("utp");
-        final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent");
-        final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT");
-        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
-
         int violationCount = 0;
-        Component testContext = fetchTestContext(model, utpTestContext, testContextName);
+        Component testContext = fetchTestContext(model, testContextName);
         if (testContext == null) {
             violationCount++;
@@ -186,11 +164,9 @@
         // fetch all SUTs and TestComponents
         HashMap<String, Property> properties = new HashMap<>();
-        for (Property property : testContext.getAllAttributes()) {
-            if (property.getAppliedStereotypes().contains(utpSUT)) {
-                properties.put(property.getName(), property);
-            }
-            else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
-                properties.put(property.getName(), property);
-            }
+        for (Property property : fetchAllSUTProperties(testContext)) {
+            properties.put(property.getName(), property);
+        }
+        for (Property property : fetchAllTestComponentProperties(testContext)) {
+            properties.put(property.getName(), property);
         }
         Console.traceln(Level.INFO, "Found the following services in the TestConfiguration:");
@@ -455,29 +431,18 @@
      *            test context found is used.
      */
-    public static void createInteractionFromEventSequence(List<Event> sequence,
-                                                          Model model,
-                                                          String interactionName,
-                                                          String testContextName)
+    public static Interaction createInteractionFromEventSequence(List<Event> sequence,
+                                                                 Model model,
+                                                                 String interactionName,
+                                                                 String testContextName)
     {
-        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");
-
-        // add UML Primitive types
-        // final UMLResource umlRes = (UMLResource)
-        // model.eResource().getResourceSet().getResource(UML_PRIMITIVE_TYPES_URI, true);
-        // model = (Model) umlRes.getContents().get(0);
-
-        Component testContext = fetchTestContext(model, utpTestContext, testContextName);
+        final Component testContext = fetchTestContext(model, testContextName);
         if (testContext == null) {
             throw new RuntimeException("Could not find any test context in the model");
         }
 
-        Operation operation = testContext.createOwnedOperation(interactionName, null, null);
-        operation.applyStereotype(utpTestCase);
-
-        Interaction interaction =
+        final Operation operation = testContext.createOwnedOperation(interactionName, null, null);
+        operation.applyStereotype(UTPUtils.getTestCaseStereotype(model));
+
+        final Interaction interaction =
             (Interaction) testContext.createPackagedElement(interactionName + "_Impl",
                                                             UMLPackage.Literals.INTERACTION);
@@ -487,19 +452,19 @@
         Lifeline userLifeline = null;
 
-        for (Property property : testContext.getAllAttributes()) {
-            if (property.getAppliedStereotypes().contains(utpSUT)) {
-                String serviceName = property.getName();
-                Lifeline targetLifeline = interaction.createLifeline(serviceName);
-                targetLifeline.setRepresents(property);
-            }
-            else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
-                if (userLifeline != null) {
-                    throw new RuntimeException(
-                                               "TestContext must only have one TestComponent for the application of usage-based testing.");
-                }
-                userLifeline = interaction.createLifeline(property.getName());
-                userLifeline.setRepresents(property);
-            }
-        }
+        for (Property property : fetchAllSUTProperties(testContext)) {
+            String serviceName = property.getName();
+            Lifeline targetLifeline = interaction.createLifeline(serviceName);
+            targetLifeline.setRepresents(property);
+        }
+        for (Property property : fetchAllTestComponentProperties(testContext)) {
+            // TODO check if this is still required
+            if (userLifeline != null) {
+                throw new RuntimeException(
+                                           "TestContext must only have one TestComponent for the application of usage-based testing.");
+            }
+            userLifeline = interaction.createLifeline(property.getName());
+            userLifeline.setRepresents(property);
+        }
+
         if (userLifeline == null) {
             throw new RuntimeException("No TestComponent found, could not create user lifeline.");
@@ -580,9 +545,12 @@
                 callRecvFragment.setCovered(msgTargetLifeline);
 
+                // get connector
+                Connector connector = inferConnector(msgSourceLifeline, msgTargetLifeline);
+
                 // create call
-                Message callMessage = interaction.createMessage(methodName);
+                Message callMessage = interaction.createMessage(prefix + "call");
                 callMessage.setSignature(calledOperation);
-                setMessageParameters(callMessage, calledOperation, event, prefix);
-                callMessage.setConnector(inferConnector(msgSourceLifeline, msgTargetLifeline));
+                setCallMessageParameters(callMessage, calledOperation, event, prefix);
+                callMessage.setConnector(connector);
                 callMessage.setSendEvent(callSendFragment);
                 callMessage.setReceiveEvent(callRecvFragment);
@@ -632,7 +600,9 @@
 
                     // create reply
-                    Message replyMessage = interaction.createMessage(methodName + "_reply");
+                    Message replyMessage = interaction.createMessage(prefix + "_reply");
                     replyMessage.setMessageSort(MessageSort.REPLY_LITERAL);
                     replyMessage.setSignature(calledOperation);
+                    setReplyMessageParameters(replyMessage, calledOperation);
+                    replyMessage.setConnector(connector);
                     replyMessage.setSendEvent(replySendFragment);
                     replyMessage.setReceiveEvent(replyRecvFragment);
@@ -644,4 +614,5 @@
             }
         }
+        return interaction;
     }
 
@@ -662,26 +633,23 @@
     {
         double usageScore = 0.0d;
-
         EList<InteractionFragment> interactionFragments = interaction.getFragments();
         List<Event> eventSequence = new LinkedList<>();
         eventSequence.add(Event.STARTEVENT);
         for (InteractionFragment interactionFragment : interactionFragments) {
-            if (interactionFragment.getName() != null &&
-                interactionFragment.getName().endsWith("_recvFragment")) // TODO must be more
-                                                                         // generic
-            {
-                String serviceName =
-                    interactionFragment.getCovereds().get(0).getRepresents().getName().split("_")[0];
-                String methodName = "UNKNOWN";
-                if (interactionFragment instanceof MessageOccurrenceSpecification) {
-                    methodName =
-                        ((MessageOccurrenceSpecification) interactionFragment).getMessage()
-                            .getName();
-                }
-                // eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName, "",
-                // ))); // TODO
-                // add
-                // client
-                // name
+            if (interactionFragment instanceof MessageOccurrenceSpecification) {
+                Message message =
+                    ((MessageOccurrenceSpecification) interactionFragment).getMessage();
+                if (message.getReceiveEvent().equals(interactionFragment) && isCallMessage(message))
+                {
+                    String clientName =
+                        ((MessageOccurrenceSpecification) message.getSendEvent()).getCovereds()
+                            .get(0).getName();
+                    String serviceName =
+                        ((MessageOccurrenceSpecification) message.getReceiveEvent()).getCovereds()
+                            .get(0).getName();
+                    String methodName = message.getSignature().getName();
+                    eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName,
+                                                                        clientName, null)));
+                }
             }
         }
@@ -707,10 +675,5 @@
                                         String testContextName)
     {
-
-        final Profile utpProfile = model.getAppliedProfile("utp");
-        final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase");
-        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
-
-        Component testContext = fetchTestContext(model, utpTestContext, testContextName);
+        final Component testContext = fetchTestContext(model, testContextName);
         if (testContext == null) {
             throw new RuntimeException("Could not find any test context in the model");
@@ -720,6 +683,9 @@
 
         // first, we determine all test cases and calculate their usage scores
+        final Stereotype utpTestCase = UTPUtils.getTestCaseStereotype(model);
         for (Operation operation : testContext.getAllOperations()) {
-            if (operation.getAppliedStereotypes().contains(utpTestCase)) {
+            if (operation.getAppliedStereotypes().contains(utpTestCase) &&
+                !operation.getMethods().isEmpty())
+            {
                 Interaction interaction = (Interaction) operation.getMethods().get(0);
                 usageScoreMapUnsorted
@@ -764,25 +730,4 @@
         edge.setSource(currentOperationNode);
         edge.setTarget(finalNode);
-    }
-
-    /**
-     * From
-     * http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java
-     * and adapted to do an inverse sorting
-     */
-    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
-        List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
-        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
-            @Override
-            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
-                return -1 * (o1.getValue()).compareTo(o2.getValue());
-            }
-        });
-
-        Map<K, V> result = new LinkedHashMap<>();
-        for (Map.Entry<K, V> entry : list) {
-            result.put(entry.getKey(), entry.getValue());
-        }
-        return result;
     }
 
@@ -862,12 +807,32 @@
     }
 
+    /**
+     * <p>
+     * Fetches all realized interfaces from the type of a UML {@link Property} (i.e.,
+     * property.getType()). If no interfaces are realized, an empty set is returned.
+     * </p>
+     * 
+     * @param property
+     *            property, of the whose realized interfaces of the type are determined
+     * @return realized interfaces
+     */
     private static Set<Interface> getRealizedInterfacesFromProperty(Property property) {
         return getRealizedInterfaceFromComponent((Component) property.getType());
     }
 
-    private static Set<Interface> getRealizedInterfaceFromComponent(Component comp) {
+    /**
+     * <p>
+     * Fetches all realized interfaces from a UML {@link Component}. If no interfaces are realized,
+     * an empty set is returned.
+     * </p>
+     * 
+     * @param comp
+     *            component whose realized interfaces are determined
+     * @return realized interfaces
+     */
+    private static Set<Interface> getRealizedInterfaceFromComponent(Component component) {
         Set<Interface> interfaces = new HashSet<>();
         // Interface myInterface = null;
-        for (Property property : comp.getAttributes()) {
+        for (Property property : component.getAttributes()) {
             if (property instanceof Port) {
                 Port port = (Port) property;
@@ -880,9 +845,22 @@
     }
 
-    private static Component fetchTestContext(Package pkg,
-                                              Stereotype utpTestContext,
-                                              String testContextName)
-    {
-        List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext);
+    /**
+     * <p>
+     * Determines searches within a {@link Package} for a component to which the UTP TestContext
+     * stereotype is applied.
+     * <ul>
+     * <li>If no testContextName is provided, the first test context found is returned.</li>
+     * <li>In case no test context is found, null is returned.</li>
+     * </p>
+     * 
+     * @param pkg
+     *            package where the test context is being locked for
+     * @param testContextName
+     *            name of the test context; in case no test name is specified, use null and not the
+     *            empty String.
+     * @return {@link Component} to which the TestContext stereotype is applied
+     */
+    private static Component fetchTestContext(final Package pkg, final String testContextName) {
+        List<Component> testContexts = fetchAllTestContexts(pkg);
         if (testContexts.isEmpty()) {
             return null;
@@ -901,11 +879,24 @@
     }
 
-    private static List<Component> fetchTestContextRecursively(Package pkg,
-                                                               Stereotype utpTestContext)
-    {
-        List<Component> testContexts = new LinkedList<>();
+    /**
+     * <p>
+     * Retrieves all UML {@link Component}s to which the UTP TestContext stereotype is applied from
+     * a package. This method calls itself recursively to include all components contained in
+     * sub-packages.
+     * </p>
+     * <p>
+     * In case no test context is found, an empty list is returned.
+     * </p>
+     * 
+     * @param pkg
+     *            package from which the test contexts are retrieved
+     * @return {@link List} of test contexts
+     */
+    private static List<Component> fetchAllTestContexts(final Package pkg) {
+        final Stereotype utpTestContext = UTPUtils.getTestContextStereotype(pkg.getModel());
+        final List<Component> testContexts = new LinkedList<>();
         for (Element element : pkg.getOwnedElements()) {
             if (element instanceof Package) {
-                testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext));
+                testContexts.addAll(fetchAllTestContexts((Package) element));
             }
             if (element instanceof Component &&
@@ -920,10 +911,58 @@
     /**
      * <p>
-     * Infers connector between two lifelines. TODO: currently assumes only one connector between
-     * two lifelines possible. I need to make sure this assumption is valid.
-     * </p>
-     * 
-     * @param userAttributes
+     * Retrieves all properties that represent a UTP TestComponent from a test context.
+     * </p>
+     * 
+     * @param testContext
+     *            test context from which the properties are retrieved
+     * @return properties that represent test components
+     */
+    private static Set<Property> fetchAllTestComponentProperties(final Component testContext) {
+        // fetch all SUTs and TestComponents
+        final Stereotype utpTestComponent =
+            UTPUtils.getTestComponentStereotype(testContext.getModel());
+        final Set<Property> properties = new HashSet<>();
+        for (Property property : testContext.getAllAttributes()) {
+            if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
+                properties.add(property);
+            }
+        }
+        return properties;
+    }
+
+    /**
+     * <p>
+     * Retrieves all properties that represent a UTP SUT from a test context.
+     * </p>
+     * 
+     * @param testContext
+     *            test context from which the properties are retrieved
+     * @return properties that represent the SUTs
+     */
+    private static Set<Property> fetchAllSUTProperties(final Component testContext) {
+        // fetch all SUTs and TestComponents
+        final Stereotype utpSUT = UTPUtils.getSUTStereotype(testContext.getModel());
+        final Set<Property> properties = new HashSet<>();
+        for (Property property : testContext.getAllAttributes()) {
+            if (property.getAppliedStereotypes().contains(utpSUT)) {
+                properties.add(property);
+            }
+        }
+        return properties;
+    }
+
+    /**
+     * <p>
+     * Infers connector between two lifelines.
+     * </p>
+     * <p>
+     * TODO: Currently assumes only one connector between two lifelines possible. This assumption is
+     * invalid as soon as there are two ports that connect the same two properties.
+     * </p>
+     * 
+     * @param msgSourceLifeline
+     *            source lifeline of the message
      * @param targetAttributes
+     *            target lifeline of the message
      */
     private static Connector inferConnector(Lifeline msgSourceLifeline, Lifeline msgTargetLifeline)
@@ -955,20 +994,29 @@
     }
 
+    /**
+     * <p>
+     * Creates a map that maps the interfaces to the properties, i.e., services that they are
+     * represented by.
+     * </p>
+     * <p>
+     * TODO: currently assumes that each interfaces is only realized by one property
+     * </p>
+     * 
+     * @param model
+     *            model for which the interface->service map is created
+     * @return the map
+     */
     private static Map<Interface, String> createInterfaceServiceMap(Model model) {
         Map<Interface, String> interfaceServiceMap = new HashMap<>();
-        final Profile utpProfile = model.getModel().getAppliedProfile("utp");
-        final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent");
-        final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT");
-        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");
-        List<Component> testContexts =
-            fetchTestContextRecursively(model.getModel(), utpTestContext);
+        List<Component> testContexts = fetchAllTestContexts(model.getModel());
         for (Component testContext : testContexts) {
-            for (Property property : testContext.getAllAttributes()) {
-                if (property.getAppliedStereotypes().contains(utpSUT) ||
-                    property.getType().getAppliedStereotypes().contains(utpTestComponent))
-                {
-                    for (Interface intface : getRealizedInterfacesFromProperty(property)) {
-                        interfaceServiceMap.put(intface, property.getName());
-                    }
+            for (Property property : fetchAllSUTProperties(testContext)) {
+                for (Interface intface : getRealizedInterfacesFromProperty(property)) {
+                    interfaceServiceMap.put(intface, property.getName());
+                }
+            }
+            for (Property property : fetchAllTestComponentProperties(testContext)) {
+                for (Interface intface : getRealizedInterfacesFromProperty(property)) {
+                    interfaceServiceMap.put(intface, property.getName());
                 }
             }
@@ -977,27 +1025,62 @@
     }
 
-    private static void setMessageParameters(Message callMessage,
-                                             Operation calledOperation,
-                                             Event event,
-                                             String prefix)
+    /**
+     * <p>
+     * Sets values for the parameters of a call message. The values are, if possible, inferred from
+     * the event that is provided.
+     * </p>
+     * 
+     * @param callMessage
+     *            call message for which the parameters are set
+     * @param calledOperation
+     *            operation that is called by the message
+     * @param event
+     *            event that provides the parameters; in case of null, default values are assumed
+     * @param prefix
+     *            prefix of the call message; used to create good warnings and debugging information
+     */
+    private static void setCallMessageParameters(Message callMessage,
+                                                 Operation calledOperation,
+                                                 Event event,
+                                                 String prefix)
     {
         org.w3c.dom.Element requestBody = SOAPUtils.getSoapRequestBodyFromEvent(event);
+        Package instSpecPkg = null;
 
         // Set parameters of operation
         for (Parameter param : calledOperation.getOwnedParameters()) {
+            if (instSpecPkg == null) {
+                instSpecPkg = getOrCreateInstanceSpecificationPackage(param.getModel(), event);
+            }
+
+            String path = calledOperation.getName() + ":" + param.getName();
             Expression argument =
                 (Expression) callMessage.createArgument(param.getName(), param.getType(),
                                                         UMLPackage.Literals.EXPRESSION);
 
-            if (param.getDirection() == ParameterDirectionKind.IN_LITERAL ||
-                param.getDirection() == ParameterDirectionKind.INOUT_LITERAL)
-            {
+            if (isInParameter(param)) {
                 if (param.getType() instanceof DataType) {
                     List<org.w3c.dom.Element> paramNodes =
-                        getMatchingChildNode((DataType) param.getType(), requestBody);
-                    for (org.w3c.dom.Element paramNode : paramNodes) {
+                        SOAPUtils.getMatchingChildNode(param.getType().getName(), requestBody);
+                    int multiplicityChosen = paramNodes.size();
+
+                    if (multiplicityChosen == 0 && param.getLower() > 0) {
+                        Console.traceln(Level.WARNING,
+                                        "required attribute not found in SOAP message: " + path);
+                        Console
+                            .traceln(Level.WARNING,
+                                     "setting default values for this attribute and all its children");
+                        Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
+                            SOAPUtils.getSerialization(requestBody));
+                        multiplicityChosen = param.getLower();
+                    }
+                    for (int i = 0; i < multiplicityChosen; i++) {
+                        org.w3c.dom.Element paramNode = null;
+                        if (!paramNodes.isEmpty()) {
+                            paramNode = paramNodes.get(i);
+                        }
                         InstanceSpecification instSpec =
-                            createInstanceSpecification((DataType) param.getType(), event, prefix,
-                                                        paramNode, "");
+                            createInstanceSpecification((DataType) param.getType(), instSpecPkg,
+                                                        prefix, paramNode, path);
 
                         InstanceValue value =
@@ -1009,5 +1092,5 @@
                 }
                 else if (param.getType() instanceof PrimitiveType) {
-                    createOperandPrimitiveType(param, argument, requestBody);
+                    createOperandPrimitiveType(param, argument, requestBody, path);
                 }
             }
@@ -1019,6 +1102,26 @@
     }
 
+    /**
+     * <p>
+     * Creates an {@link InstanceSpecification} for a data type in the given package. The values are
+     * inferred, if possible, from the DOM node. The prefix and the path are used for naming the
+     * instance specification and to provide good warnings and debug information in case of
+     * problems.
+     * </p>
+     * 
+     * @param type
+     *            DataType for which the {@link InstanceSpecification} is created
+     * @param pkg
+     *            package in which the {@link InstanceSpecification} is created
+     * @param prefix
+     *            prefix used for naming the {@link InstanceSpecification}
+     * @param currentNode
+     *            node of a DOM from which values are inferred
+     * @param path
+     *            used for warnings and debug information
+     * @return {@link InstanceSpecification} for the given type
+     */
     private static InstanceSpecification createInstanceSpecification(DataType type,
-                                                                     Event event,
+                                                                     Package pkg,
                                                                      String prefix,
                                                                      org.w3c.dom.Element currentNode,
@@ -1028,24 +1131,9 @@
             path = type.getName();
         }
-        // System.out.println(path);
-        String pkgUBTInstSpecs = "UBT_InstanceSpecifications";
-        Model model = type.getModel();
-        Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs);
-        if (ubtInstSpecPkg == null) {
-            ubtInstSpecPkg =
-                (Package) type.getModel().createPackagedElement(pkgUBTInstSpecs,
-                                                                UMLPackage.Literals.PACKAGE);
-        }
-        String serviceName = SOAPUtils.getServiceNameFromEvent(event);
-        Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName);
-        if (serviceInstSpecPkg == null) {
-            serviceInstSpecPkg =
-                (Package) ubtInstSpecPkg.createPackagedElement(serviceName,
-                                                               UMLPackage.Literals.PACKAGE);
-        }
 
         InstanceSpecification instSpec =
-            (InstanceSpecification) serviceInstSpecPkg.createPackagedElement(prefix + "instspec_" +
-                type.getName(), UMLPackage.Literals.INSTANCE_SPECIFICATION);
+            (InstanceSpecification) pkg
+                .createPackagedElement(prefix + "instspec_" + type.getName(),
+                                       UMLPackage.Literals.INSTANCE_SPECIFICATION);
         instSpec.getClassifiers().add(type);
         for (Property prop : type.getAllAttributes()) {
@@ -1057,5 +1145,5 @@
                 int multiplicityChosen = 0;
                 if (currentNode != null) {
-                    attributeNodes = getMatchingChildNode(prop, currentNode);
+                    attributeNodes = SOAPUtils.getMatchingChildNode(prop.getName(), currentNode);
                     multiplicityChosen = attributeNodes.size();
                 }
@@ -1086,5 +1174,5 @@
                         (InstanceValue) slot.createValue(prop.getName() + "_" + i, prop.getType(),
                                                          UMLPackage.Literals.INSTANCE_VALUE);
-                    value.setInstance(createInstanceSpecification((DataType) prop.getType(), event,
+                    value.setInstance(createInstanceSpecification((DataType) prop.getType(), pkg,
                                                                   prefix, attributeNode, path +
                                                                       "." + prop.getName()));
@@ -1100,34 +1188,138 @@
     }
 
+    /**
+     * <p>
+     * Gets or creates a {@link Package} for {@link InstanceSpecification} created by the
+     * usage-based testing. Each service gets its own sub-package within a package called
+     * UBT_InstanceSpecifications. "
+     * </p>
+     * 
+     * @param model
+     *            model in which the package is generated
+     * @param event
+     *            event from which the service name is inferred
+     * @return package for the {@link InstanceSpecification}s
+     */
+    private static Package getOrCreateInstanceSpecificationPackage(Model model, Event event) {
+        String pkgUBTInstSpecs = "UBT_InstanceSpecifications";
+        Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs);
+        if (ubtInstSpecPkg == null) {
+            ubtInstSpecPkg =
+                (Package) model.createPackagedElement(pkgUBTInstSpecs, UMLPackage.Literals.PACKAGE);
+        }
+        String serviceName = SOAPUtils.getServiceNameFromEvent(event);
+        Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName);
+        if (serviceInstSpecPkg == null) {
+            serviceInstSpecPkg =
+                (Package) ubtInstSpecPkg.createPackagedElement(serviceName,
+                                                               UMLPackage.Literals.PACKAGE);
+        }
+        return serviceInstSpecPkg;
+    }
+
+    /**
+     * <p>
+     * Creates an operand that defines a {@link PrimitiveType}.
+     * </p>
+     * <p>
+     * TODO: Currently does nothing in case of multiplicity 0. I am not sure if, in that case, one
+     * has to define LiteralNull instead.
+     * </p>
+     * 
+     * @param param
+     *            parameter for which the operand is created
+     * @param argument
+     *            argument to which the operand is added
+     * @param currentNode
+     *            DOM node from which is value for the operand is inferred
+     * @param path
+     *            used for warnings and debug information
+     */
     private static void createOperandPrimitiveType(Parameter param,
                                                    Expression argument,
-                                                   org.w3c.dom.Element currentNode)
+                                                   org.w3c.dom.Element currentNode,
+                                                   String path)
     {
-        if ("String".equals(param.getType().getName())) {
-            LiteralString spec =
-                (LiteralString) argument.createOperand(param.getName(), null,
-                                                       UMLPackage.Literals.LITERAL_STRING);
-            spec.setValue("foobar"); // TODO needs to be real value
-        }
-        else if ("Integer".equals(param.getType().getName())) {
-            LiteralInteger spec =
-                (LiteralInteger) argument.createOperand(param.getName(), null,
-                                                        UMLPackage.Literals.LITERAL_INTEGER);
-            spec.setValue(42); // TODO needs to be real value
-        }
-        else if ("Boolean".equals(param.getType().getName())) {
-            LiteralBoolean spec =
-                (LiteralBoolean) argument.createOperand(param.getName(), null,
-                                                        UMLPackage.Literals.LITERAL_BOOLEAN);
-            spec.setValue(true); // TODO needs to be real value
-        }
-        else if ("Real".equals(param.getType().getName())) {
-            LiteralReal spec =
-                (LiteralReal) argument.createOperand(param.getName(), null,
-                                                     UMLPackage.Literals.LITERAL_REAL);
-            spec.setValue(3.14); // TODO needs to be real value
-        }
-    }
-
+        List<String> attributeValues = SOAPUtils.getValuesFromElement(param.getName(), currentNode);
+
+        if (attributeValues.isEmpty()) {
+            if (param.getLower() == 0) {
+                // ignoring optional attribute
+                return;
+            }
+            else {
+                if (currentNode != null) {
+                    Console.traceln(Level.WARNING,
+                                    "required attribute not found in SOAP message: " + path + "." +
+                                        param.getName());
+                    Console.traceln(Level.WARNING, "setting default values for this attribute");
+                    Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
+                        SOAPUtils.getSerialization(currentNode));
+                }
+                attributeValues.add(null);
+            }
+        }
+        for (String attributeValue : attributeValues) {
+            if ("String".equals(param.getType().getName())) {
+                LiteralString spec =
+                    (LiteralString) argument.createOperand(param.getName(), null,
+                                                           UMLPackage.Literals.LITERAL_STRING);
+                if (attributeValue != null) {
+                    spec.setValue(attributeValue);
+                }
+                else {
+                    spec.setValue("foobar");
+                }
+            }
+            else if ("Integer".equals(param.getType().getName())) {
+                LiteralInteger spec =
+                    (LiteralInteger) argument.createOperand(param.getName(), null,
+                                                            UMLPackage.Literals.LITERAL_INTEGER);
+                if (attributeValue != null) {
+                    spec.setValue(Integer.parseInt(attributeValue));
+                }
+                else {
+                    spec.setValue(42);
+                }
+            }
+            else if ("Boolean".equals(param.getType().getName())) {
+                LiteralBoolean spec =
+                    (LiteralBoolean) argument.createOperand(param.getName(), null,
+                                                            UMLPackage.Literals.LITERAL_BOOLEAN);
+                if (attributeValue != null) {
+                    spec.setValue(Boolean.parseBoolean(attributeValue));
+                }
+                else {
+                    spec.setValue(true);
+                }
+            }
+            else if ("Real".equals(param.getType().getName())) {
+                LiteralReal spec =
+                    (LiteralReal) argument.createOperand(param.getName(), null,
+                                                         UMLPackage.Literals.LITERAL_REAL);
+                if (attributeValue != null) {
+                    spec.setValue(Double.parseDouble(attributeValue));
+                }
+                else {
+                    spec.setValue(3.14);
+                }
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * Creates a {@link Slot} in an {@link InstanceSpecification} for a primitive type.
+     * </p>
+     * 
+     * @param instSpec
+     *            instance specification to which the slot is added
+     * @param prop
+     *            property that describes the slot
+     * @param currentNode
+     *            DOM node from which is value for the slot is inferred
+     * @param path
+     *            used for warnings and debug information
+     */
     private static void createSlotPrimitiveType(InstanceSpecification instSpec,
                                                 Property prop,
@@ -1135,5 +1327,5 @@
                                                 String path)
     {
-        List<String> attributeValues = getPrimitiveTypeValuesFromElement(prop, currentNode);
+        List<String> attributeValues = SOAPUtils.getValuesFromElement(prop.getName(), currentNode);
 
         if (attributeValues.isEmpty()) {
@@ -1148,7 +1340,7 @@
                                         prop.getName());
                     Console.traceln(Level.WARNING, "setting default values for this attribute");
-                }
-                Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
-                    SOAPUtils.getSerialization(currentNode));
+                    Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
+                        SOAPUtils.getSerialization(currentNode));
+                }
                 attributeValues.add(null);
             }
@@ -1198,5 +1390,5 @@
                 }
                 else {
-                    value.setValue(3.14); // TODO needs to be real value
+                    value.setValue(3.14);
                 }
             }
@@ -1209,76 +1401,105 @@
     }
 
-    // TODO comment
-    private static List<org.w3c.dom.Element> getMatchingChildNode(Type type,
-                                                                  org.w3c.dom.Element parentNode)
-    {
-        return getMachingChildNode(type.getName(), parentNode);
-    }
-
-    // TODO comment
-    private static List<org.w3c.dom.Element> getMatchingChildNode(Property prop,
-                                                                  org.w3c.dom.Element parentNode)
-    {
-        return getMachingChildNode(prop.getName(), parentNode);
-    }
-
-    // TODO comment
-    private static List<org.w3c.dom.Element> getMachingChildNode(String typeNameRaw,
-                                                                 org.w3c.dom.Element parentNode)
-    {
-        List<org.w3c.dom.Element> matchingNodes = new ArrayList<>();
-        Node parameterNode = null;
-        if (parentNode != null) {
-            NodeList parameterNodes = parentNode.getChildNodes();
-            String[] typeNameSplit = typeNameRaw.split(":");
-            String typeName = typeNameSplit[typeNameSplit.length - 1];
-            for (int i = 0; i < parameterNodes.getLength(); i++) {
-                parameterNode = parameterNodes.item(i);
-                if (parameterNode.getNodeType() == Node.ELEMENT_NODE) {
-                    String[] parameterNodeSplit = parameterNode.getNodeName().split(":");
-                    String parameterNodeName = parameterNodeSplit[parameterNodeSplit.length - 1];
-                    if (typeName.equals(parameterNodeName)) {
-                        matchingNodes.add((org.w3c.dom.Element) parameterNode);
-                    }
-                }
-            }
-            /*
-             * if( !matchingSOAPFound) { Console.traceln(Level.WARNING,
-             * "could not look up name of parameter in SOAP request: " + typeName); if(
-             * elementCount==0 ) { Console.traceln(Level.INFO, "\tno parameters found"); } else {
-             * Console.traceln(Level.INFO, "\tparameters found:"); for( int i=0 ;
-             * i<parameterNodes.getLength(); i++ ) { if(
-             * parameterNodes.item(i).getNodeType()==Node.ELEMENT_NODE ) {
-             * Console.traceln(Level.INFO, "\t\t" + parameterNodes.item(i).getNodeName()); } } }
-             * Console.traceln(Level.WARNING,
-             * "using dummy values for this parameter (and nested values) instead"); }
-             */
-        }
-        return matchingNodes;
-    }
-
-    // TODO
-    private static List<String> getPrimitiveTypeValuesFromElement(Property prop,
-                                                                  org.w3c.dom.Element currentNode)
-    {
-        List<String> attributeValues = new LinkedList<>();
-
-        if (currentNode != null) {
-            // first check attributes of the node
-            Attr attribute = currentNode.getAttributeNode(prop.getName());
-            if (attribute != null) {
-                attributeValues.add(attribute.getValue());
+    /**
+     * <p>
+     * Sets values for the parameters of a reply message. The values are, all LiterealNull and to
+     * the INOUT, OUT and REPLY parameters, the UTP stereotype LiteralAny is applied.
+     * </p>
+     * 
+     * @param replyMessage
+     *            reply message for which the parameters are set
+     * @param calledOperation
+     *            operation that is replied for by the message
+     */
+    private static void setReplyMessageParameters(Message replyMessage, Operation calledOperation) {
+        for (Parameter param : calledOperation.getOwnedParameters()) {
+            Expression argument =
+                (Expression) replyMessage.createArgument(param.getName(), param.getType(),
+                                                         UMLPackage.Literals.EXPRESSION);
+            if (isOutParameter(param)) {
+                ValueSpecification operand =
+                    argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
+                operand.applyStereotype(UTPUtils.getLiteralAnyStereotype(param.getModel()));
             }
             else {
-                // now check elements
-                List<org.w3c.dom.Element> elements = getMatchingChildNode(prop, currentNode);
-                for (org.w3c.dom.Element element : elements) {
-                    attributeValues.add(element.getTextContent());
-                }
-            }
-        }
-
-        return attributeValues;
-    }
-
+                argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * Checks if a parameter has the direction IN or INOUT
+     * </p>
+     * 
+     * @param parameter
+     *            parameter that is checked
+     * @return true if the direction is IN or INOUT; false otherwise
+     */
+    private static boolean isInParameter(Parameter parameter) {
+        return parameter.getDirection() == ParameterDirectionKind.IN_LITERAL ||
+            parameter.getDirection() == ParameterDirectionKind.INOUT_LITERAL;
+    }
+
+    /**
+     * <p>
+     * Checks if a parameter has the direction RETURN, OUT or INOUT
+     * </p>
+     * 
+     * @param parameter
+     *            parameter that is checked
+     * @return true if the direction is RETURN, OUT, or INOUT; false otherwise
+     */
+    private static boolean isOutParameter(Parameter parameter) {
+        return parameter.getDirection() == ParameterDirectionKind.RETURN_LITERAL ||
+            parameter.getDirection() == ParameterDirectionKind.OUT_LITERAL ||
+            parameter.getDirection() == ParameterDirectionKind.INOUT_LITERAL;
+    }
+
+    /**
+     * <p>
+     * Checks if the {@link MessageSort} of a message is a call message, i.e., ASYNCH_CALL or
+     * SYNCH_CALL.
+     * </p>
+     * 
+     * @param message
+     *            message that is checked
+     * @return true if the message is a call message; false otherwise
+     */
+    private static boolean isCallMessage(Message message) {
+        if (message == null) {
+            return false;
+        }
+        MessageSort msgSort = message.getMessageSort();
+        return msgSort == MessageSort.ASYNCH_CALL_LITERAL ||
+            msgSort == MessageSort.SYNCH_CALL_LITERAL;
+    }
+
+    /**
+     * <p>
+     * inverse-sorts the values of a map. Has been adapted from <a href=
+     * "http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java"
+     * >this</a> StackOverflow post.
+     * </p>
+     * 
+     * @param map
+     *            map whose values are sorted
+     * @return sorted version of the map
+     */
+    private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
+        // TODO possibly move to another class
+        List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
+        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
+            @Override
+            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
+                return -1 * (o1.getValue()).compareTo(o2.getValue());
+            }
+        });
+
+        Map<K, V> result = new LinkedHashMap<>();
+        for (Map.Entry<K, V> entry : list) {
+            result.put(entry.getKey(), entry.getValue());
+        }
+        return result;
+    }
 }
Index: trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UTPUtils.java
===================================================================
--- trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UTPUtils.java	(revision 1929)
+++ trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UTPUtils.java	(revision 1929)
@@ -0,0 +1,115 @@
+//   Copyright 2012 Georg-August-Universität Göttingen, Germany
+//
+//   Licensed under the Apache License, Version 2.0 (the "License");
+//   you may not use this file except in compliance with the License.
+//   You may obtain a copy of the License at
+//
+//       http://www.apache.org/licenses/LICENSE-2.0
+//
+//   Unless required by applicable law or agreed to in writing, software
+//   distributed under the License is distributed on an "AS IS" BASIS,
+//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//   See the License for the specific language governing permissions and
+//   limitations under the License.
+
+package de.ugoe.cs.autoquest.plugin.uml;
+
+import org.eclipse.uml2.uml.Model;
+import org.eclipse.uml2.uml.Package;
+import org.eclipse.uml2.uml.Profile;
+import org.eclipse.uml2.uml.Stereotype;
+
+/**
+ * <p>
+ * Helper functions for working with the UML Testing Profile (UTP).
+ * </p>
+ * 
+ * @author Steffen Herbold
+ */
+public class UTPUtils {
+
+    /**
+     * <p>
+     * Retrieves the UTP profile from a UML {@link Package}, if it is applied.
+     * </p>
+     * 
+     * @param umlModel
+     *            model where the profile is retrieved from or added to
+     * @return UTP profile
+     */
+    public static Profile getOrApplyUtpProfile(Model umlModel) {
+        // TODO apply UTP if it is missing
+        return umlModel.getAppliedProfile("utp");
+    }
+
+    /**
+     * <p>
+     * Retrieves the UTP TestCase stereotype. In case UTP is not yet applied to the model, it is
+     * applied automatically.
+     * </p>
+     * 
+     * @param umlModel
+     *            model for the which the UTP TestCase stereotype is retrieved
+     * @return UTP TestCase stereotype
+     */
+    public static Stereotype getTestCaseStereotype(Model umlModel) {
+        return (Stereotype) getOrApplyUtpProfile(umlModel).getOwnedMember("TestCase");
+    }
+
+    /**
+     * <p>
+     * Retrieves the UTP TestComponent stereotype. In case UTP is not yet applied to the model, it
+     * is applied automatically.
+     * </p>
+     * 
+     * @param umlModel
+     *            model for the which the UTP TestComponent stereotype is retrieved
+     * @return UTP TestComponent stereotype
+     */
+    public static Stereotype getTestComponentStereotype(Model umlModel) {
+        return (Stereotype) getOrApplyUtpProfile(umlModel).getOwnedMember("TestComponent");
+    }
+
+    /**
+     * <p>
+     * Retrieves the UTP TestContext stereotype. In case UTP is not yet applied to the model, it is
+     * applied automatically.
+     * </p>
+     * 
+     * @param umlModel
+     *            model for the which the UTP TestContext stereotype is retrieved
+     * @return UTP TestContext stereotype
+     */
+    public static Stereotype getTestContextStereotype(Model umlModel) {
+        return (Stereotype) getOrApplyUtpProfile(umlModel).getOwnedMember("TestContext");
+    }
+
+    /**
+     * <p>
+     * Retrieves the UTP SUT stereotype. In case UTP is not yet applied to the model, it is applied
+     * automatically.
+     * </p>
+     * 
+     * @param umlModel
+     *            model for the which the UTP SUT stereotype is retrieved
+     * @return UTP SUT stereotype
+     */
+    public static Stereotype getSUTStereotype(Model umlModel) {
+        return (Stereotype) getOrApplyUtpProfile(umlModel).getOwnedMember("SUT");
+    }
+
+    /**
+     * <p>
+     * Retrieves the UTP LiteralAny stereotype. In case UTP is not yet applied to the model, it is
+     * applied automatically.
+     * </p>
+     * 
+     * @param umlModel
+     *            model for the which the UTP LiteralAny stereotype is retrieved
+     * @return UTP LiteralAny stereotype
+     */
+    public static Stereotype getLiteralAnyStereotype(Model umlModel) {
+        return (Stereotype) getOrApplyUtpProfile(umlModel).getOwnedMember("LiteralAny");
+    }
+
+}
