1 | package de.uka.ipd.sdq.probespec.framework.calculator; |
2 | |
3 | import java.math.BigDecimal; |
4 | import java.util.Vector; |
5 | |
6 | import javax.measure.Measure; |
7 | import javax.measure.quantity.Duration; |
8 | import javax.measure.quantity.Quantity; |
9 | |
10 | import de.uka.ipd.sdq.probespec.framework.ProbeSample; |
11 | import de.uka.ipd.sdq.probespec.framework.ProbeSetSample; |
12 | import de.uka.ipd.sdq.probespec.framework.ProbeSpecContext; |
13 | import de.uka.ipd.sdq.probespec.framework.ProbeType; |
14 | import de.uka.ipd.sdq.probespec.framework.exceptions.CalculatorException; |
15 | import de.uka.ipd.sdq.probespec.framework.matching.IMatchRule; |
16 | import de.uka.ipd.sdq.probespec.framework.matching.ProbeTypeMatchRule; |
17 | |
18 | /** |
19 | * Calculates a time span. It expects two ProbeSets each containing at least a |
20 | * {@link ProbeType#CURRENT_TIME} probe. |
21 | * |
22 | * @author Faber, Philipp Merkle |
23 | * |
24 | */ |
25 | public abstract class TimeSpanCalculator extends BinaryCalculator { |
26 | |
27 | public TimeSpanCalculator(ProbeSpecContext ctx, Integer startProbeSetID, Integer endProbeSetID) { |
28 | super(ctx, startProbeSetID, endProbeSetID); |
29 | } |
30 | |
31 | /** |
32 | * Calculates the time span. |
33 | * |
34 | * @param start |
35 | * the ProbeSample of the start ProbeSet (ProbeType.CURRENT_TIME) |
36 | * @param end |
37 | * the ProbeSample of the end ProbeSet (ProbeType.CURRENT_TIME) |
38 | * @return a result tuple containing two components. The first component is |
39 | * the calculated time span, the second component is the end time of |
40 | * the time span. |
41 | */ |
42 | @Override |
43 | protected Vector<Measure<?, ? extends Quantity>> calculate( |
44 | ProbeSetSample start, ProbeSetSample end) |
45 | throws CalculatorException { |
46 | // Obtain start time sample |
47 | ProbeSample<Double, Duration> startTimeSample = obtainCurrentTimeProbeSample(start); |
48 | if (startTimeSample == null) { |
49 | throw new CalculatorException( |
50 | "Could not access start probe sample."); |
51 | } |
52 | |
53 | // Obtain end time sample |
54 | ProbeSample<Double, Duration> endTimeSample = obtainCurrentTimeProbeSample(end); |
55 | if (endTimeSample == null) { |
56 | throw new CalculatorException("Could not access end probe sample."); |
57 | } |
58 | |
59 | // Calculate time span |
60 | BigDecimal endTime = BigDecimal.valueOf(endTimeSample.getMeasure().doubleValue( |
61 | startTimeSample.getMeasure().getUnit())); |
62 | BigDecimal startTime = BigDecimal.valueOf(startTimeSample.getMeasure().doubleValue( |
63 | startTimeSample.getMeasure().getUnit())); |
64 | |
65 | BigDecimal responseTime = endTime.subtract(startTime); |
66 | |
67 | // Create result tuple |
68 | Measure<Double, Duration> timeSpanMeasure = Measure.valueOf( |
69 | responseTime.doubleValue(), startTimeSample.getMeasure().getUnit()); |
70 | Measure<Double, Duration> endTimeMeasure = endTimeSample.getMeasure(); |
71 | Vector<Measure<?, ? extends Quantity>> resultTuple = new Vector<Measure<?, ? extends Quantity>>(); |
72 | resultTuple.add(timeSpanMeasure); |
73 | resultTuple.add(endTimeMeasure); |
74 | |
75 | return resultTuple; |
76 | } |
77 | |
78 | @SuppressWarnings("unchecked") |
79 | private ProbeSample<Double, Duration> obtainCurrentTimeProbeSample( |
80 | ProbeSetSample probeSetSample) { |
81 | IMatchRule[] rules = new IMatchRule[1]; |
82 | rules[0] = new ProbeTypeMatchRule(ProbeType.CURRENT_TIME); |
83 | Vector<ProbeSample<?, ? extends Quantity>> result = probeSetSample |
84 | .getProbeSamples(rules); |
85 | |
86 | if (result != null && result.size() > 0) |
87 | return (ProbeSample<Double, Duration>) result.get(0); |
88 | |
89 | return null; |
90 | } |
91 | |
92 | } |