Skip navigation.
Home

Flex interacting with a Repast simulation model

1. Introduction

This article mainly introduces the steps on how to interact with a REPAST Simphony model using Flex, 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 Flex, you also need Java as a bridge. Here we use a webservice as this bridge. It's similar to Flex with REPAST.

2. Steps

2.1 Pre-requisite

  1. Create a web project
  2. Copy blazeDS related jars to the web project's lib folder, copy blazeDS/flex folder to WEB-INF folder.
  3. Configure the java called by Flex in flex/remoting-config.xml file, e.g.:

    <?xml version="1.0" encoding="UTF-8"?>
    <service id="remoting-service" 
        class="flex.messaging.services.RemotingService">
        <adapters>
            <adapter-definition id="java-object" 
    		class="flex.messaging.services.remoting.adapters.
    				JavaAdapter" 
    		default="true"/>
        </adapters>
        <default-channels>
            <channel ref="my-amf"/>
        </default-channels>
        <destination id="gameService">
           <properties>
                <source>com.decisci.RepastRunnerClient</source>
            </properties>
        </destination>
    </service>
    

2.2 Java class codes:

public  class RepastRunnerClient {
    ............
    public RepastRunnerClient() {
    	
    }
  
    public int init(){
    	return port.init();
    }

    public MoveResponse playerMovePiece(String pieceIndex){
    	....
        return moveResponse;
    }
    public int stopGame(){
    	.....
    }
}

Flex invokes Java class:

private function remoteResultHandler(event:ResultEvent):void
{
	var moveResult = event.result;
	if(moveResult.gameState == 1){
	this.gameOver = true;
	if(moveResult.winner == 'computer'){	
		piecesCollection.getItemAt(moveResult.responsePieceIndex).
			imgPath = this.computerPlayer;
		piecesCollection.getItemAt(moveResult.responsePieceIndex).
			state = 1;
		Alert.show("Game Over!");
	} else{
		Alert.show("Congratulations!");
	}
	markWinnerPieces(moveResult);
      } else if(moveResult.gameState == 0){
		piecesCollection.getItemAt(moveResult.responsePieceIndex).
			imgPath = this.computerPlayer;
		piecesCollection.getItemAt(moveResult.responsePieceIndex).
			state = 1;
      } else {
	Alert.show("Draw! keep fighting!");
	this.gameOver = true;
      }
}
private function remoteFaultHandler(event:FaultEvent):void
{
	Alert.show(event.fault.toString(), "error");
}



For the part:

<mx:RemoteObject id="remoteGameService" destination="gameService" 
		source="com.decisci.RepastRunnerClient"
        result="remoteResultHandler(event)" 
		fault="remoteFaultHandler(event)"/>

The value of destination in the code above must be the same as that in remoting-cinfig.xml file.

For Flex to call Java method, we use:

remoteGameService.playerMovePiece(String(currentClickedPieceIndex));

3. Example

Here is a simple example of how to interact with a REPAST model.

The diagrams above give us the basic deployment 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 Flex frontend component interact with the REPAST server using Webservice protocol, and the interaction between them is shown as the diagrams above. Here are some screen shots of the UI.

AttachmentSize
TicTacToeFlexDeployment.png5.62 KB
TicTacToeFlexInteraction.png6.42 KB
FlexScreenShot001.jpg5.71 KB
FlexScreenShot002.jpg9.56 KB
FlexScreenShot003.jpg13.19 KB

Latest image