Changeset 1897
- Timestamp:
- 03/05/15 17:58:16 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java
r1896 r1897 270 270 * name of the interaction 271 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. 272 * Name of the test context that should be used. If this value is null, the first 273 * test context found is used. 273 274 */ 274 275 public static void createInteractionFromEventSequence(List<Event> sequence, … … 284 285 285 286 Component testContext = fetchTestContext(model, utpTestContext, testContextName); 286 if ( testContext==null) {287 if (testContext == null) { 287 288 throw new RuntimeException("Could not find any test context in the model"); 288 289 } 289 290 290 291 Operation operation = testContext.createOwnedOperation(interactionName, null, null); 291 292 operation.applyStereotype(utpTestCase); … … 298 299 // create lifelines 299 300 Lifeline userLifeline = null; 300 // List<Port> userPorts = new LinkedList<>();301 301 302 for (Property property : testContext.getAllAttributes()) { 302 303 if (property.getAppliedStereotypes().contains(utpSUT)) { … … 306 307 } 307 308 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 } 308 312 userLifeline = interaction.createLifeline(property.getName()); 309 313 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."); 311 321 } 312 322 313 // TODO sanity checks for userLifeline!=null, etc.314 315 323 int i = 0; 316 324 for (Event event : sequence) { … … 321 329 Lifeline msgTargetLifeline; 322 330 Lifeline msgSourceLifeline; 323 324 if ( serviceName.equals(userLifeline.getName())) {331 332 if (serviceName.equals(userLifeline.getName())) { 325 333 // message being send to user 326 334 // 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. 328 336 // however, identification of the source of a message is still an open issue 329 337 msgSourceLifeline = null; 330 for ( Lifeline lifeline : interaction.getLifelines()) {331 if (!lifeline.equals(userLifeline)){338 for (Lifeline lifeline : interaction.getLifelines()) { 339 if (!lifeline.equals(userLifeline)) { 332 340 msgSourceLifeline = lifeline; 333 341 break; … … 335 343 } 336 344 msgTargetLifeline = userLifeline; 337 } else { 345 } 346 else { 338 347 msgSourceLifeline = userLifeline; 339 348 msgTargetLifeline = interaction.getLifeline(serviceName); 340 349 } 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()); 348 362 } 349 363 Interface targetInterface = null; 350 for ( Interface intface : targetInterfaces) {364 for (Interface intface : targetInterfaces) { 351 365 System.out.println(intface.getOperations()); 352 366 if (getOperationFromName(intface.getOperations(), methodName) != null) { … … 356 370 } 357 371 } 358 if ( targetInterface == null) {372 if (targetInterface == null) { 359 373 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 ("); 361 375 Iterator<Interface> iter = targetInterfaces.iterator(); 362 while ( iter.hasNext()) {376 while (iter.hasNext()) { 363 377 String interfaceName = iter.next().getName(); 364 378 errStrBuilder.append(interfaceName); 365 if ( iter.hasNext()) {379 if (iter.hasNext()) { 366 380 errStrBuilder.append(","); 367 } else { 381 } 382 else { 368 383 errStrBuilder.append("): " + methodName); 369 384 } … … 371 386 throw new RuntimeException(errStrBuilder.toString()); 372 387 } 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 375 416 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 { 425 422 // 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 435 426 MessageOccurrenceSpecification replySendFragment = 436 427 (MessageOccurrenceSpecification) interaction … … 441 432 .createFragment(i + ":" + methodName + "_replyRecvFragment", 442 433 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 443 444 BehaviorExecutionSpecification sourceBehaviorExecutionSpecification = 434 435 replySendFragment.setCovered(msgTargetLifeline); 436 replyRecvFragment.setCovered(msgSourceLifeline); 437 438 /*BehaviorExecutionSpecification sourceBehaviorExecutionSpecification = 445 439 (BehaviorExecutionSpecification) interaction 446 440 .createFragment(":" + methodName + "_sourceBhvExecSpec", 447 441 UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION); 448 BehaviorExecutionSpecification targetBehaviorExecutionSpecification = 442 BehaviorExecutionSpecification targetBehaviorExecutionSpecification = 449 443 (BehaviorExecutionSpecification) interaction 450 444 .createFragment(":" + methodName + "_targetBhvExecSpec", 451 445 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 459 447 sourceBehaviorExecutionSpecification.setStart(callSendFragment); 460 448 sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment); 461 449 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 499 452 // create reply 500 453 Message replyMessage = interaction.createMessage(methodName + "_reply"); … … 506 459 replyRecvFragment.setMessage(replyMessage); 507 460 } 508 509 461 510 462 i++; … … 535 487 for (InteractionFragment interactionFragment : interactionFragments) { 536 488 if (interactionFragment.getName() != null && 537 interactionFragment.getName().endsWith("_recvFragment")) 489 interactionFragment.getName().endsWith("_recvFragment")) // TODO must be more 490 // generic 538 491 { 539 492 String serviceName = … … 563 516 * model to be extended 564 517 * @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 { 568 524 569 525 final Profile utpProfile = model.getAppliedProfile("utp"); … … 572 528 573 529 Component testContext = fetchTestContext(model, utpTestContext, testContextName); 574 if ( testContext==null) {530 if (testContext == null) { 575 531 throw new RuntimeException("Could not find any test context in the model"); 576 532 } … … 743 699 return matching; 744 700 } 745 701 746 702 private static List<Interface> getRealizedInterfacesFromProperty(Property property) { 747 703 return getRealizedInterfaceFromComponent((Component) property.getType()); 748 704 } 749 705 750 706 private static List<Interface> getRealizedInterfaceFromComponent(Component comp) { 751 707 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) { 755 711 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()); 766 714 } 767 715 } 768 716 } 769 717 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 { 777 724 List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext); 778 if ( testContexts.isEmpty()) {725 if (testContexts.isEmpty()) { 779 726 return null; 780 727 } 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())) { 784 731 return testContext; 785 732 } 786 733 } 787 734 return null; 788 } else { 735 } 736 else { 789 737 return testContexts.get(0); 790 738 } 791 739 } 792 793 private static List<Component> fetchTestContextRecursively(Package pkg, Stereotype utpTestContext) { 740 741 private static List<Component> fetchTestContextRecursively(Package pkg, 742 Stereotype utpTestContext) 743 { 794 744 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) { 797 747 testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext)); 798 748 } 799 if( element instanceof Component && element.getAppliedStereotypes().contains(utpTestContext) ) { 749 if (element instanceof Component && 750 element.getAppliedStereotypes().contains(utpTestContext)) 751 { 800 752 testContexts.add((Component) element); 801 753 } … … 803 755 return testContexts; 804 756 } 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 } 805 794 }
Note: See TracChangeset
for help on using the changeset viewer.