1 | package de.uka.ipd.sdq.stoex.analyser.operations; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
7 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
8 | import de.uka.ipd.sdq.probfunction.math.ISample; |
9 | import de.uka.ipd.sdq.probfunction.math.IUnit; |
10 | |
11 | /** |
12 | * Abstract class with interface for logical operations (AND, OR). |
13 | * |
14 | * @author brosch |
15 | * |
16 | */ |
17 | public abstract class LogicalOperation { |
18 | |
19 | /** |
20 | * String constant for FALSE value. |
21 | */ |
22 | private static final Object FALSE_STRING = "false"; |
23 | |
24 | /** |
25 | * String constant for TRUE value. |
26 | */ |
27 | private static final Object TRUE_STRING = "true"; |
28 | |
29 | /** |
30 | * Evaluates two objects that represent the left-hand side and the |
31 | * right-hand side of a logical expression. |
32 | * |
33 | * @param left |
34 | * left-hand side of expression |
35 | * @param right |
36 | * right-hand side of expression |
37 | * @return evaluation result |
38 | */ |
39 | public IProbabilityMassFunction evaluate(Object left, Object right) { |
40 | |
41 | // Assure proper handling of boolean values: |
42 | if (left instanceof String) { |
43 | if (((String) left).toLowerCase().equals(TRUE_STRING)) { |
44 | left = new Boolean(true); |
45 | } else if (((String) left).toLowerCase().equals(FALSE_STRING)) { |
46 | left = new Boolean(false); |
47 | } |
48 | } |
49 | if (right instanceof String) { |
50 | if (((String) right).toLowerCase().equals(TRUE_STRING)) { |
51 | right = new Boolean(true); |
52 | } else if (((String) right).toLowerCase().equals(FALSE_STRING)) { |
53 | right = new Boolean(false); |
54 | } |
55 | } |
56 | |
57 | // Propagate to the type-dependent handling: |
58 | if ((left instanceof IProbabilityMassFunction) |
59 | && (right instanceof IProbabilityMassFunction)) { |
60 | return getEvaluatedPMF((IProbabilityMassFunction) left, |
61 | (IProbabilityMassFunction) right); |
62 | } |
63 | if ((left instanceof Boolean) |
64 | && (right instanceof IProbabilityMassFunction)) { |
65 | return getEvaluatedPMF((Boolean) left, |
66 | (IProbabilityMassFunction) right); |
67 | } |
68 | if ((left instanceof IProbabilityMassFunction) |
69 | && (right instanceof Boolean)) { |
70 | return getEvaluatedPMF((IProbabilityMassFunction) left, |
71 | (Boolean) right); |
72 | } |
73 | if ((left instanceof Boolean) && (right instanceof Boolean)) { |
74 | return getEvaluatedPMF((Boolean) left, (Boolean) right); |
75 | } |
76 | |
77 | // Type not supported: |
78 | throw new UnsupportedOperationException(); |
79 | } |
80 | |
81 | /** |
82 | * Retrieves the probability of the given boolean value from the given |
83 | * Boolean PMF. |
84 | * |
85 | * @param left |
86 | * @return |
87 | */ |
88 | protected double findBooleanProbability( |
89 | final IProbabilityMassFunction left, final boolean value) { |
90 | |
91 | // Search for the sample that represents the TRUE value: |
92 | for (ISample leftSamplingPoint : left.getSamples()) { |
93 | if (!(leftSamplingPoint.getValue() instanceof String)) { |
94 | // Found non-boolean sample: |
95 | throw new UnsupportedOperationException(); |
96 | } |
97 | if (((String) leftSamplingPoint.getValue()).toUpperCase().equals( |
98 | Boolean.toString(value).toUpperCase())) { |
99 | return leftSamplingPoint.getProbability(); |
100 | } |
101 | } |
102 | |
103 | // Did not find a sample for the requested boolean value: |
104 | throw new UnsupportedOperationException(); |
105 | } |
106 | |
107 | /** |
108 | * Converts a boolean value to a boolean PMF. |
109 | * |
110 | * @param value |
111 | * @return |
112 | */ |
113 | protected IProbabilityMassFunction getBoolPMF(boolean value) { |
114 | return (value) ? getBoolPMF(1.0) : getBoolPMF(0.0); |
115 | } |
116 | |
117 | /** |
118 | * Converts a probability into a boolean PMF. |
119 | * |
120 | * @param left |
121 | * @param right |
122 | * @return |
123 | */ |
124 | protected IProbabilityMassFunction getBoolPMF(double trueProb) { |
125 | IProbabilityFunctionFactory probFac = IProbabilityFunctionFactory.eINSTANCE; |
126 | |
127 | IUnit unit = probFac.createUnit("bool"); |
128 | |
129 | List<ISample> sampleList = new ArrayList<ISample>(); |
130 | sampleList.add(probFac.createSample(TRUE_STRING, trueProb)); |
131 | sampleList.add(probFac.createSample(FALSE_STRING, 1 - trueProb)); |
132 | |
133 | IProbabilityMassFunction boolPMF = probFac |
134 | .createProbabilityMassFunction(sampleList, unit, true); |
135 | |
136 | return boolPMF; |
137 | } |
138 | |
139 | /** |
140 | * Evaluates two boolean values. |
141 | */ |
142 | protected IProbabilityMassFunction getEvaluatedPMF(Boolean left, |
143 | Boolean right) { |
144 | return getBoolPMF(new Boolean(left && right)); |
145 | } |
146 | |
147 | /** |
148 | * Evaluates a boolean value and a PMF. |
149 | */ |
150 | protected IProbabilityMassFunction getEvaluatedPMF(Boolean left, |
151 | IProbabilityMassFunction right) { |
152 | return getEvaluatedPMF(getBoolPMF(left), right); |
153 | } |
154 | |
155 | /** |
156 | * Evaluates a boolean value and a PMF. |
157 | */ |
158 | protected IProbabilityMassFunction getEvaluatedPMF( |
159 | IProbabilityMassFunction left, Boolean right) { |
160 | return getEvaluatedPMF(left, getBoolPMF(right)); |
161 | } |
162 | |
163 | protected abstract IProbabilityMassFunction getEvaluatedPMF( |
164 | IProbabilityMassFunction left, IProbabilityMassFunction right); |
165 | } |