Jade Library-代理容器之间的移动性

时间:2012-03-22 12:06:13

标签: java containers agent agents-jade mobility

我写了一段代码,代表一个代理人使用jade library穿越容器。我的代理有一个Cyclic Behavior,它使用简单的switch-case语句来跨容器传输。它运行在主要容器上。然后去容器1'然后去容器2'然后去容器1'等等! 这个问题就在这里,当它想要回来时,它并没有!没有关于未知Container或类似内容的错误。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyAgentPool;

import jade.core.Agent;
import jade.core.ContainerID;
import jade.core.behaviours.CyclicBehaviour;

/**
 *
 * @author King Hadi
*/
public class MyAgent00 extends Agent {       
@Override
protected void takeDown() {
    // TODO Auto-generated method stub
    super.takeDown();
    System.out.print("goodbye!");
}

@Override
protected void setup() {
    // TODO Auto-generated method stub
    super.setup();
    System.out.println("Hello I'm " + this.getLocalName());
    this.addBehaviour(new MyBehaviour());
   }
}

class MyBehaviour extends CyclicBehaviour {

private int step = 0;

@Override
public void action() {
    // TODO Auto-generated method stub
    switch (step) {
        case 0: {
            System.out.println("step variable is: "+ step);
            step++;
            ContainerID destination = new ContainerID();
            destination.setName("Container-2");                                
            System.out.println("waiting 2 seconds! before traveling ... ");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                

            myAgent.doMove(destination);
            break;
        }
        case 1: {
            System.out.println("step variable is: "+ step);
            step++;
            ContainerID destination1 = new ContainerID();
            destination1.setName("Container-1");                
            System.out.println("waiting 2 seconds! before traveling ... ");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                

            myAgent.doMove(destination1);
            break;
        }
        case 2: {
            System.out.println("step variable is: "+ step);
            step--;
            ContainerID destination2 = new ContainerID();
            destination2.setName("Container-2");                                
            System.out.println("waiting 2 seconds! before traveling ...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                                
            myAgent.doMove(destination2);
            break;
        }
        default: {
            System.out.println("step variable is: "+ step);
            step = 0;
            ContainerID destination = new ContainerID();
            destination.setName("Main-Contianer");
            myAgent.doMove(destination);
            System.out.println("waiting 2 seconds! before traveling ...");

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                
            break;
        }
    }
}
}

有谁知道为什么这段代码不起作用? 谢谢! :)

1 个答案:

答案 0 :(得分:0)

您在Default Switch语句中犯了拼写错误:

destination.setName("Main-Contianer");

应该是:

destination.setName("Main-Container");