JACK——PaintRobot Exercise7

来源:http://aosgrp.com/

 

Exercise 7

Create a multi-agent system consisting of a robot agent and a part agent.

 

Introduction

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.

 

Instructions

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:

clip_image001

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

  • Drag a new agent from the design palette onto the new canvas. Name the new agent type Part and add it to a new part package.
  • Drag a new event from the design palette onto the Part_AD design diagram. Name the new event type PaintRequest and add it to the part package.
  • On the design canvas, create a posts link from the Part agent to the PaintRequest event.

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

  • Drag the Part agent from the browser onto the Part_cap_hier canvas.
  • Drag a new capability from the design palette onto the Part-cap_hier canvas. The new capability is called PaintRequesting and must be added to the part package.
  • Create a has link from the Part agent to the PaintRequesting capability.

4. Create a new diagram called PaintRequesting_DEP and

  • Drag a new plan from the design palette onto the new PaintRequesting_DEP canvas. This plan is called SendPaintCommand and is to be included in the part package.
  • Drag the PaintRequest event from the browser onto the PaintRequesting_DEP canvas.
  • Drag the Paint event from the browser onto the PaintRequesting_DEP canvas.
  • Create a handles link from the PaintRequest event to the SendPaintCommand plan on the PaintRequesting_DEP design diagram.
  • Create a sends link from the SendPaintCommand plan to the Paint event on the PaintRequesting_DEP design diagram. Note that it will be necessary to double-click on the link and change it from the default posts link to a sends link.
  • Drag the PaintRequesting capability from the browser onto the PaintRequesting_DEP design diagram. We will only have this on the diagram while we create/check the required links between the capability and its components, otherwise the design diagram will become too cluttered. While the capability is on the canvas, create the following links:

§ 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.

  •  Remove the PaintRequesting capability from the diagram. Note that the capability and the newly created links still remain in the browser. (If you were to drag the capability back onto the design diagram, you would see the links on the diagram.) Your PaintRequesting_DEP diagram should be similar to the following design diagram:

clip_image002

Figure 10: The PaintReqesting_DEP design diagram

5. Edit the PaintRequest event as follows. The event must

  • Be a BDIGoalEvent;
  • Have two members:

String robot; // The name of robot agent to receive the

// paint request.

String colour; // The colour to paint the part.

  • Have a posting method request(String r, String c). Inside the posting method, the parameter r should be assigned to the event data member robot, and the parameter c should be assigned to the event data member colour.

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:

  • Change the name of the PaintRequest event being handled by the plan to preqev.
  • Change the reference to the Paint event type in the sends declaration to pev.
  • The body of this plan must use the @send reasoning statement to send a Paint event to preqev.robot using the Paint event's paint() posting method. preqev.colour is passed as the argument to the paint() posting method. The send statement to be included in the body is therefore:

@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:

  •  Creates a part agent as well as a robot agent;
  •  Invokes the part's submitPaintRequest(), with the name of the robot and the required colour (instead of invoking the now defunct paintPart() method). The code should contain tests similar to the following:

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");

  • Includes an import part.Part; statement.

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)的结果。

 

Questions

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.解释请参考运行结果分析。

你可能感兴趣的:(paint)