来源:http://aosgrp.com/
Exercise 8
Make the part/robot messaging protocol two-way and demonstrate the use of the JACK Interaction Diagram.
Introduction
In this exercise the robot agent will send a message to the part agent when it has finished painting the part. The part agent will use a DisplayFinished plan to handle the Finished event and will print a message to indicate that it has been painted.
Instructions
- Now that we have illustrated the reposting of events, modify the PaintSpecifiedNewColour plan so that it no longer fails, but instead has two painting print statements (to provide two coats of paint).
- Open the PaintRequesting_DEP.
- drag a new event onto the design diagram. This new event is to be called Finished and is to be in the part package. A Finished event is sent by the robot to indicate that it has finished painting the part the specified colour.
- Drag a new plan onto the canvas. This new plan is called DisplayFinished and is to be in the part package. The DisplayFinished plan is to handle a Finished event sent to the Part agent by the Robot agent when it has finished painting the part. This will allow us to view the interaction between the agents on the interaction diagram.
- Create a handles link from the Finished event to the DisplayFinished plan.
- Drag the PaintRequesting capability from the browser onto the design diagram.
- Create a uses link from the PaintRequesting capability to the DisplayFinished plan.
- Create a handles link from the Finished event to the PaintRequesting capability.
- Remove the PaintRequesting capability from the design diagram. Your PaintRequesting_DEP diagram should be similar to the following:
Figure 11: The PaintReqesting_DEP design diagram with the DisplayFinished plan
- Edit the Finished event and complete its definition. It must:
- extend BDIMessageEvent;
- have a String data member called colour;
- have a posting method finished(String c)which assigns c to the colour data member;
As this is a message event, it can display information on an interaction diagram – include the following statement in the posting method
message = "finished painting " + c;
where c is the colour passed to the posting method and message is the (inherited) String data member that will be displayed on the interaction diagram.
If editing the file as a JACK file, save and close the file before continuing.
- Edit the DisplayFinished plan as follows:
- change the reference for the Finished event to fev
- add a #uses interface Part self; declaration to the plan
- add the following print statement to the body reasoning method of the plan:
System.out.println(self.name() + " has been painted " +
fev.colour);
If editing the file as a JACK file, save and close the file before continuing.
- Open the Painting_DEP design diagram and
- Drag the Finished event from the browser onto the canvas.
- Create a sends link from each of the three painting plans to the Finished event. Remember that the default link type is posts – this will need to be changed to sends.
- Drag the Painting capability from the browser onto the design diagram and create a sends link from the Painting capability to the Finished event.
- Remove the Painting capability from the Painting_DEP design diagram. Your diagram should be similar to the following:
Figure 12: The Painting_DEP design diagram
- Modify the PaintAnyColour, PaintSpecifiedCurrentColour and PaintSpecifiedNewColour plans so that when they have finished painting a part, they send the part involved a Finished event using:
@send(ev.from, fev.finished(self.paintColour));
where ev is the reference to the Paint event being handled by the plan, and fev is the reference used in the #sends event Finished fev declaration in the plan. from is an inherited String data member.
If editing the file as a JACK file, save and close the file before continuing.
- Edit the Paint event, so that the message member contains information about the paint colour. Inside the posting method assign a String to message, composed of Paint colour plus the colour passed to the posting method. The inherited String data member message will be displayed on the interaction diagram.
If editing the file as a JACK file, save and close the file before continuing.
- Modify the main() method in Program.java, so that instead of one part agent there are four. Modify the code so that each submitPaintRequest() is associated with a different part.
Save and close the file to apply the changes before continuing.
- Save the project.
- Predict what you would expect to be output by the program. Compile and run your program to test your predictions. When all the parts have been painted, press the Stop button to stop the application.
- Run the program with the interaction diagram. This is achieved by running the program with -Djack.tracing.idisplay.type=id set in the Java Args text box in the Compiler Utility's Run Application window.
示例程序
运行结果:
(1) test with red
(2) test with no specified colour
(3) Painting part the requested colour (1st coat) red
(4) Painting part the requested colour (2nd coat) red
(5) test with green
(6) part1@%portal has been painted red
(7) No specified colour. Painting the part red
(8) test with green again
(9) part2@%portal has been painted red
(10) Painting part the requested colour (1st coat) green
(11) Painting part the requested colour (2nd coat) green
(12) part3@%portal has been painted green
(13) painting the part the current colour: green
(14) part4@%portal has been painted green
运行结果分析:
首先,外部要求绘制红色(1),Part1 Agent向Robot Agent提交绘制红色请求,Robot Agent 接收到Paint(BDIMessageEvent)事件,由于当前Robot Agent保存的颜色为黑色,选择PaintSpecifiedNewColour规划,将Robot Agent保存的颜色置为红色,输出(3)、(4),规划完成后发送回复,触发Finished事件,得到结果(6);
其次,外部要求绘制Null,Part2 Agent向Robot Agent提交绘制Null请求,Robot Agent 接收到Paint(BDIMessageEvent)事件,选择PaintAnyColour规划,输出(7),规划完成后发送回复,触发Finished事件,当前Robot Agent保存的颜色为红色,因此得到结果(9);
再次,外部要求绘制绿色,Part3 Agent向Robot Agent提交绘制绿色请求,Robot Agent 接收到Paint(BDIMessageEvent)事件,选择PaintSpecifiedNewColour规划,将Robot Agent保存的颜色置为绿色,输出结果(10)、(11),规划完成后发送回复,触发Finished事件,得到结果(12);
最后,外部要求再次绘制绿色,Part4 Agent向Robot Agent提交绘制绿色请求,Robot Agent 接收到Paint(BDIMessageEvent)事件,由于当前Robot Agent保存颜色已经为绿色,选择PaintSpecifiedCurrentColour规划,输出结果(13),规划完成后发送回复,触发Finished事件,得到结果(14)。
Questions
1. Explain the order of the output from trace statements from the main program and the JACK agents.