1 | package de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies; |
2 | |
3 | import java.util.HashMap; |
4 | |
5 | import javax.measure.Measure; |
6 | import javax.measure.quantity.Dimensionless; |
7 | import javax.measure.quantity.Duration; |
8 | import javax.measure.unit.SI; |
9 | |
10 | import de.uka.ipd.sdq.pipesandfilters.framework.MetaDataInit; |
11 | import de.uka.ipd.sdq.pipesandfilters.framework.PipeData; |
12 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.SensorHelper; |
13 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
14 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
15 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
16 | import de.uka.ipd.sdq.sensorframework.entities.State; |
17 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
18 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
19 | |
20 | public class UtilisationWriteDataStrategy extends AbstractWriteDataStrategy { |
21 | |
22 | private HashMap<String, State> statesCache = new HashMap<String, State>(); |
23 | |
24 | private State idleState; |
25 | |
26 | private State busyState; |
27 | |
28 | public UtilisationWriteDataStrategy(IDAOFactory daoFactory, |
29 | Experiment experiment, ExperimentRun run) { |
30 | super(daoFactory, experiment, run); |
31 | } |
32 | |
33 | public void initialise(MetaDataInit metaData) { |
34 | String sensorId = metaData.getMeasurementName(); |
35 | this.idleState = SensorHelper.createOrReuseState(daoFactory, "Idle"); |
36 | this.busyState = SensorHelper.createOrReuseState(daoFactory, "Busy"); |
37 | sensor = SensorHelper.createOrReuseStateSensor(daoFactory, experiment, |
38 | sensorId, idleState); |
39 | if (!((StateSensor) sensor).getSensorStates().contains(idleState)) { |
40 | ((StateSensor) sensor).addSensorState(idleState); |
41 | } |
42 | ((StateSensor) sensor).addSensorState(busyState); |
43 | } |
44 | |
45 | @SuppressWarnings("unchecked") |
46 | @Override |
47 | public void writeData(PipeData data) { |
48 | Measure<Double, Duration> measurementTimeMeasure = (Measure<Double, Duration>) data |
49 | .getTupleElement(0); |
50 | double measurementTime = measurementTimeMeasure.doubleValue(SI.SECOND); |
51 | Measure<Integer, Dimensionless> numericStateMeasure = (Measure<Integer, Dimensionless>) data |
52 | .getTupleElement(1); |
53 | int numericState = numericStateMeasure.intValue(Dimensionless.UNIT); |
54 | State state = null; |
55 | if (numericState == 0) { |
56 | state = idleState; |
57 | } else { |
58 | String stateLiteral = "Busy " + Integer.toString(numericState) |
59 | + " Job(s)"; |
60 | if (!statesCache.containsKey(stateLiteral)) { |
61 | State newState = SensorHelper.createOrReuseState(daoFactory, |
62 | stateLiteral); |
63 | statesCache.put(stateLiteral, newState); |
64 | if (!((StateSensor) sensor).getSensorStates() |
65 | .contains(newState)) |
66 | ((StateSensor) sensor).addSensorState(newState); |
67 | } |
68 | state = statesCache.get(stateLiteral); |
69 | } |
70 | run.addStateMeasurement((StateSensor)sensor, state, measurementTime); |
71 | } |
72 | |
73 | } |