Hello World!

From ABL
Jump to: navigation, search

This tutorial will show you the basic structure of an ABL source code file and how to compile and run ABL source code. The goal is to ensure that you have ABL installed correctly. We assume some familiarity with Java. ABL is very Java-like, and we will point out where the two are similar and where they differ. You will need Java 1.5 to compile and run ABL code, but currently only Java 1.4 syntax is allowed inside mental acts.


A sample agent

Let's take a look at the contents of HelloWorld.abl:

package test;

behaving_entity HelloWorld {
    initial_tree {
        mental_act { 
            System.out.println("Hello world!");
        }
    }
}

An ABL source code file must be named AgentName.abl, where AgentName is the name used in the behaving_entity declaration. An agent must have a package declaration; this is a limitation of the current version of ABL.

Though the analogy is false, you can think of ABL behaving entities as Java classes and ABL behaviors as Java methods. Likewise, the initial_tree can be thought of as the agent's main() method. The initial tree controls the main flow of behaviors in an ABL agent, and every ABL agent has one. The initial tree is just a behavior, and like any behavior, it contains a set of steps. In this case, it has a single step, which is a mental act that prints a message. Normally, agents have sensors and actions. In this case, we have an agent who has neither because we did not register any. Thus, we are left with an agent that can only perform mental acts; i.e. it can "think" but can't "see" or "do".


How to compile and run/use an agent

Assume HelloWorld.abl lives in a directory named test and that we are working from the directory containing test.

Summary

ablc test/HelloWorld.abl
javac -cp $ABLPATH:. test/HelloWorld*.java
abl test.HelloWorld

1. Run the ABL compiler on AgentName.abl

You can invoke the ABL compiler as follows:

java -cp $ABLPATH:. abl.compiler.Abl test/HelloWorld.abl

where ABLPATH is an environment variable that contains items needed to run the ABL compiler or items that the ABL compiler will need to resolve certain references in your ABL code. For convenience, we have included a helper script which enables you to type the following instead:

ablc test/HelloWorld.abl

This works provided that (1) abl.jar is on the ABLPATH; and (2) the ablc script is on the path.

2. Compile the resultant Java files

Running the ABL compiler will generate a dozen or so Java files in the same package. Compile them:

javac -cp $ABLPATH:. test/HelloWorld*.java

Typically, you will have other code that your agent refers to, so you will need to include them on your classpath as needed.

3. Invoke AgentName.startBehaving()

An agent AgentName.abl will generate a file AgentName.java, which will have a method startBehaving(). Call this method to get your agent to start executing its flow of behavior described in initial_tree. This method must be invoked in the same thread in which the agent is constructed/new'ed. A safe invocation pattern is thus:

new HelloWorld().startBehaving();

For convenience, AgentName.java contains a simple main() that does exactly this. So you could invoke the HelloWorld agent as follows:

java -cp $ABLPATH:. test.HelloWorld

Again, we have included a helper script, abl, that does exactly this and saves you some typing:

abl test.HelloWorld

This simple main() probably won't be suitable for more complex agents, as you will typically need to do some initialization of the sensorimotor system first. This depends on your sensorimotor system though; it is possible to do all initialization in static initializers, and this may be appropriate for sensorimotor systems of medium to low complexity.

A note on the initial_tree

Because the initial tree is a collection behavior (i.e. a parallel behavior with all effect_only steps), it does not return with any success or failure token...it is merely a container for its steps to run. Thus, it does not terminate unless the programmer specifies otherwise (by calling System.exit(), for instance). So the HelloWorld agent, for example, prints "Hello world!" to the screen and then "hangs"; this is expected behavior.