Ignore:
Timestamp:
03/05/15 17:58:16 (9 years ago)
Author:
sherbold
Message:
  • refactorings
File:
1 edited

Legend:

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

    r1896 r1897  
    270270     *            name of the interaction 
    271271     * @param testContextName 
    272      *            Name of the test context that should be used. If this value is null, the first test context found is used. 
     272     *            Name of the test context that should be used. If this value is null, the first 
     273     *            test context found is used. 
    273274     */ 
    274275    public static void createInteractionFromEventSequence(List<Event> sequence, 
     
    284285 
    285286        Component testContext = fetchTestContext(model, utpTestContext, testContextName); 
    286         if( testContext==null ) { 
     287        if (testContext == null) { 
    287288            throw new RuntimeException("Could not find any test context in the model"); 
    288289        } 
    289          
     290 
    290291        Operation operation = testContext.createOwnedOperation(interactionName, null, null); 
    291292        operation.applyStereotype(utpTestCase); 
     
    298299        // create lifelines 
    299300        Lifeline userLifeline = null; 
    300         // List<Port> userPorts = new LinkedList<>(); 
     301         
    301302        for (Property property : testContext.getAllAttributes()) { 
    302303            if (property.getAppliedStereotypes().contains(utpSUT)) { 
     
    306307            } 
    307308            else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) { 
     309                if( userLifeline!=null ) { 
     310                    throw new RuntimeException("TestContext must only have one TestComponent for the application of usage-based testing."); 
     311                } 
    308312                userLifeline = interaction.createLifeline(property.getName()); 
    309313                userLifeline.setRepresents(property); 
    310             }     
     314            } 
     315        } 
     316        if( userLifeline==null ) { 
     317            throw new RuntimeException("No TestComponent found, could not create user lifeline."); 
     318        } 
     319        if( interaction.getLifelines().size()<2 ) { 
     320            throw new RuntimeException("Fewer than two lifelines created. No SUT found."); 
    311321        } 
    312322         
    313         // TODO sanity checks for userLifeline!=null, etc. 
    314  
    315323        int i = 0; 
    316324        for (Event event : sequence) { 
     
    321329                Lifeline msgTargetLifeline; 
    322330                Lifeline msgSourceLifeline; 
    323                  
    324                 if( serviceName.equals(userLifeline.getName()) ) { 
     331 
     332                if (serviceName.equals(userLifeline.getName())) { 
    325333                    // message being send to user 
    326334                    // currently we just select the first lifeline that is not the user 
    327                     // this, obviously, has to be replaced with the real service.  
     335                    // this, obviously, has to be replaced with the real service. 
    328336                    // however, identification of the source of a message is still an open issue 
    329337                    msgSourceLifeline = null; 
    330                     for( Lifeline lifeline : interaction.getLifelines() ) { 
    331                         if(!lifeline.equals(userLifeline)){ 
     338                    for (Lifeline lifeline : interaction.getLifelines()) { 
     339                        if (!lifeline.equals(userLifeline)) { 
    332340                            msgSourceLifeline = lifeline; 
    333341                            break; 
     
    335343                    } 
    336344                    msgTargetLifeline = userLifeline; 
    337                 } else { 
     345                } 
     346                else { 
    338347                    msgSourceLifeline = userLifeline; 
    339348                    msgTargetLifeline = interaction.getLifeline(serviceName); 
    340349                } 
    341                  
    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()); 
     350                if( msgSourceLifeline==null ) { 
     351                    throw new RuntimeException("Error creating message: could not determine source lifeline."); 
     352                } 
     353                if( msgTargetLifeline==null ) { 
     354                    throw new RuntimeException("Error creating message: could not determine target lifeline."); 
     355                } 
     356                // determine correct target interface 
     357                List<Interface> targetInterfaces = 
     358                    getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents()); 
     359                if (targetInterfaces.isEmpty()) { 
     360                    throw new RuntimeException("no interface associated with the property " + 
     361                        msgTargetLifeline.getRepresents().getName()); 
    348362                } 
    349363                Interface targetInterface = null; 
    350                 for( Interface intface : targetInterfaces ) { 
     364                for (Interface intface : targetInterfaces) { 
    351365                    System.out.println(intface.getOperations()); 
    352366                    if (getOperationFromName(intface.getOperations(), methodName) != null) { 
     
    356370                    } 
    357371                } 
    358                 if( targetInterface == null ) { 
     372                if (targetInterface == null) { 
    359373                    StringBuilder errStrBuilder = new StringBuilder(); 
    360                     errStrBuilder.append("operation not found in the implementing interfaces ("); 
     374                    errStrBuilder.append("Error creating message: operation not found in the implementing interfaces ("); 
    361375                    Iterator<Interface> iter = targetInterfaces.iterator(); 
    362                     while( iter.hasNext() ) { 
     376                    while (iter.hasNext()) { 
    363377                        String interfaceName = iter.next().getName(); 
    364378                        errStrBuilder.append(interfaceName); 
    365                         if( iter.hasNext() ) { 
     379                        if (iter.hasNext()) { 
    366380                            errStrBuilder.append(","); 
    367                         } else { 
     381                        } 
     382                        else { 
    368383                            errStrBuilder.append("): " + methodName); 
    369384                        } 
     
    371386                    throw new RuntimeException(errStrBuilder.toString()); 
    372387                } 
    373                  
    374                 // create message ASYNCH 
     388 
     389                Operation calledOperation = 
     390                    getOperationFromName(targetInterface.getOperations(), methodName); 
     391 
     392                // setup for both SYNCH and ASYNCH calls 
     393                MessageOccurrenceSpecification callSendFragment = 
     394                    (MessageOccurrenceSpecification) interaction 
     395                        .createFragment(i + ":" + methodName + "_callSendFragment", 
     396                                        UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     397                MessageOccurrenceSpecification callRecvFragment = 
     398                    (MessageOccurrenceSpecification) interaction 
     399                        .createFragment(i + ":" + methodName + "_callRecvFragment", 
     400                                        UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
     401 
     402                callSendFragment.setCovered(msgSourceLifeline); 
     403                callRecvFragment.setCovered(msgTargetLifeline); 
     404 
     405                // create call 
     406                Message callMessage = interaction.createMessage(methodName); 
     407                callMessage.setSignature(calledOperation); 
     408                callMessage.setConnector(inferConnector(msgSourceLifeline, msgTargetLifeline)); 
     409                callMessage.setSendEvent(callSendFragment); 
     410                callMessage.setReceiveEvent(callRecvFragment); 
     411                callSendFragment.setMessage(callMessage); 
     412                callRecvFragment.setMessage(callMessage); 
     413 
     414                // TODO somehow infer if called operation is SYNCH or ASYNCH 
     415                // possibly requires additional stereotype 
    375416                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                                             } 
    416                                         } 
    417                                     } 
    418                                 } 
    419                             } 
    420                         } 
    421                     } 
    422                     sendFragment.setMessage(message); 
    423                     recvFragment.setMessage(message); 
    424                 } else { 
     417                if (asynch) { 
     418                    // Create ASYNCH call 
     419                    callMessage.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL); 
     420                } 
     421                else { 
    425422                    // 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                      
     423                    callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL); 
     424 
     425                    // setup reply and behavior execution specifications 
    435426                    MessageOccurrenceSpecification replySendFragment = 
    436427                        (MessageOccurrenceSpecification) interaction 
     
    441432                            .createFragment(i + ":" + methodName + "_replyRecvFragment", 
    442433                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 
    443      
    444                     BehaviorExecutionSpecification sourceBehaviorExecutionSpecification =  
     434 
     435                    replySendFragment.setCovered(msgTargetLifeline); 
     436                    replyRecvFragment.setCovered(msgSourceLifeline); 
     437                     
     438                    /*BehaviorExecutionSpecification sourceBehaviorExecutionSpecification = 
    445439                        (BehaviorExecutionSpecification) interaction 
    446440                            .createFragment(":" + methodName + "_sourceBhvExecSpec", 
    447441                                            UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION); 
    448                     BehaviorExecutionSpecification targetBehaviorExecutionSpecification =  
     442                    BehaviorExecutionSpecification targetBehaviorExecutionSpecification = 
    449443                        (BehaviorExecutionSpecification) interaction 
    450444                            .createFragment(":" + methodName + "_targetBhvExecSpec", 
    451445                                            UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION); 
    452                      
    453                      
    454                     callSendFragment.setCovered(msgSourceLifeline); 
    455                     callRecvFragment.setCovered(msgTargetLifeline); 
    456                     replySendFragment.setCovered(msgTargetLifeline); 
    457                     replyRecvFragment.setCovered(msgSourceLifeline); 
    458                      
     446 
    459447                    sourceBehaviorExecutionSpecification.setStart(callSendFragment); 
    460448                    sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment); 
    461449                    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                      
     450                    targetBehaviorExecutionSpecification.setFinish(replySendFragment);*/ 
     451 
    499452                    // create reply 
    500453                    Message replyMessage = interaction.createMessage(methodName + "_reply"); 
     
    506459                    replyRecvFragment.setMessage(replyMessage); 
    507460                } 
    508                  
    509461 
    510462                i++; 
     
    535487        for (InteractionFragment interactionFragment : interactionFragments) { 
    536488            if (interactionFragment.getName() != null && 
    537                 interactionFragment.getName().endsWith("_recvFragment")) 
     489                interactionFragment.getName().endsWith("_recvFragment")) // TODO must be more 
     490                                                                         // generic 
    538491            { 
    539492                String serviceName = 
     
    563516     *            model to be extended 
    564517     * @param usageProfile 
    565      *            usage profile used as foundation  
    566      */ 
    567     public static void createScheduling(Model model, IStochasticProcess usageProfile, String testContextName) { 
     518     *            usage profile used as foundation 
     519     */ 
     520    public static void createScheduling(Model model, 
     521                                        IStochasticProcess usageProfile, 
     522                                        String testContextName) 
     523    { 
    568524 
    569525        final Profile utpProfile = model.getAppliedProfile("utp"); 
     
    572528 
    573529        Component testContext = fetchTestContext(model, utpTestContext, testContextName); 
    574         if( testContext==null ) { 
     530        if (testContext == null) { 
    575531            throw new RuntimeException("Could not find any test context in the model"); 
    576532        } 
     
    743699        return matching; 
    744700    } 
    745      
     701 
    746702    private static List<Interface> getRealizedInterfacesFromProperty(Property property) { 
    747703        return getRealizedInterfaceFromComponent((Component) property.getType()); 
    748704    } 
    749      
     705 
    750706    private static List<Interface> getRealizedInterfaceFromComponent(Component comp) { 
    751707        List<Interface> interfaces = new LinkedList<>(); 
    752         //Interface myInterface = null; 
    753         for( Property property : comp.getAttributes() ) { 
    754             if( property instanceof Port ) { 
     708        // Interface myInterface = null; 
     709        for (Property property : comp.getAttributes()) { 
     710            if (property instanceof Port) { 
    755711                Port port = (Port) property; 
    756                 if( !port.isConjugated() ) { 
    757                     interfaces.addAll(port.getProvideds()); 
    758                     /* 
    759                     if( myInterface==null ) { 
    760                         myInterface = port.getProvideds().get(0); 
    761                     }  
    762                     else if( myInterface!=port.getProvideds().get(0)) { 
    763                         System.err.println("multiple different interfaces found"); 
    764                     } 
    765                     */ 
     712                if (!port.isConjugated()) { 
     713                    interfaces.addAll(port.getProvideds());                   
    766714                } 
    767715            } 
    768716        } 
    769717        return interfaces; 
    770         //return myInterface; 
    771         //return ((Port) comp.getAttributes().get(0)).getInterface(); 
    772         //Realization realization = (Realization) comp.getNestedClassifiers().get(0).getRelationships(UMLPackage.Literals.REALIZATION).get(0); 
    773         //return (Interface) realization.getSuppliers().get(0); 
    774     } 
    775      
    776     private static Component fetchTestContext(Package pkg, Stereotype utpTestContext, String testContextName) { 
     718    } 
     719 
     720    private static Component fetchTestContext(Package pkg, 
     721                                              Stereotype utpTestContext, 
     722                                              String testContextName) 
     723    { 
    777724        List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext); 
    778         if( testContexts.isEmpty() ) { 
     725        if (testContexts.isEmpty()) { 
    779726            return null; 
    780727        } 
    781         if( testContextName!=null ) { 
    782             for( Component testContext : testContexts ) { 
    783                 if( testContextName.equals(testContext.getName()) ) { 
     728        if (testContextName != null) { 
     729            for (Component testContext : testContexts) { 
     730                if (testContextName.equals(testContext.getName())) { 
    784731                    return testContext; 
    785732                } 
    786733            } 
    787734            return null; 
    788         } else { 
     735        } 
     736        else { 
    789737            return testContexts.get(0); 
    790738        } 
    791739    } 
    792      
    793     private static List<Component> fetchTestContextRecursively(Package pkg, Stereotype utpTestContext) { 
     740 
     741    private static List<Component> fetchTestContextRecursively(Package pkg, 
     742                                                               Stereotype utpTestContext) 
     743    { 
    794744        List<Component> testContexts = new LinkedList<>(); 
    795         for( Element element : pkg.getOwnedElements() ) { 
    796             if( element instanceof Package ) { 
     745        for (Element element : pkg.getOwnedElements()) { 
     746            if (element instanceof Package) { 
    797747                testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext)); 
    798748            } 
    799             if( element instanceof Component && element.getAppliedStereotypes().contains(utpTestContext) ) { 
     749            if (element instanceof Component && 
     750                element.getAppliedStereotypes().contains(utpTestContext)) 
     751            { 
    800752                testContexts.add((Component) element); 
    801753            } 
     
    803755        return testContexts; 
    804756    } 
     757 
     758    /** 
     759     * <p> 
     760     * Infers connector between two lifelines.  
     761     * TODO: currently assumes only one connector between two lifelines possible. I need to make sure this assumption is valid. 
     762     * </p> 
     763     *  
     764     * @param userAttributes 
     765     * @param targetAttributes 
     766     */ 
     767    private static Connector inferConnector(Lifeline msgSourceLifeline, Lifeline msgTargetLifeline) 
     768    { 
     769        EList<Property> userAttributes = 
     770            ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes(); 
     771        EList<Property> targetAttributes = 
     772            ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes(); 
     773        for (Property userAttribute : userAttributes) { 
     774            if (userAttribute instanceof Port) { 
     775                EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds(); 
     776                for (ConnectorEnd userEnd : userEnds) { 
     777                    Connector userConnector = (Connector) userEnd.eContainer(); 
     778                    for (Property targetAttribute : targetAttributes) { 
     779                        if (targetAttribute instanceof Port) { 
     780                            EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds(); 
     781                            for (ConnectorEnd targetEnd : targetEnds) { 
     782                                Connector targetConnector = (Connector) targetEnd.eContainer(); 
     783                                if (targetConnector == userConnector) { 
     784                                    return targetConnector; 
     785                                } 
     786                            } 
     787                        } 
     788                    } 
     789                } 
     790            } 
     791        } 
     792        return null; 
     793    } 
    805794} 
Note: See TracChangeset for help on using the changeset viewer.