1 | package de.uka.ipd.sdq.probfunction.print; |
2 | |
3 | import java.text.DecimalFormat; |
4 | import java.text.DecimalFormatSymbols; |
5 | import java.util.List; |
6 | import java.util.Locale; |
7 | |
8 | import de.uka.ipd.sdq.probfunction.BoxedPDF; |
9 | import de.uka.ipd.sdq.probfunction.ContinuousSample; |
10 | import de.uka.ipd.sdq.probfunction.ExponentialDistribution; |
11 | import de.uka.ipd.sdq.probfunction.GammaDistribution; |
12 | import de.uka.ipd.sdq.probfunction.LognormalDistribution; |
13 | import de.uka.ipd.sdq.probfunction.NormalDistribution; |
14 | import de.uka.ipd.sdq.probfunction.ProbabilityMassFunction; |
15 | import de.uka.ipd.sdq.probfunction.Sample; |
16 | import de.uka.ipd.sdq.probfunction.util.ProbfunctionSwitch; |
17 | |
18 | public class ProbFunctionPrettyPrint extends ProbfunctionSwitch<String> { |
19 | |
20 | /* |
21 | * (non-Javadoc) |
22 | * |
23 | * @see de.uka.ipd.sdq.probfunction.util.ProbfunctionSwitch#caseBoxedPDF(de.uka.ipd.sdq.probfunction.BoxedPDF) |
24 | */ |
25 | @Override |
26 | public String caseBoxedPDF(BoxedPDF object) { |
27 | String sampleString = ""; |
28 | for (ContinuousSample s : (List<ContinuousSample>) object.getSamples()) { |
29 | double value = s.getProbability(); |
30 | //double precision = 0.00001; |
31 | double precision = 0.00000001; |
32 | |
33 | value *= 1 / precision; |
34 | long temp = Math.round(value); |
35 | double prob = temp * precision; |
36 | DecimalFormat df = new DecimalFormat("0.00000000", new DecimalFormatSymbols(Locale.US)); |
37 | |
38 | sampleString += " (" + s.getValue() + "; " + df.format(prob) |
39 | + ")"; |
40 | } |
41 | |
42 | return "DoublePDF[" + sampleString + " ]"; |
43 | } |
44 | |
45 | /* |
46 | * (non-Javadoc) |
47 | * |
48 | * @see de.uka.ipd.sdq.probfunction.util.ProbfunctionSwitch#caseProbabilityMassFunction(de.uka.ipd.sdq.probfunction.ProbabilityMassFunction) |
49 | */ |
50 | @Override |
51 | public String caseProbabilityMassFunction(ProbabilityMassFunction object) { |
52 | Sample sample = (Sample)object.getSamples().get(0); |
53 | |
54 | String pmfType = detectType(sample); |
55 | String sampleString = ""; |
56 | String leftSeparator = ""; |
57 | String rightSeparator = ";"; |
58 | |
59 | if (pmfType.equals("EnumPMF")) { |
60 | leftSeparator = "\""; |
61 | rightSeparator = "\";"; |
62 | } |
63 | |
64 | for (Sample s : (List<Sample>) object.getSamples()) { |
65 | sampleString += " (" + leftSeparator + s.getValue() |
66 | + rightSeparator + s.getProbability() + ")"; |
67 | } |
68 | return pmfType + "[" + sampleString + " ]"; |
69 | } |
70 | |
71 | @Override |
72 | public String caseExponentialDistribution(ExponentialDistribution object) { |
73 | return "Exp("+object.getRate()+")"; |
74 | } |
75 | |
76 | |
77 | |
78 | @Override |
79 | public String caseGammaDistribution(GammaDistribution object) { |
80 | return "Gamma("+object.getAlpha()+", "+object.getTheta()+")"; |
81 | } |
82 | |
83 | @Override |
84 | public String caseLognormalDistribution(LognormalDistribution object) { |
85 | return "Lognorm("+object.getMu()+", "+object.getSigma()+")"; |
86 | } |
87 | |
88 | @Override |
89 | public String caseNormalDistribution(NormalDistribution object) { |
90 | return "Norm("+object.getMu()+", "+object.getSigma()+")"; |
91 | } |
92 | |
93 | private String detectType(Sample object) { |
94 | if (object.getValue() instanceof Integer) |
95 | return "IntPMF"; |
96 | if (object.getValue() instanceof Double) |
97 | return "DoublePMF"; |
98 | if (object.getValue().equals("true") || object.getValue().equals("false") || object.getValue() instanceof Boolean) |
99 | return "BoolPMF"; |
100 | if (object.getValue() instanceof String) |
101 | return "EnumPMF"; |
102 | throw new UnsupportedOperationException("Unknown PMF found!"); |
103 | } |
104 | |
105 | } |