Import
From Agent Factory
Editorial History
19/12/08 (Version 1): Added basic info on the IMPORT construct
Overview
Support for the reuse of AFAPL2 code is provided in the form of an IMPORT statement that is based on the C #include statement. When building AFAPL2 programs, source code is written in a file that has a “.afapl2” extension. These files are incorporated into the Java package structure, and can be referenced using standard Java naming conventions.
The ability to write reusable blocks of program code is an essential requirement of any practical programming language. While the reuse mechanism presented here is quite simplistic, it has had a significant impact on the usability of AFAPL2 as it facilitates the creation of code libraries that can easily be reused. This has led to the creation of a number of basic AFAPL2 programs that act as a starting point for developers. In this light, we have created the com.agentfactory. core.fipa.agent.FIPACore agent program, which contains the basic set of preceptors and actions required for agent communication. We have then built on this to create the com.agentfactory.core.agent.BasicAgent agent program which extends the core communication behaviours to include a number of core perceptors, actions and behaviours that we believe are core to all agents. This second program includes: actions for the adoption and retraction of beliefs; actions that support the binding to and unbinding from platform services; perceptors that generate beliefs about the current state of the agent platform; preceptors that generate beliefs about the current state of the agent (e.g. name, iteration); and so on.
Practical Example
The example code below illustrated the AFAPL2 reuse mechanism through two example AFAPL2 programs where the second program reuses the code of the first program:
// Program 1: Monitored.afapl (in agent package)
BELIEF(fipaMessage(request, sender(?name, ?addr), ping) =>
COMMIT(?self, ?now, BELIEF(true),
inform(agentID(?name, ?addr), pong));
// Program 2: Alive.afapl IMPORT agent.Monitored;
ACTION alive {
PRECONDITION BELIEF(true);
POSTCONDITION BELIEF(true);
CLASS actuator.Alive; }
COMMIT(?self, ?now, BELIEF(true), alive);
The first program builds on the health monitor example presented earlier, and implements the other half of the “ping” protocol (i.e. it defines how a monitored agent should respond to a health check). The second program is the AFAPL equivalent of the “Hello World” program. It defines a single action, called alive, whose implementation (actuator) will print out “Hello World!” to the console, and a single initial commitment to the alive action. The key point demonstrated here is the use of the IMPORT statement, which integrates the code for interacting with a HealthMonitor agent with the Alive agent program.
As is discussed later, the AFAPL compiler converts source code into a more machine-oriented target code, denoted by files with a “.agent” extension. As part of this process, the compiler combines all of the partial agent programs into a single block of code, which it then converts into the target code.
