In contrast to → literals, → variables and → beliefs and facts, plans and rules are executable structures.
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.
To transform an imperative function to a logical plan, proceed as follows:
newduration
$\Rightarrow$ NewDuration
.;
, except the last one!.
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
+!phaseduration(NewDuration) : 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 ) .
Rules
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:
fibonacci(X,R) // 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) .