博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JACK——PaintRobot Exercise2
阅读量:6860 次
发布时间:2019-06-26

本文共 5295 字,大约阅读时间需要 17 分钟。

来源:

 

Extend the robot agent to use a JACK plan.

 

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.

 

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:

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

 

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

你可能感兴趣的文章
CentOS环境下安装无线网卡
查看>>
在Exchange 2013中重置用户密码
查看>>
什么是UTM?
查看>>
微软最新认证体系
查看>>
SQL Server 2012 开发新特性:Sequence
查看>>
集成服务入门(实验9)日志记录和邮件通知
查看>>
玩转百度即用API(5)——空气质量指数查询
查看>>
zabbix中文版安装部署及配置说明
查看>>
Redis 过期淘汰策略
查看>>
运维自动化之puppet基础应用解析(经典版)
查看>>
最新版 WordPress 3.6 “Oscar” 简体中文版介绍及下载
查看>>
golang slice 和 array的区别
查看>>
SSDT Hook的妙用-对抗ring0 inline hook
查看>>
zabbix安装和使用
查看>>
SUSE Login incorrect
查看>>
批量备份脚本
查看>>
安全尽职是企业的阿克琉斯之踵
查看>>
Apache Qpid深入介绍
查看>>
java中获得对象构造器及其构造器参数类型
查看>>
TCP/IP 2.2配置静态路由
查看>>