Ignore:
Timestamp:
03/05/15 17:19:36 (9 years ago)
Author:
sherbold
Message:
  • UMLUtils can now find test contexts within packages
  • UMLUtils can now work with components that implement multiple interfaces
  • UMLUtils can now create synchronous and asynchronous messages
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java

    r1835 r1896  
    3030import org.eclipse.uml2.uml.ActivityEdge; 
    3131import org.eclipse.uml2.uml.ActivityNode; 
     32import org.eclipse.uml2.uml.BehaviorExecutionSpecification; 
    3233import org.eclipse.uml2.uml.CallOperationAction; 
    3334import org.eclipse.uml2.uml.Comment; 
     
    4546import org.eclipse.uml2.uml.Model; 
    4647import org.eclipse.uml2.uml.Operation; 
     48import org.eclipse.uml2.uml.Package; 
    4749import org.eclipse.uml2.uml.Port; 
    4850import org.eclipse.uml2.uml.Profile; 
     
    267269     * @param interactionName 
    268270     *            name of the interaction 
     271     * @param testContextName 
     272     *            Name of the test context that should be used. If this value is null, the first test context found is used. 
    269273     */ 
    270274    public static void createInteractionFromEventSequence(List<Event> sequence, 
    271275                                                          Model model, 
    272                                                           String interactionName) 
     276                                                          String interactionName, 
     277                                                          String testContextName) 
    273278    { 
    274  
    275         Component testContext = fetchTestContext(model); 
    276  
    277279        final Profile utpProfile = model.getAppliedProfile("utp"); 
    278280        final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase"); 
    279281        final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent"); 
    280282        final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT"); 
     283        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 
     284 
     285        Component testContext = fetchTestContext(model, utpTestContext, testContextName); 
     286        if( testContext==null ) { 
     287            throw new RuntimeException("Could not find any test context in the model"); 
     288        } 
    281289         
    282290        Operation operation = testContext.createOwnedOperation(interactionName, null, null); 
     
    310318                String serviceName = getServiceNameFromEvent(event); 
    311319                String methodName = getCalledMethodFromEvent(event); 
    312                  
    313320                // determine lifelines 
    314321                Lifeline msgTargetLifeline; 
     
    332339                    msgTargetLifeline = interaction.getLifeline(serviceName); 
    333340                } 
    334  
    335                 // determine target interface 
    336                 Interface targetInterface = getRealizedInterfaceFromProperty((Property) msgTargetLifeline.getRepresents()); 
    337341                 
    338                 // create message 
    339                 MessageOccurrenceSpecification sendFragment = 
    340                     (MessageOccurrenceSpecification) interaction 
    341                         .createFragment(i + ":" + methodName + "_sendFragment", 
    342                                         UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
    343                 MessageOccurrenceSpecification recvFragment = 
    344                     (MessageOccurrenceSpecification) interaction 
    345                         .createFragment(i + ":" + methodName + "_recvFragment", 
    346                                         UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
    347  
    348                 sendFragment.setCovered(msgSourceLifeline); 
    349                 recvFragment.setCovered(msgTargetLifeline); 
    350  
    351                 Message message = interaction.createMessage(methodName); 
    352                 if (getOperationFromName(targetInterface.getOperations(), methodName) == null) { 
    353                     System.out.println("operation not found in the " + targetInterface.getName() + " interface: " + methodName); 
    354                 } 
    355                 message.setSignature(getOperationFromName(targetInterface.getOperations(), 
    356                                                           methodName)); 
    357                 message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL); 
    358                 message.setSendEvent(sendFragment); 
    359                 message.setReceiveEvent(recvFragment); 
    360  
    361                 // now the connector needs to be determined 
    362                 EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes(); 
    363                 EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes(); 
     342                // TODO null checks for lifelines 
     343 
     344                // determine target interface and be able to deal with multiple interfaces 
     345                List<Interface> targetInterfaces = getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents()); 
     346                if( targetInterfaces.isEmpty() ) { 
     347                    throw new RuntimeException("no interface associated with the property " + msgTargetLifeline.getRepresents().getName()); 
     348                } 
     349                Interface targetInterface = null; 
     350                for( Interface intface : targetInterfaces ) { 
     351                    System.out.println(intface.getOperations()); 
     352                    if (getOperationFromName(intface.getOperations(), methodName) != null) { 
     353                        // interface found 
     354                        targetInterface = intface; 
     355                        break; 
     356                    } 
     357                } 
     358                if( targetInterface == null ) { 
     359                    StringBuilder errStrBuilder = new StringBuilder(); 
     360                    errStrBuilder.append("operation not found in the implementing interfaces ("); 
     361                    Iterator<Interface> iter = targetInterfaces.iterator(); 
     362                    while( iter.hasNext() ) { 
     363                        String interfaceName = iter.next().getName(); 
     364                        errStrBuilder.append(interfaceName); 
     365                        if( iter.hasNext() ) { 
     366                            errStrBuilder.append(","); 
     367                        } else { 
     368                            errStrBuilder.append("): " + methodName); 
     369                        } 
     370                    } 
     371                    throw new RuntimeException(errStrBuilder.toString()); 
     372                } 
    364373                 
    365                 for( Property userAttribute : userAttributes ) { 
    366                     if( userAttribute instanceof Port ) { 
    367                         EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds(); 
    368                         for( ConnectorEnd userEnd : userEnds ) { 
    369                             Connector userConnector = (Connector) userEnd.eContainer(); 
    370                             for( Property targetAttribute : targetAttributes ) { 
    371                                 if( targetAttribute instanceof Port ) { 
    372                                     EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds(); 
    373                                     for( ConnectorEnd targetEnd : targetEnds ) { 
    374                                         Connector targetConnector = (Connector) targetEnd.eContainer(); 
    375                                         if( targetConnector==userConnector ) { 
    376                                             message.setConnector(targetConnector); 
     374                // create message ASYNCH 
     375                boolean asynch = false; 
     376                if( asynch ) { 
     377                    MessageOccurrenceSpecification sendFragment = 
     378                        (MessageOccurrenceSpecification) interaction 
     379                            .createFragment(i + ":" + methodName + "_sendFragment", 
     380                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     381                    MessageOccurrenceSpecification recvFragment = 
     382                        (MessageOccurrenceSpecification) interaction 
     383                            .createFragment(i + ":" + methodName + "_recvFragment", 
     384                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     385     
     386                    sendFragment.setCovered(msgSourceLifeline); 
     387                    recvFragment.setCovered(msgTargetLifeline); 
     388     
     389                    Message message = interaction.createMessage(methodName); 
     390                    if (getOperationFromName(targetInterface.getOperations(), methodName) == null) { 
     391                        System.out.println("operation not found in the " + targetInterface.getName() + " interface: " + methodName); 
     392                    } 
     393                    message.setSignature(getOperationFromName(targetInterface.getOperations(), 
     394                                                              methodName)); 
     395                    message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL); 
     396                    message.setSendEvent(sendFragment); 
     397                    message.setReceiveEvent(recvFragment); 
     398     
     399                    // now the connector needs to be determined 
     400                    EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes(); 
     401                    EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes(); 
     402                     
     403                    for( Property userAttribute : userAttributes ) { 
     404                        if( userAttribute instanceof Port ) { 
     405                            EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds(); 
     406                            for( ConnectorEnd userEnd : userEnds ) { 
     407                                Connector userConnector = (Connector) userEnd.eContainer(); 
     408                                for( Property targetAttribute : targetAttributes ) { 
     409                                    if( targetAttribute instanceof Port ) { 
     410                                        EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds(); 
     411                                        for( ConnectorEnd targetEnd : targetEnds ) { 
     412                                            Connector targetConnector = (Connector) targetEnd.eContainer(); 
     413                                            if( targetConnector==userConnector ) { 
     414                                                message.setConnector(targetConnector); 
     415                                            } 
    377416                                        } 
    378417                                    } 
     
    381420                        } 
    382421                    } 
     422                    sendFragment.setMessage(message); 
     423                    recvFragment.setMessage(message); 
     424                } else { 
     425                    // SYNCH call 
     426                    MessageOccurrenceSpecification callSendFragment = 
     427                        (MessageOccurrenceSpecification) interaction 
     428                            .createFragment(i + ":" + methodName + "_callSendFragment", 
     429                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     430                    MessageOccurrenceSpecification callRecvFragment = 
     431                        (MessageOccurrenceSpecification) interaction 
     432                            .createFragment(i + ":" + methodName + "_callRecvFragment", 
     433                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     434                     
     435                    MessageOccurrenceSpecification replySendFragment = 
     436                        (MessageOccurrenceSpecification) interaction 
     437                            .createFragment(i + ":" + methodName + "_replySendFragment", 
     438                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     439                    MessageOccurrenceSpecification replyRecvFragment = 
     440                        (MessageOccurrenceSpecification) interaction 
     441                            .createFragment(i + ":" + methodName + "_replyRecvFragment", 
     442                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     443     
     444                    BehaviorExecutionSpecification sourceBehaviorExecutionSpecification =  
     445                        (BehaviorExecutionSpecification) interaction 
     446                            .createFragment(":" + methodName + "_sourceBhvExecSpec", 
     447                                            UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION); 
     448                    BehaviorExecutionSpecification targetBehaviorExecutionSpecification =  
     449                        (BehaviorExecutionSpecification) interaction 
     450                            .createFragment(":" + methodName + "_targetBhvExecSpec", 
     451                                            UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION); 
     452                     
     453                     
     454                    callSendFragment.setCovered(msgSourceLifeline); 
     455                    callRecvFragment.setCovered(msgTargetLifeline); 
     456                    replySendFragment.setCovered(msgTargetLifeline); 
     457                    replyRecvFragment.setCovered(msgSourceLifeline); 
     458                     
     459                    sourceBehaviorExecutionSpecification.setStart(callSendFragment); 
     460                    sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment); 
     461                    targetBehaviorExecutionSpecification.setStart(callRecvFragment); 
     462                    targetBehaviorExecutionSpecification.setFinish(replySendFragment); 
     463     
     464                    Operation calledOperation = getOperationFromName(targetInterface.getOperations(), methodName); 
     465                     
     466                    // create call 
     467                    Message callMessage = interaction.createMessage(methodName); 
     468                    callMessage.setSignature(calledOperation); 
     469                    callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL); 
     470                    callMessage.setSendEvent(callSendFragment); 
     471                    callMessage.setReceiveEvent(callRecvFragment); 
     472                    callSendFragment.setMessage(callMessage); 
     473                    callRecvFragment.setMessage(callMessage); 
     474     
     475                    // now the connector needs to be determined 
     476                    EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes(); 
     477                    EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes(); 
     478                     
     479                    for( Property userAttribute : userAttributes ) { 
     480                        if( userAttribute instanceof Port ) { 
     481                            EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds(); 
     482                            for( ConnectorEnd userEnd : userEnds ) { 
     483                                Connector userConnector = (Connector) userEnd.eContainer(); 
     484                                for( Property targetAttribute : targetAttributes ) { 
     485                                    if( targetAttribute instanceof Port ) { 
     486                                        EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds(); 
     487                                        for( ConnectorEnd targetEnd : targetEnds ) { 
     488                                            Connector targetConnector = (Connector) targetEnd.eContainer(); 
     489                                            if( targetConnector==userConnector ) { 
     490                                                callMessage.setConnector(targetConnector); 
     491                                            } 
     492                                        } 
     493                                    } 
     494                                } 
     495                            } 
     496                        } 
     497                    } 
     498                     
     499                    // create reply 
     500                    Message replyMessage = interaction.createMessage(methodName + "_reply"); 
     501                    replyMessage.setMessageSort(MessageSort.REPLY_LITERAL); 
     502                    replyMessage.setSignature(calledOperation); 
     503                    replyMessage.setSendEvent(replySendFragment); 
     504                    replyMessage.setReceiveEvent(replyRecvFragment); 
     505                    replySendFragment.setMessage(replyMessage); 
     506                    replyRecvFragment.setMessage(replyMessage); 
    383507                } 
    384508                 
    385                 sendFragment.setMessage(message); 
    386                 recvFragment.setMessage(message); 
    387509 
    388510                i++; 
     
    441563     *            model to be extended 
    442564     * @param usageProfile 
    443      *            usage profile used as foundation 
    444      */ 
    445     public static void createScheduling(Model model, IStochasticProcess usageProfile) { 
     565     *            usage profile used as foundation  
     566     */ 
     567    public static void createScheduling(Model model, IStochasticProcess usageProfile, String testContextName) { 
    446568 
    447569        final Profile utpProfile = model.getAppliedProfile("utp"); 
    448570        final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase"); 
    449  
    450         Component testContext = fetchTestContext(model); 
     571        final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 
     572 
     573        Component testContext = fetchTestContext(model, utpTestContext, testContextName); 
     574        if( testContext==null ) { 
     575            throw new RuntimeException("Could not find any test context in the model"); 
     576        } 
    451577 
    452578        Map<Operation, Double> usageScoreMapUnsorted = new HashMap<>(); 
     
    618744    } 
    619745     
    620     private static Interface getRealizedInterfaceFromProperty(Property property) { 
     746    private static List<Interface> getRealizedInterfacesFromProperty(Property property) { 
    621747        return getRealizedInterfaceFromComponent((Component) property.getType()); 
    622748    } 
    623749     
    624     private static Interface getRealizedInterfaceFromComponent(Component comp) { 
    625         Interface myInterface = null; 
     750    private static List<Interface> getRealizedInterfaceFromComponent(Component comp) { 
     751        List<Interface> interfaces = new LinkedList<>(); 
     752        //Interface myInterface = null; 
    626753        for( Property property : comp.getAttributes() ) { 
    627754            if( property instanceof Port ) { 
    628755                Port port = (Port) property; 
    629756                if( !port.isConjugated() ) { 
     757                    interfaces.addAll(port.getProvideds()); 
     758                    /* 
    630759                    if( myInterface==null ) { 
    631760                        myInterface = port.getProvideds().get(0); 
     
    634763                        System.err.println("multiple different interfaces found"); 
    635764                    } 
    636                 } 
    637             } 
    638         } 
    639         return myInterface; 
     765                    */ 
     766                } 
     767            } 
     768        } 
     769        return interfaces; 
     770        //return myInterface; 
    640771        //return ((Port) comp.getAttributes().get(0)).getInterface(); 
    641772        //Realization realization = (Realization) comp.getNestedClassifiers().get(0).getRelationships(UMLPackage.Literals.REALIZATION).get(0); 
     
    643774    } 
    644775     
    645     private static Component fetchTestContext(Model model) { 
    646         final Profile utpProfile = model.getAppliedProfile("utp"); 
    647         final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 
    648          
    649         for( Element element : model.getOwnedElements() ) { 
    650             if( element instanceof Component && element.getApplicableStereotypes().contains(utpTestContext) ) { 
    651                 return (Component) element; 
    652             } 
    653         } 
    654         return null; 
     776    private static Component fetchTestContext(Package pkg, Stereotype utpTestContext, String testContextName) { 
     777        List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext); 
     778        if( testContexts.isEmpty() ) { 
     779            return null; 
     780        } 
     781        if( testContextName!=null ) { 
     782            for( Component testContext : testContexts ) { 
     783                if( testContextName.equals(testContext.getName()) ) { 
     784                    return testContext; 
     785                } 
     786            } 
     787            return null; 
     788        } else { 
     789            return testContexts.get(0); 
     790        } 
     791    } 
     792     
     793    private static List<Component> fetchTestContextRecursively(Package pkg, Stereotype utpTestContext) { 
     794        List<Component> testContexts = new LinkedList<>(); 
     795        for( Element element : pkg.getOwnedElements() ) { 
     796            if( element instanceof Package ) { 
     797                testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext)); 
     798            } 
     799            if( element instanceof Component && element.getAppliedStereotypes().contains(utpTestContext) ) { 
     800                testContexts.add((Component) element); 
     801            } 
     802        } 
     803        return testContexts; 
    655804    } 
    656805} 
Note: See TracChangeset for help on using the changeset viewer.