JACK——PaintRobot Exercise2

来源:http://aosgrp.com/

 

Exercise 2

Extend the robot agent to use a JACK plan.

 

Introduction

In this exercise, the robot agent's paintPart() method will post a Paint event, thus allowing a plan to be chosen to respond to this event. At this stage we will only have one plan, PaintSpecifiedCurrentColour, which handles the Paint event. The plan will print a message to indicate that the robot is painting a part a particular colour. Note that the plan PaintSpecifiedCurrentColour would be more appropriately named PaintSpecifiedColour at this stage in the practical. However, this plan will be used to paint a part a specified colour which matches the robot's current paint colour in later exercises, and it was felt that it would be less confusing if the name was not changed.

As before, paintPart() is invoked from a Java main() method (and runs on the Java main thread) and is followed by a System.exit(0) call.

Plans and events are described in the Introduction to JACK notes. If necessary, read these before beginning the exercise.

 

Instructions:

1. Use the design tool to add a Paint event to the application.

  • If necessary, open the Robot_AD canvas by right-clicking on Robot_AD in the Design Views folder and selecting Edit "Robot_AD" from the pop-up menu.
  •  Drag an event from the design palette onto the canvas.
  • Fill in the pop-up dialog box with details of the Paint event. Make sure that the Paint event is in the robot package.
  • Click on the Add New button. (The event will now be displayed in the browser in the Event Types folder inside the Agent Model.)

2. On the design canvas, create a posts link from the Robot agent to the Paint event. Observe that a declaration is added automatically by the JDE in the External Events folder of the Robot agent.

3. On the design canvas, create a handles link from the Paint event to the Robot agent.

4. Add a PaintSpecifiedCurrentColour plan to the Robot_AD canvas. The plan must also be in the robot package.

5. On the design canvas, create a uses link from the Robot agent to the PaintSpecifiedCurrentColour plan.

6. On the design canvas, create a handles link from the Paint event to the PaintSpecifiedCurrentColour plan. Your design diagram should now be similar to the following diagram:

JACK——PaintRobot Exercise2

Figure 3: The Robot_AD design diagram with one plan, PaintSpecifiedCurrentColour

7. In the browser, right-click on the Paint event and select Edit as JACK file from the pop-up menu. Complete the Paint event by making it:

  •  Extend BDIGoalEvent;
  • Contain a String data member(field) called colour (this is the information that will be carried with the event); and
  • Include a posting method, paint(String c). The parameter c is to be assigned to the event data member colour inside the posting method. The completed Paint event follows:

package robot;

public event Paint extends BDIGoalEvent {

public String colour;

#posted as

paint(String c)

{

    colour = c;

}

}

Close the file to ensure that there are no conflicts between editing the file in the JDE browser and as a JACK file. In the editor window of the file, click the Save button and then the Close button.

8. Using the Edit as a JACK File option, edit the Robot agent and

  • modify the #posts event Paint declaration so that it becomes:

#posts event Paint pev;

  • modify the paintPart() method in the Robot agent so that it uses the postEventAndWait() method to post a Paint event with the colour "white" as follows:

postEventAndWait(pev.paint("white"));

Close the file to ensure that there are no conflicts between editing the file in the JDE browser and as a JACK file. In the editor window of the file, click the Save button and then the Close button.

9. Using the Edit as a JACK File option edit the PaintSpecifiedCurrentColour plan as follows:

  • check that the reference to the Paint event handled by the plan is ev
  • add the following statement to the plan's body reasoning method:

System.out.println("Painting the part the requested colour: " +

ev.colour);

Close the file to ensure that there are no conflicts between editing the file in the JDE browser and as a JACK file. In the editor window of the file, click the Save button and then the Close button.

10. Save, compile and run the application.

 

示例程序 

运行结果:painting the part the requested colour: white

 

Questions

1. What would happen if you used postEvent() inside paintPart() instead of postEventAndWait()? Try it!

If you did not observe any difference, add @sleep(2) before the print the statement in the PaintSpecifiedCurrentColour plan. Run the program using postEventAndWait() in the paintPart() method. Replace the postEventAndWait with postEvent() and run the program again. Explain your observations. Remove the @sleep(2) from the plan before you begin the next exercise.

Make sure you change postEvent() back into postEventAndWait() before you begin the next exercise.

2. postEventAndWait() should only be called from the Java main thread or from other Java threads that are external to JACK. While the agent handles this event, the calling thread is blocked and must wait until the agent returns its result. What problem would you envisage if postEventAndWait() was called from an agent task?

 

Answers

1. 使用postEvent()未能输出postEventAndWait()之前的运行结果,原因在于postEvent()为异步事件,Agent推送完事件后,不会等待事件选择某个plan去执行,而是直接回到main方法的主线程中继续执行,至System.exit(0)退出。

2. 应该还是同步执行

转载于:https://www.cnblogs.com/6DAN_HUST/archive/2011/06/09/2076068.html