This final example illustrates how AgentSpeak(L) permits the modification of the agents internal state through the belief update plan operators. One operator is provided to support the addition of new beliefs and a second operator is provided to support the removal of existing beliefs. No operator is provided for the modification or an existing belief (this is achieved through the retraction of the existing belief and the subsequent adoption of the new belief).
01 light(on). 02 03 +light(on) <- 04 println(the light is on, turn it off!); 05 -light(on); 06 +light(off). 07 08 +light(off) <- 09 println(the light is off, turn it on!); 10 -light(off); 11 +light(on).
This program includes an initial belief, representing the fact the a light is on, and two rules. The triggering event of the first rule (lines 03-06) is the event that the agent adopts a belief that the light is on (like the initial belief). The body of the rule consists of a sequence of three actions: (1) It prints out a message to the console, (2) it retracts the belief that the light is on, and (3) it adopts a belief that the light is off. The second rule (lines 08-11) does the opposite of this – it retracts the belief that the light is off and adopts the belief that the light is on. In terms of behaviour, this program implements an infinite loop, where either the first rule or the second rule is executed on each iteration. In fact, the last operation of each rule generates the event that triggers the next rule.
In terms of execution, the following happens: on iteration 1, the agent adopts the belief that the light is on and adds the associated belief adoption event to the event queue. The agent then selects that event from the event queue and handles it by matching the event with the first rule (lines 03-06) and adopting a new intention that contains the associated plan. The agent then selects this intention for execution and executes the first step of the plan which prints out the “the light is on, turn it off”. On iteration 2, the agent does not select an event because the event queue is empty. It does, however select the intention again, this time executing the second step of the plan, which causes the belief light(on) to be dropped. This action has the side effect of generating a belief retraction event that is added to the agents event queue. On the 3rd iteration, the agent selects the belief retraction event from the event queue and attempts to match it against rule. No matching rule exists, so this event is ignored (in some implementations the event queue is filtered so that this type of event is never added as it can never affect the behaviour of the agent). The intention is selected for a third time, and the last step is executed, resulting in the belief light(off) being adopted. Again, this has a side effect – namely the generation of a belief adoption event, which is added to the event queue. At the end of this iteration, the intention is marked as completed and dropped. On the 4th iteration, the agent selects the belief adoption event and matches it to the second rule (lines 08-11). This results in the adoption of a new intention that contains the associated plan. The agent selects this intention, and executes the first step, which results in the following message being printed to the console: “the light is off, turn it on”. Over the next two iterations, the agent drops the belief light(off) and adopts the belief light(on), resulting in first rule being triggered and the behaviour described on iterations 1-3 is repeated. this behaviour finishes on iteration 9,resulting in the behaviour of iterations 4-6 being repeated and so on. In fact, the overall behaviour of the agent is an infinite loop where the agent prints out the statement that the “light is on…” followed by the statement that the “light is off…” repeatedly.