Initializing the Message Handler, Post Processing Percepts

The file qulog/examples/navigation/navigate.qlg contains a program where the robots follow paths between rooms on tracks while avoiding other robots.

In this section we discuss two system declared, user defined actions.

The first is init_message_handler that, if defined, is called when the message handler is first created. In this program it is used to set up subscriptions to messages from other robots and is defined below.

init_message_handler() ~>
subscribe("door_status(D,Rm,S)", _);
subscribe("finished_charging(Rb)", _);
subscribe("path(Rb,Path,Rm)", _);
subscribe("new_loc(Rb,Rm)", _);
subscribe("backed_up(Rob,Rm,Dir)", _);
subscribe("no_path(Rob)", _);
subscribe("end_backed_up(Me)", _);
subscribe("reserve_room(Rob, Rm)", _);
subscribe("new_activity_room(Rom, Rm)", _);
subscribe("joining(_)", _);
write_list(["All Pedro subscriptions lodged",nl_]);
?(my_name(Me));
joining(Me) to pedro

The second is post_process_percepts. If defined, the percept handler thread calls this action while atomically processing the percepts. It is called immediately after the handler updates the belief store. The arguments are the list of percepts that are forgotten and the list of percepts that are remembered. This gives the programmer an opportunity to carry out inferences and perform other actions based on this information.

In this program we use the percepts to infer when the robot has moved into a new room and to let other robots know. We also use it to announce when a robot is moving towards the centre of the room and is therefore no longer backed up.

The code is below.

post_process_percepts(Forgets, Remembers) ~>
check_for_room_change(Forgets, Remembers);
check_for_backup_change(Remembers)

act check_for_room_change(!list(percept_term), !list(percept_term))
%% A robot gets a forgets close_doorway percepts and a remember through_doorway
%% percept and that signifies that the robot has just moved into a room
%% It notifies all robots about its new room
check_for_room_change(Forgets, Remembers) ::
close_doorway(Door) in Forgets &
through_doorway(Door) in Remembers &
my_name(Me) & loc(Me,InRm) &
connected(Door,InRm, OutRm, _Dir) ~>
new_loc(Me,OutRm) to pedro
% Empty action when robot has not just passed through a door.
check_for_room_change(_,_) ~> {}


act check_for_backup_change(!list(percept_term))
check_for_backup_change(Remembers) ::
see_centre_close() in Remembers &
my_name(Me) &
has_backed_up(Me, ChgrRm, _Dir) &
reserved(Me, ChgrRm) &
correctly_backed_up_in_room(Me, ChgrRm) ~>
end_backed_up(Me) to pedro;
finished_charging(Me) to pedro
check_for_backup_change(Remembers) ::
see_centre_close() in Remembers &
my_name(Me) & loc(Me,MyRm) &
has_backed_up(Me, MyRm, _) ~>
end_backed_up(Me) to pedro
check_for_backup_change(_) ~> {}