agents-jade

How to pass Agent as argument


How it is possible to Pass agent as argument to another agent? I know that we should pass arguments as an Object to createNewAgent method But I don't know how to retrieve recently created agent which exists in agent controller.

/**
 * 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();
    }

}

And here is Searcher Agent which needs argument:

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));
    }

}

Solution

  • It's not needed to pass Agent as argument because you can have access to other agents from any Agent class like this:

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

    This will give you the agents that are existing .The first argument is the agent's name given while creating them.