| 1 | package de.uka.ipd.sdq.probespec.framework.calculator; |
| 2 | |
| 3 | import java.util.Vector; |
| 4 | import java.util.concurrent.CopyOnWriteArrayList; |
| 5 | |
| 6 | import javax.measure.Measure; |
| 7 | import javax.measure.quantity.Quantity; |
| 8 | |
| 9 | import de.uka.ipd.sdq.pipesandfilters.framework.MeasurementMetric; |
| 10 | import de.uka.ipd.sdq.probespec.framework.BlackboardVote; |
| 11 | import de.uka.ipd.sdq.probespec.framework.IBlackboardListener; |
| 12 | import de.uka.ipd.sdq.probespec.framework.ISampleBlackboard; |
| 13 | import de.uka.ipd.sdq.probespec.framework.ProbeSetSample; |
| 14 | import de.uka.ipd.sdq.probespec.framework.ProbeSpecContext; |
| 15 | import de.uka.ipd.sdq.probespec.framework.exceptions.CalculatorException; |
| 16 | |
| 17 | /** |
| 18 | * This class is the abstract super class for all Calculator implementations. |
| 19 | * All specific Calculators have to inherit from this class. |
| 20 | * <p> |
| 21 | * Calculators observe the {@link ISampleBlackboard} for probe set samples |
| 22 | * (Observer Pattern). As soon as a new probe set sample is published at the |
| 23 | * blackboard, the {@link #execute(ProbeSetSample)} method is invoked. The |
| 24 | * calculator have to decide, whether the probe set sample is of interest for |
| 25 | * the calculation. |
| 26 | * |
| 27 | * @author Faber |
| 28 | * |
| 29 | */ |
| 30 | public abstract class Calculator implements IBlackboardListener { |
| 31 | |
| 32 | private ProbeSpecContext probeSpecContext; |
| 33 | private Vector<MeasurementMetric> measurementMetrics; |
| 34 | |
| 35 | // copy on write enables listeners to unregister during event processing. |
| 36 | private CopyOnWriteArrayList<ICalculatorListener> listeners; |
| 37 | |
| 38 | protected Calculator(ProbeSpecContext ctx) { |
| 39 | this.probeSpecContext = ctx; |
| 40 | this.measurementMetrics = getConcreteMeasurementMetrics(); |
| 41 | this.listeners = new CopyOnWriteArrayList<ICalculatorListener>(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * This method is called to return meta data about the result tuples of the |
| 46 | * calculator. E.g. it is used initialize the pipe and filter chain. |
| 47 | * |
| 48 | * @return |
| 49 | */ |
| 50 | public Vector<MeasurementMetric> getMeasurementMetrics() { |
| 51 | return measurementMetrics; |
| 52 | } |
| 53 | |
| 54 | // /** |
| 55 | // * The update method is called by the SampleBlackboard (observable entity) |
| 56 | // * containing all ProbeSetSamples. The method casts the two objects and then |
| 57 | // * calls the execute method of the specific calculator |
| 58 | // * |
| 59 | // * TODO If a logging framework is added to this project, handle the |
| 60 | // * exception below correctly. |
| 61 | // * |
| 62 | // * @param o |
| 63 | // * The observable object (SampleBlackboard) |
| 64 | // * @param arg |
| 65 | // * The ProbeSetSample object written on the SampleBlackboard |
| 66 | // */ |
| 67 | // @Override |
| 68 | // public void update(Observable o, Object arg) { |
| 69 | // if (o instanceof SampleBlackboard && arg instanceof ProbeSetSample) { |
| 70 | // ProbeSetSample pss = (ProbeSetSample) arg; |
| 71 | // try { |
| 72 | // execute(pss); |
| 73 | // } catch (CalculatorException e) { |
| 74 | // e.printStackTrace(); |
| 75 | // } |
| 76 | // |
| 77 | // } |
| 78 | // } |
| 79 | |
| 80 | @Override |
| 81 | public BlackboardVote sampleArrived(ProbeSetSample pss) { |
| 82 | try { |
| 83 | return execute(pss); |
| 84 | } catch (CalculatorException e) { |
| 85 | e.printStackTrace(); |
| 86 | } |
| 87 | // return decideBlackboardVote(); |
| 88 | return BlackboardVote.DISCARD; |
| 89 | } |
| 90 | |
| 91 | protected ProbeSpecContext getProbeSpecContext() { |
| 92 | return probeSpecContext; |
| 93 | } |
| 94 | |
| 95 | // public abstract BlackboardVote decideBlackboardVote(); |
| 96 | |
| 97 | abstract protected BlackboardVote execute(ProbeSetSample pss) |
| 98 | throws CalculatorException; |
| 99 | |
| 100 | abstract protected Vector<MeasurementMetric> getConcreteMeasurementMetrics(); |
| 101 | |
| 102 | public void addCalculatorListener(ICalculatorListener l) { |
| 103 | listeners.add(l); |
| 104 | } |
| 105 | |
| 106 | protected void fireCalculated( |
| 107 | Vector<Measure<?, ? extends Quantity>> resultTuple) { |
| 108 | for (ICalculatorListener l : listeners) { |
| 109 | l.calculated(resultTuple); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } |