Changeset 1926 for trunk/autoquest-plugin-uml
- Timestamp:
- 03/18/15 10:05:36 (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
r1916 r1926 15 15 package de.ugoe.cs.autoquest.plugin.uml; 16 16 17 import java.io.StringWriter; 18 import java.util.ArrayList; 17 19 import java.util.Collection; 18 20 import java.util.Collections; … … 30 32 import java.util.TreeSet; 31 33 34 import javax.xml.transform.Transformer; 35 import javax.xml.transform.TransformerException; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.TransformerFactoryConfigurationError; 38 import javax.xml.transform.dom.DOMSource; 39 import javax.xml.transform.stream.StreamResult; 40 32 41 import org.eclipse.emf.common.util.EList; 42 import org.eclipse.emf.common.util.URI; 33 43 import org.eclipse.uml2.uml.Activity; 34 44 import org.eclipse.uml2.uml.ActivityEdge; 35 45 import org.eclipse.uml2.uml.ActivityNode; 36 46 import org.eclipse.uml2.uml.CallConcurrencyKind; 47 import org.eclipse.uml2.uml.CallEvent; 37 48 import org.eclipse.uml2.uml.CallOperationAction; 38 49 import org.eclipse.uml2.uml.Comment; … … 66 77 import org.eclipse.uml2.uml.Property; 67 78 import org.eclipse.uml2.uml.Region; 79 import org.eclipse.uml2.uml.Relationship; 68 80 import org.eclipse.uml2.uml.Slot; 69 81 import org.eclipse.uml2.uml.StateMachine; 70 82 import org.eclipse.uml2.uml.Stereotype; 71 83 import org.eclipse.uml2.uml.Transition; 84 import org.eclipse.uml2.uml.Trigger; 85 import org.eclipse.uml2.uml.Type; 72 86 import org.eclipse.uml2.uml.UMLPackage; 73 87 import org.eclipse.uml2.uml.Vertex; 88 import org.w3c.dom.Attr; 89 import org.w3c.dom.Node; 90 import org.w3c.dom.NodeList; 74 91 75 92 import de.ugoe.cs.autoquest.eventcore.Event; 93 import de.ugoe.cs.autoquest.plugin.http.SOAPUtils; 76 94 import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; 77 95 import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 78 96 import de.ugoe.cs.autoquest.plugin.uml.eventcore.UMLTransitionType; 79 97 import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess; 98 import de.ugoe.cs.util.StringTools; 80 99 import de.ugoe.cs.util.console.Console; 81 100 … … 88 107 */ 89 108 public class UMLUtils { 109 110 /** 111 * In case a multiplicity is defined as *, this value defines the highest one that can be picked 112 */ 113 final static int MAX_MULTIPLICITY = 10; 114 115 final public static URI UML_PRIMITIVE_TYPES_URI = URI 116 .createURI("pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml", true); 90 117 91 118 /** … … 135 162 for (List<Event> sequence : sequences) { 136 163 for (Event event : sequence) { 137 String serviceName = getServiceNameFromEvent(event);138 String calledMethod = getCalledMethodFromEvent(event);139 if ( serviceName!=null) {164 String serviceName = SOAPUtils.getServiceNameFromEvent(event); 165 String calledMethod = SOAPUtils.getCalledMethodFromEvent(event); 166 if (serviceName != null) { 140 167 Set<String> curCalledMethods = calledMethods.get(serviceName); 141 168 if (curCalledMethods == null) { … … 152 179 for (Entry<String, Set<String>> entry : calledMethods.entrySet()) { 153 180 Console.traceln(Level.INFO, "\tService \"" + entry.getKey() + "\": "); 154 for (String method : entry.getValue()) {155 Console.traceln(Level.INFO, "\t\t" +method);181 for (String method : entry.getValue()) { 182 Console.traceln(Level.INFO, "\t\t" + method); 156 183 } 157 184 } … … 204 231 "\tFound the following realized interfaces for the service \"" + 205 232 serviceName + "\": "); 206 for ( Interface intface : interfaces) {233 for (Interface intface : interfaces) { 207 234 Console.traceln(Level.INFO, "\t" + intface.getName()); 208 for ( Operation operation : intface.getAllOperations()) {235 for (Operation operation : intface.getAllOperations()) { 209 236 Console.traceln(Level.INFO, "\t\t" + operation.getName()); 210 237 } … … 235 262 * <p> 236 263 * Creates a sequence of events with {@link UMLTransitionType} as event type from a given 237 * sequence of events with the {@link SOAPEventType} , by matching the sequences to a state238 * machine.264 * sequence of events with the {@link SOAPEventType} or {@link SimpleSOAPEventType}, by matching 265 * the sequences to a state machine. 239 266 * </p> 240 267 * … … 248 275 StateMachine stateMachine) 249 276 { 277 System.out.println("foo"); 250 278 List<List<Transition>> matchingSequences = 251 279 determineMatchingTransitionSequences(sequence, stateMachine); 280 System.out.println(matchingSequences.size()); 252 281 253 282 if (matchingSequences.size() != 1) { … … 370 399 for (Iterator<Event> eventIterator = sequence.iterator(); eventIterator.hasNext();) { 371 400 Event event = eventIterator.next(); 372 if (event.getType() instanceof SOAPEventType) { 373 SOAPEventType eventType = (SOAPEventType) event.getType(); 374 if (matchingSequences == null) { 375 matchingSequences = new LinkedList<>(); 376 List<Transition> initialMatches = matchTransitions(allTransitions, eventType); 377 for (Transition transition : initialMatches) { 378 List<Transition> candidate = new LinkedList<>(); 379 candidate.add(transition); 380 matchingSequences.add(candidate); 381 } 382 currentTransitions = initialMatches; 383 } 384 else { 385 List<List<Transition>> nextMatchingSequences = new LinkedList<>(); 386 List<Transition> nextCurrentTransitions = new LinkedList<>(); 387 Iterator<Transition> currentTransitionIterator = currentTransitions.iterator(); 388 Iterator<List<Transition>> currentMatchingSequencesIterator = 389 matchingSequences.iterator(); 390 while (currentTransitionIterator.hasNext()) { 391 Transition currentTransition = currentTransitionIterator.next(); 392 List<Transition> currentMatch = currentMatchingSequencesIterator.next(); 393 394 List<Transition> matches = 395 matchTransitions(currentTransition.getTarget().getOutgoings(), 396 eventType); 397 if (matches.isEmpty()) { 398 throw new RuntimeException("no matches found"); 399 } 400 for (Transition matchingTransition : matches) { 401 List<Transition> candidate = new LinkedList<>(currentMatch); 402 candidate.add(matchingTransition); 403 nextMatchingSequences.add(candidate); 404 nextCurrentTransitions.add(matchingTransition); 405 } 406 } 407 matchingSequences = nextMatchingSequences; 408 currentTransitions = nextCurrentTransitions; 409 } 401 System.out.println(event); 402 System.out.println(matchingSequences); 403 if (matchingSequences == null) { 404 matchingSequences = new LinkedList<>(); 405 List<Transition> initialMatches = matchTransitions(allTransitions, event); 406 for (Transition transition : initialMatches) { 407 List<Transition> candidate = new LinkedList<>(); 408 candidate.add(transition); 409 matchingSequences.add(candidate); 410 } 411 currentTransitions = initialMatches; 410 412 } 411 413 else { 412 throw new RuntimeException( 413 "Wrong event type. Only UMLTransitionType supported but was: " + 414 event.getType().getClass().getName()); 414 List<List<Transition>> nextMatchingSequences = new LinkedList<>(); 415 List<Transition> nextCurrentTransitions = new LinkedList<>(); 416 Iterator<Transition> currentTransitionIterator = currentTransitions.iterator(); 417 Iterator<List<Transition>> currentMatchingSequencesIterator = 418 matchingSequences.iterator(); 419 while (currentTransitionIterator.hasNext()) { 420 Transition currentTransition = currentTransitionIterator.next(); 421 List<Transition> currentMatch = currentMatchingSequencesIterator.next(); 422 423 List<Transition> matches = 424 matchTransitions(currentTransition.getTarget().getOutgoings(), event); 425 if (matches.isEmpty()) { 426 throw new RuntimeException("no matches found"); 427 } 428 for (Transition matchingTransition : matches) { 429 List<Transition> candidate = new LinkedList<>(currentMatch); 430 candidate.add(matchingTransition); 431 nextMatchingSequences.add(candidate); 432 nextCurrentTransitions.add(matchingTransition); 433 } 434 } 435 matchingSequences = nextMatchingSequences; 436 currentTransitions = nextCurrentTransitions; 415 437 } 416 438 } … … 444 466 final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 445 467 468 // add UML Primitive types 469 // final UMLResource umlRes = (UMLResource) 470 // model.eResource().getResourceSet().getResource(UML_PRIMITIVE_TYPES_URI, true); 471 // model = (Model) umlRes.getContents().get(0); 472 446 473 Component testContext = fetchTestContext(model, utpTestContext, testContextName); 447 474 if (testContext == null) { … … 485 512 for (Event event : sequence) { 486 513 if (!(event.equals(Event.STARTEVENT) || event.equals(Event.ENDEVENT))) { 487 String serviceName = getServiceNameFromEvent(event); 488 String methodName = getCalledMethodFromEvent(event); 489 String clientName = getClientNameFromEvent(event); 514 String serviceName = SOAPUtils.getServiceNameFromEvent(event); 515 String methodName = SOAPUtils.getCalledMethodFromEvent(event); 516 String clientName = SOAPUtils.getClientNameFromEvent(event); 517 String prefix = interactionName + ":" + i + ":" + methodName + "_"; 490 518 // determine lifelines 491 519 Lifeline msgTargetLifeline; 492 520 Lifeline msgSourceLifeline; 493 521 494 522 msgSourceLifeline = interaction.getLifeline(clientName); 495 523 msgTargetLifeline = interaction.getLifeline(serviceName); 496 524 497 525 if (msgSourceLifeline == null) { 498 526 throw new RuntimeException( 499 "Error creating message: could not determine source lifeline for component: " + clientName); 527 "Error creating message: could not determine source lifeline for component: " + 528 clientName); 500 529 } 501 530 if (msgTargetLifeline == null) { 502 531 throw new RuntimeException( 503 "Error creating message: could not determine target lifeline for component: " + serviceName); 532 "Error creating message: could not determine target lifeline for component: " + 533 serviceName); 504 534 } 505 535 // determine correct target interface … … 541 571 // setup for both SYNCH and ASYNCH calls 542 572 MessageOccurrenceSpecification callSendFragment = 543 (MessageOccurrenceSpecification) interaction 544 .createFragment(i + ":" + methodName + "_callSendFragment", 545 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 573 (MessageOccurrenceSpecification) interaction.createFragment(prefix + 574 "callSendFragment", UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 546 575 MessageOccurrenceSpecification callRecvFragment = 547 (MessageOccurrenceSpecification) interaction 548 .createFragment(i + ":" + methodName + "_callRecvFragment", 549 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 576 (MessageOccurrenceSpecification) interaction.createFragment(prefix + 577 "callRecvFragment", UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 550 578 551 579 callSendFragment.setCovered(msgSourceLifeline); … … 554 582 // create call 555 583 Message callMessage = interaction.createMessage(methodName); 556 callMessage.setSignature(calledOperation); 557 setMessageParameters(callMessage, calledOperation, event );584 callMessage.setSignature(calledOperation); 585 setMessageParameters(callMessage, calledOperation, event, prefix); 558 586 callMessage.setConnector(inferConnector(msgSourceLifeline, msgTargetLifeline)); 559 587 callMessage.setSendEvent(callSendFragment); … … 577 605 MessageOccurrenceSpecification replySendFragment = 578 606 (MessageOccurrenceSpecification) interaction 579 .createFragment( i + ":" + methodName + "_replySendFragment",607 .createFragment(prefix + "replySendFragment", 580 608 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 581 609 MessageOccurrenceSpecification replyRecvFragment = 582 610 (MessageOccurrenceSpecification) interaction 583 .createFragment( i + ":" + methodName + "_replyRecvFragment",611 .createFragment(prefix + "replyRecvFragment", 584 612 UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION); 585 613 … … 618 646 } 619 647 620 621 622 648 /** 623 649 * <p> … … 653 679 .getName(); 654 680 } 655 eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName,""))); // TODO add client name 681 // eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName, "", 682 // ))); // TODO 683 // add 684 // client 685 // name 656 686 } 657 687 } … … 755 785 } 756 786 return result; 757 }758 759 /**760 * <p>761 * Helper function to get the name of a service from a SOAP event.762 * </p>763 *764 * @param event765 * event for which the service name is retrieved766 * @return service name767 */768 protected static String getServiceNameFromEvent(Event event) {769 if (event.getType() instanceof SOAPEventType) {770 return ((SOAPEventType) event.getType()).getServiceName();771 }772 else if (event.getType() instanceof SimpleSOAPEventType) {773 return ((SimpleSOAPEventType) event.getType()).getServiceName();774 }775 else {776 throw new RuntimeException(777 "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +778 event.getType().getClass().getName());779 }780 }781 782 /**783 *784 * <p>785 * Helper function to get the called method from a SOAP event786 * </p>787 *788 * @param event789 * event for which the called method is retrieved790 * @return called method791 */792 private static String getCalledMethodFromEvent(Event event) {793 if (event.getType() instanceof SOAPEventType) {794 return ((SOAPEventType) event.getType()).getCalledMethod();795 }796 else if (event.getType() instanceof SimpleSOAPEventType) {797 return ((SimpleSOAPEventType) event.getType()).getCalledMethod();798 }799 else {800 throw new RuntimeException(801 "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +802 event.getType().getClass().getName());803 }804 }805 806 /**807 * <p>808 * Helper function to get the name of a client from a SOAP event.809 * </p>810 *811 * @param event812 * event for which the client name is retrieved813 * @return service name814 */815 protected static String getClientNameFromEvent(Event event) {816 if (event.getType() instanceof SOAPEventType) {817 return ((SOAPEventType) event.getType()).getClientName();818 }819 else if (event.getType() instanceof SimpleSOAPEventType) {820 return ((SimpleSOAPEventType) event.getType()).getClientName();821 }822 else {823 throw new RuntimeException(824 "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +825 event.getType().getClass().getName());826 }827 787 } 828 788 … … 864 824 * @return matching transitions 865 825 */ 866 private static List<Transition> matchTransitions(List<Transition> transitions, 867 SOAPEventType eventType) 868 { 826 private static List<Transition> matchTransitions(List<Transition> transitions, Event event) { 827 String eventService = SOAPUtils.getServiceNameFromEvent(event); 828 String eventMethod = SOAPUtils.getCalledMethodFromEvent(event); 829 830 Map<Interface, String> interfaceServiceMap = 831 createInterfaceServiceMap(transitions.get(0).getModel()); 832 869 833 List<Transition> matching = new LinkedList<>(); 870 834 for (Transition transition : transitions) { 871 // String serviceName = transition.getName().split("\\.")[0]; // TODO service name check 872 String methodName = transition.getName().split("\\.")[1]; 873 if (methodName.equals(eventType.getCalledMethod())) { 874 matching.add(transition); 875 } 835 EList<Trigger> triggers = transition.getTriggers(); 836 if (triggers.size() == 1) { 837 if (triggers.get(0).getEvent() instanceof CallEvent) { 838 CallEvent callEvent = (CallEvent) triggers.get(0).getEvent(); 839 String transitionMethod = callEvent.getOperation().getName(); 840 841 Interface intface = callEvent.getOperation().getInterface(); 842 for (Relationship relationship : intface.getRelationships()) { 843 for (Element element : relationship.getRelatedElements()) { 844 if (element instanceof Component) { 845 846 } 847 } 848 } 849 850 String transitionService = 851 interfaceServiceMap.get(callEvent.getOperation().getInterface()); 852 if (eventMethod.equals(transitionMethod) && 853 eventService.equals(transitionService)) 854 { 855 matching.add(transition); 856 } 857 } 858 } 859 876 860 } 877 861 return matching; … … 970 954 return null; 971 955 } 972 973 private static void setMessageParameters(Message callMessage, Operation calledOperation, Event event) { 956 957 private static Map<Interface, String> createInterfaceServiceMap(Model model) { 958 Map<Interface, String> interfaceServiceMap = new HashMap<>(); 959 final Profile utpProfile = model.getModel().getAppliedProfile("utp"); 960 final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent"); 961 final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT"); 962 final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 963 List<Component> testContexts = 964 fetchTestContextRecursively(model.getModel(), utpTestContext); 965 for (Component testContext : testContexts) { 966 for (Property property : testContext.getAllAttributes()) { 967 if (property.getAppliedStereotypes().contains(utpSUT) || 968 property.getType().getAppliedStereotypes().contains(utpTestComponent)) 969 { 970 for (Interface intface : getRealizedInterfacesFromProperty(property)) { 971 interfaceServiceMap.put(intface, property.getName()); 972 } 973 } 974 } 975 } 976 return interfaceServiceMap; 977 } 978 979 private static void setMessageParameters(Message callMessage, 980 Operation calledOperation, 981 Event event, 982 String prefix) 983 { 984 org.w3c.dom.Element requestBody = SOAPUtils.getSoapRequestBodyFromEvent(event); 985 974 986 // Set parameters of operation 975 for( Parameter param : calledOperation.getOwnedParameters() ) { 976 Expression argument = (Expression) callMessage.createArgument(param.getName(), param.getType(), UMLPackage.Literals.EXPRESSION); 977 978 if( param.getDirection()==ParameterDirectionKind.IN_LITERAL || param.getDirection()==ParameterDirectionKind.INOUT_LITERAL) { 979 if( param.getType() instanceof DataType ) { 980 InstanceSpecification instSpec = createInstanceSpecification((DataType) param.getType(), event); 981 982 InstanceValue value = (InstanceValue) argument.createOperand(null, param.getType(), UMLPackage.Literals.INSTANCE_VALUE); 983 value.setInstance(instSpec); 984 } 985 else if( param.getType() instanceof PrimitiveType ) { 986 if( "String".equals(param.getType().getName()) ) { 987 LiteralString spec = (LiteralString) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_STRING); 988 spec.setValue("foobar"); // TODO needs to be real value 989 } 990 else if( "Integer".equals(param.getType().getName()) ) { 991 LiteralInteger spec = (LiteralInteger) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_INTEGER); 992 spec.setValue(42); // TODO needs to be real value 993 } 994 else if( "Boolean".equals(param.getType().getName()) ) { 995 LiteralBoolean spec = (LiteralBoolean) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_BOOLEAN); 996 spec.setValue(true); // TODO needs to be real value 997 } 998 else if( "Real".equals(param.getType().getName()) ) { 999 LiteralReal spec = (LiteralReal) argument.createOperand(param.getName(), null, UMLPackage.Literals.LITERAL_REAL); 1000 spec.setValue(3.14); // TODO needs to be real value 1001 } 1002 } 1003 } else { 987 for (Parameter param : calledOperation.getOwnedParameters()) { 988 Expression argument = 989 (Expression) callMessage.createArgument(param.getName(), param.getType(), 990 UMLPackage.Literals.EXPRESSION); 991 992 if (param.getDirection() == ParameterDirectionKind.IN_LITERAL || 993 param.getDirection() == ParameterDirectionKind.INOUT_LITERAL) 994 { 995 if (param.getType() instanceof DataType) { 996 List<org.w3c.dom.Element> paramNodes = 997 getMatchingChildNode((DataType) param.getType(), requestBody); 998 for (org.w3c.dom.Element paramNode : paramNodes) { 999 InstanceSpecification instSpec = 1000 createInstanceSpecification((DataType) param.getType(), event, prefix, 1001 paramNode, ""); 1002 1003 InstanceValue value = 1004 (InstanceValue) argument 1005 .createOperand(null, param.getType(), 1006 UMLPackage.Literals.INSTANCE_VALUE); 1007 value.setInstance(instSpec); 1008 } 1009 } 1010 else if (param.getType() instanceof PrimitiveType) { 1011 createOperandPrimitiveType(param, argument, requestBody); 1012 } 1013 } 1014 else { 1004 1015 // set literalNull for out and return parameters 1005 1016 argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL); … … 1007 1018 } 1008 1019 } 1009 1010 public static InstanceSpecification createInstanceSpecification(DataType type, Event event) { 1020 1021 private static InstanceSpecification createInstanceSpecification(DataType type, 1022 Event event, 1023 String prefix, 1024 org.w3c.dom.Element currentNode, 1025 String path) 1026 { 1027 if ("".equals(path)) { 1028 path = type.getName(); 1029 } 1030 // System.out.println(path); 1011 1031 String pkgUBTInstSpecs = "UBT_InstanceSpecifications"; 1012 1032 Model model = type.getModel(); 1013 1033 Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs); 1014 if( ubtInstSpecPkg==null ) { 1015 ubtInstSpecPkg = (Package) type.getModel().createPackagedElement(pkgUBTInstSpecs, UMLPackage.Literals.PACKAGE); 1016 } 1017 String serviceName = getServiceNameFromEvent(event); 1034 if (ubtInstSpecPkg == null) { 1035 ubtInstSpecPkg = 1036 (Package) type.getModel().createPackagedElement(pkgUBTInstSpecs, 1037 UMLPackage.Literals.PACKAGE); 1038 } 1039 String serviceName = SOAPUtils.getServiceNameFromEvent(event); 1018 1040 Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName); 1019 if( serviceInstSpecPkg==null ) { 1020 serviceInstSpecPkg = (Package) ubtInstSpecPkg.createPackagedElement(serviceName, UMLPackage.Literals.PACKAGE); 1021 } 1022 1023 InstanceSpecification instSpec = (InstanceSpecification) serviceInstSpecPkg.createPackagedElement("instspec_"+type.getName(), UMLPackage.Literals.INSTANCE_SPECIFICATION); 1041 if (serviceInstSpecPkg == null) { 1042 serviceInstSpecPkg = 1043 (Package) ubtInstSpecPkg.createPackagedElement(serviceName, 1044 UMLPackage.Literals.PACKAGE); 1045 } 1046 1047 InstanceSpecification instSpec = 1048 (InstanceSpecification) serviceInstSpecPkg.createPackagedElement(prefix + "instspec_" + 1049 type.getName(), UMLPackage.Literals.INSTANCE_SPECIFICATION); 1024 1050 instSpec.getClassifiers().add(type); 1025 for( Property prop : type.getAllAttributes() ) { 1026 // TODO handle multiplicity 1027 //int lowerBound = prop.getLower(); 1028 //int upperBound = prop.getUpper(); 1029 1051 for (Property prop : type.getAllAttributes()) { 1052 if (prop.getType() instanceof PrimitiveType) { 1053 createSlotPrimitiveType(instSpec, prop, currentNode, path); 1054 } 1055 else if (prop.getType() instanceof DataType) { 1056 List<org.w3c.dom.Element> attributeNodes = null; 1057 int multiplicityChosen = 0; 1058 if (currentNode != null) { 1059 attributeNodes = getMatchingChildNode(prop, currentNode); 1060 multiplicityChosen = attributeNodes.size(); 1061 } 1062 1063 if (multiplicityChosen == 0 && prop.getLower() > 0) { 1064 if (currentNode != null) { 1065 Console.traceln(Level.WARNING, 1066 "required attribute not found in SOAP message: " + path + 1067 "." + prop.getName()); 1068 Console 1069 .traceln(Level.WARNING, 1070 "setting default values for this attribute and all its children"); 1071 Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE + 1072 SOAPUtils.getSerialization(currentNode)); 1073 } 1074 multiplicityChosen = prop.getLower(); 1075 } 1076 for (int i = 0; i < multiplicityChosen; i++) { 1077 org.w3c.dom.Element attributeNode = null; 1078 if (attributeNodes != null && !attributeNodes.isEmpty()) { 1079 attributeNode = attributeNodes.get(i); 1080 } 1081 1082 Slot slot = instSpec.createSlot(); 1083 slot.setDefiningFeature(prop); 1084 1085 InstanceValue value = 1086 (InstanceValue) slot.createValue(prop.getName() + "_" + i, prop.getType(), 1087 UMLPackage.Literals.INSTANCE_VALUE); 1088 value.setInstance(createInstanceSpecification((DataType) prop.getType(), event, 1089 prefix, attributeNode, path + 1090 "." + prop.getName())); 1091 } 1092 } 1093 else { 1094 Console.traceln(Level.SEVERE, "property neither DataType nor PrimitiveType: " + 1095 prop.getType()); 1096 // TODO abort? 1097 } 1098 } 1099 return instSpec; 1100 } 1101 1102 private static void createOperandPrimitiveType(Parameter param, 1103 Expression argument, 1104 org.w3c.dom.Element currentNode) 1105 { 1106 if ("String".equals(param.getType().getName())) { 1107 LiteralString spec = 1108 (LiteralString) argument.createOperand(param.getName(), null, 1109 UMLPackage.Literals.LITERAL_STRING); 1110 spec.setValue("foobar"); // TODO needs to be real value 1111 } 1112 else if ("Integer".equals(param.getType().getName())) { 1113 LiteralInteger spec = 1114 (LiteralInteger) argument.createOperand(param.getName(), null, 1115 UMLPackage.Literals.LITERAL_INTEGER); 1116 spec.setValue(42); // TODO needs to be real value 1117 } 1118 else if ("Boolean".equals(param.getType().getName())) { 1119 LiteralBoolean spec = 1120 (LiteralBoolean) argument.createOperand(param.getName(), null, 1121 UMLPackage.Literals.LITERAL_BOOLEAN); 1122 spec.setValue(true); // TODO needs to be real value 1123 } 1124 else if ("Real".equals(param.getType().getName())) { 1125 LiteralReal spec = 1126 (LiteralReal) argument.createOperand(param.getName(), null, 1127 UMLPackage.Literals.LITERAL_REAL); 1128 spec.setValue(3.14); // TODO needs to be real value 1129 } 1130 } 1131 1132 private static void createSlotPrimitiveType(InstanceSpecification instSpec, 1133 Property prop, 1134 org.w3c.dom.Element currentNode, 1135 String path) 1136 { 1137 List<String> attributeValues = getPrimitiveTypeValuesFromElement(prop, currentNode); 1138 1139 if (attributeValues.isEmpty()) { 1140 if (prop.getLower() == 0) { 1141 // ignoring optional attribute 1142 return; 1143 } 1144 else { 1145 if (currentNode != null) { 1146 Console.traceln(Level.WARNING, 1147 "required attribute not found in SOAP message: " + path + "." + 1148 prop.getName()); 1149 Console.traceln(Level.WARNING, "setting default values for this attribute"); 1150 } 1151 Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE + 1152 SOAPUtils.getSerialization(currentNode)); 1153 attributeValues.add(null); 1154 } 1155 } 1156 for (String attributeValue : attributeValues) { 1030 1157 Slot slot = instSpec.createSlot(); 1031 1158 slot.setDefiningFeature(prop); 1032 if( prop.getType() instanceof PrimitiveType ) { 1033 if( "String".equals(prop.getType().getName()) ) { 1034 LiteralString value = (LiteralString) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_STRING); 1035 value.setValue("foobar"); // TODO needs to be real value 1036 } 1037 else if( "Integer".equals(prop.getType().getName()) ) { 1038 LiteralInteger value = (LiteralInteger) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_INTEGER); 1039 value.setValue(42); // TODO needs to be real value 1040 } 1041 else if( "Boolean".equals(prop.getType().getName()) ) { 1042 LiteralBoolean value = (LiteralBoolean) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_BOOLEAN); 1043 value.setValue(true); // TODO needs to be real value 1044 } 1045 else if( "Real".equals(prop.getType().getName()) ) { 1046 LiteralReal value = (LiteralReal) slot.createValue(prop.getName(), null, UMLPackage.Literals.LITERAL_REAL); 1159 if ("String".equals(prop.getType().getName())) { 1160 LiteralString value = 1161 (LiteralString) slot.createValue(prop.getName(), null, 1162 UMLPackage.Literals.LITERAL_STRING); 1163 if (attributeValue != null) { 1164 value.setValue(attributeValue); 1165 } 1166 else { 1167 value.setValue("foobar"); 1168 } 1169 } 1170 else if ("Integer".equals(prop.getType().getName())) { 1171 LiteralInteger value = 1172 (LiteralInteger) slot.createValue(prop.getName(), null, 1173 UMLPackage.Literals.LITERAL_INTEGER); 1174 if (attributeValue != null) { 1175 value.setValue(Integer.parseInt(attributeValue)); 1176 } 1177 else { 1178 value.setValue(42); 1179 } 1180 } 1181 else if ("Boolean".equals(prop.getType().getName())) { 1182 LiteralBoolean value = 1183 (LiteralBoolean) slot.createValue(prop.getName(), null, 1184 UMLPackage.Literals.LITERAL_BOOLEAN); 1185 if (attributeValue != null) { 1186 value.setValue(Boolean.parseBoolean(attributeValue)); 1187 } 1188 else { 1189 value.setValue(true); 1190 } 1191 } 1192 else if ("Real".equals(prop.getType().getName())) { 1193 LiteralReal value = 1194 (LiteralReal) slot.createValue(prop.getName(), null, 1195 UMLPackage.Literals.LITERAL_REAL); 1196 if (attributeValue != null) { 1197 value.setValue(Double.parseDouble(attributeValue)); 1198 } 1199 else { 1047 1200 value.setValue(3.14); // TODO needs to be real value 1048 1201 } 1049 1202 } 1050 else if( prop.getType() instanceof DataType ) { 1051 InstanceValue value = (InstanceValue) slot.createValue(prop.getName(), prop.getType(), UMLPackage.Literals.INSTANCE_VALUE); 1052 value.setInstance(createInstanceSpecification((DataType) prop.getType(), event)); 1053 } 1054 } 1055 return instSpec; 1056 } 1203 else { 1204 Console.traceln(Level.SEVERE, "could not create literal for primitive type: " + 1205 prop.getType().getName()); 1206 // TODO abort? 1207 } 1208 } 1209 } 1210 1211 // TODO comment 1212 private static List<org.w3c.dom.Element> getMatchingChildNode(Type type, 1213 org.w3c.dom.Element parentNode) 1214 { 1215 return getMachingChildNode(type.getName(), parentNode); 1216 } 1217 1218 // TODO comment 1219 private static List<org.w3c.dom.Element> getMatchingChildNode(Property prop, 1220 org.w3c.dom.Element parentNode) 1221 { 1222 return getMachingChildNode(prop.getName(), parentNode); 1223 } 1224 1225 // TODO comment 1226 private static List<org.w3c.dom.Element> getMachingChildNode(String typeNameRaw, 1227 org.w3c.dom.Element parentNode) 1228 { 1229 List<org.w3c.dom.Element> matchingNodes = new ArrayList<>(); 1230 Node parameterNode = null; 1231 if (parentNode != null) { 1232 NodeList parameterNodes = parentNode.getChildNodes(); 1233 String[] typeNameSplit = typeNameRaw.split(":"); 1234 String typeName = typeNameSplit[typeNameSplit.length - 1]; 1235 for (int i = 0; i < parameterNodes.getLength(); i++) { 1236 parameterNode = parameterNodes.item(i); 1237 if (parameterNode.getNodeType() == Node.ELEMENT_NODE) { 1238 String[] parameterNodeSplit = parameterNode.getNodeName().split(":"); 1239 String parameterNodeName = parameterNodeSplit[parameterNodeSplit.length - 1]; 1240 if (typeName.equals(parameterNodeName)) { 1241 matchingNodes.add((org.w3c.dom.Element) parameterNode); 1242 } 1243 } 1244 } 1245 /* 1246 * if( !matchingSOAPFound) { Console.traceln(Level.WARNING, 1247 * "could not look up name of parameter in SOAP request: " + typeName); if( 1248 * elementCount==0 ) { Console.traceln(Level.INFO, "\tno parameters found"); } else { 1249 * Console.traceln(Level.INFO, "\tparameters found:"); for( int i=0 ; 1250 * i<parameterNodes.getLength(); i++ ) { if( 1251 * parameterNodes.item(i).getNodeType()==Node.ELEMENT_NODE ) { 1252 * Console.traceln(Level.INFO, "\t\t" + parameterNodes.item(i).getNodeName()); } } } 1253 * Console.traceln(Level.WARNING, 1254 * "using dummy values for this parameter (and nested values) instead"); } 1255 */ 1256 } 1257 return matchingNodes; 1258 } 1259 1260 // TODO 1261 private static List<String> getPrimitiveTypeValuesFromElement(Property prop, 1262 org.w3c.dom.Element currentNode) 1263 { 1264 List<String> attributeValues = new LinkedList<>(); 1265 1266 if (currentNode != null) { 1267 // first check attributes of the node 1268 Attr attribute = currentNode.getAttributeNode(prop.getName()); 1269 if (attribute != null) { 1270 attributeValues.add(attribute.getValue()); 1271 } 1272 else { 1273 // now check elements 1274 List<org.w3c.dom.Element> elements = getMatchingChildNode(prop, currentNode); 1275 for (org.w3c.dom.Element element : elements) { 1276 attributeValues.add(element.getTextContent()); 1277 } 1278 } 1279 } 1280 1281 return attributeValues; 1282 } 1283 1057 1284 }
Note: See TracChangeset
for help on using the changeset viewer.