JADE学习笔记之三:行为

本文介绍JADE框架中Agent行为的定义与使用方法。通过一个四步骤行为的实例演示了如何在一个Agent中添加和执行不同类型的Behavior,包括CyclicBehaviour和OneShotBehaviour,并展示了这些行为如何交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
一个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!
 
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值