Skip to content

Storing simulation data

sammacbeth edited this page Jan 14, 2013 · 5 revisions

This guide follows on from the Creating a basic simulation guide and will demonstrate how to do the following:

  • Define a database for your simulation to use when it is run.
  • Store simulation data during its execution.

Using a database

Presage2 using a basic API for storing simulation data. There are currently implementations for PostgreSQL, MySQL, MongoDB and hibernate. We recommend using the PostgreSQL driver if possible as it combines the power of a relational DBMS with key/value stores which match the data we store in Presage2 simulations. This guide will assume you are using PostgreSQL.

Install and start PostgreSQL

The PostgreSQL driver requires version 9.1 or greater, with the hstore extension enabled for the database you're using. To install PostgreSQL follow the instructions under the 'PostgreSQL' heading in the storage documentation.

Defining the database in Presage2

Firstly we need to load the Presage2 SQL drivers. To do this we need to uncomment the relevant section in our pom.xml file to ensure the SQL drivers are loaded, you should uncomment the following lines:

<dependency>
	<groupId>uk.ac.imperial.presage2</groupId>
	<artifactId>presage2-sqldb</artifactId>
	<version>${presage.version}</version>
</dependency>

Database settings are read from src/main/resources/db.properties. Edit this file to contain the following lines (replacing user and password with appropriate values for your setup):

module=uk.ac.imperial.presage2.db.sql.SqlModule
url=jdbc:postgresql://localhost/presage
user=presage_user
password={password}

Once the database is defined in this file then any run of Presage2 will be stored in the database. For example using the RunSimulation configuration used in the previous guide will store information about the simulation run automatically.

Storing data in the simulation

You can store any data you wish during simulation execution provided it can be represented as a string. You can associate data with the environment or an agent and with any timestep.

Agent data

If your agents extend AbstractParticipant then the persist variable will provide a reference to the current agent’s PersistentAgent if it is available to allow data storage. We can utilise this in our MyAgent class to store a representation of our location at a timestep:

// get current simulation time
int time = SimTime.get().intValue();
// check db is available
if (this.persist != null) {
	// save our location for this timestep
	this.persist.getState(time).setProperty("location", this.myLoc.toString());
}

Plugins

Plugins are classes which are run every timestep and can read all of the simulation’s shared state. Thus they are useful for taking code like the above snippet and packaging it into modules. For example instead of using the above code in every agent we write the LocationStoragePlugin will store this information for every agent in the simulation who has location data in the shared state.

Summary

We can now store runtime data from our simulations for later processing. In the next guide we will look at how to view this data and how to manage batches of simulations.