Context Simulator is a tool that allows to simulate context events stored in SQL database in real time. It is being designed to work with AWARE framework.
This tool is under development, but the developer version can be downloaded from: https://bitbucket.org/sbobek/context-simulator
This example can be found in the project files, in edu.agh.awaresim.example.Main
.
// 1. First, create DataSource (in this example MysqlDataSource) by providing // hostname, port, username, password, and name of the databese where the data for simluation are DataSource ds = new MysqlDataSource("localhost", 3306, "root", "toor", "aware"); // 2. Then, we create a new simulator, which will be using DataSource created in the previous step // We set the timestamp from which the simulation should start, and UUID of the device which data should // be used for the simulation AwareSimulator sim = new AwareSimulator(ds, 1391062684000L, UUID.fromString("92d47d9d-a600-4309-b340-b58314c2e429")); // 3. We can set the simulation rate. In the example below we set the simulation // to be 1000x faster than in real time. sim.setSpeed(1000.0); // 4. We then create a listener that will listen for the specific event (context) during a simulation. // In this example we create ApplicationForeground listener, which will be printing out the // name of the application that was launched according to the simulation data launched. sim.applicationsForeground.addListener(new AwareSimulator.Listener<ApplicationsForeground>() { public void onEvent(ApplicationsForeground event) { System.out.println("App in foreground: " + event.applicationName); } }); // 5. Now we start the simulation sim.start();
You can also set the speed of the simulation while the simulation is running (AwareSimulator#setSpeed
).
This also apply to adding new listeners (AwareSimulator.Listener<T>
) and turing on/off event handlers (AwareSimulator.EventsHandler<T>
).
toString()
method: package edu.agh.awaresim.table; import edu.agh.awaresim.AbstractEvent; import java.util.UUID; public class Accelerometer implements AbstractEvent{ public int id() { return _id; } public long timestamp() { return _timestamp; } public UUID device() { return _device; } private final int _id; private final long _timestamp; private final UUID _device; public final double doubleValues0; public final double doubleValues1; public final double doubleValues2; public final int accuracy; public final String label; public Accelerometer(int id, long timestamp, UUID device, double doubleValues0, double doubleValues1, double doubleValues2, int accuracy, String label) { this._id = id; this._timestamp = timestamp; this._device = device; this.doubleValues0 = doubleValues0; this.doubleValues1 = doubleValues1; this.doubleValues2 = doubleValues2; this.accuracy = accuracy; this.label = label; } public String toString() { return "["+id()+"] - ["+timestamp()+"] - ["+device()+"] - ["+ doubleValues0 +"] - ["+ doubleValues1 +"] - ["+ doubleValues2 +"] - ["+ accuracy +"] - "+ label +""; } }
MysqlDataSource
and create a method that will return records from your AWARe database table: package edu.agh.awaresim; ... public Source<Accelerometer> accelerometer() { return new Source<Accelerometer>() { public List<Accelerometer> apply(UUID device, int withIdGreaterThan, long withTimestampGreaterEqualTo, int number) { List<Accelerometer> rv = new ArrayList<Accelerometer>(); try{ ResultSet result = query("accelerometer", device, withIdGreaterThan, withTimestampGreaterEqualTo, number); while (result.next()) rv.add(new Accelerometer( result.getInt("_id"), result.getLong("timestamp"), UUID.fromString(result.getString("device_id")), result.getDouble("double_value_0"), result.getDouble("double_value_1"), result.getDouble("double_value_2"), result.getInt("accuracy"), result.getString("label"))); } catch (SQLException e) { e.printStackTrace(); } return rv; } }; }
main
method, add a listener that will handle the new context events:package edu.agh.awaresim.example; ... sim.accelerometer.addListener(new AwareSimulator.Listener<Accelerometer>() { public void onEvent(Accelerometer event) { System.out.println("Accelerometer: " + event); } });