Required Downloads
In order to create agent-based models in the Eclipse programming environment with RePast you need to download and install the following three items. Make sure that you install Java before installing Eclipse. This tutorial has been tested on Repast 2.2, 3.x, and RepastJ. While the tutorial was designed for a windows OS, the proceedure is relatively the same on a MAC so this tutorial should also be useful for MAC users.
1) Java SDK 1.4.2+ or higher
2) Repast versions 2.2, 3.x, RepastJ
3) Eclipse 3.3 or higher
Preparing RePast .jars For Use in Eclipse
I am assuming that you have been able to download and install the programs listed above. Once they are installed and you are able to run Eclipse these instructions will help you to create a project in Eclipse that will contain the RePast libraries (.jars) for future referencing in models you create. I've labelled the steps merely for referencing (just in case you need to discuss where you are stuck with someone).
- In the table of contents on the left side of the programming environment in eclipse, right click, select new -> project -> java project.
- Click next.
- Type the name "Repast" in the project name textbox.
- Click next.
- Click on the "libraries" tab.
- We want to associate this project with the java libraries provided by repast, click the "add external jars..." button.
- Navigate to the repast library (lib) folder to find the repast java files, probably a location such as "C:/program files/repast-2.2/lib" and select all of the jar files in this folder.
- Click the "open" button.
- Make sure that the repast.jar file has been selected and is part of the list. If the repast.jar file is not found with all the other .jar files you just added, then you have to click the Add External JARs button under the libraries tab in the project properties window. Navigate to the repast.jar file which in repastJ (i.e. repast 3) is located in the "repast J" folder, which is above the lib folder used above.
- You should now see all the jars in the "jars and class folders on the build path:" list box in the "new java project" window. Next, click the "order and export" tab.
- Click the "select all" button.
- Deselect the "JRE system library [j2re1.4.2_05] or something similar.
- Click finish.
- Eclipse may ask if you want to associate these files with the "java perspective" which is a layout for programming, just click "yes".
- Always leave this RePast folder OPEN if you wish to access the libraries that you have stored within it.
Setting up an Eclipse Project That Points to The RePast Libraries
The instructions below show you how to create a new project and run an ABM in Eclipse that uses previously created .java files built to work with RePast. We'll use Tom Carter's "WealthModel" in the examples below so we will enter this as our project name at the appropriate step.
- Open Eclipse.
- Right click in the table of contents (the tree structure located on the left side of the program).
- Select "new" -> "project".
- Make sure the "java project" option is highlighted.
- Click next.
- Enter the project name "WealthModel".
- Click next.
- The source tab should now be selected showing you the source project folder of your model, which contains the .java files you are going to use. Select the "projects" tab.
- If the repast project is open in the table of contents then it should show up in the listbox associated with this tab. If it is not than you have to cancel this proceedure, open the "repast" project and start again.
- Assuming that the repast project is open - you should see it listed in the listbox. The items in the list box are other open projects (in your table of contents) that you have the option of associating with the project you are currently creating.
- Check the box next to the "repast" project folder in the listbox in the "new java project" window to tell the project you are creating to use the contents of the "repast" project.
- Next, click the "order and export" tab and check the box next to the "repast" project to make sure that the current project includes all of the appropriate build paths for the "repast" project that you wish to use.
- Click finish
- Now you should see your WealthModel project folder listed in the table of contents on the left side of the eclipse programming environment. Click the plus sign beside the "wealthmodel" project folder to see what is contained within.
so far you should only see the jre system library [j2re1.4.2_04] or something similar, indicating that the project you created points to the java source libraries.
Creating a Java Package in Eclipse
At the highest level of organisation is the Java project, inside a project must be one or more java packages. We now need to create a java package. Packages typically use a standard naming convension that is the url of the project in reverse. Since we are using Tom Carter's WealthModel we'll use tom.edu.csustan.astarte as our package name.
- Right click the "WealthModel" project folder.
- Select "new" -> "package".
- Enter the package name, which we defined above as tom.edu.csustan.astarte
- Click finish.
- You should now see the package added to the "wealthmodel" project.
Associating The Simulation .java Files With The Package
Next we have to move the wealth files (agent, model, space) into the package, and the .pgm file into the wealthmodel project folder.
- Go to Tom Carter's webpage, then navigate to "csss", then go to "repast models".
- Download the .java files for the wealth model and the initial spatial conditions file (.pgm) to a folder on your computer (there should be three .java files agent, space, model - and the .pgm). When you use Eclipse you don't need the .make or .build files, they are primarily used for running RePast-based models in unix-type platforms.
- You can now use windows explorer to copy and paste the three .java files you downloaded into your package. Copy and paste the .pgm into the project folder.
- In Eclipse, move your mouse pointer to the package you just created (which should show a transparent box). Right click and and select paste (the box should turn brown).
Fixing Import Errors
- If you doubleclick on any of the .java files we just moved into the package we'll see a bunch of red boxes telling us that we have errors. This is because the package name in the files is different from the package name we created. We can fix this by double clicking the "lightbulb-red error box" icon to the left of the package name text located near the top of each of the .java files (agent, space, model).
- When you double click you're presented with two possible solutions 1) move wealthmodel.java to package to wealthmodel or 2) change package declaration to tom.edu.csustan.astarte. Select the second option and this will change the package declaration in your file to the name we specified above.
- Do step 2 for all three files (agent, space, model) saving each file after each change! RePast won't register the changes until you select the save button for each file. Files that require saving have an asterix beside their names in their tab at the top of the coding window. Clicking the save button for each file should eliminate most of the red boxes (errors).
- In the wealthAgent.java file there is an import statement for all files in the WealthModel package, since we renamed this package we also have to rename the package name in this import statement. Change (import WealthModel.*;) to (import tom.edu.csustan.astarte.*;) and save the file again.
- Now all the red boxes should be gone indicating that there are no errors.
Awesome! Now we just need to set up the eclipse environment for running your model!
Telling Eclipse How to Run Your Model
- Beside the run button in the toolbar (green arrow - looks like a vcr play button) is a black arrow pointing down. Click the black arrow pointing down beside the green run arrow.
- Select the "Run..." menu item located near the bottom of the available choices.
- When the new window pops up, make sure that the "java application" item is selected in the table of contents and then click the new button located in the bottom left to create a new runnable model.
- Type in a name at the top "Wealth Model".
- The project textbox should already list "WealthModel".
- Beside the "main class" text box is a search button, click this button. Clicking the search button beside the "main class" text box tells Eclipse to search for the file in the current project you're creating that has a main class.
- The "choose main type" window should appear listing the "wealthModel" in your tom.edu.csustan.astarte package click ok.
- Click the apply button to apply your changes.
- Then click the run button.
Hopefully the program now runs!
In the future, after closing the WealthModel you should only have to click the run icon (green arrow) or click the black arrow beside the run button and select the model from the list to run it.