1 | package de.uka.ipd.sdq.pipesandfilters.framework.recorder; |
2 | |
3 | import java.util.LinkedList; |
4 | import java.util.Vector; |
5 | |
6 | import javax.measure.Measure; |
7 | import javax.measure.quantity.Quantity; |
8 | |
9 | import de.uka.ipd.sdq.pipesandfilters.framework.MetaDataInit; |
10 | import de.uka.ipd.sdq.pipesandfilters.framework.PipeData; |
11 | |
12 | /** |
13 | * This recorder calculates the sliding mean, i.e. the average value of a |
14 | * specified number of last result tuple element whenever a new pipe data |
15 | * element is received. |
16 | * |
17 | * @author Baum |
18 | * |
19 | */ |
20 | public class SlidingMeanRecorder extends AggregationRecorder { |
21 | private LinkedList<PipeData> dataQueue = new LinkedList<PipeData>(); |
22 | private int dataQueueSize = 0; |
23 | |
24 | /** |
25 | * The constructor of SlidingMeanRecorder. |
26 | * |
27 | * @param writeStrategy |
28 | * The write strategy of the recorder. |
29 | * @param dataQueueSize |
30 | * The window size of the sliding mean value, i.e. the number of |
31 | * last incoming result tuples the mean is computed on. |
32 | */ |
33 | public SlidingMeanRecorder(IAggregationWriteStrategy writeStrategy, |
34 | int dataQueueSize) { |
35 | super(writeStrategy); |
36 | this.dataQueueSize = dataQueueSize; |
37 | } |
38 | |
39 | /** |
40 | * This method initializes the aggregation write strategy, providing it with |
41 | * all information that is necessary. |
42 | * |
43 | * @param metaData |
44 | * The meta data for the incoming result tuples. |
45 | */ |
46 | protected void initialize(MetaDataInit metaData) { |
47 | |
48 | writeStrategy.initialize(metaData); |
49 | |
50 | int aggregatedMetricIndex = metaData.getMeasuredMetrics().size() - 1; |
51 | |
52 | // Create initializing meta data for the aggregation to initialize the |
53 | // aggregation write strategy. |
54 | AggregationMetaDataInit aggregationMetaData = new AggregationMetaDataInit( |
55 | aggregatedMetricIndex); |
56 | aggregationMetaData.setAggregationFunctionName("Sliding Mean"); |
57 | aggregationMetaData |
58 | .setAggregationFunctionDescription("Computes the average value of the last element of the result tuple at every processData step."); |
59 | aggregationMetaData.setValid(false); |
60 | |
61 | ((IAggregationWriteStrategy) writeStrategy) |
62 | .initializeAggregatedMeasurements(aggregationMetaData); |
63 | } |
64 | |
65 | /** |
66 | * This method calculates the sliding mean for each incoming data element |
67 | * with the specified window size. |
68 | * |
69 | * @param data |
70 | * The data to be processed. |
71 | */ |
72 | protected void processData(PipeData data) { |
73 | // add element to data queue |
74 | dataQueue.addLast(data); |
75 | if (dataQueue.size() > dataQueueSize) { |
76 | dataQueue.remove(); |
77 | } |
78 | |
79 | // Aggregation is always performed on the last result tuple element |
80 | Measure<?, ? extends Quantity> measure = data.getTupleElement(data |
81 | .getTupleSize() - 1); |
82 | |
83 | // Return value |
84 | Measure<?, ? extends Quantity> resultMeasure = Measure.valueOf(0, |
85 | measure.getUnit()); |
86 | |
87 | if (measure.getValue() instanceof Long) { |
88 | double value = 0; |
89 | for (PipeData p : dataQueue) { |
90 | value += (Long) p.getTupleElement(data.getTupleSize() - 1) |
91 | .getValue(); |
92 | } |
93 | resultMeasure = Measure.valueOf(value / dataQueue.size(), measure |
94 | .getUnit()); |
95 | } else if (measure.getValue() instanceof Integer) { |
96 | double value = 0; |
97 | for (PipeData p : dataQueue) { |
98 | value += (Integer) p.getTupleElement(data.getTupleSize() - 1) |
99 | .getValue(); |
100 | } |
101 | resultMeasure = Measure.valueOf(value / dataQueue.size(), measure |
102 | .getUnit()); |
103 | } else if (measure.getValue() instanceof Double) { |
104 | double value = 0; |
105 | for (PipeData p : dataQueue) { |
106 | value += (Double) p.getTupleElement(data.getTupleSize() - 1) |
107 | .getValue(); |
108 | } |
109 | resultMeasure = Measure.valueOf(value / dataQueue.size(), measure |
110 | .getUnit()); |
111 | } else if (measure.getValue() instanceof Float) { |
112 | double value = 0; |
113 | for (PipeData p : dataQueue) { |
114 | value += (Float) p.getTupleElement(data.getTupleSize() - 1) |
115 | .getValue(); |
116 | } |
117 | resultMeasure = Measure.valueOf(value / dataQueue.size(), measure |
118 | .getUnit()); |
119 | } |
120 | |
121 | Vector<Measure<?, ? extends Quantity>> aggregatedTuple = new Vector<Measure<?, ? extends Quantity>>(); |
122 | aggregatedTuple.add(resultMeasure); |
123 | PipeData aggregatedData = new PipeData(aggregatedTuple); |
124 | |
125 | writeStrategy.writeData(aggregatedData); |
126 | } |
127 | |
128 | /** |
129 | * This method tells the write Strategy's flush method. |
130 | */ |
131 | protected void flush() { |
132 | writeStrategy.flush(); |
133 | } |
134 | } |