Example 1
From Agent Factory
Necessary Packages
To follow the following example, you must download the following packages:
- Agent Factory Runtime Environment (AF-RTE)
- AFAPL2 Development Kit
NOTE: As of version 1.1.0 of the AF Run Time Environment, the Actuator, Perceptor, and Module base classes have been moved to a new package: com.agentfactory.logic.agent and have been moved to the Logic API. Details of this API can be found in The Logical Agent API Guide
The example code below is based on a prior version of Agent Factory, and has not yet been updated to reflect the new package structure. Backwards compatibility with this prior version is discussed in the Compatibility API Guide.
Implementing a Perceptor
To implement a perceptor, we start by creating a Java class that is a subclass of the com.agentfactory.platform.interfaces.Perceptor class, and implementing the perceive() method:
import com.agentfactory.platform.interfaces.Perceptor;
public class AlivePerceptor extends Perceptor {
public void perceive() {
adoptBelief( "BELIEF(alive)" );
}
}
The above perceptor generates a single belief that represents the fact that the agent is "alive". This belief is added to the agent's belief set at the start of each iteration of the interpreter cycle.
To create our first agent, we must compile the above class (you may need to include the af_rte.jar file as shown below). To do this, open a command prompt, change the directory to be the one containing the Java class (e.g. d:\quickstart) and type in the following line:
javac -cp .;d:\agentfactory\lib\af_rte.jar AlivePerceptor.java
If you're using Linux, the command will look like this:
javac -cp .:/usr/lib/agentfactory/lib/af_rte.jar AlivePerceptor.java
In each case, you should ensure that the path to af_rte.jar is correct for your installation.
Now you have compiled your first Perceptor.
Next, we need to create our first AFAPL2 agent, which will be stored in a file named "alive.afapl2" (use TextPad or some other text editor - such as NotePad - to create it). Our first program contains a single line of AFAPL2 code (see below). This line of code declares that the agent will include an instance of the AlivePerceptor:
PERCEPTOR AlivePerceptor;
Once you have created the "alive.afapl2" file you must complete the process by compiling it into a ".agent" file. This second type of file is used to deploy the agent.
To compile a ".afapl" file into a ".agent" file use the AFAPL2 compiler in the same command prompt that you used to compile the Java file:
afapl2c alive.afapl2
Figure 2 below presents a screenshot of a command prompt that shows both the file structure and the compilation commands:
The ".agent" file is also a text file (just like the ".afapl2" file). If you open it, you will see that it basically contains the same code as the ".afapl2" file. This will change later on, when you use some of the more advanced features of AFAPL2.
To deploy an instance of our newly created agent program, we need to create (use) two addtional files: the platform configuration file (a file with a ".cfg" extension) and the agent platform script file (a file with a ".aps" extension). For the purposes of this example, we have provided an example platform configuration file here. Download this file, and save it in the "quickstart" folder (i.e. the same folder as AlivePerceptor.java and alive.afapl2).
The second file you need to create is the agent platform script file. This file is used to specify the initial set of agents that will be created when the agent platform is started. For the purposes of our example, this script should contain the following:
CREATE_AGENT myfirstagent alive.agent
To create this file, again use your text editor, and save the file as "alive.aps".
You are now ready to deploy your first agent!
To do this, you simply need to start up an agent platform, passing the platform.cfg and alive.aps files as parameters. This is done by typing the following command in your command prompt:
agentfactory -s alive.aps -c platform.cfg -f
This should also cause the Agent Factory Debugger to be started, giving you the following graphical interface:
Double-click on myfirstagent to get the controller view for that agent - it should look like this:
Finally, to run your first agent, simply click the "Start" button (either the one at the top, or the one in the agent controller view). This should result in the belief BELIEF(alive) appearing in the Perceptor output view for myfirstagent (see below).




