Create a multi-agent system consisting of a robot agent and a part agent.
In this exercise you will create a part agent that interacts with the robot agent. The part agent will have a PaintRequesting capability that will enable it to send painting requests to the robot agent. The part agent will post itself PaintRequest events (by the invocation of a submitPaintRequest() method in the main Java thread). The PaintRequest event will be handled by the part agent's SendPaintCommand plan. The SendPaintCommand plan will send a Paint event to the robot agent.
Note that the Paint event must now be a BDIMessageEvent (a message event is required if the event is to be sent between agents). We will also need to add
#set behavior Recover repost
to the Paint event. BDI events have a default behaviour that determines what happens with respect to plan reconsideration, applicable plan set recalculation and meta-level reasoning (reasoning about which plan to choose). These default behaviours can be modified by setting behaviour attributes. In this example, we set the recover attribute to repost which means that the event will be reposted on failure. The applicable plan set will be recomputed (with possibly different results/bindings – in this case our failed PaintSpecifiedNewColour plan will have changed the agent's paintColour member) and another applicable plan will be tried. This should become clearer when you run the example.
7a – Organise the code into sub-folders in the JDE browser.
Before we begin adding the definitions for our Part agent, we will organise the code so that all the code related to the Robot will be in Robot sub-folders in the JDE browser.
It is useful to organise the project in the browser according to entities that relate to the Robot and entities that relate to the Part. This can be achieved by right-clicking on a folder/container and selecting Add Nested Container. The new nested container can be given an appropriate name (e.g. robot or part). Components can be dragged and dropped into the appropriate nested container (e.g. all the painting plans can be dragged into a nested robot container inside the Plan Types folder).
1. Organise the remainder of the project in the browser so that each folder has a nested robot folder containing the definitions related to the robot. (It is not always clear which package an event should belong to. We generally add them to the package of the agent that can handle the event.)
2. Save the project. The browser window should look similar to the following:
Figure 9: The browser window with the project organised into nested containers
3. Compile and run the application.
7b – Create the Part agent and incorporate it into the application
1. Create a new design diagram called Part_AD and
2. In the browser create a nested container for the Part agent in the Event Types folder. Drag the PaintRequest event into the Part folder. As more components (plans etc.) are added for the Part and Robot, store them in appropriate nested folders in the browser.
3. Create a new design diagram called Part_cap_hier and
4. Create a new diagram called PaintRequesting_DEP and
§ a uses link from the PaintRequesting capability to the SendPaintCommand plan.
§ a handles link from the PaintRequest event to the PaintRequesting capability
§ a sends event from the PaintRequesting capability to the Paint event.
Figure 10: The PaintReqesting_DEP design diagram
5. Edit the PaintRequest event as follows. The event must
String robot; // The name of robot agent to receive the
// paint request.
String colour; // The colour to paint the part.
If editing the file as a JACK file, save and close the file before continuing.
6. Edit the Part agent and add a submitPaintRequest(String robot, String colour) method that will post a PaintRequest event containing the name of the robot to send the paint request to and the colour that the part is to be painted. Use postEventAndWait() to post the event.
If editing the file as a JACK file, save and close the file before continuing.
7. Edit the SendPaintCommand plan as follows:
@send(preqev.robot, pev.paint(preqev.colour));
If editing the file as a JACK file, save and close the file before continuing.
8. Edit the Paint event to extend BDIMessageEvent, and set its Recover behaviour attribute to be repost (#set behavior Recover repost).
If editing the file as a JACK file, save and close the file before continuing.
9. Edit the Robot agent and remove the paintPart() method and the #posts event Paint declaration.
If editing the file as a JACK file, save and close the file before continuing.
10. Modify the main() method in Program.java so that it:
System.out.println("test with red");
part1.submitPaintRequest("robbie","red");
System.out.println("test with no specified colour (null)");
part1.submitPaintRequest("robbie",null);
System.out.println("test with green");
part1.submitPaintRequest("robbie","green");
System.out.println("test with green again");
part1.submitPaintRequest("robbie","green");
11. Comment out the System.exit(0); statement in Program.java.
Save and close the file to apply the changes before continuing.
12. Save the project.
13. Compile the program.
14. Predict what you would expect to be output by the program. Run the program. Are your predictions correct?
运行结果:
(1) test with red
(2) test with no specified colour
(3) Painting part the requested colour (1st coat) red
(4) PaintSpecifiedNewColour plan failed
(5) painting the part the current colour: red
(6) test with green
(7) No specified colour. Painting the part red
(8) test with green again
(9) Painting part the requested colour (1st coat) green
(10) PaintSpecifiedNewColour plan failed
(11) painting the part the current colour: green
(12) painting the part the current colour: green
运行结果分析:
首先,外部要求绘制红色(1),Part agent向Robot agent提交绘制红色的请求,出现(3)、(4)、(5)的结果,当前Robot agent中的颜色置为红色(例6中已经分析过了)。
然后,外部要求绘制null(2),Part agent向Robot agent提交绘制null的请求,出现(7)的结果。
再次,外部要求绘制绿色,Part agent向Robot agent提交绘制绿色的请求,由于当前Robot agent保存的颜色为红色,出现(9)、(10)、(11)的结果。
最后,外部要求再次绘制绿色,Part agent向Robot agent提交绘制绿色的请求,当前Robot agent保存的颜色为绿色,出现(12)的结果。
1. What would happen if you had not added #set behavior Recover repost to the Paint event? Test your prediction by commenting out the #set behavior statement. Why wasn't this required when the Paint event was a BDIGoalEvent?
2. Explain the order of the output statements.
Answers
1. 如果不设置#set behavior Recover repost to the Paint event,则在绘制失败时,不再重新选择新的规划。因为此时Paint事件已经改为BDIMessageEvent,如果是BDIGoalEvent则自动重新选择新的规划以完成目标。
2.解释请参考运行结果分析。