JavaFX interacting with a Repast simulation model
1. Introduction
This article mainly introduces the steps on how to interact with a REPAST Simphony model using JavaFX, and gives a gaming example.
We've already dicussed that to run a simulation purely programmatically with fine control, you need to create a class that extends repast.simphony.engine.environment.AbstractRunner and modify to suit your needs. So calling REPAST from JavaFX, you also need Java as a bridge. Here we use a webservice as this bridge.
2. Steps
2.1 Initialize the chess board:
def initRequest: HttpRequest = HttpRequest {
//webservice address
location: initHttpRequestLocation;
//String, the full HttpRequest address
onInput: function(is: java.io.InputStream) {
try {
//parse the result from the computer end
var result = chessService.parse(is);
if(result == '0'){
//2 is initialize succeeded
finishInit = 2;
} else if(result == '1'){
finishInit = 0;
}
} finally {
is.close();
}
}
}
initRequest.start();
If we successfully initilize the model, we can go on to play the game. Here we want to use syncronized remote call. However, the HttpRequest protocol only supports asynchronous method. Here we use a trick to control the HttpRequest work as a syncronized method. We judge the return value, if the value equals to the specific value we want, then set the flag; else, go on this thread.
http request:
//parse the response received from server end
public function parse(input: InputStream): String {
var result;
def parser = PullParser {
input: input
onEvent: function(event: Event) {
if (event.type == PullParser.END_ELEMENT) {
if(event.qname.name == "return" and event.level == 3) {
result = event.text;
}
}
}// end function
}//end def parser
parser.parse();
return result;
}// end customized parse function
def getRequest_run: HttpRequest = HttpRequest {
location: moveHttpRequestLocation;
onStarted: function() {
gameState = 0;// wait for the computer
println("onStarted - started performing method: {getRequest_run.method}
on location: {getRequest_run.location}");
}
onInput: function(is: java.io.InputStream) {
try {
//parse the result from the computer end
var result = chessService.parse(is);
//update the chess board after the computer reponse
updateChessboard(result);
} finally {
is.close();
}
}
onException: function(ex: java.lang.Exception) {
println("onException - exception: {ex.getClass()} {ex.getMessage()}");
}
onDoneRead: function() { println("onDoneRead") }
onDone: function() {
println("run step finshed");
gameState = 1;//go on moving
}
}
2.2 Human's move
We call the runStep method to get this done.
function runstep(chessIndex: Integer){
def getRequest_run: HttpRequest = HttpRequest {
location: moveHttpRequestLocation;
onStarted: function() {
gameState = 0;// wait for the computer
}
onInput: function(is: java.io.InputStream) {
try {
//parse the result from the computer end
var result = chessService.parse(is);
//update the chess board after the computer reponse
updateChessboard(result);
} finally {
is.close();
}
}
onDone: function() {
gameState = 1;//go on moving
}
}
getRequest_run.start();
}
3. Example
Here is a simple example of how to interact with a REPAST model.


The diagrams above give us the basic components and the sequence on interaction with the REPAST model. This model simply stands for a Tic-Tac-Toe game. Human uses the frontend to play with the computer which is built by REPAST agent.
We use the JavaFX frontend component interact with the REPAST server using HttpRequest protocol, and the interaction between them is shown as the diagrams above. Here are some screen shots of the UI.



| Attachment | Size |
|---|---|
| TicTacToeJavaFXComponent.png | 4.61 KB |
| TicTacToeJavaFXInteraction.png | 6.48 KB |
| JavaFXScreenShot001.png | 13.17 KB |
| JavaFXScreenShot002.png | 15.89 KB |
| JavaFXScreenShot003.png | 32.21 KB |

Recent comments
57 min 53 sec ago
58 min 17 sec ago
1 hour 58 min ago
1 hour 58 min ago
1 hour 58 min ago
4 hours 1 min ago
4 hours 1 min ago
4 hours 1 min ago
5 hours 3 min ago
5 hours 3 min ago