Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1256779
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类:

2006-08-27 09:44:07

在前面的例子中,agent所作的工作都定义在了setup中,实际上它具有的行为和执行的动作都应该定义在behavious类中,我们可以对生成behavios类的实例,然后将任务或者动作代码放在对behavious类中的action方法中,action方法是必须要有的。Behavious类还有很多子类,分别对应着不同类型的behavious,包括SimpleBehaviour,sequences behaviors ,parallel behaviors,cyclic behaviors等。
一个agent的行为表示它能够执行的任务,通过继承jade.core.behaviours.Behaviour来实现。然后在agent类中通过addBehaviour()方法将行为加入进来。当一个agent启动(通过setup()方法)后,行为可以在任何时间加入进来。
要定义Behaviour必须实现其action()方法,它定义了agent的执行时的实际动作,而done()方法指名了一个行为是否已执行完毕,是否要从行为池中删除。
一个agent可以并发执行多个behaviour。每个agent线程启动后执行的过程如下:
实例:在netbeans工程中编写下列程序,过程如前所描述。
package examplesbehaviours;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
/**
*
* @author gj
 */
public class SimpleAgent extends Agent {
    private class FourStepBehaviour extends Behaviour {
        private int step = 1;
 
        public void action() {
            switch (step) {
                case 1:
                // Perform operation 1: print out a message
                    System.out.println("Operation 1");
                    break;
                case 2:
                    // Perform operation 2: Add a OneShotBehaviour
                    System.out.println("Operation 2. Adding one-shot behaviour");
                    myAgent.addBehaviour(new OneShotBehaviour(myAgent) {
                        public void action() {
                            System.out.println("One-shot");
                        }
                    });
                    break;
                case 3:
                    // Perform operation 3: print out a message
                    System.out.println("Operation 3");
                    break;
                case 4:
                // Perform operation 3: print out a message
                    System.out.println("Operation 4");
                    break;
             }
             step++;
        }
 
        public boolean done() {
            return step == 5;
        }
 
        public int onEnd() {
            myAgent.doDelete();
            System.out.println("Finished!");
            return super.onEnd();
        }
    }    // END of inner class FourStepBehaviour
    /** Creates a new instance of SimpleAgent */
    public SimpleAgent() {
    }
    protected void setup() {
        System.out.println("Agent "+getLocalName()+" started.");
 
        // Add the CyclicBehaviour
        addBehaviour(new CyclicBehaviour(this) {
            public void action() {
                System.out.println("Cycling");
            }
        }
        );
 
        // Add the generic behaviour
        addBehaviour(new FourStepBehaviour());
    }
   
  }
运行参数为 jade.Boot –gui guojie: examplesbehaviours.SimpleAgent
输出结果:Agent guojie started.
Cycling
Operation 1
Cycling
Operation 2. Adding one-shot behaviour
Cycling
Operation 3
One-shot
Cycling
Operation 4
Finished!
 
阅读(3305) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~