The JAS-mine platform‎ > ‎Documentation‎ > ‎Cookbook‎ > ‎

Useful tips

This page contains sparse info that might be helpful for a kick-start when stuck. Some of the tips are a repetition of material already included in the tutorial, some others might find their way into a dedicated tutorial in the future.



Useful commands:

Random number generator:

Random r = SimulationEngine.getRnd();


Current time:

double t = SimulationEngine.getInstance().getTime();



Persistence:

The JAS-mine persistence engine saves on the dbase all the properties of an entity class that are not defined as @Transient, plus all the variables not defined as properties but implicitly defined by getters.



@Entity classes

MUST have an empty constructor. If a superclass is involved, call to the super class constructor should be like in the Extended-Schelling example:

protected ESAgent() {
     super(null);
}



PanelEntityKeys

MUST be annotated as @Id, though the PanelEntityKey object can be given any name, and it is suggested that they are not called id (key is a better name). This is because PanelEntityKey objects contain three objects:- an id field of type Long representing the agent's id, a simultion_run field storing the number of the simulation run (also of type Long) which is useful when running many simulations in sequence as each simulation run will have a unique number, and a simulation_time field (of type Double). The fields of the PanelEntityKey are thus used to uniquely identify panel entries in the database tables.

This is why to access the agent id the method getId() has to be invoked: if the PanelEntitykey is called id, this becomes id.getId(), which is confusing. If the agent id has to be accessed from other agents, for instance to perform identity checks, the following getAgentId() method should therefore be implemented:


@Entity
public class
Agent {

    @Transient
    private static
long idCounter = 1000000;

    @Id
    private
PanelEntityKey key = new PanelEntityKey(idCounter++);

    public
Long getAgentId() {
        return key.getId();
    }

}
and then used as agent.getAgentId().

With the JAS-mine persistence engine, (pointers to) objects cannot be persisted (this has to do with the fact that the PanelEntityKey is a multiple key). Therefore, the agent's PanelEntityKey should be included as an additional variable and persisted, while the (pointer to the) object should be annotated as @Transient:

@Entity
public class Application {
    @Id
    private PanelEntityKey key;
    @Transient
    private Worker worker;
    @Column(name="worker_id")
    private Long workerId;
}