1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.reliability.solver.sensitivity; |
5 | |
6 | import org.apache.log4j.Logger; |
7 | |
8 | import de.uka.ipd.sdq.pcm.reliability.FailureType; |
9 | import de.uka.ipd.sdq.pcm.reliability.HardwareInducedFailureType; |
10 | import de.uka.ipd.sdq.pcm.reliability.NetworkInducedFailureType; |
11 | import de.uka.ipd.sdq.pcm.reliability.SoftwareInducedFailureType; |
12 | import de.uka.ipd.sdq.reliability.core.MarkovFailureType; |
13 | import de.uka.ipd.sdq.reliability.core.MarkovHardwareInducedFailureType; |
14 | import de.uka.ipd.sdq.reliability.core.MarkovNetworkInducedFailureType; |
15 | import de.uka.ipd.sdq.reliability.core.MarkovSoftwareInducedFailureType; |
16 | import de.uka.ipd.sdq.reliability.solver.pcm2markov.MarkovTransformationResult; |
17 | import de.uka.ipd.sdq.sensitivity.DoubleOffsetSequence; |
18 | import de.uka.ipd.sdq.sensitivity.DoubleParameterRange; |
19 | import de.uka.ipd.sdq.sensitivity.DoubleParameterSequence; |
20 | import de.uka.ipd.sdq.sensitivity.DoubleParameterVariation; |
21 | import de.uka.ipd.sdq.sensitivity.FailureDimension; |
22 | import de.uka.ipd.sdq.sensitivity.FailureDimensionResultSpecification; |
23 | import de.uka.ipd.sdq.sensitivity.FailureTypeResultSpecification; |
24 | import de.uka.ipd.sdq.sensitivity.SensitivityParameterVariation; |
25 | import de.uka.ipd.sdq.sensitivity.SensitivityResultSpecification; |
26 | import de.uka.ipd.sdq.sensitivity.StringParameterSequence; |
27 | |
28 | /** |
29 | * @author brosch |
30 | * |
31 | */ |
32 | public class SensitivityCalculator { |
33 | |
34 | /** |
35 | * Log4J logging support. |
36 | */ |
37 | private static Logger logger = Logger.getLogger(SensitivityCalculator.class |
38 | .getName()); |
39 | |
40 | /** |
41 | * Calculates the current double value from a given parameter variation and |
42 | * step number. |
43 | * |
44 | * @param variation |
45 | * the parameter variation |
46 | * @param stepNumber |
47 | * the step number |
48 | * @param baseValue |
49 | * the original value (for offset calculations) |
50 | * @return the current value |
51 | */ |
52 | public double calculateCurrentDoubleValue( |
53 | final DoubleParameterVariation variation, final int stepNumber, |
54 | final double baseValue) { |
55 | if (variation instanceof DoubleParameterSequence) { |
56 | DoubleParameterSequence sequence = (DoubleParameterSequence) variation; |
57 | return sequence.getDoubleValues().get(stepNumber - 1); |
58 | } else if (variation instanceof DoubleOffsetSequence) { |
59 | DoubleOffsetSequence sequence = (DoubleOffsetSequence) variation; |
60 | switch (sequence.getDoubleOffsetType__DoubleOffsetSequence()) { |
61 | case ADD: |
62 | return baseValue |
63 | + sequence.getOffsetValues().get(stepNumber - 1); |
64 | case SUBTRACT: |
65 | return baseValue |
66 | - sequence.getOffsetValues().get(stepNumber - 1); |
67 | case MULTIPLY: |
68 | return baseValue |
69 | * sequence.getOffsetValues().get(stepNumber - 1); |
70 | case DIVIDE: |
71 | return baseValue |
72 | / sequence.getOffsetValues().get(stepNumber - 1); |
73 | default: |
74 | logger.error("Double offset type \"" |
75 | + sequence.getDoubleOffsetType__DoubleOffsetSequence() |
76 | .getName() + "\" not yet supported."); |
77 | return 0.0; |
78 | } |
79 | } else if (variation instanceof DoubleParameterRange) { |
80 | DoubleParameterRange range = (DoubleParameterRange) variation; |
81 | if (range.isConsiderStepSize()) { |
82 | return range.getFirstValue() + range.getStepSize() |
83 | * (stepNumber - 1); |
84 | } else { |
85 | return range.getFirstValue() |
86 | + ((range.getLastValue() - range.getFirstValue()) / (range |
87 | .getStepCount() - 1)) * (stepNumber - 1); |
88 | } |
89 | } else { |
90 | logger.error("Parameter variation type \"" |
91 | + variation.eClass().toString() + "\" not yet supported."); |
92 | return 0.0; |
93 | } |
94 | } |
95 | |
96 | /** |
97 | * Calculates the current string value from a given parameter variation and |
98 | * step number. |
99 | * |
100 | * @param sequence |
101 | * the parameter variation |
102 | * @param stepNumber |
103 | * the step number |
104 | * @return the current value |
105 | */ |
106 | public String calculateCurrentStringValue( |
107 | final StringParameterSequence sequence, final int stepNumber) { |
108 | return sequence.getStringValues().get(stepNumber - 1); |
109 | } |
110 | |
111 | /** |
112 | * Calculates the total failure potential associated to a given sensitivity |
113 | * result specification. |
114 | * |
115 | * @param markovResult |
116 | * the Markov transformation result |
117 | * @param specification |
118 | * the result specification |
119 | * @return the failure potential |
120 | */ |
121 | private double calculateFailurePotential( |
122 | MarkovTransformationResult markovResult, |
123 | FailureDimensionResultSpecification specification) { |
124 | double result = 0.0; |
125 | for (MarkovFailureType failureType : markovResult |
126 | .getCumulatedFailureTypeProbabilities().keySet()) { |
127 | if (isMatch( |
128 | specification |
129 | .getFailureDimension__FailureDimensionResultSpecification(), |
130 | failureType)) { |
131 | result += markovResult.getCumulatedFailureTypeProbabilities() |
132 | .get(failureType); |
133 | } |
134 | } |
135 | return result; |
136 | } |
137 | |
138 | /** |
139 | * Calculates the total failure potential associated to a given sensitivity |
140 | * result specification. |
141 | * |
142 | * @param markovResult |
143 | * the Markov transformation result |
144 | * @param specification |
145 | * the result specification |
146 | * @return the failure potential |
147 | */ |
148 | private double calculateFailurePotential( |
149 | MarkovTransformationResult markovResult, |
150 | FailureTypeResultSpecification specification) { |
151 | double result = 0.0; |
152 | for (MarkovFailureType failureType : markovResult |
153 | .getCumulatedFailureTypeProbabilities().keySet()) { |
154 | for (FailureType resultType : specification |
155 | .getFailureTypes__FailureTypeResultSpecification()) { |
156 | if (isMatch(failureType, resultType)) { |
157 | result += markovResult |
158 | .getCumulatedFailureTypeProbabilities().get( |
159 | failureType); |
160 | break; |
161 | } |
162 | } |
163 | } |
164 | return result; |
165 | } |
166 | |
167 | /** |
168 | * Calculates the total failure potential associated to a given sensitivity |
169 | * result specification. |
170 | * |
171 | * @param result |
172 | * the Markov transformation result |
173 | * @param specification |
174 | * the result specification |
175 | * @return the failure potential |
176 | */ |
177 | public double calculateFailurePotential( |
178 | final MarkovTransformationResult result, |
179 | final SensitivityResultSpecification specification) { |
180 | if (specification instanceof FailureTypeResultSpecification) { |
181 | return calculateFailurePotential(result, |
182 | (FailureTypeResultSpecification) specification); |
183 | } else if (specification instanceof FailureDimensionResultSpecification) { |
184 | return calculateFailurePotential(result, |
185 | (FailureDimensionResultSpecification) specification); |
186 | } else { |
187 | logger.error("Result specification type \"" |
188 | + specification.eClass().toString() |
189 | + "\" not yet supported."); |
190 | return 0.0; |
191 | } |
192 | } |
193 | |
194 | /** |
195 | * Calculates the number of steps to perform for a given parameter |
196 | * variation. |
197 | * |
198 | * @param variation |
199 | * the parameter variation |
200 | * @return the number of steps to perform |
201 | */ |
202 | public int calculateNumberOfSteps( |
203 | final SensitivityParameterVariation variation) { |
204 | if (variation instanceof DoubleParameterRange) { |
205 | if (((DoubleParameterRange) variation).isConsiderStepSize()) { |
206 | DoubleParameterRange range = (DoubleParameterRange) variation; |
207 | return (int) Math.floor(Math.abs(range.getLastValue() |
208 | - range.getFirstValue()) |
209 | / Math.abs(range.getStepSize())) + 1; |
210 | } else { |
211 | return ((DoubleParameterRange) variation).getStepCount(); |
212 | } |
213 | } else if (variation instanceof DoubleParameterSequence) { |
214 | return ((DoubleParameterSequence) variation).getDoubleValues() |
215 | .size(); |
216 | } else if (variation instanceof DoubleOffsetSequence) { |
217 | return ((DoubleOffsetSequence) variation).getOffsetValues().size(); |
218 | } else if (variation instanceof StringParameterSequence) { |
219 | return ((StringParameterSequence) variation).getStringValues() |
220 | .size(); |
221 | } else { |
222 | logger.error("Parameter variation type \"" |
223 | + variation.eClass().toString() + "\" not yet supported."); |
224 | return 0; |
225 | } |
226 | } |
227 | |
228 | /** |
229 | * Retrieves the current log entry for a certain sensitivity step. |
230 | * |
231 | * @param variation |
232 | * the parameter variation |
233 | * @param stepNumber |
234 | * the step number |
235 | * @return the current log entry |
236 | */ |
237 | public String getCurrentLogEntry( |
238 | final SensitivityParameterVariation variation, final int stepNumber) { |
239 | if (variation instanceof DoubleOffsetSequence) { |
240 | DoubleOffsetSequence sequence = (DoubleOffsetSequence) variation; |
241 | switch (sequence.getDoubleOffsetType__DoubleOffsetSequence()) { |
242 | case ADD: |
243 | return "x + " |
244 | + sequence.getOffsetValues().get(stepNumber - 1) |
245 | .toString(); |
246 | case SUBTRACT: |
247 | return "x - " |
248 | + sequence.getOffsetValues().get(stepNumber - 1) |
249 | .toString(); |
250 | case MULTIPLY: |
251 | return "x * " |
252 | + sequence.getOffsetValues().get(stepNumber - 1) |
253 | .toString(); |
254 | case DIVIDE: |
255 | return "x / " |
256 | + sequence.getOffsetValues().get(stepNumber - 1) |
257 | .toString(); |
258 | default: |
259 | logger.error("Double offset type \"" |
260 | + sequence.getDoubleOffsetType__DoubleOffsetSequence() |
261 | .getName() + "\" not yet supported."); |
262 | return null; |
263 | } |
264 | } else if (variation instanceof DoubleParameterVariation) { |
265 | return ((Double) calculateCurrentDoubleValue( |
266 | (DoubleParameterVariation) variation, stepNumber, 0.0)) |
267 | .toString(); |
268 | } else if (variation instanceof StringParameterSequence) { |
269 | return calculateCurrentStringValue( |
270 | (StringParameterSequence) variation, stepNumber); |
271 | } else { |
272 | logger.error("Parameter variation type \"" |
273 | + variation.eClass().toString() + "\" not yet supported."); |
274 | return null; |
275 | } |
276 | } |
277 | |
278 | /** |
279 | * Compares a failure dimension and a failure type with each other. |
280 | * |
281 | * @param dimension |
282 | * the failure dimension |
283 | * @param type |
284 | * the failure type |
285 | * @return TRUE if the type is of the given dimension |
286 | */ |
287 | private boolean isMatch(FailureDimension dimension, MarkovFailureType type) { |
288 | return (dimension.equals(FailureDimension.SOFTWARE) && (type instanceof MarkovSoftwareInducedFailureType)) |
289 | || (dimension.equals(FailureDimension.HARDWARE) && (type instanceof MarkovHardwareInducedFailureType)) |
290 | || (dimension.equals(FailureDimension.NETWORK) && (type instanceof MarkovNetworkInducedFailureType)); |
291 | } |
292 | |
293 | /** |
294 | * Compares a given MarkovFailureType and PCM failure type for |
295 | * compatibility. |
296 | * |
297 | * @param failureType |
298 | * the MakovFailureType |
299 | * @param resultType |
300 | * the PCM failure type |
301 | * @return TRUE if both types are compatible |
302 | */ |
303 | private boolean isMatch(MarkovFailureType failureType, |
304 | FailureType resultType) { |
305 | if ((failureType instanceof MarkovSoftwareInducedFailureType) |
306 | && (resultType instanceof SoftwareInducedFailureType)) { |
307 | MarkovSoftwareInducedFailureType markovSWType = (MarkovSoftwareInducedFailureType) failureType; |
308 | SoftwareInducedFailureType swType = (SoftwareInducedFailureType) resultType; |
309 | return markovSWType.getSoftwareFailureId().equals(swType.getId()); |
310 | } else if ((failureType instanceof MarkovHardwareInducedFailureType) |
311 | && (resultType instanceof HardwareInducedFailureType)) { |
312 | MarkovHardwareInducedFailureType markovHWType = (MarkovHardwareInducedFailureType) failureType; |
313 | HardwareInducedFailureType hwType = (HardwareInducedFailureType) resultType; |
314 | return markovHWType |
315 | .getResourceTypeId() |
316 | .equals( |
317 | hwType |
318 | .getProcessingResourceType__HardwareInducedFailureType() |
319 | .getId()); |
320 | } else if ((failureType instanceof MarkovNetworkInducedFailureType) |
321 | && (resultType instanceof NetworkInducedFailureType)) { |
322 | MarkovNetworkInducedFailureType markovNWType = (MarkovNetworkInducedFailureType) failureType; |
323 | NetworkInducedFailureType nwType = (NetworkInducedFailureType) resultType; |
324 | return markovNWType |
325 | .getCommLinkResourceTypeId() |
326 | .equals( |
327 | nwType |
328 | .getCommunicationLinkResourceType__NetworkInducedFailureType() |
329 | .getId()); |
330 | } else { |
331 | return false; |
332 | } |
333 | } |
334 | } |