1 | package de.uka.ipd.sdq.probespec.framework.calculator; |
2 | |
3 | import java.util.Vector; |
4 | |
5 | import javax.measure.Measure; |
6 | import javax.measure.quantity.Quantity; |
7 | |
8 | import de.uka.ipd.sdq.pipesandfilters.framework.MeasurementMetric; |
9 | import de.uka.ipd.sdq.probespec.framework.BlackboardVote; |
10 | import de.uka.ipd.sdq.probespec.framework.ISampleBlackboard; |
11 | import de.uka.ipd.sdq.probespec.framework.ProbeSetAndRequestContext; |
12 | import de.uka.ipd.sdq.probespec.framework.ProbeSetSample; |
13 | import de.uka.ipd.sdq.probespec.framework.ProbeSpecContext; |
14 | import de.uka.ipd.sdq.probespec.framework.exceptions.CalculatorException; |
15 | import de.uka.ipd.sdq.probespec.framework.utils.ProbeSpecUtils; |
16 | |
17 | /** |
18 | * This abstract class represents a binary calculator. A binary calculator |
19 | * expects two probe sets, which are represented by their particular ID. The |
20 | * first probe set is denoted start probe set, the latter is denoted end probe |
21 | * set. |
22 | * <p> |
23 | * As soon as a sample arrives that originates from the end probe set, the |
24 | * binary calculator does its calculation by invoking |
25 | * {@link #calculate(ProbeSetSample, ProbeSetSample)}. When a sample originating |
26 | * from the start probe set arrives, the calculator does nothing. |
27 | * |
28 | * @author Philipp Merkle |
29 | * @see Calculator |
30 | * |
31 | */ |
32 | public abstract class BinaryCalculator extends Calculator { |
33 | |
34 | private ISampleBlackboard blackboard; |
35 | |
36 | private Integer startProbeSetID; |
37 | |
38 | private Integer endProbeSetID; |
39 | |
40 | public BinaryCalculator(ProbeSpecContext ctx, Integer startProbeSetID, Integer endProbeSetID) { |
41 | super(ctx); |
42 | this.blackboard = ctx.getSampleBlackboard(); |
43 | this.startProbeSetID = startProbeSetID; |
44 | this.endProbeSetID = endProbeSetID; |
45 | |
46 | blackboard.addBlackboardListener(this, startProbeSetID, endProbeSetID); |
47 | } |
48 | |
49 | abstract protected Vector<Measure<?, ? extends Quantity>> calculate( |
50 | ProbeSetSample start, ProbeSetSample end) |
51 | throws CalculatorException; |
52 | |
53 | @Override |
54 | abstract protected Vector<MeasurementMetric> getConcreteMeasurementMetrics(); |
55 | |
56 | /** |
57 | * This method is called by the |
58 | * {@link #update(java.util.Observable, Object)} method as soon as a new |
59 | * ProbeSetSample arrives at the blackboard (Observer Pattern). If |
60 | * <code>pss</code> is an end probe set sample, the method tries to get the |
61 | * corresponding start ProbeSetSample and invokes the |
62 | * {@link #calculate(ProbeSetSample, ProbeSetSample)} method. If |
63 | * <code>pss</code> is an start ProbeSetSample, this method will do nothing. |
64 | * <p> |
65 | * After the calculation the result is passed to the pipes-and-filters |
66 | * chain. |
67 | * <p> |
68 | * Here we make the assumption that the start ProbeSetSample always arrives |
69 | * before the end ProbeSetSample. Without this assumption all binary |
70 | * calculators would also have to try to get the end ProbeSetSample when the |
71 | * start ProbeSetSample arrives. Probably this would have a negative effect |
72 | * on the performance. |
73 | * |
74 | * @param pss |
75 | * the last ProbeSetSample which was added to the |
76 | * SampleBlackboard and so triggered this Calculator. |
77 | */ |
78 | @Override |
79 | protected BlackboardVote execute(ProbeSetSample pss) throws CalculatorException { |
80 | /* |
81 | * Execute only if second ProbeSetSample arrives. Here we make the |
82 | * assumption that the start ProbeSetSample always arrives before the |
83 | * end ProbeSetSample. See JavaDoc comment above. |
84 | */ |
85 | if (endProbeSetID.equals(pss.getProbeSetAndRequestContext().getProbeSetID())) { |
86 | ProbeSetSample endSetSample = pss; |
87 | ProbeSetSample startSetSample = blackboard.getSample( |
88 | new ProbeSetAndRequestContext(startProbeSetID, pss |
89 | .getProbeSetAndRequestContext().getCtxID())); |
90 | if (startSetSample != null) { |
91 | Vector<Measure<?, ? extends Quantity>> resultTuple = calculate( |
92 | startSetSample, endSetSample); |
93 | |
94 | fireCalculated(resultTuple); |
95 | } else { |
96 | throw new CalculatorException("Could not find sample for ProbeSetID " |
97 | + ProbeSpecUtils.ProbeSetIdToString(startProbeSetID, this.getProbeSpecContext()) |
98 | + " within context " + pss.getProbeSetAndRequestContext().getCtxID()); |
99 | } |
100 | return BlackboardVote.DISCARD; |
101 | } else if (startProbeSetID.equals(pss.getProbeSetAndRequestContext().getProbeSetID())) { |
102 | return BlackboardVote.RETAIN; |
103 | } else { |
104 | return BlackboardVote.DISCARD; |
105 | } |
106 | } |
107 | |
108 | } |