The third example program specifies an AgentSpeak(L) agent that uses the belief query plan operator. It also includes an initial belief (as well as an initial goal):
01 !init(). 02 is(rem, happy). 03 04 +!init() <- 05 println(starting); 06 ?is(rem, happy); 07 println(first hurdle passed); 08 ?is(rem, sad); 09 println(ending).
This program includes an initial goal !init() (line 01); an initial belief is(rem, happy) (line 02); and a plan rule with a more complex bahaviour (lines 04-09). As in the first example, the initial goal is used to generate a goal adoption event. The addition of the initial belief results in the adoption of that belief and the generation of a belief adoption event, which is also added to the agents event queue. The plan rule involves a behaviour consisting of a sequence of 5 plan operators. Each operator is separated by a semi-colon (;) and the plan rule is terminated by a period (.). The behaviour consists of two types of plan operator: the println private action and the belief query operator. This second operator works by checking the agents beliefs. If the provided belief can be matched to a belief in the agents belief set, then the operator succeeds, otherwise it fails (and the corresponding intention fails).
In terms of execution, the following happens: on iteration 1, the agent removes the +!init() goal adoption event from the event queue and then matches it to the plan rule, causing an intention to be created. This intention is then selected by the interpreter and the first step is executed, causing “starting” to be printed to the console. On iteration 2, the agent removes the +is(rem, happy) belief adoption event, but fails to match the event to a rule – this means that the event is ignored. Next, the agent again selects the intention and performs the second step which is a belief query for the belief is(rem, happy). This belief is in the agents belief set (it was added as an initial belief) so the query succeeds. On the third iteration, the event queue is empty, so no event is processed. Again the intention is selected and the next step is executed resulting in “first hurdle passed” being printed to the console. On the fourth iteration, there is still no event and the same intention is selected (there is only one intention). The fourth step is executed, which is another belief query, this time for is(rem, sad). This belief is not in the agents belief set, so the query fails, and the intention fails. This results in the intention being dropped (the fifth step of the intention is not executed).