1 | package de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies; |
2 | |
3 | import java.util.HashMap; |
4 | import java.util.Map; |
5 | |
6 | import javax.measure.Measure; |
7 | import javax.measure.quantity.Dimensionless; |
8 | import javax.measure.quantity.Duration; |
9 | import javax.measure.unit.SI; |
10 | |
11 | import de.uka.ipd.sdq.pipesandfilters.framework.MetaDataInit; |
12 | import de.uka.ipd.sdq.pipesandfilters.framework.PipeData; |
13 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.ExecutionResultMetaDataInit; |
14 | import de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.SensorHelper; |
15 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
16 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
17 | import de.uka.ipd.sdq.sensorframework.entities.State; |
18 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
19 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
20 | |
21 | /** |
22 | * Realizes a write strategy for reliability sensors. |
23 | * |
24 | * @author brosch |
25 | * |
26 | */ |
27 | public class ExecutionResultWriteDataStrategy extends AbstractWriteDataStrategy { |
28 | |
29 | /** |
30 | * Stores the dynamically created set of states. |
31 | */ |
32 | private HashMap<Integer, State> statesCache = new HashMap<Integer, State>(); |
33 | |
34 | /** |
35 | * Constructor for the strategy. |
36 | * |
37 | * @param daoFactory |
38 | * the DAO factory |
39 | * @param experiment |
40 | * the current experiment |
41 | * @param run |
42 | * the simulation run |
43 | */ |
44 | public ExecutionResultWriteDataStrategy(final IDAOFactory daoFactory, |
45 | final Experiment experiment, final ExperimentRun run) { |
46 | super(daoFactory, experiment, run); |
47 | } |
48 | |
49 | /* |
50 | * (non-Javadoc) |
51 | * |
52 | * @see |
53 | * de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies |
54 | * . |
55 | * AbstractWriteDataStrategy#initialise(de.uka.ipd.sdq.pipesandfilters.framework |
56 | * .MetaDataInit) |
57 | */ |
58 | public void initialise(final MetaDataInit metaData) { |
59 | initStatesCache(metaData); |
60 | initSensor(metaData.getMeasurementName()); |
61 | } |
62 | |
63 | /* |
64 | * (non-Javadoc) |
65 | * |
66 | * @see |
67 | * de.uka.ipd.sdq.pipesandfilters.framework.recorder.sensorframework.strategies |
68 | * .IWriteDataStrategy#writeData(de.uka.ipd.sdq.pipesandfilters.framework. |
69 | * PipeData) |
70 | */ |
71 | @SuppressWarnings("unchecked") |
72 | public void writeData(final PipeData data) { |
73 | Measure<Double, Duration> measurementTimeMeasure = (Measure<Double, Duration>) data |
74 | .getTupleElement(0); |
75 | double measurementTime = measurementTimeMeasure.doubleValue(SI.SECOND); |
76 | Measure<Integer, Dimensionless> numericStateMeasure = (Measure<Integer, Dimensionless>) data |
77 | .getTupleElement(1); |
78 | int stateId = numericStateMeasure.intValue(Dimensionless.UNIT); |
79 | run.addStateMeasurement((StateSensor) sensor, statesCache.get(stateId), |
80 | measurementTime); |
81 | } |
82 | |
83 | /** |
84 | * Finds or creates the success state for the sensor to be used by this |
85 | * strategy. |
86 | * |
87 | * @return the success state |
88 | */ |
89 | private State findSuccessState() { |
90 | |
91 | // Assume that the success state is named "Success": |
92 | for (State state : statesCache.values()) { |
93 | if (state.getStateLiteral().equals("Success")) { |
94 | return state; |
95 | } |
96 | } |
97 | |
98 | // Assume that the success state has ID = 0: |
99 | State state = statesCache.get(0); |
100 | if (state != null) { |
101 | return state; |
102 | } |
103 | |
104 | // Return an additional success state: |
105 | int newID = 0; |
106 | while(statesCache.containsKey(newID)){ |
107 | newID++; |
108 | } |
109 | State newState = SensorHelper.createOrReuseState(daoFactory, "Success"); |
110 | statesCache.put(newID, newState); |
111 | return newState; |
112 | } |
113 | |
114 | /** |
115 | * Initializes the state sensor to be used by the strategy. |
116 | * |
117 | * @param sensorId |
118 | * the id of the sensor to be used |
119 | */ |
120 | private void initSensor(String sensorId) { |
121 | sensor = SensorHelper.createOrReuseStateSensor(daoFactory, experiment, |
122 | sensorId, findSuccessState()); |
123 | for (State state : statesCache.values()) { |
124 | if (!((StateSensor) sensor).getSensorStates().contains(state)) { |
125 | ((StateSensor) sensor).addSensorState(state); |
126 | } |
127 | } |
128 | } |
129 | |
130 | /** |
131 | * Initializes the cache of execution result states. |
132 | * |
133 | * @param metaData |
134 | * the meta data for the initialization of the strategy |
135 | */ |
136 | private void initStatesCache(final MetaDataInit metaData) { |
137 | Map<Integer, String> resultTypes = ((ExecutionResultMetaDataInit) metaData) |
138 | .getExecutionResultTypes(); |
139 | for (Integer key : resultTypes.keySet()) { |
140 | State state = SensorHelper.createOrReuseState(daoFactory, |
141 | resultTypes.get(key)); |
142 | statesCache.put(key, state); |
143 | } |
144 | } |
145 | } |