HeaRTDroid

HeaRTDroid is a rule-based inference engine both for Android mobile devices, and desktop solutions

User Tools

Site Tools


pub:software:contextsimulator:start

Context Simulator

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

Quickstart

Running the simluation

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>).

Adding new sensor to Context Simulator

  1. Open the project and find table package within it:
  2. Create the new class within the table package. Use the name that corresponds to the table name in your database. This is not a requirement, but helps in later code management:
  3. Implement in this calss methods and fields that correspond to the table columns in your database. In particular create a construcotr and 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 +"";
        }
    }
  4. Open AwareSimulator class and add appropriate event handlers:
    package edu.agh.awaresim;
    ...
     
    public final EventsHandler<Accelerometer> accelerometer;
    ...
     
    this.accelerometer = new EventsHandler<Accelerometer>(dataSource.accelerometer());
  5. Open DataSource interface and create new source:
    package edu.agh.awaresim;
    ...
     
    public Source<Accelerometer> accelerometer();
  6. Open 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;
                }
            };
        }
  7. In the main class in the 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);
       }
    });
  8. Run the simulation and check if the listener works:
pub/software/contextsimulator/start.txt · Last modified: 2019/04/10 06:54 (external edit)