【问题标题】:How to pass Agent as argument如何将代理作为参数传递
【发布时间】:2021-01-14 13:58:43
【问题描述】:

如何将代理作为参数传递给另一个代理? 我知道我们应该将参数作为对象传递给createNewAgent 方法但我不知道如何检索代理控制器中存在的最近创建的代理。

/**
 * create Three Collector agents
 */
private void _createCollectorAgent(AgentContainer agentContainer, AgentController[] controller, Map<String, String> agentNames, GUI gui) {
    try {
        Object[] arg = new Object[1];
        arg[0] = gui;

        for (int i = 0; i <= 2; i++) {
            controller[i] = agentContainer.createNewAgent(agentNames.get(String.format("CollectorAgent%d", i)), CollectorAgent.class.getName(), arg);
            controller[i].start();
        }
    } catch (StaleProxyException e) {
        e.printStackTrace();
    }
}

private void _createSearcherAgent(AgentContainer agentContainer, AgentController[] controller, Map<String, String> agentNames, GUI gui) {
    try {

        List<AID> lstCollectors = new ArrayList<>(3);
        lstCollectors.add( ?);//How get recently created agent inorder to pass in argument
        lstCollectors.add( ?);
        lstCollectors.add( ?);
        Object[] args = new Object[2];
        args[0] = gui;
        args[1] = lstCollectors;
        controller[3] = agentContainer.createNewAgent(agentNames.get("SearcherAgent"), SearcherAgent.class.getName(), args);
        controller[3].start();
    } catch (StaleProxyException e) {
        e.printStackTrace();
    }

}

这里是需要参数的 Searcher Agent:

public class SearcherAgent extends Agent implements AgentEntity {

    @Override
    protected void setup() {
        super.setup();
        System.out.println("Setup of SearcherAgent called ");
        GUI gui = (GUI) getArguments()[0];
        AID[] lstCollectors= (AID[]) getArguments()[1];//Here is Ok 
        addBehaviour(new SearchingBehaviour(this, 1000,gui,lstCollectors));
    }

}

【问题讨论】:

    标签: agents-jade


    【解决方案1】:

    不需要将 Agent 作为参数传递,因为您可以从任何 Agent 类访问其他代理,如下所示:

    final AID[] otherAgents=
            {
               new AID("otherAgents0", AID.ISLOCALNAME),
               new AID("otherAgents1", AID.ISLOCALNAME),
               new AID("otherAgents2", AID.ISLOCALNAME),
               ...
            };
    

    这将为您提供现有的代理。第一个参数是创建代理时给出的代理名称。

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 2015-06-05
      • 2012-10-18
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多