JADE编程入门
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JADE TUTORIAL(教程)
JADE PROGRAMMING FOR BEGINNERS(玉编程基础入门)
USAGE RESTRICTED ACCORDING TO LICENSE AGREEMENT.(基于许可证的用法限制)
JADE - Java Agent DEvelopment Framework(开发框架) is a framework to develop multi-agent systems (多代理系统) in compliance with the FIPA specifications(遵从FIPA规范). JADE successfully passed the 1st FIPA interoperability test (相互操作性测试)in Seoul (Jan. 99) and the 2nd FIPA interoperability test in London (Apr. 01).
Copyright (版权)(C) 2000 CSELT S.p.A. (C) 2001 TILab S.p.A. (C) 2002 TILab S.p.A.
This library is free software(这是一个自由软件库); you can redistribute(重新分配) it and/or modify it under the terms of the GNU Lesser General Public License as
published(在发表GNU公共许可证的前提下) by the Free Software Foundation, version 2.1 of the License (自由软件基金会授权的2.1版本).
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY(无任何担保); without even the implied warranty(隐含保证)of MERCHANTABILITY(merchantability 适销性) or FITNESS (fitness 针对性)FOR A PARTICULAR PURPOSE(particular purpose 特定用途). See the GNU Lesser General Public License for more
details.You should have received a copy of (副本)the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc.(公司), 59 Temple Place (59寺广场)- Suite (套房)330, Boston, MA(麻省) 02111-1307, USA.
TABLE OF CONTENTS(目录)
1 JADE OVERVIEW (概述)
1.1 Containers and Platforms (集装箱和平台)
1.2 AMS and DF (Agent Management System and Directory Facilitator代理管理系统和目录诱导者)
2 THE “BOOK TRADING” EXAMPLE (“图书交易”例子)
3 CREATING JADE AGENTS – THE AGENT CLASS (创造玉代理-代理类)
3.1 Agent identifiers (代理标识符)
3.2 Running agents (运行代理)
3.3 Agent termination (代理终止)
3.4 Passing arguments to an agent (传参给代理)
4 AGENT TASKS – THE BEHAVIOUR CLASS (代理任务-类得行为)
4.1 Behaviours scheduling(安排) and execution (执行)
4.2 One-shot behaviours, cyclic behaviours and generic behaviours (单次行为,循环行为和一般行为)
4.3 Scheduling operations at given points in time (在给定的时间安排操作点)
4.4 Behaviours required in the book trading example (在书本交易例子中需要的行为)
4.4.1 Book-buyer agent behaviours (买书代理的行为)
4.4.2 Book-seller agent behaviours (卖书代理的行为)
5 AGENT COMMUNICATION – THE ACLMESSAGE CLASS (代理沟通-aclmessage类)
5.1 The ACL language ()
5.2 Sending messages (发送消息)
5.3 The book trading example messages (图书贸易例子中的消息)
5.4 Receiving messages
5.5 Blocking a behaviour(封锁一个行为) waiting for a message
5.6 Selecting messages with given characteristics from the message queue (从信息队列中选择一个给定的特点)
5.7 Complex conversations (复杂对话)
5.8 Receiving messages in blocking mode(在封锁状态中接收信息)
6 THE YELLOW PAGES SERVICE(黄页服务)– THE DFSERVICE CLASS (DF服务类)
6.1 The DF agent
6.2 Interacting(互动) with the DF
6.2.1 Publishing services (出版服务)
6.2.2 Searching for services (找寻服务)
JADE PROGRAMMING FOR BEGINNERS
This tutorial(教程) shows how to create simple JADE agents and how to make them executing(执行) tasks and
communicate between each other. JADE is completely written in Java (完全用JAVA编写)and JADE programmers (程序员)work in full Java when developing their agents. Therefore the reader is assumed to be familiar with the Java
programming language.
This tutorial’s structured (结构)as follows.
Chapter 1 (第一章)gives a brief overview of JADE and introduces the concepts of Platform, Container, AMS and DF.
Chapter 2 presents a simple example that will be used
throughout this tutorial to illustrate(说明) the steps required to develop simple agent-based applications with
JADE.
Chapter 3 focuses on creating agents and explains the basic features (基本特点)of the Agent and AID classes.
Chapter 4 explains how to make JADE agents execute tasks(执行任务) and introduces the Behaviour class(类行为).
Chapter 5 describes how to make agents communicate and introduces the ACLMessage and MessageTemplate classes. Chapter 6 illustrates how to exploit(利用) the Yellow Pages service provided by the DF agent through the DF Service class.
Besides the basic features illustrated(基本特征说明) in this tutorial, JADE provides a number of advanced(先进) features such as the support for complex interaction protocols(对复杂交互协议的支持) and the usage of user defined ontologies(对用户定义本体的使用). For these features readers are reminded to the JADE Programmer’s guide(程序员指南)and Administrator’s guide (管理指南)available on the JADE web site.
1 JADE OVERVIEW
JADE is a middleware(中间件) that facilitates(使简化) the development of multi-agent systems. It includes
(1)A runtime environment (运行环境)where JADE agents c an “live”(生存) and that must be active(起作用的) on a given host(在给定的主机上) before
one or more agents can be executed on that host.
(2) A library of classes (类库)that programmers have to/can use (directly or by specializing (专心于)them) to develop their agents.
(3) A suite of graphical tools (一套图形工具)that allows administrating (管理)and monitoring (监测,检查) the activity of running agents.
1.1 Containers and Platforms
Each running instance(运行案例) of the JADE runtime environment is called a Container (集装箱)as it can contain several agents. The set of active containers(这种起作用的集装箱) is called a Platform(平台). A single special Main container must always be active in a platform and all other containers register with it(和它一块登记,和主集装箱绑定) as soon as they start. It follows that(因此) the first container to start(第一个开始的集装箱) in a platform must be a main container while all other containers must be “normal” (non-main) containers and must “be told” where to find (host and port主机和端口) their main container .(相当于一个工头带着一群工人,只有工头先找到工作地点,工人们与工头取得联系,找到工头在哪儿,工人们才能和工头协同工作。
)If another main container is started somewhere in the network ,it constitutes (构造)a different platform to which new normal containers can possibly register. Figure 1(图1)
illustrates the above concepts through a sample(样品) scenario(情况) showing two JADE platforms composed of 3 and 1 container respectively. JADE agents are
identified by a unique name and, provided they know each oth er’s name, they can communicate transparently regardless of their actual location: same container (e.g. agents A2 and A3 in Figure 1),different containers in the same platform (e.g. A1 and A2) or different platforms (e.g. A4 and A5).
You don’t have to know how the JADE runtime environment works, b ut just need to start it before executing your agents. (相当于:一个工程可以包给多个工头,工头们分工合作,每个工头又可以带着N 个员工,每个员工也分配不同的工作,所有人之间可以相互交流,只要知道对分所在地点即可,不需要考虑对方所属哪部分。
)Starting JADE as a main or normal container and executing agents on it, is described in the JADE Administrative Tutorial available on the JADE website.
1.2 AMS and DF
Besides the ability of accepting registrations from other containers, a main container differs from
normal containers as it holds two special agents (主集装箱与平常集装箱不同在于:主集装箱拥有两个特殊的代理,当主集装箱启动时这两个代理可以自动开始工作)(automatically started when the main container is launched).
The AMS (Agent Management System) that provides the naming service (i.e. ensures that each agent in the platform has a unique name) and represents the authority(权力) in the platform (for instance(for example) it is possible to create/kill agents on remote(远程的) containers by requesting that to the AMS). This tutorial does not illustrate how to interact with the AMS as this is part of the advanced JADE programming.(相当于一管理员,功能1就是给平台上的各个代理分配一个确定的名字,功能2就是有权力创造或着消除代理)
The DF (Directory Facilitator) that provides a Yellow Pages service(黄页服务的意思是:一个代理能够找到另一个代理提供给的(它所需要的为了达到目的)服务) by means of which an agent can find other agents providing the services he requires in order to achieve his goals. Using the Yellow Pages service provided by the DF agent is illustrated in chapter 6.
2 THE “BOOK TRADING” EXAMPLE
This chapter introduces a simple example that will be used throughout this tutorial to illustrate the steps
required to develop agent-based applications with JADE. The scenario considered in this example includes some agents selling books and other agents buying books on behalf of(代表) their users.
Each buyer agent receives the title of the book to buy (the “target book”) as a command
line argument(命令行参数)and periodically requests (定期请求)all known seller agents to provide an offer(提供报价). As soon as an offer is received(一旦收到邀请) the buyer agent accepts it and issues a purchase order(发出采购订单). If more than one seller agent provides an offer the buyer agent accepts the best one (lowest price). Having bought the target book the buyer agent terminates(终止).
Each seller agent has a minimal GUI(最小图形用户界面) by means of which the user can insert (插入)new titles (and the associated price(相关的价格) in the local catalogue of books(当地的书籍目录中) for sale. Seller agents continuously (不断)wait for requests from buyer agents. When asked to provide an offer for a book they check if the requested book is in their catalogue and in this case reply with the price. Otherwise they refuse. When they receive a purchase order (订单)they serve it and remove the requested book from their catalogue.
All issues related to electronic payment(所有与电子支付相关的问题) are outside the scope of this tutorial and are not taken into account.
The complete sources of this example are available among the examples provided with JADE in the
examples.bookTrading package.
3 CREATING JADE AGENTS – THE AGENT CLASS
Creating a JADE agent is as simple as defining a class extending the jade.core.Agent class and implementing the setup() method as shown in the code below.
import jade.core.Agent;
public class BookBuyerAgent extends Agent {
protected void setup() {
// Printout a welcome message
System.out.println(
“Hallo!Buyer-agent“+getAID().getName()+” is ready.”);
}
}
The setup() method is intended to include agent initializations(初始化). The actual job an agent has to do is
typically (典型的,一般的)carried out within “behaviours” as will be described in chapter 4.
3.1 Agent identifiers(代理标识符)
Each agent is identified by an “agent identifier”( agent identifier代表一个Jade.core.AID class 实例)represented as an instance of the jade.core.AID class. The getAID() method of the Agent class
allows retrieving(检索) the agent identifier. An AID object includes a globally unique (全局唯一的名称)name plus a number of addresses. The name in JADE has the form(玉名的形式为)
<nickname>@<platform-name> so that an agent called Peter living on a platform called P1 will have Peter@P1 as globally unique name. The addresses included in the AID are the addresses of the platform the agent lives in(包含AID的这个地址是代理存在该平台的地址). These addresses are only used when an agent needs to communicate with another agent living on a different platform. Developers need to care about them only in particular cases that are outside
the scope of(范围) this tutorial.
Knowing the nickname of an agent, its AID can be obtained(获得) as follows:
String nickname = “Peter”;
AID id = new AID(nickname, AID.ISLOCALNAME)//(islocalname:当前位置称呼);
The ISLOCALNAME constant indicates(islocalname常量代表的是) that the first parameter represents the nickname (local to the platform)(当前平台昵称的第一个参数) and not the globally unique name of the agent.
3.2 Running agents
The created agent(创建的代理) can be compiled(编译) as follows.
javac –classpath <JADE-classes> BookBuyerAgent.java
In order to execute the compiled agent the JADE runtime must be started and a nickname (for the agent to run )must be chosen:
java –classpath <JADE-classes>;. jade.Boot buyer:BookBuyerAgent
More details on compiling and running agents can be found in the JADE Administrative Tutorial or in the JADE Administrator’s Guide available on the JADE website. The result of the typed command (输出命令的结果如下)is as follows.
C:\jade>java –classpath <JADE-classes> jade.Boot buyer:BookBuyerAgent
This is JADE snapshot - 2003/10/24 13:43:39
downloaded in Open Source, under LGPL restrictions,
at http://jade.cselt.it/
IOR:000000000000001149444C3A464950412F4D54533A312E300000000000000001000000000000 0060000102000000000A3132372E302E302E310009A600000019AFABCB0000000002BCE5528F0000 000800000000000000000A0000000000000100000001000000200000000000010001000000020501 000100010020000101090000000100010100
Agent container Main-Container@JADE-IMTP://IBM8695 is ready.
Hallo! Buyer-agent buyer@IBM8695:1099/JADE is ready.
The first part of the above output is the JADE disclaimer() that is printed out each time the JADE runtime is started. The IIOP address (in the form of an IOR) of the newly started platform follows. Finally the indication that a container called “Main-Container” is ready completes the JADE runtime startup.
When the JADE runtime is up our agent is started and prints its welcome message. The nickname of the agent is“buyer” as we specified on the command line. The platform name “IBM8695:1099/JADE” is automatically(自动地) assigned on the basis of the host and port you are running JADE on (see the JADE Administrator’s guide for assigning a name to the platform).
3.3 Agent termination(代理终止)
Even if it does not have anything else to do after printing the welcome message, our agent is still running. In order to make it terminate its doDelete() method(doDelect方法必须被调用) must be called. Similarly to the setup()
method that is invoked(祈求,调用) by the JADE runtime as soon as an agent starts and is intended to include agent
initializations, the takeDown() method is invoked just before an agent terminates and is intended to include agent clean-up operations.(和setup方法一样,一旦代理开始运行,setup方法被调用,目的还包括在setup方法中对代理初始化。
takeDown方法被调用仅仅是在代理终止之前,目的还包括在takeDown 方法中对代理进行清除的操作)
3.4 Passing arguments to an agent(对agent进行传参)
Agents may get start-up arguments specified (启动命令行上的指定参数)on the command line. These arguments can be retrieved,as an array of Object, by means of the getArguments() method of the Agent class(由代理类的getArguments方法对参数,参数作为对象数组,进行检索). As mentioned in chapter 2, we want our BookBuyerAgent to get the title of the book to buy as a command line argument. To achieve that we modify it as follows1.
import jade.core.Agent;
import jade.core.AID;
public class BookBuyerAgent extends Agent {
// The title of the book to buy
(注释1:the list of known seller agents to send requests to is fixed. In chapter 6 we will describe how to dynamically discover them.)
private String targetBookTitle;
// The list of known seller agents
private AID[] sellerAgents = {new AID(“seller1”, AID.ISLOCALNAME),
new AID(“seller2”, AID.ISLOCALNAME)};
// Put agent initializations here
protected void setup() {
// Printout a welcome message
System.out.println(“Hallo! Buyer-agent “+getAID().getName()+” is ready.”);
// Get the title of the book to buy as a start-up argument
Object[] args = getArguments();
if (args != null && args.length > 0) {
targetBookTitle = (String) args[0];
System.out.println(“Trying to buy “+targetBookTitle);
}
else {
// Make the agent terminate immediately
System.out.println(“No book title specified“);
doDelete();
}
}
// Put agent clean-up operations here
protected void takeDown() {
// Printout a dismissal message
System.out.println(“Buyer-agent “+getAID().getName()+” terminating.”);
}
}
Arguments on the command line are specified included in parenthesis and separated by spaces2. C:\jade>java jade.Boot buyer:BookBuyerAgent(The-Lord-of-the-rings)
This is JADE snapshot - 2003/10/24 13:43:39
downloaded in Open Source, under LGPL restrictions,
at http://jade.cselt.it/
IOR:000000000000001149444C3A464950412F4D54533A312E300000000000000001000000000000 0060000102000000000A3132372E302E302E310009A600000019AFABCB0000000002BCE5528F0000 000800000000000000000A0000000000000100000001000000200000000000010001000000020501 000100010020000101090000000100010100
Agent container Main-Container@JADE-IMTP://IBM8695 is ready.
Hallo! Buyer-agent buyer@IBM8695:1099/JADE is ready.
Trying to buy The-Lord-of-the-rings
4 AGENT TASKS – THE BEHAVIOUR CLASS(类得行为)
As mentioned in chapter 3, the actual job an agent has to do is typically carried out within “behaviours”.
A behaviour represents a task (一个行为代表一次任务)that an agent can carry out and is implemented (被实施) as an object of a class that extends jade.core.behaviours.Behaviour.
In order to make an agent execute the task implemented by a behaviour object (为了使一个行为对象能够让一个代理完成任务)it is sufficient (足够的)to add the behaviour to the agent(通过代理类的addBehaviour方法,对代理添加足够多的行为) by means of the addBehaviour() method of the Agent class.Behaviours can be added at any time: when an agent starts (in the setup() method) or from within other behaviours.
Each class extending Behaviour must implement the action() method, that (action方法实际上是保证行为执行完毕)actually defines the operations to be performed when the behaviour is in execution and the done() method (returns a boolean value), that specifies whether or not a behaviour has completed (指定一个行为是否已经完成)and have to be removed from the pool(从池中移除) of behaviours an agent is carrying out.
(注释2 When using JADE with the LEAP add-on, arguments are separated by colon (‘;’) instead of spaces.)
4.1 Behaviours scheduling(安排) and execution(执行)
An agent can execute several behaviours concurrently(同时发生的). However it is important to notice that scheduling of behaviours in an agent is not pre-emptive (先发制人的)(as for Java threads话题,题材) but cooperative(合作性的). This means that when a behaviour is scheduled for execution its action() method is called and runs until it returns.(意思就是,当一个行为被安排执行他调用的action方法,然后运行,直到返回)Therefore it is the programmer who defines(规定) when an agent switches (from the execution of a behaviour) to the execution of the next one(从一个转到另外一个).
Though requiring a small additional effort to programmers, this approach(接近,类似) has several advantages.
• Allows having a single Java thread per agent (that is quite important especially in environments with limited resources such as cell phones).
• Provides better performances since behaviour switch is extremely faster than Java thread switch. • Eliminates(消除) all synchronization issues (同时性,同期争端)between concurrent behaviours
accessing the same resources (this speed-up performances too) since all behaviours are executed by the same Java thread.
• When a behaviour switch occurs the status of an agent does not include any stack (堆积的)information and is therefore possible to take a “snapshot” of it. This makes it possible to implement important advanced features(特点) e.g. saving the status of an agent on a persistent storage for later resumption (agent persistency持久代理) or transferring(转移) it to another container for remote execution (agent mobility活动代理). Persistency and mobility are advanced JADE features and are outside the scope of this tutorial however.The path of execution of the agent thread3 is depicted(刻画) in Figure 2.
(注释3 In JADE there is a single Java thread per agent. Since JADE agents are written in Java, however, programmers may start new Java threads at any time if they need. If you do that, remember to pay attention since the advantages mentioned in this section are no longer valid(有效的). Figure 2. Agent Thread path of execution
Taking into account the described scheduling mechanism it is important to stress that a behaviour like that reported below prevents any other behaviour to be executed since its action() method never returns.
public class OverbearingBehaviour extends Behaviour {
public void action() {
while (true) {
// do something
}
}
public boolean done() {
return true;
}
}
When there are no behaviours available for execution the agent’s thread goes to sleep in order not to
consume CPU time. It is waken up as soon as there is again a behaviour available for execution.
4.2 One-shot behaviours(单次行为), cyclic behaviours (循环)and generic(一般) behaviours We can distinguish among three types of behaviour.
1) “One-shot” behaviours that complete immediately and whose action() method is executed only once.(“一炮”行为的定义:立即完成,他的action()方法只执行一次。
)
The (jade.core.behaviours.OneShotBehaviour) already implements the done() method by returning true (通过返回true, jade.core.behaviours.OneShotBehaviour已经实现了done()方法) and can be conveniently extended to (方便的扩展实现“一次行为”)implement one-shot behaviours. public class MyOneShotBehaviour extends OneShotBehaviour {
public void action() {
// perform operation X
}
}
Operation X is performed only once(操作X只执行一次).
2) “Cyclic” behaviours that never complete and whose action() method executes the same operations each time it is called.(循环行为的定义:永远不会完成,每当他的action()方法被调用时,都执行相同的操作。
)
The jade.core.behaviours.CyclicBehaviour already implements the done() method by returning false (通过返回false, jade.core.behaviours.CyclicBehaviour已经实现了done()方法)and can be conveniently extended to implement cyclic behaviours. (能够方便的扩展实现“循环行为”)
public class MyCyclicBehaviour extends CyclicBehaviour {
public void action() {
// perform operation Y
}
}
Operation Y is performed repetitively forever(永远反复操作Y) (until the agent carrying out the above behaviour terminates直到代理人进行上述行为终止).
3) Generic behaviours that embeds(嵌入) a status and execute different operations depending on that status. (一般行为:嵌入一个状态,根据状态执行不同的操作)
They complete when a given condition is met.(当满足给定的条件时,他们完成)
public class MyThreeStepBehaviour extends Behaviour {
private int step = 0;
public void action() {
switch (step) {
case 0:
// perform operation X
step++;
break;
case 1:
// perform operation Y
step++;
break;
case 2:
// perform operation Z
step++;
break;
}
}
public boolean done() {
return step == 3;
}
}
Operations X, Y and Z are performed one after the other and then the behaviour completes. JADE provides the possibility of combining simple behaviours together(为结合简单行为,创造一个复杂的行为提供了可能) to create complex behaviours.
This feature is outside the scope of this document however(但是这一特点不在此文献的范围内). Refer to(请参阅XX的细节) the Javadoc of the SequentialBehaviour, ParallelBehaviour and FSMBehaviour for the details.
4.3 Scheduling operations at given points in time(在给定的时间点安排操作)
JADE provides two ready-made classes (in the jade.core.behaviours package) by means of which it is possible to easily implement behaviours that execute certain operations at given points in time.(JADE提供两个类,这两个类得作用是:是容易完成“在给定的时间点执行确定的操作”这一行为成为可能)
下面介绍WakerBehaviour
1) The WakerBehaviour whose action() and done() methods are already implemented in such a way to execute the handleElapsedTimeout() abstract method after a given timeout (specified in the constructor) expires(到期). After the execution of the handleElapsedTimeout() method the behaviour completes.(其action()和done()方法已经实施,在“给定超时(指定构造函数)”到期后,以这样的一种方法去执行XX的抽象方法,执行XX方法后,行为完成)
public class MyAgent extends Agent {
protected void setup() {
System.out.println(“Adding waker behaviour”);
addBehaviour(new WakerBehaviour(this, 10000) {
protected void handleElapsedTimeout() {
// perform operation X
}
} );
}
}
Operation X is performed 10 seconds after the “Adding waker behaviour” printout appears.(当“XX行为”打印出现后,操作X执行10秒)
下面介绍TickerBehaviour
2) The TickerBehaviour whose action() and done() methods are already implemented in such a way to execute the onTick() abstract method repetitively waiting a given period (specified in the constructor) after each execution. A TickerBehaviour never completes.(其action()和done()方法已经实施,在每次执行后,等待一个“给定时期(指定构造函数)”的同时,以这样的一种方法去反复执行XX的抽象方法。
)
public class MyAgent extends Agent {
protected void setup() {
addBehaviour(new TickerBehaviour(this, 10000) {
protected void onTick() {
// perform operation Y
}
} );
}
}
Operation Y is performed periodically every 10 seconds.(每10秒钟定期操作Y)
4.4 Behaviours required in the book trading example(图书交易例子中需要的行为)
Having described the basic types(基本类型)of behaviour, let’s move now to anal yse which behaviours have to be carried out by the Book-buyer agent and Book-seller agent of our book trading example.
4.4.1 Book-buyer agent behaviours
As described in chapter 2, a Book-buyer agent periodically requests seller agents the book it was Instructed(被指示的) to buy. We can easily achieve that by using a TickerBehaviour that, on each
tick(在每个节拍), adds another behaviour (添加另一个行为)that actually deals with the request to seller agents(这个行为实际上是,解决对卖方代理的请求). Here is how the setup() method of our BookBuyerAgent class can be modified.(下面是怎样修改XX类得XX方法)
protected void setup() {
// Printout a welcome message
System.out.println(“Hallo! Buyer-agent “+getAID().getName()+” is ready.”);
// Get the title of the book to buy as a start-up argument
Object[] args = getArguments();
if (args != null && args.length > 0) {
targetBookTitle = (String) args[0];
System.out.println(“Trying to buy “+targetBookTitle);
// Add a TickerBehaviour that schedules a request to seller agents every minute
addBehaviour(new TickerBehaviour(this, 60000) {
protected void onTick() {
myAgent.addBehaviour(new RequestPerformer());
}
} );
}
else {
// Make the agent terminate
System.out.println(“No target book title specified“);
doDelete();
}
}
Note(注意) the use of the myAgent protected variable(注意使用被保护的变量): each behavior has a pointer to the agent that is executing it.
The RequestPerformer behaviour actually dealing with the request to seller agents will be described in chapter 5 where we will discuss agent communication.
4.4.2 Book-seller agent behaviours
As described in chapter 2, each Book-seller agent waits for requests from buyer agents and serves them.These requests (买方的请求,卖方可以通过要求买方提供一个书籍或者是购买命令的订单来获得)can be requests to provide an offer for a book or purchase orders. A possible design to achieve that is to make a Book-seller agent execute two cyclic behaviours(两次循环行为): one dedicated
to (专注于)serve requests for offer (为出售请求服务)and the other dedicated to serve purchase orders(购买订单服务). How actually incoming requests from buyer agents (怎样实际从买方代理那儿收到请求)are detected(发现) and served is described in chapter 5 where we will discuss agent communication.
Moreover we need to make the Book-seller agent execute a one-shot behaviour updating the catalogue of books available for sale(可供出售书籍的目录) whenever the user adds a new book from the GUI (不管用户什么时候增加一本新书,这本新书来自GUI). Here is how the BookSellerAgent class can be implemented (the OfferRequestsServer and PurchaseOrdersServer classes will be presented in chapter 5).
import jade.core.Agent;
import jade.core.behaviours.*;
import java.util.*;
public class BookSellerAgent extends Agent {
// The catalogue of books for sale (maps the title of a book to its price)
private Hashtable catalogue;
// The GUI by means of which the user can add books in the catalogue
private BookSellerGui myGui;
// Put agent initializations here
protected void setup() {
// Create the catalogue
catalogue = new Hashtable();
// Create and show the GUI
myGui = new BookSellerGui(this);
myGui.show();
// Add the behaviour serving requests for offer from buyer agents
addBehaviour(new OfferRequestsServer());
// Add the behaviour serving purchase orders from buyer agents
addBehaviour(new PurchaseOrdersServer());
}
// Put agent clean-up operations here
protected void takeDown() {
// Close the GUI
myGui.dispose();
// Printout a dismissal message
System.out.println(“Seller-agent “+getAID().getName()+” terminating.”);
}
/**
This is invoked by the GUI when the user adds a new book for sale
*/
public void updateCatalogue(final String title, final int price) {
addBehaviour(new OneShotBehaviour() {
public void action() {
catalogue.put(title, new Integer(price));
}
} );
}
}
The BookSellerGui class is a simple Swing GUI and is not presented here since it is outside the scope of this tutorial. Its code is available among the sources packaged with this tutorial.
5 AGENT COMMUNICATION – THE ACLMESSAGE CLASS
One of the most important features that JADE agents provide is the ability to communicate. The communication paradigm(交流模式) adopted (采用的是)is the asynchronous message passing(异步消息传送). Each agent has a sort of mailbox (the agent message queue) where the JADE runtime posts (这个邮箱是JADE运行时,传输由其他代理发出的消息的地方)messages sent by other agents. Whenever a message is posted in the message queue the receiving agent is notified(通知). If and when the agent actually picks up the message from the message queue to process(加工、处理) it is completely up to the programmer however.(be up to:完全依靠)
5.1 The ACL language
Messages exchanged by JADE agents have a format ,specified by the ACL language (有一定的版本,版本由ACL语言指定),(ACL语言由FIPA定义)defined by the FIPA () international standard for agent interoperability(operability:行的通的). This format (格式)comprises(包含,包括) a number of fields and in particular:
(1)The sender of the message(消息发送者)
(2)The list of receivers(接受器列表)
(3)The communicative intention (also called “performative”) indicating(指示) what the sender intends to achieve by sending the message(通过发送消息,XX预测发送者目的在于接受什么).The。