Changeset 1929
- Timestamp:
- 03/20/15 16:00:10 (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/SOAPUtils.java
r1928 r1929 16 16 17 17 import java.io.StringWriter; 18 import java.util.ArrayList; 18 19 import java.util.Iterator; 19 20 import java.util.LinkedList; … … 27 28 import javax.xml.transform.stream.StreamResult; 28 29 30 import org.w3c.dom.Attr; 29 31 import org.w3c.dom.Element; 30 32 import org.w3c.dom.Node; 33 import org.w3c.dom.NodeList; 31 34 32 35 import de.ugoe.cs.autoquest.eventcore.Event; … … 43 46 */ 44 47 public class SOAPUtils { 45 48 46 49 /** 47 50 * <p> … … 69 72 eventType.getServiceName(), 70 73 eventType.getClientName(), 71 eventType.getSoapRequestBody()))); 74 eventType 75 .getSoapRequestBody()))); 72 76 } 73 77 else { … … 80 84 return newSequence; 81 85 } 82 86 83 87 /** 84 88 * <p> … … 89 93 * sequence where the events are replaced 90 94 */ 91 public static void removeNonSOAPEvents(List<Event> sequence) 92 { 95 public static void removeNonSOAPEvents(List<Event> sequence) { 93 96 for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { 94 97 Event event = eventIter.next(); … … 188 191 return requestBody; 189 192 } 190 191 /** 192 * <p> 193 * returns the XML serialization of a DOM node; located here because it is used for SOAP request bodies 193 194 /** 195 * <p> 196 * returns the XML serialization of a DOM node; located here because it is used for SOAP request 197 * bodies 194 198 * </p> 195 199 * … … 199 203 */ 200 204 public static String getSerialization(Node node) { 201 if ( node==null) {205 if (node == null) { 202 206 return null; 203 207 } … … 214 218 } 215 219 } 216 220 221 /** 222 * <p> 223 * Fetches all {@link Element}s that are direct children of the parent node, whose name matches 224 * the given name. 225 * </p> 226 * 227 * @param typeNameRaw 228 * name of elements that are looked for 229 * @param parentNode 230 * DOM node in which the elements are searched for 231 * @return found {@link Element}s 232 */ 233 public static List<Element> getMatchingChildNode(String typeNameRaw, Element parentNode) { 234 List<Element> matchingNodes = new ArrayList<>(); 235 Node parameterNode = null; 236 if (parentNode != null) { 237 NodeList parameterNodes = parentNode.getChildNodes(); 238 String[] typeNameSplit = typeNameRaw.split(":"); 239 String typeName = typeNameSplit[typeNameSplit.length - 1]; 240 for (int i = 0; i < parameterNodes.getLength(); i++) { 241 parameterNode = parameterNodes.item(i); 242 if (parameterNode.getNodeType() == Node.ELEMENT_NODE) { 243 String[] parameterNodeSplit = parameterNode.getNodeName().split(":"); 244 String parameterNodeName = parameterNodeSplit[parameterNodeSplit.length - 1]; 245 if (typeName.equals(parameterNodeName)) { 246 matchingNodes.add((Element) parameterNode); 247 } 248 } 249 } 250 } 251 return matchingNodes; 252 } 253 254 /** 255 * <p> 256 * Returns the values found in a currentNode for a defined valueName. To this aim, this methods 257 * first determines if there is an attribute with the name "valueName". If this is the case, the 258 * value of this attribute is returned. Otherwise, the methods looks for {@link Element}s that 259 * are direct children of the provided DOM node with the given name and returns the text content 260 * of those nodes. 261 * </p> 262 * <p> 263 * In case no values can be found, an empty list is returned 264 * 265 * @param valueName 266 * name of the value that is retrieved 267 * @param node 268 * node for which the value is retrieved 269 * @return list of the found values. 270 */ 271 public static List<String> getValuesFromElement(String valueName, Element node) { 272 List<String> attributeValues = new LinkedList<>(); 273 274 if (node != null) { 275 // first check attributes of the node 276 Attr attribute = node.getAttributeNode(valueName); 277 if (attribute != null) { 278 attributeValues.add(attribute.getValue()); 279 } 280 else { 281 // now check elements 282 List<Element> elements = getMatchingChildNode(valueName, node); 283 for (Element element : elements) { 284 attributeValues.add(element.getTextContent()); 285 } 286 } 287 } 288 289 return attributeValues; 290 } 291 217 292 /** 218 293 * <p> -
trunk/autoquest-plugin-uml-test/src/test/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtilsTest.java
r1927 r1929 17 17 import java.io.File; 18 18 import java.io.FileInputStream; 19 import java.io.FileOutputStream; 19 20 import java.util.Collection; 20 21 import java.util.HashSet; … … 29 30 //import static org.junit.Assert.*; 30 31 31 import org.apache.commons.lang.SerializationUtils; 32 33 34 32 35 import org.eclipse.uml2.uml.Interaction; 33 36 import org.eclipse.uml2.uml.Model; … … 43 46 import de.ugoe.cs.autoquest.plugin.http.SOAPUtils; 44 47 import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; 48 import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; 45 49 import de.ugoe.cs.autoquest.testgeneration.RandomWalkGenerator; 46 50 import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel; 47 51 import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess; 52 import de.ugoe.cs.util.SerializationUtils; 48 53 import de.ugoe.cs.util.console.TextConsole; 49 54 … … 58 63 59 64 private final static String OUTPUT_DIR = "target/tmp/test-outputs/"; 60 65 66 private final static boolean DELETE_OUTPUTS = false; 67 61 68 // for RLUS 62 69 private final static TestData deda_1 = new TestData("deda_rlus_properties.prop", 63 "deda_usagejournal.log",64 "deda_rlus_usageprofile.dat",65 "deda_model.uml",66 "deda_rlus_model_testsuite.uml",67 "deda_rlus_model_scheduling.uml");70 "deda_usagejournal.log", 71 "deda_rlus_usageprofile.dat", 72 "deda_model.uml", 73 "deda_rlus_model_testsuite.uml", 74 "deda_rlus_model_scheduling.uml"); 68 75 69 76 // for IXS 70 77 private final static TestData deda_2 = new TestData("deda_ixs_properties.prop", 71 "deda_usagejournal.log", 72 "deda_ixs_usageprofile.dat", 73 "deda_model.uml", 74 "deda_ixs_model_testsuite.uml", 75 "deda_ixs_model_scheduling.uml"); 76 77 private final static TestData ita_1 = new TestData("ita_properties.prop", "ita_usagejournal.log", 78 "ita_usageprofile.dat", "ita_model.uml", 79 "ita_model_testsuite.uml", 80 "ita_model_scheduling.uml"); 81 78 "deda_usagejournal.log", 79 "deda_ixs_usageprofile.dat", 80 "deda_model.uml", 81 "deda_ixs_model_testsuite.uml", 82 "deda_ixs_model_scheduling.uml"); 83 84 private final static TestData ita_1 = new TestData("ita_properties.prop", 85 "ita_usagejournal.log", 86 "ita_usageprofile.dat", "ita_model.uml", 87 "ita_model_testsuite.uml", 88 "ita_model_scheduling.uml"); 89 82 90 private static class TestData { 83 91 public final String propertiesFile; … … 89 97 90 98 public TestData(String propertiesFile, 91 String usageJournalFile,92 String usageProfileFile,93 String dslModelFile,94 String testSuiteFile,95 String schedulingFile)99 String usageJournalFile, 100 String usageProfileFile, 101 String dslModelFile, 102 String testSuiteFile, 103 String schedulingFile) 96 104 { 97 105 this.propertiesFile = propertiesFile; … … 101 109 this.testSuiteFile = testSuiteFile; 102 110 this.schedulingFile = schedulingFile; 103 111 104 112 } 105 113 … … 119 127 @BeforeClass 120 128 public static void setUpBeforeClass() throws Exception { 121 new TextConsole(Level. FINE);129 new TextConsole(Level.SEVERE); 122 130 } 123 131 124 132 @After 125 133 public void tearDown() throws Exception { 126 deleteFiles(new File(OUTPUT_DIR)); 134 if (DELETE_OUTPUTS) { 135 deleteFiles(new File(OUTPUT_DIR)); 136 } 127 137 } 128 138 … … 130 140 public void testCreateUMLTransitionSequence_ITA_1() throws Exception { 131 141 TestData testdata = ita_1; 132 142 133 143 Properties properties = loadProperties(testdata); 134 144 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); … … 137 147 138 148 StateMachine stateMachine = 139 (StateMachine) model.getPackagedElement("StateMachineTransportService", true, 140 UMLPackage.Literals.STATE_MACHINE, true); 141 142 149 (StateMachine) model.getPackagedElement("StateMachineTransportService", true, 150 UMLPackage.Literals.STATE_MACHINE, true); 151 143 152 Collection<List<Event>> umlSequences = new LinkedList<>(); 144 153 145 154 // remove everything but transport from sequences 146 155 for (List<Event> sequence : sequences) { … … 159 168 public void testConvertStateMachineToUsageProfile__ITA_1() throws Exception { 160 169 TestData testdata = ita_1; 161 170 162 171 Properties properties = loadProperties(testdata); 163 172 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); … … 190 199 createInteractionFromEventSequenceWorkflow(deda_1); 191 200 } 192 201 193 202 @Test 194 203 public void testCreateInteractionFromEventSequence_DEDA_2() throws Exception { 195 204 createInteractionFromEventSequenceWorkflow(deda_2); 196 205 } 197 206 198 207 @Test 199 208 public void testCreateInteractionFromEventSequence_ITA_1() throws Exception { … … 205 214 calculateUsageScoreWorkflow(deda_1); 206 215 } 207 216 208 217 @Test 209 218 public void testCalculateUsageScore_DEDA_2() throws Exception { … … 211 220 } 212 221 213 214 222 @Test 215 223 public void testCalculateUsageScore_ITA_1() throws Exception { … … 221 229 createSchedulingWorkflow(deda_1); 222 230 } 223 231 224 232 @Test 225 233 public void testCreateScheduling_DEDA_2() throws Exception { 226 234 createSchedulingWorkflow(deda_2); 227 235 } 228 236 229 237 @Test 230 238 public void testCreateScheduling_ITA() throws Exception { … … 236 244 validateModelWithLogWorkflow(deda_1); 237 245 } 238 239 246 240 247 @Test … … 247 254 validateModelWithLogWorkflow(ita_1); 248 255 } 249 256 250 257 private void validateModelWithLogWorkflow(TestData testdata) throws Exception { 251 258 Properties properties = loadProperties(testdata); 252 259 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 253 Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 254 260 Model model = 261 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 262 255 263 // run validation 256 int violations = UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context")); 264 int violations = 265 UMLUtils.validateModelWithLog(sequences, model, properties.getProperty("test.context")); 257 266 if (violations == 0) { 258 267 System.out.println("No problems found."); … … 262 271 } 263 272 } 264 273 265 274 private void createInteractionFromEventSequenceWorkflow(TestData testdata) throws Exception { 266 275 Properties properties = loadProperties(testdata); 267 276 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 268 Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 269 277 Model model = 278 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 279 270 280 // create a test case for each observed sequence 271 int i=0; 272 for( List<Event> sequence : sequences ) { 273 UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 274 properties.getProperty("test.context")); 281 int i = 0; 282 for (List<Event> sequence : sequences) { 283 UMLUtils.createInteractionFromEventSequence(sequence, model, 284 properties.getProperty("testcases.prefix") + 285 "_" + i, 286 properties.getProperty("test.context")); 275 287 i++; 276 288 } 277 } 278 289 290 ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.testSuiteFile); 291 } 292 279 293 private void calculateUsageScoreWorkflow(TestData testdata) throws Exception { 280 294 Properties properties = loadProperties(testdata); 281 295 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 282 296 Model model = 283 ModelUtils.loadModel(ClassLoader 284 .getSystemResourceAsStream(testdata.dslModelFile));285 IStochasticProcess usageProfile = createUsageProfile(sequences);286 Collection<List<Event>> generatedSequences =createRandomSequences(usageProfile, properties);287 297 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 298 IStochasticProcess usageProfile = createUsageProfile(testdata, sequences); 299 Collection<List<Event>> generatedSequences = 300 createRandomSequences(usageProfile, properties); 301 288 302 int i = 1; 303 List<Interaction> interactions = new LinkedList<>(); 289 304 int[] lengths = new int[generatedSequences.size()]; 290 305 for (List<Event> sequence : generatedSequences) { 291 UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 292 properties.getProperty("test.context")); 306 interactions.add(UMLUtils.createInteractionFromEventSequence(sequence, model, 307 properties.getProperty("testcases.prefix") + 308 "_" + i, 309 properties.getProperty("test.context"))); 293 310 lengths[i - 1] = sequence.size(); 294 311 i++; 295 312 } 296 for (int j = 0; j < generatedSequences.size(); j++) { 297 Interaction interaction = 298 (Interaction) model.getPackagedElement(properties.getProperty("testcases.prefix") + j, true, 299 UMLPackage.Literals.INTERACTION, true); 300 double usageScore = UMLUtils.calculateUsageScore(interaction, usageProfile); 313 for (int j = 0; j < interactions.size(); j++) { 314 double usageScore = UMLUtils.calculateUsageScore(interactions.get(j), usageProfile); 301 315 System.out.format("usage score %02d: %.2f \t %d\n", j + 1, usageScore, lengths[j]); 302 316 } 303 317 } 304 318 305 319 private void createSchedulingWorkflow(TestData testdata) throws Exception { 306 320 Properties properties = loadProperties(testdata); 307 321 Collection<List<Event>> sequences = loadAndPreprocessUsageJournal(testdata, properties); 308 Model model = ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 309 IStochasticProcess usageProfile = createUsageProfile(sequences); 310 Collection<List<Event>> generatedSequences = createRandomSequences(usageProfile, properties); 322 Model model = 323 ModelUtils.loadModel(ClassLoader.getSystemResourceAsStream(testdata.dslModelFile)); 324 IStochasticProcess usageProfile = createUsageProfile(testdata, sequences); 325 Collection<List<Event>> generatedSequences = 326 createRandomSequences(usageProfile, properties); 311 327 int i = 1; 312 328 for (List<Event> sequence : generatedSequences) { 313 UMLUtils.createInteractionFromEventSequence(sequence, model, properties.getProperty("testcases.prefix")+"_"+i, 329 UMLUtils.createInteractionFromEventSequence(sequence, model, 330 properties.getProperty("testcases.prefix") + 331 "_" + i, 314 332 properties.getProperty("test.context")); 315 333 i++; 316 334 } 317 318 UMLUtils.createScheduling(model, usageProfile, null);319 320 //ModelUtils.writeModelToFile(model, OUTPUT_DIR + "testCreateScheduling_1_result.uml");321 } 322 335 336 UMLUtils.createScheduling(model, usageProfile, properties.getProperty("test.context")); 337 338 ModelUtils.writeModelToFile(model, OUTPUT_DIR + testdata.schedulingFile); 339 } 340 323 341 private Properties loadProperties(TestData testdata) throws Exception { 324 342 Properties properties = new Properties(); 325 properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile).getFile())); 343 properties.load(new FileInputStream(ClassLoader.getSystemResource(testdata.propertiesFile) 344 .getFile())); 326 345 return properties; 327 346 } 328 329 private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, Properties properties) throws Exception { 347 348 private Collection<List<Event>> loadAndPreprocessUsageJournal(TestData testdata, 349 Properties properties) 350 throws Exception 351 { 330 352 // load usage journal 331 353 HTTPLogParser parser = 332 354 new HTTPLogParser(new File(ClassLoader.getSystemResource(testdata.propertiesFile) 333 355 .getFile())); 334 parser 335 .parseFile(new File(ClassLoader 336 .getSystemResource(testdata.usageJournalFile) 337 .getFile())); 356 parser.parseFile(new File(ClassLoader.getSystemResource(testdata.usageJournalFile) 357 .getFile())); 338 358 Collection<List<Event>> sequences = parser.getSequences(); 339 359 340 // remove non SOAP events 360 // remove non SOAP events and convert to SimpleSOAPEventType 361 Collection<List<Event>> simpleSOAPSequences = new LinkedList<>(); 341 362 for (List<Event> sequence : sequences) { 342 SOAPUtils.removeNonSOAPEvents(sequence);343 } 344 363 simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true)); 364 } 365 345 366 // remove calls to ingored services 346 367 Set<String> ignoredServices = new HashSet<>(); 347 368 String ignoredServicesString = properties.getProperty("test.ignored.services"); 348 if ( ignoredServicesString!=null) {349 for ( String service : ignoredServicesString.split(",")) {369 if (ignoredServicesString != null) { 370 for (String service : ignoredServicesString.split(",")) { 350 371 ignoredServices.add(service.trim()); 351 372 } 352 373 } 353 354 for (List<Event> sequence : s equences) {374 375 for (List<Event> sequence : simpleSOAPSequences) { 355 376 for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) { 356 377 Event event = eventIter.next(); 357 S OAPEventType eventType = (SOAPEventType) event.getType();378 SimpleSOAPEventType eventType = (SimpleSOAPEventType) event.getType(); 358 379 if (ignoredServices.contains(eventType.getServiceName())) { 359 380 eventIter.remove(); … … 361 382 } 362 383 } 363 return sequences; 364 } 365 366 private IStochasticProcess createUsageProfile(Collection<List<Event>> sequences) throws Exception { 367 Collection<List<Event>> simpleSOAPSequences = new LinkedList<>(); 368 for (List<Event> sequence : sequences) { 369 simpleSOAPSequences.add(SOAPUtils.convertToSimpleSOAPEvent(sequence, true)); 370 } 371 384 return simpleSOAPSequences; 385 } 386 387 private IStochasticProcess createUsageProfile(TestData testdata, Collection<List<Event>> sequences) 388 throws Exception 389 { 372 390 FirstOrderMarkovModel usageProfile = new FirstOrderMarkovModel(new Random(1)); 373 usageProfile.train(simpleSOAPSequences); 391 usageProfile.train(sequences); 392 FileOutputStream fos = new FileOutputStream(OUTPUT_DIR + testdata.usageProfileFile); 393 SerializationUtils.serialize(usageProfile, fos); 394 fos.close(); 374 395 return usageProfile; 375 396 } 376 377 private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, Properties properties) throws Exception { 397 398 private Collection<List<Event>> createRandomSequences(IStochasticProcess usageProfile, 399 Properties properties) throws Exception 400 { 378 401 int numberOfTestCases = Integer.parseInt(properties.getProperty("testcases.number")); 379 402 int testCaseMinLength = Integer.parseInt(properties.getProperty("testcases.minlenth", "1")); 380 int testCaseMaxLength = Integer.parseInt(properties.getProperty("testcases.maxlenth", "100")); 403 int testCaseMaxLength = 404 Integer.parseInt(properties.getProperty("testcases.maxlenth", "100")); 381 405 int maxIter = numberOfTestCases * 100; 382 RandomWalkGenerator testGenerator = new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, maxIter); 406 RandomWalkGenerator testGenerator = 407 new RandomWalkGenerator(numberOfTestCases, testCaseMinLength, testCaseMaxLength, true, 408 maxIter); 383 409 return testGenerator.generateTestSuite(usageProfile); 384 410 } 385 411 386 412 private void deleteFiles(File file) { 387 413 if (file.exists()) { -
trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java
r1926 r1929 15 15 package de.ugoe.cs.autoquest.plugin.uml; 16 16 17 import java.io.StringWriter;18 import java.util.ArrayList;19 17 import java.util.Collection; 20 18 import java.util.Collections; … … 32 30 import java.util.TreeSet; 33 31 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 41 32 import org.eclipse.emf.common.util.EList; 42 import org.eclipse.emf.common.util.URI;43 33 import org.eclipse.uml2.uml.Activity; 44 34 import org.eclipse.uml2.uml.ActivityEdge; … … 74 64 import org.eclipse.uml2.uml.Port; 75 65 import org.eclipse.uml2.uml.PrimitiveType; 76 import org.eclipse.uml2.uml.Profile;77 66 import org.eclipse.uml2.uml.Property; 78 67 import org.eclipse.uml2.uml.Region; … … 83 72 import org.eclipse.uml2.uml.Transition; 84 73 import org.eclipse.uml2.uml.Trigger; 85 import org.eclipse.uml2.uml.Type;86 74 import org.eclipse.uml2.uml.UMLPackage; 75 import org.eclipse.uml2.uml.ValueSpecification; 87 76 import org.eclipse.uml2.uml.Vertex; 88 import org.w3c.dom.Attr;89 import org.w3c.dom.Node;90 import org.w3c.dom.NodeList;91 77 92 78 import de.ugoe.cs.autoquest.eventcore.Event; … … 113 99 final static int MAX_MULTIPLICITY = 10; 114 100 115 final public static URI UML_PRIMITIVE_TYPES_URI = URI116 .createURI("pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml", true);117 118 101 /** 119 102 * <p> … … 134 117 String testContextName) 135 118 { 136 final Profile utpProfile = model.getAppliedProfile("utp");137 final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent");138 final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT");139 final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext");140 141 119 int violationCount = 0; 142 Component testContext = fetchTestContext(model, utpTestContext,testContextName);120 Component testContext = fetchTestContext(model, testContextName); 143 121 if (testContext == null) { 144 122 violationCount++; … … 186 164 // fetch all SUTs and TestComponents 187 165 HashMap<String, Property> properties = new HashMap<>(); 188 for (Property property : testContext.getAllAttributes()) { 189 if (property.getAppliedStereotypes().contains(utpSUT)) { 190 properties.put(property.getName(), property); 191 } 192 else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) { 193 properties.put(property.getName(), property); 194 } 166 for (Property property : fetchAllSUTProperties(testContext)) { 167 properties.put(property.getName(), property); 168 } 169 for (Property property : fetchAllTestComponentProperties(testContext)) { 170 properties.put(property.getName(), property); 195 171 } 196 172 Console.traceln(Level.INFO, "Found the following services in the TestConfiguration:"); … … 455 431 * test context found is used. 456 432 */ 457 public static voidcreateInteractionFromEventSequence(List<Event> sequence,458 Model model,459 String interactionName,460 String testContextName)433 public static Interaction createInteractionFromEventSequence(List<Event> sequence, 434 Model model, 435 String interactionName, 436 String testContextName) 461 437 { 462 final Profile utpProfile = model.getAppliedProfile("utp"); 463 final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase"); 464 final Stereotype utpTestComponent = (Stereotype) utpProfile.getOwnedMember("TestComponent"); 465 final Stereotype utpSUT = (Stereotype) utpProfile.getOwnedMember("SUT"); 466 final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 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 473 Component testContext = fetchTestContext(model, utpTestContext, testContextName); 438 final Component testContext = fetchTestContext(model, testContextName); 474 439 if (testContext == null) { 475 440 throw new RuntimeException("Could not find any test context in the model"); 476 441 } 477 442 478 Operation operation = testContext.createOwnedOperation(interactionName, null, null);479 operation.applyStereotype( utpTestCase);480 481 Interaction interaction =443 final Operation operation = testContext.createOwnedOperation(interactionName, null, null); 444 operation.applyStereotype(UTPUtils.getTestCaseStereotype(model)); 445 446 final Interaction interaction = 482 447 (Interaction) testContext.createPackagedElement(interactionName + "_Impl", 483 448 UMLPackage.Literals.INTERACTION); … … 487 452 Lifeline userLifeline = null; 488 453 489 for (Property property : testContext.getAllAttributes()) {490 if (property.getAppliedStereotypes().contains(utpSUT)) {491 String serviceName = property.getName();492 Lifeline targetLifeline = interaction.createLifeline(serviceName);493 targetLifeline.setRepresents(property);494 }495 else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {496 497 498 499 500 501 502 503 } 454 for (Property property : fetchAllSUTProperties(testContext)) { 455 String serviceName = property.getName(); 456 Lifeline targetLifeline = interaction.createLifeline(serviceName); 457 targetLifeline.setRepresents(property); 458 } 459 for (Property property : fetchAllTestComponentProperties(testContext)) { 460 // TODO check if this is still required 461 if (userLifeline != null) { 462 throw new RuntimeException( 463 "TestContext must only have one TestComponent for the application of usage-based testing."); 464 } 465 userLifeline = interaction.createLifeline(property.getName()); 466 userLifeline.setRepresents(property); 467 } 468 504 469 if (userLifeline == null) { 505 470 throw new RuntimeException("No TestComponent found, could not create user lifeline."); … … 580 545 callRecvFragment.setCovered(msgTargetLifeline); 581 546 547 // get connector 548 Connector connector = inferConnector(msgSourceLifeline, msgTargetLifeline); 549 582 550 // create call 583 Message callMessage = interaction.createMessage( methodName);551 Message callMessage = interaction.createMessage(prefix + "call"); 584 552 callMessage.setSignature(calledOperation); 585 set MessageParameters(callMessage, calledOperation, event, prefix);586 callMessage.setConnector( inferConnector(msgSourceLifeline, msgTargetLifeline));553 setCallMessageParameters(callMessage, calledOperation, event, prefix); 554 callMessage.setConnector(connector); 587 555 callMessage.setSendEvent(callSendFragment); 588 556 callMessage.setReceiveEvent(callRecvFragment); … … 632 600 633 601 // create reply 634 Message replyMessage = interaction.createMessage( methodName+ "_reply");602 Message replyMessage = interaction.createMessage(prefix + "_reply"); 635 603 replyMessage.setMessageSort(MessageSort.REPLY_LITERAL); 636 604 replyMessage.setSignature(calledOperation); 605 setReplyMessageParameters(replyMessage, calledOperation); 606 replyMessage.setConnector(connector); 637 607 replyMessage.setSendEvent(replySendFragment); 638 608 replyMessage.setReceiveEvent(replyRecvFragment); … … 644 614 } 645 615 } 616 return interaction; 646 617 } 647 618 … … 662 633 { 663 634 double usageScore = 0.0d; 664 665 635 EList<InteractionFragment> interactionFragments = interaction.getFragments(); 666 636 List<Event> eventSequence = new LinkedList<>(); 667 637 eventSequence.add(Event.STARTEVENT); 668 638 for (InteractionFragment interactionFragment : interactionFragments) { 669 if (interactionFragment.getName() != null && 670 interactionFragment.getName().endsWith("_recvFragment")) // TODO must be more 671 // generic 672 { 673 String serviceName = 674 interactionFragment.getCovereds().get(0).getRepresents().getName().split("_")[0]; 675 String methodName = "UNKNOWN"; 676 if (interactionFragment instanceof MessageOccurrenceSpecification) { 677 methodName = 678 ((MessageOccurrenceSpecification) interactionFragment).getMessage() 679 .getName(); 680 } 681 // eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName, "", 682 // ))); // TODO 683 // add 684 // client 685 // name 639 if (interactionFragment instanceof MessageOccurrenceSpecification) { 640 Message message = 641 ((MessageOccurrenceSpecification) interactionFragment).getMessage(); 642 if (message.getReceiveEvent().equals(interactionFragment) && isCallMessage(message)) 643 { 644 String clientName = 645 ((MessageOccurrenceSpecification) message.getSendEvent()).getCovereds() 646 .get(0).getName(); 647 String serviceName = 648 ((MessageOccurrenceSpecification) message.getReceiveEvent()).getCovereds() 649 .get(0).getName(); 650 String methodName = message.getSignature().getName(); 651 eventSequence.add(new Event(new SimpleSOAPEventType(methodName, serviceName, 652 clientName, null))); 653 } 686 654 } 687 655 } … … 707 675 String testContextName) 708 676 { 709 710 final Profile utpProfile = model.getAppliedProfile("utp"); 711 final Stereotype utpTestCase = (Stereotype) utpProfile.getOwnedMember("TestCase"); 712 final Stereotype utpTestContext = (Stereotype) utpProfile.getOwnedMember("TestContext"); 713 714 Component testContext = fetchTestContext(model, utpTestContext, testContextName); 677 final Component testContext = fetchTestContext(model, testContextName); 715 678 if (testContext == null) { 716 679 throw new RuntimeException("Could not find any test context in the model"); … … 720 683 721 684 // first, we determine all test cases and calculate their usage scores 685 final Stereotype utpTestCase = UTPUtils.getTestCaseStereotype(model); 722 686 for (Operation operation : testContext.getAllOperations()) { 723 if (operation.getAppliedStereotypes().contains(utpTestCase)) { 687 if (operation.getAppliedStereotypes().contains(utpTestCase) && 688 !operation.getMethods().isEmpty()) 689 { 724 690 Interaction interaction = (Interaction) operation.getMethods().get(0); 725 691 usageScoreMapUnsorted … … 764 730 edge.setSource(currentOperationNode); 765 731 edge.setTarget(finalNode); 766 }767 768 /**769 * From770 * http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java771 * and adapted to do an inverse sorting772 */773 public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {774 List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());775 Collections.sort(list, new Comparator<Map.Entry<K, V>>() {776 @Override777 public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {778 return -1 * (o1.getValue()).compareTo(o2.getValue());779 }780 });781 782 Map<K, V> result = new LinkedHashMap<>();783 for (Map.Entry<K, V> entry : list) {784 result.put(entry.getKey(), entry.getValue());785 }786 return result;787 732 } 788 733 … … 862 807 } 863 808 809 /** 810 * <p> 811 * Fetches all realized interfaces from the type of a UML {@link Property} (i.e., 812 * property.getType()). If no interfaces are realized, an empty set is returned. 813 * </p> 814 * 815 * @param property 816 * property, of the whose realized interfaces of the type are determined 817 * @return realized interfaces 818 */ 864 819 private static Set<Interface> getRealizedInterfacesFromProperty(Property property) { 865 820 return getRealizedInterfaceFromComponent((Component) property.getType()); 866 821 } 867 822 868 private static Set<Interface> getRealizedInterfaceFromComponent(Component comp) { 823 /** 824 * <p> 825 * Fetches all realized interfaces from a UML {@link Component}. If no interfaces are realized, 826 * an empty set is returned. 827 * </p> 828 * 829 * @param comp 830 * component whose realized interfaces are determined 831 * @return realized interfaces 832 */ 833 private static Set<Interface> getRealizedInterfaceFromComponent(Component component) { 869 834 Set<Interface> interfaces = new HashSet<>(); 870 835 // Interface myInterface = null; 871 for (Property property : comp .getAttributes()) {836 for (Property property : component.getAttributes()) { 872 837 if (property instanceof Port) { 873 838 Port port = (Port) property; … … 880 845 } 881 846 882 private static Component fetchTestContext(Package pkg, 883 Stereotype utpTestContext, 884 String testContextName) 885 { 886 List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext); 847 /** 848 * <p> 849 * Determines searches within a {@link Package} for a component to which the UTP TestContext 850 * stereotype is applied. 851 * <ul> 852 * <li>If no testContextName is provided, the first test context found is returned.</li> 853 * <li>In case no test context is found, null is returned.</li> 854 * </p> 855 * 856 * @param pkg 857 * package where the test context is being locked for 858 * @param testContextName 859 * name of the test context; in case no test name is specified, use null and not the 860 * empty String. 861 * @return {@link Component} to which the TestContext stereotype is applied 862 */ 863 private static Component fetchTestContext(final Package pkg, final String testContextName) { 864 List<Component> testContexts = fetchAllTestContexts(pkg); 887 865 if (testContexts.isEmpty()) { 888 866 return null; … … 901 879 } 902 880 903 private static List<Component> fetchTestContextRecursively(Package pkg, 904 Stereotype utpTestContext) 905 { 906 List<Component> testContexts = new LinkedList<>(); 881 /** 882 * <p> 883 * Retrieves all UML {@link Component}s to which the UTP TestContext stereotype is applied from 884 * a package. This method calls itself recursively to include all components contained in 885 * sub-packages. 886 * </p> 887 * <p> 888 * In case no test context is found, an empty list is returned. 889 * </p> 890 * 891 * @param pkg 892 * package from which the test contexts are retrieved 893 * @return {@link List} of test contexts 894 */ 895 private static List<Component> fetchAllTestContexts(final Package pkg) { 896 final Stereotype utpTestContext = UTPUtils.getTestContextStereotype(pkg.getModel()); 897 final List<Component> testContexts = new LinkedList<>(); 907 898 for (Element element : pkg.getOwnedElements()) { 908 899 if (element instanceof Package) { 909 testContexts.addAll(fetch TestContextRecursively((Package) element, utpTestContext));900 testContexts.addAll(fetchAllTestContexts((Package) element)); 910 901 } 911 902 if (element instanceof Component && … … 920 911 /** 921 912 * <p> 922 * Infers connector between two lifelines. TODO: currently assumes only one connector between 923 * two lifelines possible. I need to make sure this assumption is valid. 924 * </p> 925 * 926 * @param userAttributes 913 * Retrieves all properties that represent a UTP TestComponent from a test context. 914 * </p> 915 * 916 * @param testContext 917 * test context from which the properties are retrieved 918 * @return properties that represent test components 919 */ 920 private static Set<Property> fetchAllTestComponentProperties(final Component testContext) { 921 // fetch all SUTs and TestComponents 922 final Stereotype utpTestComponent = 923 UTPUtils.getTestComponentStereotype(testContext.getModel()); 924 final Set<Property> properties = new HashSet<>(); 925 for (Property property : testContext.getAllAttributes()) { 926 if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) { 927 properties.add(property); 928 } 929 } 930 return properties; 931 } 932 933 /** 934 * <p> 935 * Retrieves all properties that represent a UTP SUT from a test context. 936 * </p> 937 * 938 * @param testContext 939 * test context from which the properties are retrieved 940 * @return properties that represent the SUTs 941 */ 942 private static Set<Property> fetchAllSUTProperties(final Component testContext) { 943 // fetch all SUTs and TestComponents 944 final Stereotype utpSUT = UTPUtils.getSUTStereotype(testContext.getModel()); 945 final Set<Property> properties = new HashSet<>(); 946 for (Property property : testContext.getAllAttributes()) { 947 if (property.getAppliedStereotypes().contains(utpSUT)) { 948 properties.add(property); 949 } 950 } 951 return properties; 952 } 953 954 /** 955 * <p> 956 * Infers connector between two lifelines. 957 * </p> 958 * <p> 959 * TODO: Currently assumes only one connector between two lifelines possible. This assumption is 960 * invalid as soon as there are two ports that connect the same two properties. 961 * </p> 962 * 963 * @param msgSourceLifeline 964 * source lifeline of the message 927 965 * @param targetAttributes 966 * target lifeline of the message 928 967 */ 929 968 private static Connector inferConnector(Lifeline msgSourceLifeline, Lifeline msgTargetLifeline) … … 955 994 } 956 995 996 /** 997 * <p> 998 * Creates a map that maps the interfaces to the properties, i.e., services that they are 999 * represented by. 1000 * </p> 1001 * <p> 1002 * TODO: currently assumes that each interfaces is only realized by one property 1003 * </p> 1004 * 1005 * @param model 1006 * model for which the interface->service map is created 1007 * @return the map 1008 */ 957 1009 private static Map<Interface, String> createInterfaceServiceMap(Model model) { 958 1010 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); 1011 List<Component> testContexts = fetchAllTestContexts(model.getModel()); 965 1012 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 } 1013 for (Property property : fetchAllSUTProperties(testContext)) { 1014 for (Interface intface : getRealizedInterfacesFromProperty(property)) { 1015 interfaceServiceMap.put(intface, property.getName()); 1016 } 1017 } 1018 for (Property property : fetchAllTestComponentProperties(testContext)) { 1019 for (Interface intface : getRealizedInterfacesFromProperty(property)) { 1020 interfaceServiceMap.put(intface, property.getName()); 973 1021 } 974 1022 } … … 977 1025 } 978 1026 979 private static void setMessageParameters(Message callMessage, 980 Operation calledOperation, 981 Event event, 982 String prefix) 1027 /** 1028 * <p> 1029 * Sets values for the parameters of a call message. The values are, if possible, inferred from 1030 * the event that is provided. 1031 * </p> 1032 * 1033 * @param callMessage 1034 * call message for which the parameters are set 1035 * @param calledOperation 1036 * operation that is called by the message 1037 * @param event 1038 * event that provides the parameters; in case of null, default values are assumed 1039 * @param prefix 1040 * prefix of the call message; used to create good warnings and debugging information 1041 */ 1042 private static void setCallMessageParameters(Message callMessage, 1043 Operation calledOperation, 1044 Event event, 1045 String prefix) 983 1046 { 984 1047 org.w3c.dom.Element requestBody = SOAPUtils.getSoapRequestBodyFromEvent(event); 1048 Package instSpecPkg = null; 985 1049 986 1050 // Set parameters of operation 987 1051 for (Parameter param : calledOperation.getOwnedParameters()) { 1052 if (instSpecPkg == null) { 1053 instSpecPkg = getOrCreateInstanceSpecificationPackage(param.getModel(), event); 1054 } 1055 1056 String path = calledOperation.getName() + ":" + param.getName(); 988 1057 Expression argument = 989 1058 (Expression) callMessage.createArgument(param.getName(), param.getType(), 990 1059 UMLPackage.Literals.EXPRESSION); 991 1060 992 if (param.getDirection() == ParameterDirectionKind.IN_LITERAL || 993 param.getDirection() == ParameterDirectionKind.INOUT_LITERAL) 994 { 1061 if (isInParameter(param)) { 995 1062 if (param.getType() instanceof DataType) { 996 1063 List<org.w3c.dom.Element> paramNodes = 997 getMatchingChildNode((DataType) param.getType(), requestBody); 998 for (org.w3c.dom.Element paramNode : paramNodes) { 1064 SOAPUtils.getMatchingChildNode(param.getType().getName(), requestBody); 1065 int multiplicityChosen = paramNodes.size(); 1066 1067 if (multiplicityChosen == 0 && param.getLower() > 0) { 1068 Console.traceln(Level.WARNING, 1069 "required attribute not found in SOAP message: " + path); 1070 Console 1071 .traceln(Level.WARNING, 1072 "setting default values for this attribute and all its children"); 1073 Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE + 1074 SOAPUtils.getSerialization(requestBody)); 1075 multiplicityChosen = param.getLower(); 1076 } 1077 for (int i = 0; i < multiplicityChosen; i++) { 1078 org.w3c.dom.Element paramNode = null; 1079 if (!paramNodes.isEmpty()) { 1080 paramNode = paramNodes.get(i); 1081 } 999 1082 InstanceSpecification instSpec = 1000 createInstanceSpecification((DataType) param.getType(), event, prefix,1001 p aramNode, "");1083 createInstanceSpecification((DataType) param.getType(), instSpecPkg, 1084 prefix, paramNode, path); 1002 1085 1003 1086 InstanceValue value = … … 1009 1092 } 1010 1093 else if (param.getType() instanceof PrimitiveType) { 1011 createOperandPrimitiveType(param, argument, requestBody );1094 createOperandPrimitiveType(param, argument, requestBody, path); 1012 1095 } 1013 1096 } … … 1019 1102 } 1020 1103 1104 /** 1105 * <p> 1106 * Creates an {@link InstanceSpecification} for a data type in the given package. The values are 1107 * inferred, if possible, from the DOM node. The prefix and the path are used for naming the 1108 * instance specification and to provide good warnings and debug information in case of 1109 * problems. 1110 * </p> 1111 * 1112 * @param type 1113 * DataType for which the {@link InstanceSpecification} is created 1114 * @param pkg 1115 * package in which the {@link InstanceSpecification} is created 1116 * @param prefix 1117 * prefix used for naming the {@link InstanceSpecification} 1118 * @param currentNode 1119 * node of a DOM from which values are inferred 1120 * @param path 1121 * used for warnings and debug information 1122 * @return {@link InstanceSpecification} for the given type 1123 */ 1021 1124 private static InstanceSpecification createInstanceSpecification(DataType type, 1022 Event event,1125 Package pkg, 1023 1126 String prefix, 1024 1127 org.w3c.dom.Element currentNode, … … 1028 1131 path = type.getName(); 1029 1132 } 1030 // System.out.println(path);1031 String pkgUBTInstSpecs = "UBT_InstanceSpecifications";1032 Model model = type.getModel();1033 Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs);1034 if (ubtInstSpecPkg == null) {1035 ubtInstSpecPkg =1036 (Package) type.getModel().createPackagedElement(pkgUBTInstSpecs,1037 UMLPackage.Literals.PACKAGE);1038 }1039 String serviceName = SOAPUtils.getServiceNameFromEvent(event);1040 Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName);1041 if (serviceInstSpecPkg == null) {1042 serviceInstSpecPkg =1043 (Package) ubtInstSpecPkg.createPackagedElement(serviceName,1044 UMLPackage.Literals.PACKAGE);1045 }1046 1133 1047 1134 InstanceSpecification instSpec = 1048 (InstanceSpecification) serviceInstSpecPkg.createPackagedElement(prefix + "instspec_" + 1049 type.getName(), UMLPackage.Literals.INSTANCE_SPECIFICATION); 1135 (InstanceSpecification) pkg 1136 .createPackagedElement(prefix + "instspec_" + type.getName(), 1137 UMLPackage.Literals.INSTANCE_SPECIFICATION); 1050 1138 instSpec.getClassifiers().add(type); 1051 1139 for (Property prop : type.getAllAttributes()) { … … 1057 1145 int multiplicityChosen = 0; 1058 1146 if (currentNode != null) { 1059 attributeNodes = getMatchingChildNode(prop, currentNode);1147 attributeNodes = SOAPUtils.getMatchingChildNode(prop.getName(), currentNode); 1060 1148 multiplicityChosen = attributeNodes.size(); 1061 1149 } … … 1086 1174 (InstanceValue) slot.createValue(prop.getName() + "_" + i, prop.getType(), 1087 1175 UMLPackage.Literals.INSTANCE_VALUE); 1088 value.setInstance(createInstanceSpecification((DataType) prop.getType(), event,1176 value.setInstance(createInstanceSpecification((DataType) prop.getType(), pkg, 1089 1177 prefix, attributeNode, path + 1090 1178 "." + prop.getName())); … … 1100 1188 } 1101 1189 1190 /** 1191 * <p> 1192 * Gets or creates a {@link Package} for {@link InstanceSpecification} created by the 1193 * usage-based testing. Each service gets its own sub-package within a package called 1194 * UBT_InstanceSpecifications. " 1195 * </p> 1196 * 1197 * @param model 1198 * model in which the package is generated 1199 * @param event 1200 * event from which the service name is inferred 1201 * @return package for the {@link InstanceSpecification}s 1202 */ 1203 private static Package getOrCreateInstanceSpecificationPackage(Model model, Event event) { 1204 String pkgUBTInstSpecs = "UBT_InstanceSpecifications"; 1205 Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs); 1206 if (ubtInstSpecPkg == null) { 1207 ubtInstSpecPkg = 1208 (Package) model.createPackagedElement(pkgUBTInstSpecs, UMLPackage.Literals.PACKAGE); 1209 } 1210 String serviceName = SOAPUtils.getServiceNameFromEvent(event); 1211 Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName); 1212 if (serviceInstSpecPkg == null) { 1213 serviceInstSpecPkg = 1214 (Package) ubtInstSpecPkg.createPackagedElement(serviceName, 1215 UMLPackage.Literals.PACKAGE); 1216 } 1217 return serviceInstSpecPkg; 1218 } 1219 1220 /** 1221 * <p> 1222 * Creates an operand that defines a {@link PrimitiveType}. 1223 * </p> 1224 * <p> 1225 * TODO: Currently does nothing in case of multiplicity 0. I am not sure if, in that case, one 1226 * has to define LiteralNull instead. 1227 * </p> 1228 * 1229 * @param param 1230 * parameter for which the operand is created 1231 * @param argument 1232 * argument to which the operand is added 1233 * @param currentNode 1234 * DOM node from which is value for the operand is inferred 1235 * @param path 1236 * used for warnings and debug information 1237 */ 1102 1238 private static void createOperandPrimitiveType(Parameter param, 1103 1239 Expression argument, 1104 org.w3c.dom.Element currentNode) 1240 org.w3c.dom.Element currentNode, 1241 String path) 1105 1242 { 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 1243 List<String> attributeValues = SOAPUtils.getValuesFromElement(param.getName(), currentNode); 1244 1245 if (attributeValues.isEmpty()) { 1246 if (param.getLower() == 0) { 1247 // ignoring optional attribute 1248 return; 1249 } 1250 else { 1251 if (currentNode != null) { 1252 Console.traceln(Level.WARNING, 1253 "required attribute not found in SOAP message: " + path + "." + 1254 param.getName()); 1255 Console.traceln(Level.WARNING, "setting default values for this attribute"); 1256 Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE + 1257 SOAPUtils.getSerialization(currentNode)); 1258 } 1259 attributeValues.add(null); 1260 } 1261 } 1262 for (String attributeValue : attributeValues) { 1263 if ("String".equals(param.getType().getName())) { 1264 LiteralString spec = 1265 (LiteralString) argument.createOperand(param.getName(), null, 1266 UMLPackage.Literals.LITERAL_STRING); 1267 if (attributeValue != null) { 1268 spec.setValue(attributeValue); 1269 } 1270 else { 1271 spec.setValue("foobar"); 1272 } 1273 } 1274 else if ("Integer".equals(param.getType().getName())) { 1275 LiteralInteger spec = 1276 (LiteralInteger) argument.createOperand(param.getName(), null, 1277 UMLPackage.Literals.LITERAL_INTEGER); 1278 if (attributeValue != null) { 1279 spec.setValue(Integer.parseInt(attributeValue)); 1280 } 1281 else { 1282 spec.setValue(42); 1283 } 1284 } 1285 else if ("Boolean".equals(param.getType().getName())) { 1286 LiteralBoolean spec = 1287 (LiteralBoolean) argument.createOperand(param.getName(), null, 1288 UMLPackage.Literals.LITERAL_BOOLEAN); 1289 if (attributeValue != null) { 1290 spec.setValue(Boolean.parseBoolean(attributeValue)); 1291 } 1292 else { 1293 spec.setValue(true); 1294 } 1295 } 1296 else if ("Real".equals(param.getType().getName())) { 1297 LiteralReal spec = 1298 (LiteralReal) argument.createOperand(param.getName(), null, 1299 UMLPackage.Literals.LITERAL_REAL); 1300 if (attributeValue != null) { 1301 spec.setValue(Double.parseDouble(attributeValue)); 1302 } 1303 else { 1304 spec.setValue(3.14); 1305 } 1306 } 1307 } 1308 } 1309 1310 /** 1311 * <p> 1312 * Creates a {@link Slot} in an {@link InstanceSpecification} for a primitive type. 1313 * </p> 1314 * 1315 * @param instSpec 1316 * instance specification to which the slot is added 1317 * @param prop 1318 * property that describes the slot 1319 * @param currentNode 1320 * DOM node from which is value for the slot is inferred 1321 * @param path 1322 * used for warnings and debug information 1323 */ 1132 1324 private static void createSlotPrimitiveType(InstanceSpecification instSpec, 1133 1325 Property prop, … … 1135 1327 String path) 1136 1328 { 1137 List<String> attributeValues = getPrimitiveTypeValuesFromElement(prop, currentNode);1329 List<String> attributeValues = SOAPUtils.getValuesFromElement(prop.getName(), currentNode); 1138 1330 1139 1331 if (attributeValues.isEmpty()) { … … 1148 1340 prop.getName()); 1149 1341 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));1342 Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE + 1343 SOAPUtils.getSerialization(currentNode)); 1344 } 1153 1345 attributeValues.add(null); 1154 1346 } … … 1198 1390 } 1199 1391 else { 1200 value.setValue(3.14); // TODO needs to be real value1392 value.setValue(3.14); 1201 1393 } 1202 1394 } … … 1209 1401 } 1210 1402 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()); 1403 /** 1404 * <p> 1405 * Sets values for the parameters of a reply message. The values are, all LiterealNull and to 1406 * the INOUT, OUT and REPLY parameters, the UTP stereotype LiteralAny is applied. 1407 * </p> 1408 * 1409 * @param replyMessage 1410 * reply message for which the parameters are set 1411 * @param calledOperation 1412 * operation that is replied for by the message 1413 */ 1414 private static void setReplyMessageParameters(Message replyMessage, Operation calledOperation) { 1415 for (Parameter param : calledOperation.getOwnedParameters()) { 1416 Expression argument = 1417 (Expression) replyMessage.createArgument(param.getName(), param.getType(), 1418 UMLPackage.Literals.EXPRESSION); 1419 if (isOutParameter(param)) { 1420 ValueSpecification operand = 1421 argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL); 1422 operand.applyStereotype(UTPUtils.getLiteralAnyStereotype(param.getModel())); 1271 1423 } 1272 1424 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 1425 argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL); 1426 } 1427 } 1428 } 1429 1430 /** 1431 * <p> 1432 * Checks if a parameter has the direction IN or INOUT 1433 * </p> 1434 * 1435 * @param parameter 1436 * parameter that is checked 1437 * @return true if the direction is IN or INOUT; false otherwise 1438 */ 1439 private static boolean isInParameter(Parameter parameter) { 1440 return parameter.getDirection() == ParameterDirectionKind.IN_LITERAL || 1441 parameter.getDirection() == ParameterDirectionKind.INOUT_LITERAL; 1442 } 1443 1444 /** 1445 * <p> 1446 * Checks if a parameter has the direction RETURN, OUT or INOUT 1447 * </p> 1448 * 1449 * @param parameter 1450 * parameter that is checked 1451 * @return true if the direction is RETURN, OUT, or INOUT; false otherwise 1452 */ 1453 private static boolean isOutParameter(Parameter parameter) { 1454 return parameter.getDirection() == ParameterDirectionKind.RETURN_LITERAL || 1455 parameter.getDirection() == ParameterDirectionKind.OUT_LITERAL || 1456 parameter.getDirection() == ParameterDirectionKind.INOUT_LITERAL; 1457 } 1458 1459 /** 1460 * <p> 1461 * Checks if the {@link MessageSort} of a message is a call message, i.e., ASYNCH_CALL or 1462 * SYNCH_CALL. 1463 * </p> 1464 * 1465 * @param message 1466 * message that is checked 1467 * @return true if the message is a call message; false otherwise 1468 */ 1469 private static boolean isCallMessage(Message message) { 1470 if (message == null) { 1471 return false; 1472 } 1473 MessageSort msgSort = message.getMessageSort(); 1474 return msgSort == MessageSort.ASYNCH_CALL_LITERAL || 1475 msgSort == MessageSort.SYNCH_CALL_LITERAL; 1476 } 1477 1478 /** 1479 * <p> 1480 * inverse-sorts the values of a map. Has been adapted from <a href= 1481 * "http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java" 1482 * >this</a> StackOverflow post. 1483 * </p> 1484 * 1485 * @param map 1486 * map whose values are sorted 1487 * @return sorted version of the map 1488 */ 1489 private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) { 1490 // TODO possibly move to another class 1491 List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); 1492 Collections.sort(list, new Comparator<Map.Entry<K, V>>() { 1493 @Override 1494 public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) { 1495 return -1 * (o1.getValue()).compareTo(o2.getValue()); 1496 } 1497 }); 1498 1499 Map<K, V> result = new LinkedHashMap<>(); 1500 for (Map.Entry<K, V> entry : list) { 1501 result.put(entry.getKey(), entry.getValue()); 1502 } 1503 return result; 1504 } 1284 1505 }
Note: See TracChangeset
for help on using the changeset viewer.