| 1 | package de.uka.ipd.sdq.simucomframework.probes; |
| 2 | |
| 3 | import static javax.measure.unit.SI.SECOND; |
| 4 | |
| 5 | import javax.measure.Measure; |
| 6 | import javax.measure.quantity.Duration; |
| 7 | import javax.measure.unit.SI; |
| 8 | |
| 9 | import de.uka.ipd.sdq.probespec.framework.ProbeSample; |
| 10 | import de.uka.ipd.sdq.probespec.framework.ProbeType; |
| 11 | import de.uka.ipd.sdq.probespec.framework.probes.IProbeStrategy; |
| 12 | import de.uka.ipd.sdq.simulation.abstractsimengine.ISimulationControl; |
| 13 | |
| 14 | /** |
| 15 | * ProbeStrategy which is able to measure the current simulated time. The |
| 16 | * simulated time's unit is assumed to be {@link SI#SECOND}. |
| 17 | * |
| 18 | * @author Philipp Merkle |
| 19 | * |
| 20 | */ |
| 21 | public class TakeSimulatedTimeStrategy implements IProbeStrategy { |
| 22 | |
| 23 | /** |
| 24 | * @param o |
| 25 | * expects a {@link ISimulationControl} |
| 26 | */ |
| 27 | public ProbeSample<Double, Duration> takeSample(String probeId, Object... o) { |
| 28 | double simTime = 0; |
| 29 | if (o.length >= 1 && (o[0] instanceof ISimulationControl || o[0] instanceof Double)) { |
| 30 | if (o[0] instanceof ISimulationControl) { |
| 31 | ISimulationControl simControl = (ISimulationControl) o[0]; |
| 32 | simTime = simControl.getCurrentSimulationTime(); |
| 33 | } else { |
| 34 | simTime = (Double) o[0]; |
| 35 | } |
| 36 | } else { |
| 37 | throw new IllegalArgumentException( |
| 38 | "Expected an argument implementing " |
| 39 | + ISimulationControl.class.getSimpleName() |
| 40 | + " or an argument of type " + Double.class.getSimpleName() + "."); |
| 41 | } |
| 42 | |
| 43 | // Here it is assumed that the simulation time's unit is SI.SECOND |
| 44 | Measure<Double, Duration> time = Measure.valueOf(simTime, SECOND); |
| 45 | ProbeSample<Double, Duration> sample = new ProbeSample<Double, Duration>( |
| 46 | time, probeId, ProbeType.CURRENT_TIME); |
| 47 | |
| 48 | return sample; |
| 49 | } |
| 50 | |
| 51 | } |