Changeset 1763 for trunk/autoquest-plugin-uml/src/main/java/de/ugoe
- Timestamp:
- 09/25/14 15:41:50 (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
r1761 r1763 16 16 17 17 import java.util.Collection; 18 import java.util.Collections; 19 import java.util.Comparator; 18 20 import java.util.HashMap; 19 21 import java.util.Iterator; 22 import java.util.LinkedHashMap; 20 23 import java.util.LinkedList; 21 24 import java.util.List; … … 24 27 25 28 import org.eclipse.emf.common.util.EList; 29 import org.eclipse.uml2.uml.Activity; 30 import org.eclipse.uml2.uml.ActivityEdge; 31 import org.eclipse.uml2.uml.ActivityNode; 32 import org.eclipse.uml2.uml.CallOperationAction; 26 33 import org.eclipse.uml2.uml.Comment; 27 34 import org.eclipse.uml2.uml.Component; 28 35 import org.eclipse.uml2.uml.Connector; 29 36 import org.eclipse.uml2.uml.ConnectorEnd; 30 import org.eclipse.uml2.uml.Element;31 37 import org.eclipse.uml2.uml.Interaction; 32 38 import org.eclipse.uml2.uml.InteractionFragment; … … 41 47 import org.eclipse.uml2.uml.Profile; 42 48 import org.eclipse.uml2.uml.Property; 49 import org.eclipse.uml2.uml.Realization; 43 50 import org.eclipse.uml2.uml.Region; 44 51 import org.eclipse.uml2.uml.StateMachine; … … 52 59 import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 53 60 import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType; 54 import de.ugoe.cs.autoquest.usageprofiles. TrieBasedModel;61 import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess; 55 62 56 63 /** … … 265 272 String interactionName) 266 273 { 267 Map<String, Interface> interfaceMap = new HashMap<>();268 274 Map<String, Port> portMap = new HashMap<>(); 269 270 EList<Element> elements = model.getOwnedElements();271 for (Element element : elements) {272 if (element instanceof Interface) {273 interfaceMap.put(((Interface) element).getName(), (Interface) element);274 }275 }276 275 277 276 Component testContext = … … 281 280 final Profile utpProfile = model.getAppliedProfile("utp"); 282 281 final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase"); 282 final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent"); 283 final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT"); 284 283 285 Operation operation = testContext.createOwnedOperation(interactionName, null, null); 284 286 operation.applyStereotype(utpTestCase); … … 287 289 (Interaction) testContext.createPackagedElement(interactionName + "_Impl", 288 290 UMLPackage.Literals.INTERACTION); 291 operation.getMethods().add(interaction); 289 292 290 operation.getMethods().add(interaction); 291 292 Lifeline userLifeline = interaction.createLifeline("user"); 293 294 userLifeline.setRepresents(testContext.getAttribute("user", null)); 295 296 Component userComponent = 297 (Component) model.getPackagedElement("User", true, UMLPackage.Literals.COMPONENT, true); 298 299 EList<Property> userAttributes = userComponent.getAttributes(); 293 // create lifelines 294 Lifeline userLifeline = null; 300 295 List<Port> userPorts = new LinkedList<>(); 301 for( Property userAttribute : userAttributes ) { 302 if( userAttribute instanceof Port ) { 303 userPorts.add((Port) userAttribute); 304 } 296 for (Property property : testContext.getAllAttributes()) { 297 if (property.getAppliedStereotypes().contains(utpSUT)) { 298 String serviceName = getRealizedInterfaceFromProperty(property).getName(); 299 300 Lifeline targetLifeline = interaction.createLifeline(serviceName); 301 targetLifeline.setRepresents(property); 302 portMap.put(serviceName, 303 (Port) ((Component) property.getType()).getAttribute("p_" + serviceName, null)); 304 } 305 else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) { 306 userLifeline = interaction.createLifeline(property.getName()); 307 userLifeline.setRepresents(property); 308 EList<Property> userAttributes = ((Component) property.getType()).getAttributes(); 309 for (Property userAttribute : userAttributes) { 310 if (userAttribute instanceof Port) { 311 userPorts.add((Port) userAttribute); 312 } 313 } 314 } 305 315 } 306 316 … … 311 321 String methodName = getCalledMethodFromEvent(event); 312 322 313 Interface targetInterface = interfaceMap.get(serviceName);314 if (targetInterface == null) {315 throw new RuntimeException(316 "Could not find interface in the UML model that belong to the service: " +317 serviceName);318 }319 320 323 Lifeline targetLifeline = interaction.getLifeline(serviceName); 321 if (targetLifeline == null) { 322 targetLifeline = interaction.createLifeline(serviceName); 323 324 targetLifeline.setRepresents(testContext 325 .getAttribute(serviceName + "_property", null)); 326 327 Component component = 328 (Component) model.getPackagedElement(serviceName, true, 329 UMLPackage.Literals.COMPONENT, true); 330 portMap.put(serviceName, 331 (Port) component.getAttribute("p_" + serviceName, null)); 332 } 324 Interface targetInterface = getRealizedInterfaceFromProperty((Property) targetLifeline.getRepresents()); 325 333 326 MessageOccurrenceSpecification sendFragment = 334 327 (MessageOccurrenceSpecification) interaction … … 352 345 message.setSendEvent(sendFragment); 353 346 message.setReceiveEvent(recvFragment); 354 347 355 348 EList<ConnectorEnd> targetEnds = portMap.get(serviceName).getEnds(); 356 349 357 for ( Port userPort : userPorts) {350 for (Port userPort : userPorts) { 358 351 EList<ConnectorEnd> sourceEnds = userPort.getEnds(); 359 352 for (ConnectorEnd sourceEnd : sourceEnds) { … … 388 381 * @return calculated usage score 389 382 */ 390 public static double calculateUsageScore(Interaction interaction, TrieBasedModel usageProfile) { 383 public static double calculateUsageScore(Interaction interaction, 384 IStochasticProcess usageProfile) 385 { 391 386 double usageScore = 0.0d; 392 387 … … 399 394 { 400 395 String serviceName = 401 interactionFragment.getCovereds().get(0).getRepresents().getName() ;396 interactionFragment.getCovereds().get(0).getRepresents().getName().split("_")[0]; 402 397 String methodName = "UNKNOWN"; 403 398 if (interactionFragment instanceof MessageOccurrenceSpecification) { … … 409 404 } 410 405 } 406 eventSequence.add(Event.ENDEVENT); 411 407 double prob = usageProfile.getLogSum(eventSequence); 412 usageScore = prob * eventSequence.size();408 usageScore = eventSequence.size() * prob; 413 409 414 410 return usageScore; 411 } 412 413 /** 414 * <p> 415 * Extends the given model with an activity for usage-based scheduling of the test cases. 416 * </p> 417 * 418 * @param model 419 * model to be extended 420 * @param usageProfile 421 * usage profile used as foundation 422 */ 423 public static void createScheduling(Model model, IStochasticProcess usageProfile) { 424 425 final Profile utpProfile = model.getAppliedProfile("utp"); 426 final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase"); 427 428 Component testContext = 429 (Component) model.getPackagedElement("TestContext", true, 430 UMLPackage.Literals.COMPONENT, true); 431 432 Map<Operation, Double> usageScoreMapUnsorted = new HashMap<>(); 433 434 // first, we determine all test cases and calculate their usage scores 435 for (Operation operation : testContext.getAllOperations()) { 436 if (operation.getAppliedStereotypes().contains(utpTestCase)) { 437 Interaction interaction = (Interaction) operation.getMethods().get(0); 438 usageScoreMapUnsorted 439 .put(operation, calculateUsageScore(interaction, usageProfile)); 440 } 441 } 442 Map<Operation, Double> usageScoreMapSorted = sortByValue(usageScoreMapUnsorted); 443 444 // now we create the scheduling 445 Activity schedulingActivity = 446 (Activity) testContext.createOwnedBehavior("UsageBasedScheduling", 447 UMLPackage.Literals.ACTIVITY); 448 testContext.setClassifierBehavior(schedulingActivity); 449 450 ActivityNode startNode = 451 schedulingActivity.createOwnedNode("final", UMLPackage.Literals.INITIAL_NODE); 452 ActivityNode finalNode = 453 schedulingActivity.createOwnedNode("final", UMLPackage.Literals.ACTIVITY_FINAL_NODE); 454 455 ActivityNode currentOperationNode = startNode; 456 457 for (Entry<Operation, Double> entry : usageScoreMapSorted.entrySet()) { 458 Operation operation = entry.getKey(); 459 CallOperationAction nextOperationNode = 460 (CallOperationAction) schedulingActivity 461 .createOwnedNode(operation.getName(), UMLPackage.Literals.CALL_OPERATION_ACTION); 462 nextOperationNode.setOperation(operation); 463 464 ActivityEdge edge = 465 schedulingActivity.createEdge(currentOperationNode.getName() + "_to_" + 466 nextOperationNode.getName(), UMLPackage.Literals.CONTROL_FLOW); 467 edge.setSource(currentOperationNode); 468 edge.setTarget(nextOperationNode); 469 470 currentOperationNode = nextOperationNode; 471 } 472 473 ActivityEdge edge = 474 schedulingActivity 475 .createEdge(currentOperationNode.getName() + "_to_" + finalNode.getName(), 476 UMLPackage.Literals.CONTROL_FLOW); 477 edge.setSource(currentOperationNode); 478 edge.setTarget(finalNode); 479 } 480 481 /** 482 * From 483 * http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java 484 * and adapted to do an inverse sorting 485 */ 486 public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { 487 List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); 488 Collections.sort(list, new Comparator<Map.Entry<K, V>>() { 489 @Override 490 public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { 491 return -1 * (o1.getValue()).compareTo(o2.getValue()); 492 } 493 }); 494 495 Map<K, V> result = new LinkedHashMap<>(); 496 for (Map.Entry<K, V> entry : list) { 497 result.put(entry.getKey(), entry.getValue()); 498 } 499 return result; 415 500 } 416 501 … … 512 597 return matching; 513 598 } 599 600 private static Interface getRealizedInterfaceFromProperty(Property property) { 601 return getRealizedInterfaceFromComponent((Component) property.getType()); 602 } 603 604 private static Interface getRealizedInterfaceFromComponent(Component comp) { 605 Realization realization = (Realization) comp.getNestedClassifiers().get(0).getRelationships(UMLPackage.Literals.REALIZATION).get(0); 606 return (Interface) realization.getSuppliers().get(0); 607 } 514 608 515 609 }
Note: See TracChangeset
for help on using the changeset viewer.