1 | package de.uka.ipd.sdq.sensorframework.util; |
2 | |
3 | import java.util.Collection; |
4 | import java.util.Date; |
5 | import java.util.Hashtable; |
6 | import java.util.List; |
7 | |
8 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
9 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
10 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
11 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
12 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
13 | import de.uka.ipd.sdq.sensorframework.entities.dao.IExperimentDAO; |
14 | |
15 | /** |
16 | * TODO this class has no documentation at all! |
17 | * It is not used by any other class, so there is no need for it, unless it is a "hidden" test class. |
18 | * |
19 | * @author Jens Happe |
20 | * @author Steffen Becker |
21 | * |
22 | * |
23 | */ |
24 | public class ExperimentManager { |
25 | |
26 | public final static double SCALING_FACTOR = 1000000; |
27 | |
28 | private Hashtable<String, TimeSpanSensor> timeSpanSensorHash = new Hashtable<String, TimeSpanSensor>(); |
29 | |
30 | private Experiment experiment; |
31 | |
32 | private ExperimentRun run; |
33 | |
34 | private IDAOFactory factory; |
35 | |
36 | public ExperimentManager(String experimentName, IDAOFactory factory){ |
37 | this.factory = factory; |
38 | this.experiment = getExperiment(experimentName); |
39 | run = experiment.addExperimentRun("Run "+new Date().toString()); |
40 | } |
41 | |
42 | private Experiment getExperiment(String experimentName) { |
43 | IExperimentDAO expDAO = factory.createExperimentDAO(); |
44 | Collection<Experiment> experimentColl = expDAO.findByExperimentName(experimentName); |
45 | if (experimentColl.size() > 0){ |
46 | return experimentColl.iterator().next(); |
47 | } else { |
48 | return expDAO.addExperiment(experimentName); |
49 | } |
50 | } |
51 | |
52 | public void storeTimeSpan(String sensorName, long startTime, long stopTime){ |
53 | double time = (stopTime - startTime) / SCALING_FACTOR; |
54 | if (sensorName.equals("ServiceTime") && time < 0.5){ |
55 | System.out.println("Time: "+time); |
56 | System.out.println("startTime: "+startTime); |
57 | System.out.println("stopTime: "+stopTime); |
58 | } |
59 | TimeSpanSensor sensor = getTimeSpanSensor(sensorName); |
60 | run.addTimeSpanMeasurement(sensor, startTime, time); |
61 | } |
62 | |
63 | public void storeTimeSpans(String sensorName, double[] durationArray, double[] timeArray, int upperBound) { |
64 | TimeSpanSensor sensor = getTimeSpanSensor(sensorName); |
65 | for (int i=0; i<upperBound; i++){ |
66 | run.addTimeSpanMeasurement(sensor, timeArray[i], durationArray[i]); |
67 | } |
68 | } |
69 | |
70 | public void storeTimeSpans(String sensorName, List<Double> elements) { |
71 | TimeSpanSensor sensor = getTimeSpanSensor(sensorName); |
72 | int i=0; |
73 | for (Double d: elements){ |
74 | run.addTimeSpanMeasurement(sensor, i, d); |
75 | i++; |
76 | } |
77 | } |
78 | |
79 | public void close(){ |
80 | factory.finalizeAndClose(); |
81 | } |
82 | |
83 | |
84 | private TimeSpanSensor getTimeSpanSensor(String name) { |
85 | TimeSpanSensor result = timeSpanSensorHash.get(name); |
86 | if (result == null){ |
87 | for (Sensor sensor : experiment.getSensors()) { |
88 | if ( sensor.getSensorName().equals(name) ) { |
89 | result = (TimeSpanSensor) sensor; |
90 | break; |
91 | } |
92 | } |
93 | if (result == null){ |
94 | result = experiment.addTimeSpanSensor(name); |
95 | } |
96 | timeSpanSensorHash.put(name, result); |
97 | } |
98 | return result; |
99 | } |
100 | |
101 | public void finalizeAndClose() { |
102 | factory.finalizeAndClose(); |
103 | } |
104 | |
105 | } |