1 | package de.uka.ipd.sdq.statistics.test; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.Iterator; |
6 | import java.util.List; |
7 | import java.util.Random; |
8 | |
9 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
10 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
11 | import de.uka.ipd.sdq.sensorframework.entities.Measurement; |
12 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
13 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
14 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
15 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
16 | import de.uka.ipd.sdq.sensorframework.entities.dao.IExperimentDAO; |
17 | |
18 | public class TestUtils { |
19 | |
20 | /** |
21 | * |
22 | * @param numberOfSamples |
23 | * @param desiredLag |
24 | * set lag > 1 in order to obtain a sequence of dependent values. |
25 | * @return |
26 | */ |
27 | public static List<Double> createSampleSequence(int numberOfSamples, |
28 | int desiredLag) { |
29 | Random random = new Random(); |
30 | List<Double> samples = new ArrayList<Double>(); |
31 | for (int i = 0; i < numberOfSamples; i += desiredLag) { |
32 | double randDbl = random.nextDouble(); |
33 | for (int j = 0; j < desiredLag && i + j < numberOfSamples; j++) { |
34 | samples.add(randDbl + (j * randDbl)); |
35 | } |
36 | } |
37 | |
38 | return samples; |
39 | } |
40 | |
41 | public static Collection<Measurement> loadMeasurements(IDAOFactory factory, |
42 | String experimentName, int runId, int sensorId) { |
43 | |
44 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
45 | |
46 | Collection<Experiment> experiment = experimentDAO.findByExperimentName(experimentName); |
47 | Iterator iterator = experiment.iterator(); |
48 | if (iterator.hasNext()) { |
49 | Experiment experiment2 = (Experiment) iterator.next(); |
50 | return loadMeasurementsFromExperiment(experiment2, runId, sensorId); |
51 | } else { |
52 | throw new RuntimeException("No Experiment with name "+experimentName+" available."); |
53 | } |
54 | |
55 | |
56 | |
57 | } |
58 | |
59 | private static Collection<Measurement> loadMeasurementsFromExperiment( |
60 | Experiment experiment, int runId, int sensorId) { |
61 | // Obtain (time span) sensor with specified id |
62 | TimeSpanSensor sensor = null; |
63 | for (Sensor s : experiment.getSensors()) { |
64 | if (s.getSensorID() == sensorId) { |
65 | if (s instanceof TimeSpanSensor) { |
66 | sensor = (TimeSpanSensor)s; |
67 | break; |
68 | } |
69 | } |
70 | } |
71 | |
72 | // Obtain experiment run with specified id |
73 | ExperimentRun run = null; |
74 | for (ExperimentRun r : experiment.getExperimentRuns()) { |
75 | if (r.getExperimentRunID() == runId) { |
76 | run = r; |
77 | break; |
78 | } |
79 | } |
80 | |
81 | SensorAndMeasurements sam = run.getMeasurementsOfSensor(sensor); |
82 | return sam.getMeasurements(); |
83 | } |
84 | |
85 | public static Collection<Measurement> loadMeasurements(IDAOFactory factory, |
86 | int experimentId, int runId, int sensorId) { |
87 | IExperimentDAO experimentDAO = factory.createExperimentDAO(); |
88 | |
89 | // Obtain experiment with specified id |
90 | Experiment experiment = null; |
91 | for (Experiment e : experimentDAO.getExperiments()) { |
92 | System.out.println(e.getExperimentName()); |
93 | if (e.getExperimentID() == experimentId) { |
94 | experiment = e; |
95 | break; |
96 | } |
97 | } |
98 | return loadMeasurementsFromExperiment(experiment, runId, sensorId); |
99 | |
100 | |
101 | } |
102 | |
103 | } |