Skip navigation.
Home

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

1. Open the eclipse tool, create a default java project, named it “SimphonyInvoke” just for example,


figure1: creating project

2. In the SimphonyInvoke project, create a new directory named it “lib”,
3. Go to the plugins directory where your Repast Simphony installed,for example: “D:\RepastS\eclipse\plugins”,
4. Copy “org.eclipse.emf.common_2.3.0.v200709252135.jar” “org.eclipse.emf.ecore.xmi_2.3.1.v200709252135.jar” “org.eclipse.emf.ecore_2.3.1.v200709252135.jar” these three jar files in “D:\RepastS\eclipse\plugins” to the lib directory in the SimphonyInvoke project (Maybe you used different version Repast S, the three jars’ name may be a little different),
5. Copy all jars in the directory: “D:\RepastS\eclipse\plugins\repast.simphony.core\lib” to the lib directory in the SimphonyInvoke project,
6. Copy all jars in the directory: “D:\RepastS\eclipse\plugins\repast.simphony.runtime\lib” to the lib directory in the SimphonyInvoke project,
7. Right click on the SimphonyInvoke project, choose “property”>> “Java Build path” >> “Libraries” >> “Add Jars”, add all the jars in the lib directory.


figure2: adding jars

8. Add the model file you created in the Repast Simphony ,for example, we create a Sample_Test model in Repast S, model description file in the sampletest.rs, so in SimphonyInvoke project create a directory named Sample_Test, and copy sampletest.rs to this directory.
9. Go to src directory, create a package named “test” for example
10. Copy the source code of your Repast Simphony model. Here we copy the sampletest package in the Sample_Test project in Repast Simphony.
11. In the test package,create two class “TestRunner.java”, “TestMain.java”,in the two java files write code to invoke the Sample_Test Repast Simphony model,the source is as below:

TestRunner.java

package test;

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 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)rs.getMasterContext(); //get the main context of the model
}

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 getAgentIterator(){ //get the collection of the agents
Iterator it=context.getObjects(SampleAgent.class).iterator();
return it;
}
public static void main(String[] args){
System.err.println(new TestRunner().getModelActionCount());
}
}

TestMain.java

package test;

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 it=runner.getAgentIterator();
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();
}
}

12. run the TestMain.java in your eclipse,you will seet the running information in console.

Latest image