1 | import java.util.Date; |
2 | |
3 | import de.uka.ipd.sdq.sensorframework.SensorFrameworkDataset; |
4 | import de.uka.ipd.sdq.sensorframework.dao.memory.MemoryDAOFactory; |
5 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
6 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
7 | import de.uka.ipd.sdq.sensorframework.entities.State; |
8 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
9 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
10 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
11 | |
12 | /** |
13 | * This class demonstrates the use of the API of the Sensorframework Plugin. |
14 | * It creates an experiment, some sensors, |
15 | * and finally adds measurements to it. |
16 | * For additional examples, see the JUnit tests of the sensor framework in their |
17 | * own plugin. |
18 | * @author Steffen Becker |
19 | */ |
20 | public class SensorFrameworkAPIDemo { |
21 | |
22 | /** |
23 | * TODO should be called as a test by the test suite, too! E.g. with try/catch, |
24 | * where the catch clause sets a variable to false befor Assert.assertEquals on booleans is called. |
25 | * @param args |
26 | */ |
27 | public static void main(String[] args) { |
28 | SensorFrameworkDataset.singleton().addDataSource(new MemoryDAOFactory(IDAOFactory.ID_NOT_SET)); |
29 | IDAOFactory f = SensorFrameworkDataset.singleton().getDataSourceByID(1); |
30 | Experiment e = f.createExperimentDAO().addExperiment("Test"); |
31 | ExperimentRun er =e.addExperimentRun("TestRun "+new Date().toString()); |
32 | TimeSpanSensor s = e.addTimeSpanSensor("ATimeSpanSensor"); |
33 | State busyState = f.createStateDAO().addState("Busy"); |
34 | State idleState = f.createStateDAO().addState("Idle"); |
35 | StateSensor stateSen = e.addStateSensor(idleState,"AStateSensor"); |
36 | stateSen.addSensorState(busyState); |
37 | stateSen.addSensorState(idleState); |
38 | boolean flag = false; |
39 | double valueSum = 0; |
40 | long start = System.nanoTime(); |
41 | for (int i=0; i<200000; i++) { |
42 | if (i % 1000 == 0) System.out.print(".");//progress bar simulation... not to be replaced by proper logging... |
43 | if (i % 100000 == 0) System.out.print("\n"); |
44 | double value = Math.random() * 5000; |
45 | er.addTimeSpanMeasurement(s, value, value);//2nd param: event time, 3rd param: timespan |
46 | er.addStateMeasurement(stateSen, flag ? busyState : idleState, valueSum);//the last parameter is used as event time :-( |
47 | flag = !flag; |
48 | valueSum += value; |
49 | } |
50 | System.out.println("\nDone creating measurements: "+(System.nanoTime()-start)/Math.pow(10, 9)+" s"); |
51 | f.finalizeAndClose(); |
52 | System.out.println("Done storing: "+(System.nanoTime()-start)/Math.pow(10, 9)+" s"); |
53 | System.out.println(er.getMeasurementsOfSensor(s).getMeasurements().size()); |
54 | System.out.println("Done "+(System.nanoTime()-start)/Math.pow(10, 9)+" s"); |
55 | } |
56 | |
57 | } |