1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.sensorframework.dao.file.entities; |
5 | |
6 | import java.io.IOException; |
7 | import java.io.Serializable; |
8 | import java.util.ArrayList; |
9 | import java.util.Collection; |
10 | import java.util.HashMap; |
11 | |
12 | import de.uka.ipd.sdq.sensorframework.dao.file.FileDAOFactory; |
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.Measurement; |
16 | import de.uka.ipd.sdq.sensorframework.entities.ScalabilityMeasurement; |
17 | import de.uka.ipd.sdq.sensorframework.entities.ScalabilitySensor; |
18 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
19 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
20 | import de.uka.ipd.sdq.sensorframework.entities.State; |
21 | import de.uka.ipd.sdq.sensorframework.entities.StateMeasurement; |
22 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
23 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanMeasurement; |
24 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
25 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
26 | |
27 | /** |
28 | * Entity implementation for Experiment Runs. Contains BackgroundMemoryLists to quickly store the |
29 | * huge amounts of sensor data generated by simulations. |
30 | * |
31 | * @author Ihssane El-Oudghiri |
32 | * @author Steffen Becker |
33 | * |
34 | */ |
35 | public class ExperimentRunImpl extends AbstractFileEntity implements ExperimentRun, Serializable { |
36 | |
37 | private static final long serialVersionUID = 6496657460961660218L; |
38 | |
39 | /** |
40 | * Persistent ID of this entity |
41 | */ |
42 | private long experimentRunID; |
43 | |
44 | /** |
45 | * Date and Time of the experiment run |
46 | */ |
47 | private String experimentDateTime; |
48 | |
49 | /** |
50 | * Contains the measurements and event times for each sensor in this experiment run. |
51 | * Hashes sensor ID on the sensor's measurements. It is transient as it uses fast |
52 | * BackgroundMemoryLists which persist themselves |
53 | */ |
54 | private transient HashMap<Long, AbstractSensorAndMeasurements> measurementsForSensor; |
55 | |
56 | /** |
57 | * ID of the Experiment of this Experiment Run. Used for internal checks |
58 | */ |
59 | public long idOfParentExperiment; |
60 | |
61 | public ExperimentRunImpl(IDAOFactory factory) { |
62 | super(factory); |
63 | measurementsForSensor = new HashMap<Long, AbstractSensorAndMeasurements>(); |
64 | } |
65 | |
66 | /* Operation is unsupported as the file provider relies on fast SensorAndMeasurement objects |
67 | * (non-Javadoc) |
68 | * @see de.uka.ipd.sdq.sensorframework.entities.ExperimentRun#addMeasurement(de.uka.ipd.sdq.sensorframework.entities.Measurement) |
69 | */ |
70 | public void addMeasurement(Measurement value) { |
71 | throw new UnsupportedOperationException(); |
72 | } |
73 | |
74 | public ScalabilityMeasurement addScalabilityMeasurement(ScalabilitySensor p_sensor, |
75 | Double[] p_parameters, double p_result) { |
76 | AbstractSensorAndMeasurements sam = saveGetSensorAndMeasurements(p_sensor); |
77 | ((ScalabilitySensorAndMeasurement) sam).addResult(p_parameters, p_result); |
78 | |
79 | // We do not really have a DAO, hence we do not support returning an measurement entity. |
80 | // This is uncritical as our caller is also not expecting to get one |
81 | return null; |
82 | } |
83 | |
84 | public StateMeasurement addStateMeasurement(StateSensor p_sensor, |
85 | State p_sensorstate, double p_eventtime) { |
86 | if (p_sensor == null || p_sensorstate == null) |
87 | throw new IllegalArgumentException("p_sensor or p_sensorstate is null, eventtime is " + p_eventtime); |
88 | |
89 | AbstractSensorAndMeasurements sam = saveGetSensorAndMeasurements(p_sensor); |
90 | ((StateSensorAndMeasurement) sam).addState(p_eventtime, p_sensorstate); |
91 | |
92 | // We do not really have a DAO, hence we do not support returning an measurement entity. |
93 | // This is uncritical as our caller is also not expecting to get one |
94 | return null; |
95 | } |
96 | |
97 | public TimeSpanMeasurement addTimeSpanMeasurement(TimeSpanSensor p_sensor, |
98 | double p_eventtime, double p_timespan) { |
99 | AbstractSensorAndMeasurements sam = saveGetSensorAndMeasurements(p_sensor); |
100 | ((TimeSpanSensorAndMeasurement) sam).addTimeSpan(p_eventtime, p_timespan); |
101 | |
102 | // We do not really have a DAO, hence we do not support returning an measurement entity. |
103 | // This is uncritical as our caller is also not expecting to get one |
104 | return null; |
105 | } |
106 | |
107 | private AbstractSensorAndMeasurements createMeasurementStorage( |
108 | Sensor sensor) { |
109 | AbstractSensorAndMeasurements sam = null; |
110 | try { |
111 | if (sensor instanceof TimeSpanSensor) |
112 | sam = new TimeSpanSensorAndMeasurement(((FileDAOFactory)factory).getFileManager(),this,sensor); |
113 | else if (sensor instanceof StateSensor) |
114 | sam = new StateSensorAndMeasurement(((FileDAOFactory)factory).getFileManager(),this,sensor); |
115 | else if (sensor instanceof ScalabilitySensor) |
116 | sam = new ScalabilitySensorAndMeasurement(((FileDAOFactory)factory).getFileManager(),this,sensor); |
117 | else |
118 | throw new RuntimeException("Invalid sensor type found: " + |
119 | sensor.getClass()+" is not a TimeSpanSensor, a StateSensor, " + |
120 | "or a Scalability sensor - fix your implementation, please!"); |
121 | } catch(IOException e) { |
122 | throw new RuntimeException(e); |
123 | } |
124 | measurementsForSensor.put(sensor.getSensorID(), sam); |
125 | return sam; |
126 | } |
127 | |
128 | @Override |
129 | public boolean equals(Object obj) { |
130 | if (!(obj instanceof ExperimentRunImpl)) |
131 | return false; |
132 | ExperimentRunImpl er = (ExperimentRunImpl) obj; |
133 | |
134 | if (!(experimentRunID == er.getExperimentRunID() && experimentDateTime |
135 | .equals(er.getExperimentDateTime()))) |
136 | return false; |
137 | |
138 | return true; |
139 | } |
140 | |
141 | public String getExperimentDateTime() { |
142 | return experimentDateTime; |
143 | } |
144 | |
145 | public long getExperimentRunID() { |
146 | return experimentRunID; |
147 | } |
148 | |
149 | public long getID() { |
150 | return this.getExperimentRunID(); |
151 | } |
152 | |
153 | public Collection<Measurement> getMeasurements() { |
154 | ArrayList<Measurement> m = new ArrayList<Measurement>(); |
155 | |
156 | for (Sensor s : getParentExperiment().getSensors()) { |
157 | m.addAll(getMeasurementsOfSensor(s).getMeasurements()); |
158 | } |
159 | return m; |
160 | } |
161 | |
162 | public SensorAndMeasurements getMeasurementsOfSensor(Sensor sensor) { |
163 | if (!getParentExperiment().getSensors().contains(sensor)) { |
164 | throw new IllegalArgumentException("Error: Sensor given is not part of this experiment " |
165 | + sensor.getSensorName()); |
166 | } |
167 | AbstractSensorAndMeasurements sam = saveGetSensorAndMeasurements(sensor); |
168 | return new SensorAndMeasurements(sensor, sam.getMeasurements()); |
169 | } |
170 | |
171 | private Experiment getParentExperiment() { |
172 | return factory.createExperimentDAO().get(idOfParentExperiment); |
173 | } |
174 | |
175 | /** Returns the sensor and measurement object to the given sensor - creating it if neccessary |
176 | * @param sensor The sensor of the SAM |
177 | * @return The created SAM |
178 | */ |
179 | private AbstractSensorAndMeasurements saveGetSensorAndMeasurements( |
180 | Sensor sensor) { |
181 | AbstractSensorAndMeasurements sam = measurementsForSensor.get(sensor |
182 | .getSensorID()); |
183 | if (sam == null) { |
184 | sam = createMeasurementStorage(sensor); |
185 | } |
186 | return sam; |
187 | } |
188 | |
189 | public void setExperimentDateTime(String experimetDateTime) { |
190 | this.experimentDateTime = experimetDateTime; |
191 | } |
192 | |
193 | public void setExperimentRunID(long experimentRunID) { |
194 | this.experimentRunID = experimentRunID; |
195 | } |
196 | |
197 | @Override |
198 | public void setFactory(FileDAOFactory factory) { |
199 | super.setFactory(factory); |
200 | if (measurementsForSensor == null) |
201 | measurementsForSensor = new HashMap<Long, AbstractSensorAndMeasurements>(); |
202 | } |
203 | |
204 | public void setParentExperimentID(long id) { |
205 | this.idOfParentExperiment = id; |
206 | } |
207 | |
208 | |
209 | } |