From ABL
Revision as of 06:36, 24 August 2006 by Sooraj (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Currently, ABL has no native construct for conditionally executing steps inside of a behavior (other languages have if, cond, switch etc.). We can, however, simulate conditional statements using other ABL constructs. Consider the following agent:

package examples;


behaving_entity Switch
    BufferedReader input;
    // Different branches of the switch statement. 
    // Note that behaviors can be overloaded.

    sequential behavior Case(int x) {
        precondition { (x<10) }
        mental_act { System.out.println(x + " is small."); }

    sequential behavior Case(int x) {
        precondition { (10<=x && x<100) }
        mental_act { System.out.println(x + " is medium."); }        

    sequential behavior Case(int x) {
        precondition { (100<=x) }
        mental_act { System.out.println(x + " is large."); }

    // The main behavior

    sequential behavior Prompt() {
        int z;
        mental_act {
            System.out.print("Enter a number: ");
            try { z = Integer.parseInt(input.readLine()); }
            catch (IOException e) { }
        subgoal Case(z);

    initial_tree {
        // 'persistent' means we keep attempting the subgoal,
        // regardless of whether is succeeds or fails.
        with (persistent) subgoal Prompt();
        // The initial tree is essentially a parallel behavior, so
        // there is no guarantee as to which step gets exectued
        // first. Thus, we need to give the mental act a higher
        // priority so that 'input' is initialized before use.
        with (priority_modifier 1) mental_act {
            input = new BufferedReader(new InputStreamReader(;

This agent repeatedly prompts the user for a number and reports whether or not the number is 'small', 'medium' or 'large'. The basic approach for doing conditional code is to write up the different branches of the conditional statement in different behaviors of the same name, with each behavior implementing a different precondition, corresponding to the case that the branch represents. Here's a sample run:

% abl examples.Switch
Enter a number: 7
7 is small.
Enter a number: 31
31 is medium.
Enter a number: 1234
1234 is large.
Enter a number: -12
-12 is small.