Invoking Repast Simphony Model in a Java Application
1. Introduction
The simulation model created in the Repast Simphony just can be built as an jar file. You should install it, and start it from a bat file. We can’t directly control the step running of the model, and can’t get the property information of the model directly. Here we introduce how to create a java application, and in this application invoke the Repast Simphony Model, control its step running, get properties and set properties.
2. Problems in invoking Repast S model in another Java Application
In the document of the new released Repast S, it gives an example about invoke simphony model in another java application. But in such example, we just can control the step run globally, can’t get the reference of agents. By reading the source code about classes this part used, The RunState class supply an interface to get the reference of Master Context that contains all agents or other contexts. So you can get the references of agents by invoking this interface, control agents’ step running.
3. Create Java Application in Eclipse
figure1: creating project
figure2: adding jars
TestRunner.java
import java.io.*;
import java.util.Iterator;
import repast.simphony.batch.BatchScenarioLoader;
import repast.simphony.context.Context;
import repast.simphony.engine.controller.Controller;
import repast.simphony.engine.controller.DefaultController;
import repast.simphony.engine.environment.AbstractRunner;
import repast.simphony.engine.environment.ControllerRegistry;
import repast.simphony.engine.environment.DefaultRunEnvironmentBuilder;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.engine.environment.RunEnvironmentBuilder;
import repast.simphony.engine.environment.RunState;
import repast.simphony.engine.schedule.ISchedule;
import repast.simphony.engine.schedule.Schedule;
import repast.simphony.parameter.SweeperProducer;
import simphony.util.messages.MessageCenter;
import sampletest.SampleAgent;
public class TestRunner extends AbstractRunner {
private static MessageCenter msgCenter = MessageCenter.getMessageCenter(TestRunner.class);
private RunEnvironmentBuilder runEnvironmentBuilder;
protected Controller controller;
protected boolean pause = false;
protected Object monitor = new Object();
protected SweeperProducer producer;
private ISchedule schedule;
private Context
public TestRunner() {
runEnvironmentBuilder = new DefaultRunEnvironmentBuilder(this, true);
controller = new DefaultController(runEnvironmentBuilder);
controller.setScheduleRunner(this);
}
public void load(File scenarioDir) throws Exception{
if (scenarioDir.exists()) {
BatchScenarioLoader loader = new BatchScenarioLoader(scenarioDir);
ControllerRegistry registry = loader.load(runEnvironmentBuilder);
controller.setControllerRegistry(registry);
} else {
msgCenter.error("Scenario not found", new IllegalArgumentException(
"Invalid scenario " + scenarioDir.getAbsolutePath()));
return;
}
controller.batchInitialize();
controller.runParameterSetters(null);
}
public void runInitialize(){
controller.runInitialize(null);
RunState rs=RunState.getInstance();
schedule = rs.getScheduleRegistry().getModelSchedule(); //get the schedule
context = (Context
}
public void cleanUpRun(){
controller.runCleanup();
}
public void cleanUpBatch(){
controller.batchCleanup();
}
// returns the tick count of the next scheduled item
public double getNextScheduledTime(){
return ((Schedule)RunEnvironment.getInstance().getCurrentSchedule()).peekNextAction().getNextTime();
}
// returns the number of model actions on the schedule
public int getModelActionCount(){
return schedule.getModelActionCount();
}
// returns the number of non-model actions on the schedule
public int getActionCount(){
return schedule.getActionCount();
}
// Step the schedule
public void step(){
schedule.execute();
}
// stop the schedule
public void stop(){
if ( schedule != null )
schedule.executeEndActions();
}
public void setFinishing(boolean fin){
schedule.setFinishing(fin);
}
public void execute(RunState toExecuteOn) {
// required AbstractRunner stub. We will control the
// schedule directly.
}
public Iterator
Iterator
return it;
}
public static void main(String[] args){
System.err.println(new TestRunner().getModelActionCount());
}
}
TestMain.java
import java.io.File;
import java.io.File;
import java.util.Iterator;
import sampletest.SampleAgent;
public class TestMain {
private TestRunner runner;
public TestMain(){
runner=new TestRunner();
}
public void runStep(){
File file = new File("Sample_Test\\sampletest.rs"); // the scenario dir
try {
runner.load(file); // load the repast scenario
} catch (Exception e) {
e.printStackTrace();
}
runner.runInitialize(); // initialize the run
// RunEnvironment.getInstance().endAt(endTime);
int i=10;
while (i > 0){ // loop until last action is left
if (runner.getModelActionCount() == 0) {
runner.setFinishing(true);
}
System.err.print(runner.getActionCount());
Iterator
while(it.hasNext()){
SampleAgent sa=(SampleAgent)it.next();
sa.doAction("add");
}
runner.step(); // execute all scheduled actions at next tick
i--;
}
runner.stop(); // execute any actions scheduled at run end
runner.cleanUpRun();
//}
runner.cleanUpBatch(); // run a
}
public static void main(String[] args){
new TestMain().runStep();
}
}

Recent comments
57 min 19 sec ago
57 min 31 sec ago
1 hour 2 min ago
1 hour 3 min ago
1 hour 3 min ago
2 hours 7 min ago
2 hours 7 min ago
2 hours 7 min ago
2 hours 29 min ago
2 hours 29 min ago