Basic Knowledge: Plans and Logic Rules

In contrast to → literals, → variables and → beliefs and facts, plans and rules are executable structures.

Contents [Hide]

In general Prolog uses only logical rules. AgentSpeak(L) and AgentSpeak(L++) also use rules but additionally provide a plan structure. Within a Prolog structure the ordering is relevant to the execution semantic (see in Learn Prolog Now!).

But in AgentSpeak(L++) the ordering of rules and plans are not relevant for the execution semantic. Furthermore plan and rule structures can be grouped within the source code to simplify modelling different paths of execution.


Plans are like static methods or functions in an imperative programming language, with an execution condition and boolean return value.


Transforming Imperative Functions to Plans

To transform an imperative function to a logical plan, proceed as follows:

  • Consider every execution path! For each terminating path create a plan condition.
  • Variables have to start with a capital letter, e.g. newduration $\Rightarrow$ NewDuration.
  • Statements have to be separated by a semicolon ;, except the last one!
  • A plan has to be terminated by a full stop symbol .

Plan Example

Consider the following Java method to change the phase duration of a traffic light:

public static boolean phaseduration( int newduration )
  if ( newduration < 1 )   // Plan condition: plan fails if
      return false;        // new duration is unreasonably small

  System.out.println( “For safety, changing light to RED” );
  System.out.println( “Changing phase duration to ” + newduration );
  return true;             // Plan succeeded

transformed into a plan, by considering the above procedure, will result in

    : NewDuration < 1 <-    // path 1: plan condition to always fail
        fail                // fail simply marks the plan as failed.
    : NewDuration >= 1 <-   // path 2: plan condition to change light and succeed
        generic/print( “For safety, changing light to RED” );
        generic/print( “Changing phase duration to”, NewDuration )




  • can be seen as static functions in a logic programming language with some additional structure.
  • are similar to plans, but without context and trigger event.

Rule Example

One of the most famous examples for rules in logic programs is the Fibonacci sequence. Mathematically this sequences is defined as $$F_n = F_{n-1} + F_{n-2}$$ $$F_0 = 0$$ $$F_1 = F_2 = 1$$ For the value $n=5$ the sequence is calculated as $$F_5 = F_4 + F_3 = (F_3 + 1) + (1+1) = ((1+1)+1) + (1+1) = 5$$ Based on this calculation you can see that each function element $F_n$ which is not defined as $1$ gets resolved in a recursive way. A rule, which calculates the Fibonacci number of any input can be written as follows:

    // order of the rules are indeterministic, so for avoid indeterministic behaviour
    // add the condition, when the rule can be executed first
    :- X <= 2;  R = 1
    :- X > 2;   TA = X - 1; TB = X - 2; $fibonacci(TA,A); $fibonacci(TB,B); R = A+B
!main <-
    $fibonacci(8, FIB)