1 | package de.uka.ipd.sdq.dsexplore.helper; |
2 | |
3 | import java.io.BufferedWriter; |
4 | import java.io.FileNotFoundException; |
5 | import java.io.FileOutputStream; |
6 | import java.io.FileWriter; |
7 | import java.io.IOException; |
8 | import java.io.OutputStreamWriter; |
9 | import java.text.SimpleDateFormat; |
10 | import java.util.ArrayList; |
11 | import java.util.Calendar; |
12 | import java.util.Collection; |
13 | import java.util.Date; |
14 | import java.util.List; |
15 | import java.util.Map.Entry; |
16 | |
17 | import org.apache.log4j.Logger; |
18 | import org.opt4j.core.Constraint; |
19 | import org.opt4j.core.Criterion; |
20 | import org.opt4j.core.Individual; |
21 | import org.opt4j.core.IntegerValue; |
22 | import org.opt4j.core.Objective; |
23 | import org.opt4j.core.Objectives; |
24 | import org.opt4j.core.Value; |
25 | |
26 | import de.uka.ipd.sdq.dsexplore.analysis.PCMPhenotype; |
27 | import de.uka.ipd.sdq.dsexplore.launch.DSEConstantsContainer; |
28 | import de.uka.ipd.sdq.dsexplore.opt4j.genotype.DesignDecisionGenotype; |
29 | import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators.ITactic; |
30 | import de.uka.ipd.sdq.dsexplore.opt4j.optimizer.heuristic.operators.TacticsResultCandidate; |
31 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEDecoder; |
32 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEIndividual; |
33 | import de.uka.ipd.sdq.dsexplore.opt4j.representation.DSEObjectives; |
34 | import de.uka.ipd.sdq.dsexplore.opt4j.start.Opt4JStarter; |
35 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.EntryLevelSystemCallCriterion; |
36 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.EvaluationAspectWithContext; |
37 | import de.uka.ipd.sdq.dsexplore.qml.pcm.datastructures.UsageScenarioBasedCriterion; |
38 | import de.uka.ipd.sdq.dsexplore.qml.pcm.reader.PCMDeclarationsReader; |
39 | import de.uka.ipd.sdq.pcm.designdecision.Candidates; |
40 | import de.uka.ipd.sdq.pcm.resourceenvironment.LinkingResource; |
41 | import de.uka.ipd.sdq.pcm.resourceenvironment.ProcessingResourceSpecification; |
42 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceContainer; |
43 | import de.uka.ipd.sdq.pcm.resourcetype.ProcessingResourceType; |
44 | import de.uka.ipd.sdq.pcm.resultdecorator.ResultDecoratorRepository; |
45 | import de.uka.ipd.sdq.pcm.resultdecorator.resourceenvironmentdecorator.LinkingResourceResults; |
46 | import de.uka.ipd.sdq.pcm.resultdecorator.resourceenvironmentdecorator.ProcessingResourceSpecificationResult; |
47 | import de.uka.ipd.sdq.pcm.resultdecorator.resourceenvironmentdecorator.UtilisationResult; |
48 | import de.uka.ipd.sdq.pcmsolver.models.PCMInstance; |
49 | import de.uka.ipd.sdq.statistics.estimation.ConfidenceInterval; |
50 | |
51 | /** |
52 | * XXX: Maybe make this a proper label provider for the results? Metamodel results? |
53 | * |
54 | * After calling close(), the ResultWriter should not be used anymore, because it will have closed its internal writer and set the reference to null. Any further logs are written to Log4J. |
55 | * |
56 | * @author Anne |
57 | * |
58 | */ |
59 | public class ResultsWriter { |
60 | |
61 | /** Logger for log4j. */ |
62 | private static Logger logger = |
63 | Logger.getLogger("de.uka.ipd.sdq.dsexplore.ResultsWriter"); |
64 | // |
65 | // Part for heuristics |
66 | // |
67 | /** |
68 | * Formating string used for logging purposes |
69 | */ |
70 | private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; |
71 | private String myfilename; |
72 | FileWriter fileWriter; |
73 | private List<Criterion> criteriaToSave; |
74 | private List<Criterion> criteriaWithConfidence; |
75 | |
76 | public ResultsWriter(String filename) { |
77 | this.myfilename = filename+"_"+getTimeDateString()+".csv"; |
78 | try { |
79 | this.fileWriter = new FileWriter(myfilename); |
80 | } catch (IOException e) { |
81 | logger.error("Cannot write to file "+myfilename+" to store individuals. I will print them to the logger instead. Cause: "+e.getMessage()); |
82 | e.printStackTrace(); |
83 | } |
84 | } |
85 | |
86 | |
87 | /** |
88 | * Write all individuals to new file (file current time in filename). |
89 | * @param individuals |
90 | * @param filename |
91 | * @param iteration |
92 | * @param exceptionList |
93 | */ |
94 | public static void writeDSEIndividualsToFile(Collection<DSEIndividual> individuals, String filename, int iteration, boolean asEMF, |
95 | boolean asCVS, List<Exception> exceptionList){ |
96 | |
97 | if (individuals.size() > 0){ |
98 | if (asCVS){ |
99 | StringBuilder results = addResultsToCSVString(individuals, exceptionList); |
100 | writeToNewFile(filename, results, iteration, exceptionList, ".csv"); |
101 | } |
102 | if (asEMF){ |
103 | // save as EMF files |
104 | Candidates candidates = EMFHelper.createEMFCandidates(individuals); |
105 | EMFHelper.saveToXMIFile(candidates, getFilenameForIteration(filename, iteration, ".designdecision")); |
106 | } |
107 | } |
108 | } |
109 | |
110 | /** |
111 | * Write all individuals to new file (file current time in filename). |
112 | * @param individuals |
113 | * @param filename |
114 | * @param iteration |
115 | * @param exceptionList |
116 | * @param asEMF |
117 | */ |
118 | public static void writeIndividualsToFile(Collection<Individual> individuals, String filename, int iteration, |
119 | List<Exception> exceptionList, boolean asEMF, boolean asCSV){ |
120 | List<DSEIndividual> dseIndList = new ArrayList<DSEIndividual>(individuals.size()); |
121 | for (Individual ind : individuals) { |
122 | if (ind instanceof DSEIndividual){ |
123 | dseIndList.add((DSEIndividual)ind); |
124 | } |
125 | } |
126 | writeDSEIndividualsToFile(dseIndList, filename, iteration, asEMF, asCSV, exceptionList); |
127 | } |
128 | |
129 | |
130 | /** |
131 | * Write any string to the given file. |
132 | * @param filename |
133 | * @param content |
134 | * @param iteration Is used for the filename |
135 | * @param exceptionList |
136 | */ |
137 | public static void writeStringToFile(String filename, String content, int iteration, List<Exception> exceptionList, String fileEnding){ |
138 | writeToNewFile(filename, new StringBuilder(content), iteration, exceptionList, fileEnding); |
139 | } |
140 | |
141 | |
142 | /** |
143 | * Write individuals to Logger using WARN level. |
144 | * @param individuals |
145 | * @param collectionName |
146 | */ |
147 | public static void printOutIndividuals(Collection<DSEIndividual> individuals, |
148 | String collectionName) { |
149 | |
150 | if (individuals.iterator().hasNext()){ |
151 | |
152 | DSEIndividual firstIndividual = individuals.iterator().next(); |
153 | List<Criterion> criteriaToSave = determineCriterionsToSave(firstIndividual); |
154 | List<Criterion> criteriaWithConfidence = determineCriteriaWithConfidenceInterval(firstIndividual); |
155 | |
156 | logger.warn("------------ RESULTS " + collectionName |
157 | + " ----------------------"); |
158 | logger.warn("Printing results (number is " + individuals.size() + ")."); |
159 | |
160 | List<Exception> exceptionList = new ArrayList<Exception>(); |
161 | |
162 | int counter = 0; |
163 | |
164 | StringBuilder output = new StringBuilder(10000); |
165 | |
166 | logger.warn("------------ PRETTY CSV RESULTS " + collectionName |
167 | + " ----------------------"); |
168 | |
169 | output.append("\n"); |
170 | output = prettyPrintHeadlineCSV(individuals, output,criteriaToSave); |
171 | |
172 | // content |
173 | for (DSEIndividual ind2 : individuals) { |
174 | try { |
175 | output = prettyPrintResultLineCSV(output, ind2,criteriaToSave, criteriaWithConfidence); |
176 | } catch (Exception e){ |
177 | exceptionList.add(new Exception("Encountered corrupted result number "+counter+", skipped it", e)); |
178 | } |
179 | counter++; |
180 | } |
181 | logger.warn(output); |
182 | |
183 | if (exceptionList.size() > 0){ |
184 | logger.warn("Encountered exceptions while printing results"); |
185 | for (Exception exception : exceptionList) { |
186 | exception.printStackTrace(); |
187 | } |
188 | } |
189 | } |
190 | } |
191 | |
192 | |
193 | |
194 | public static String formatDouble(Double gene) { |
195 | return Double.toString(gene); |
196 | } |
197 | |
198 | |
199 | |
200 | public void writeIndividual(DSEIndividual i){ |
201 | |
202 | List<DSEIndividual> individualList = new ArrayList<DSEIndividual>(1); |
203 | individualList.add(i); |
204 | |
205 | StringBuilder result = new StringBuilder(100); |
206 | |
207 | if (this.criteriaToSave == null){ |
208 | //This is the first individual, so write headline, too. |
209 | //this method also determines the objectives |
210 | this.criteriaToSave = determineCriterionsToSave(i); |
211 | result = prettyPrintHeadlineCSV(individualList, result, this.criteriaToSave); |
212 | } |
213 | |
214 | if (this.criteriaWithConfidence == null){ |
215 | this.criteriaWithConfidence = determineCriteriaWithConfidenceInterval(i); |
216 | } |
217 | //Write the result line |
218 | result = prettyPrintResultLineCSV(result, i, this.criteriaToSave, this.criteriaWithConfidence); |
219 | |
220 | this.write(result); |
221 | |
222 | } |
223 | |
224 | |
225 | |
226 | public String getFilename() { |
227 | return this.myfilename; |
228 | } |
229 | |
230 | |
231 | |
232 | public void writeTacticCandidateInfo(ITactic heuristic, Collection<TacticsResultCandidate> candidatesFromCurrentHeuristic){ |
233 | //writeToLogFile(heuristic.getClass() + ";" + candidatesFromCurrentHeuristic.size() + "; candidate(s)"); |
234 | for (TacticsResultCandidate tacticsResultCandidate : candidatesFromCurrentHeuristic) { |
235 | StringBuilder builder = new StringBuilder(30); |
236 | builder.append(heuristic.getClass().getSimpleName()+";"+tacticsResultCandidate.getNumericID()+";"+tacticsResultCandidate.getParent().getNumericID()+";"+tacticsResultCandidate.getID()+";"+tacticsResultCandidate.getParent().getID()+";"); |
237 | builder = printUtilResultLine(tacticsResultCandidate.getParent(), builder); |
238 | writeToLogFile(builder.toString()+"\n"); |
239 | } |
240 | } |
241 | |
242 | public void writeTacticManagerChoice(TacticsResultCandidate c){ |
243 | writeToLogFile(c.getHeuristic().getClass().getSimpleName() +";"+c.getNumericID()+";"+c.getParent().getNumericID()+ ";"+c.getID()+";"+c.getParent().getID()+";candidate returned\n"); |
244 | } |
245 | |
246 | |
247 | /** |
248 | * Writes String entry to log file in results directory. |
249 | * @param entry |
250 | */ |
251 | public void writeToLogFile(String entry) { |
252 | try{ |
253 | StringBuilder out = new StringBuilder(50); |
254 | Calendar cal = Calendar.getInstance(); |
255 | SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); |
256 | out.append(sdf.format(cal.getTime()) + ";" + entry); |
257 | |
258 | this.write(out); |
259 | }catch (Exception e){//Catch exception if any |
260 | System.err.println("Error: " + e.getMessage()); |
261 | } |
262 | } |
263 | |
264 | |
265 | public void flush() { |
266 | try { |
267 | this.fileWriter.flush(); |
268 | } catch (IOException e) { |
269 | e.printStackTrace(); |
270 | } |
271 | |
272 | } |
273 | |
274 | |
275 | |
276 | /** |
277 | * Closes the internal file writer (as defined in FileWriter.close()). |
278 | * After closing, the ResultWriter should not be used anymore. |
279 | * Further calls of the ResultWriter will be logged to Log4J with error messages, as if |
280 | * no file writer could be initialised. |
281 | * Multiple calls of close() have no effect. |
282 | */ |
283 | public void close(){ |
284 | if (this.fileWriter != null){ |
285 | try { |
286 | this.fileWriter.flush(); |
287 | this.fileWriter.close(); |
288 | this.fileWriter = null; |
289 | } catch (IOException e) { |
290 | logger.error("Cannot close the file handle "+this.myfilename+". Your results might be lost. Cause: "+e.getMessage()); |
291 | e.printStackTrace(); |
292 | } |
293 | |
294 | } |
295 | } |
296 | |
297 | @Override |
298 | protected void finalize() throws Throwable { |
299 | this.close(); |
300 | super.finalize(); |
301 | } |
302 | |
303 | |
304 | |
305 | /** |
306 | * Writes the individuals to a StringBuffer in csv format. |
307 | * @param individuals |
308 | * @param exceptionList |
309 | * @return |
310 | */ |
311 | private static StringBuilder addResultsToCSVString(Collection<DSEIndividual> individuals, |
312 | List<Exception> exceptionList) { |
313 | StringBuilder output = new StringBuilder(10000); |
314 | |
315 | DSEIndividual firstIndividual = individuals.iterator().next(); |
316 | |
317 | List<Criterion> criteriaToSave = determineCriterionsToSave(firstIndividual); |
318 | List<Criterion> criteriaWithConfidence = determineCriteriaWithConfidenceInterval(firstIndividual); |
319 | |
320 | output = prettyPrintHeadlineCSV(individuals, output, criteriaToSave); |
321 | int counter = 0; |
322 | |
323 | // content |
324 | for (DSEIndividual ind2 : individuals) { |
325 | try { |
326 | output = prettyPrintResultLineCSV(output, ind2,criteriaToSave, criteriaWithConfidence); |
327 | } catch (Exception e){ |
328 | exceptionList.add(new Exception("Encountered corrupted result number "+counter+", skipped it", e)); |
329 | } |
330 | counter++; |
331 | } |
332 | return output; |
333 | } |
334 | |
335 | private static List<Criterion> determineCriteriaWithConfidenceInterval( |
336 | DSEIndividual firstIndividual) { |
337 | DSEObjectives objs = firstIndividual.getObjectives(); |
338 | List<Criterion> criteriaWithConfidence = new ArrayList<Criterion>(); |
339 | |
340 | if (objs != null && objs.size() > 0){ |
341 | List<String> coveredDimensions = new ArrayList<String>(); |
342 | |
343 | for (Objective objective : objs.getKeys()) { |
344 | if (objs.getConfidenceIntervalForObjective(objective) != null){ |
345 | criteriaWithConfidence.add(objective); |
346 | coveredDimensions.add(getDimensionName(objective)); |
347 | } |
348 | } |
349 | |
350 | for (Constraint constraint : objs.getConstraints().getKeys()) { |
351 | if (!coveredDimensions.contains(getDimensionName(constraint))){ |
352 | if (objs.getConfidenceIntervalForObjective(constraint) != null){ |
353 | criteriaWithConfidence.add(constraint); |
354 | } |
355 | } |
356 | } |
357 | } |
358 | |
359 | return criteriaWithConfidence; |
360 | } |
361 | |
362 | |
363 | private static void writeToNewFile(String filename, StringBuilder results, int iteration, List<Exception> exceptionList, String fileEnding) { |
364 | filename = getFilenameForIteration(filename, iteration, fileEnding); |
365 | try { |
366 | BufferedWriter w = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(filename))); |
367 | |
368 | w.write(results.toString()); |
369 | |
370 | w.flush(); |
371 | |
372 | w.close(); |
373 | } catch (FileNotFoundException e) { |
374 | exceptionList.add(e); |
375 | e.printStackTrace(); |
376 | } catch (IOException e) { |
377 | exceptionList.add(e); |
378 | e.printStackTrace(); |
379 | } |
380 | |
381 | } |
382 | |
383 | |
384 | private static String getFilenameForIteration(String basicFilename, int iteration, String fileEnding) { |
385 | return basicFilename +iteration+"_" +getTimeDateString()+fileEnding; |
386 | } |
387 | |
388 | private static StringBuilder prettyPrintResultLineCSV(StringBuilder output, DSEIndividual ind, List<Criterion> criterionsToSave, List<Criterion> criteriaWithConfidence) { |
389 | |
390 | // first objectives |
391 | DSEObjectives objs = ind.getObjectives(); |
392 | //for (Entry<Objective, Value<?>> entry : objs) { |
393 | for (Criterion o : criterionsToSave) { |
394 | double value = Double.NaN; |
395 | if (objs != null){ |
396 | output.append(formatValue(objs.getValueForCriterion(o)) + ";"); |
397 | } else { |
398 | output.append(formatDouble(Double.NaN) + ";"); |
399 | } |
400 | } |
401 | |
402 | //then confidences if available |
403 | if (ind.getPhenotype() instanceof PCMPhenotype){ |
404 | for (Criterion o : criteriaWithConfidence) { |
405 | ConfidenceInterval c = objs.getConfidenceIntervalForObjective(o); |
406 | if (c != null){ |
407 | output.append(c.getLowerBound()+";"+c.getUpperBound()+";"+c.getLevel()+";"); |
408 | } else { |
409 | output.append(Double.NaN+";"+Double.NaN+";"+Double.NaN+";"); |
410 | } |
411 | } |
412 | } |
413 | |
414 | //then genes |
415 | DesignDecisionGenotype genes = (DesignDecisionGenotype) ind.getGenotype(); |
416 | if (genes.size() == 0){ |
417 | logger.error("Encountered empty genotype, filling it with blanks"); |
418 | int problemsize = Opt4JStarter.getProblem().getDesignDecisions().size(); |
419 | for (int i = 0; i < problemsize; i++) { |
420 | output.append(";"); |
421 | } |
422 | |
423 | } else { |
424 | output.append(DSEDecoder.getGenotypeString(genes)); |
425 | } |
426 | |
427 | output = printUtilResultLine(ind,output); |
428 | |
429 | output.append(((PCMPhenotype)ind.getPhenotype()).getNumericID()+";"); |
430 | |
431 | output.append("\n"); |
432 | return output; |
433 | } |
434 | |
435 | /** |
436 | * Print one column per objective with results and per active processing resource in the resourceenvironment. |
437 | * @param ind |
438 | * @param output |
439 | * @return |
440 | */ |
441 | private static StringBuilder printUtilResultLine(DSEIndividual ind, |
442 | StringBuilder output) { |
443 | |
444 | Objectives obs = ind.getObjectives(); |
445 | if (obs instanceof DSEObjectives){ |
446 | DSEObjectives dseObj = ((DSEObjectives)obs); |
447 | for (Entry<Objective, Value<?>> o : dseObj) { |
448 | if (dseObj.hasResultDecoratorFor(o.getKey())){ |
449 | ResultDecoratorRepository results = dseObj.getResultDecoratorFor(o.getKey()); |
450 | List<UtilisationResult> utilisations = results.getUtilisationResults_ResultDecoratorRepository(); |
451 | PCMInstance pcm = Opt4JStarter.getProblem().getInitialInstance(); |
452 | List<ResourceContainer> containers = pcm.getResourceEnvironment().getResourceContainer_ResourceEnvironment(); |
453 | List<LinkingResource> links = pcm.getResourceEnvironment().getLinkingResources__ResourceEnvironment(); |
454 | if (utilisations != null){ |
455 | for (ResourceContainer resourceContainer : containers) { |
456 | for (ProcessingResourceSpecification processingResourceSpecification : resourceContainer.getActiveResourceSpecifications_ResourceContainer()) { |
457 | for (UtilisationResult utilisationResult : utilisations) { |
458 | if (utilisationResult instanceof ProcessingResourceSpecificationResult){ |
459 | ProcessingResourceSpecificationResult procResResult = ((ProcessingResourceSpecificationResult)utilisationResult); |
460 | // compare container and resource type, because the proc resource may have changed (the printed candidate is not necessarily the current one on the blackboard). |
461 | if (EMFHelper.checkIdentity((ResourceContainer)procResResult.getProcessingResourceSpecification_ProcessingResourceSpecificationResult().eContainer(),resourceContainer) |
462 | && EMFHelper.checkIdentity(procResResult.getProcessingResourceSpecification_ProcessingResourceSpecificationResult().getActiveResourceType_ActiveResourceSpecification(), processingResourceSpecification.getActiveResourceType_ActiveResourceSpecification())){ |
463 | output.append(procResResult.getResourceUtilisation()); |
464 | break; |
465 | } |
466 | } |
467 | } |
468 | output.append(";"); |
469 | } |
470 | } |
471 | for (LinkingResource linkingResource : links) { |
472 | for (UtilisationResult utilisationResult : utilisations) { |
473 | if (utilisationResult instanceof LinkingResourceResults){ |
474 | LinkingResourceResults linkResult = ((LinkingResourceResults)utilisationResult); |
475 | if (EMFHelper.checkIdentity(linkResult.getLinkingResource_LinkingResourceResults(),linkingResource)){ |
476 | output.append(linkResult.getResourceUtilisation()); |
477 | break; |
478 | } |
479 | } |
480 | } |
481 | output.append(";"); |
482 | } |
483 | } else { |
484 | for (int i = 0; i < containers.size()+links.size(); i++){ |
485 | output.append(";"); |
486 | } |
487 | } |
488 | |
489 | } |
490 | } |
491 | } |
492 | |
493 | return output; |
494 | } |
495 | |
496 | /** |
497 | * If order of all Objectives first, then confidences is changed, also change GenotypeReader accordingly. |
498 | * Expects a list of objectives to create a headline entry for. |
499 | * @param individuals |
500 | * @param output |
501 | * @param criterionsToSave |
502 | * @return |
503 | */ |
504 | private static StringBuilder prettyPrintHeadlineCSV( |
505 | Collection<DSEIndividual> individuals, StringBuilder output, List<Criterion> criterionsToSave) { |
506 | if (individuals.size() > 0){ |
507 | DSEIndividual i = individuals.iterator().next(); |
508 | |
509 | output = printObjectives(criterionsToSave,output); |
510 | |
511 | output = determineAndPrintConfidenceIntervalHeadline(output, i.getObjectives(), criterionsToSave); |
512 | |
513 | output.append(Opt4JStarter.getProblem().toString()); |
514 | |
515 | output = printUtilisationHeadline(i,output); |
516 | |
517 | output.append("Candidate ID;"); |
518 | |
519 | output.append("\n"); |
520 | } |
521 | return output; |
522 | } |
523 | |
524 | /** |
525 | * Print one column per objective with results and per active processing resource in the resourceenvironment. |
526 | * @param i |
527 | * @param output |
528 | * @return |
529 | */ |
530 | private static StringBuilder printUtilisationHeadline(DSEIndividual i, |
531 | StringBuilder output) { |
532 | Objectives obs = i.getObjectives(); |
533 | if (obs instanceof DSEObjectives){ |
534 | DSEObjectives dseObj = ((DSEObjectives)obs); |
535 | for (Entry<Objective, Value<?>> o : dseObj) { |
536 | if (dseObj.hasResultDecoratorFor(o.getKey())){ |
537 | |
538 | PCMInstance pcm = Opt4JStarter.getProblem().getInitialInstance(); |
539 | List<ResourceContainer> containers = pcm.getResourceEnvironment().getResourceContainer_ResourceEnvironment(); |
540 | for (ResourceContainer resourceContainer : containers) { |
541 | List<ProcessingResourceSpecification> procResource = resourceContainer.getActiveResourceSpecifications_ResourceContainer(); |
542 | for (ProcessingResourceSpecification processingResourceSpecification : procResource) { |
543 | ProcessingResourceType procType = processingResourceSpecification.getActiveResourceType_ActiveResourceSpecification(); |
544 | output.append("Util of "+resourceContainer.getEntityName()+" "+procType.getEntityName()+";"); |
545 | } |
546 | |
547 | } |
548 | |
549 | List<LinkingResource> links = pcm.getResourceEnvironment().getLinkingResources__ResourceEnvironment(); |
550 | for (LinkingResource linkingResource : links) { |
551 | output.append("Util of "+linkingResource.getEntityName()+" " |
552 | +linkingResource.getCommunicationLinkResourceSpecifications_LinkingResource().getCommunicationLinkResourceType_CommunicationLinkResourceSpecification().getEntityName()+";"); |
553 | } |
554 | } |
555 | |
556 | } |
557 | } |
558 | return output; |
559 | } |
560 | |
561 | private static StringBuilder determineAndPrintConfidenceIntervalHeadline(StringBuilder output, DSEObjectives objs, List<Criterion> criterionsToSave) { |
562 | |
563 | for (Criterion criterion : criterionsToSave) { |
564 | ConfidenceInterval c = objs.getConfidenceIntervalForObjective(criterion); |
565 | if (c != null){ |
566 | output.append(DSEConstantsContainer.LOWER_BOUND_CONFIDENCE+"("+getDimensionName(criterion)+");" |
567 | + DSEConstantsContainer.UPPER_BOUND_CONFIDENCE+"("+getDimensionName(criterion)+");" |
568 | + DSEConstantsContainer.ALPHA+"("+getDimensionName(criterion)+");"); |
569 | } |
570 | |
571 | } |
572 | return output; |
573 | } |
574 | |
575 | |
576 | private static List<Criterion> determineCriterionsToSave(DSEIndividual i) { |
577 | DSEObjectives objs = i.getObjectives(); |
578 | |
579 | |
580 | List<Criterion> criterionsToSave = new ArrayList<Criterion>(); |
581 | if (objs != null && objs.size() != 0){ |
582 | criterionsToSave.addAll(objs.getKeys()); |
583 | |
584 | // check for each constraint whether an objective already covers its quality dimension. If not, it needs to be added to the list |
585 | Collection<Constraint> constraints = objs.getConstraints().getKeys(); |
586 | for (Constraint constraint : constraints) { |
587 | boolean hasToBeAdded = true; |
588 | for (Criterion coveredCriterion : criterionsToSave) { |
589 | if (getDimensionName(constraint).equals(getDimensionName(coveredCriterion))){ |
590 | // criterion is already covered |
591 | hasToBeAdded = false; |
592 | break; |
593 | } |
594 | } |
595 | if (hasToBeAdded){ |
596 | criterionsToSave.add(constraint); |
597 | } |
598 | } |
599 | } |
600 | return criterionsToSave; |
601 | } |
602 | |
603 | private static String formatValue(Value<?> value) { |
604 | |
605 | if (value instanceof IntegerValue){ |
606 | IntegerValue intValue = (IntegerValue)value; |
607 | return String.valueOf(intValue); |
608 | |
609 | } else { |
610 | double d = value.getDouble(); |
611 | return formatDouble(d); |
612 | } |
613 | |
614 | } |
615 | |
616 | |
617 | private static StringBuilder printObjectives(List<Criterion> criterionsToSave, StringBuilder output) { |
618 | for (Criterion entry : criterionsToSave) { |
619 | output.append(getDimensionName(entry) +";"); |
620 | } |
621 | |
622 | return output; |
623 | } |
624 | |
625 | private static String getTimeDateString(){ |
626 | Date date = new Date(); |
627 | SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd-HHmmss"); |
628 | String stringDate = simpleFormat.format(date); |
629 | stringDate = stringDate.replaceAll(":", "-").replaceAll(" ", "_"); |
630 | return stringDate; |
631 | } |
632 | |
633 | /** |
634 | * Write the string to this.fileWriter. |
635 | * @param result |
636 | */ |
637 | private void write(StringBuilder result) { |
638 | if (this.fileWriter != null){ |
639 | try { |
640 | fileWriter.write(result.toString()); |
641 | fileWriter.flush(); |
642 | } catch (IOException e) { |
643 | logger.error("Cannot write to file "+this.myfilename+" Logging the result at level INFO now. Cause: "+e.getMessage()); |
644 | logger.info(result.toString()); |
645 | } |
646 | } else { |
647 | logger.info(result.toString()); |
648 | } |
649 | } |
650 | |
651 | public static String getDimensionName(Criterion criterion){ |
652 | EvaluationAspectWithContext qmlCriterion = PCMDeclarationsReader.retranslateCriterionToEvaluationAspect(criterion); |
653 | String name = qmlCriterion.getDimension().getEntityName(); |
654 | if (criterion instanceof UsageScenarioBasedCriterion){ |
655 | name += ":"+((UsageScenarioBasedCriterion)criterion).getUsageScenario().getEntityName(); |
656 | } else if (criterion instanceof EntryLevelSystemCallCriterion){ |
657 | name += ":"+((EntryLevelSystemCallCriterion)criterion).getEntryLevelSystemCall().getEntityName(); |
658 | } |
659 | return name; |
660 | } |
661 | |
662 | |
663 | |
664 | |
665 | } |