如何将 Agent 作为参数传递
How to pass Agent as argument
如何将代理作为参数传递给另一个代理?
我知道我们应该将参数作为对象传递给 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();
}
}
这是需要参数的搜索器代理:
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));
}
}
不需要将 Agent 作为参数传递,因为您可以像这样从任何 Agent class 访问其他代理:
final AID[] otherAgents=
{
new AID("otherAgents0", AID.ISLOCALNAME),
new AID("otherAgents1", AID.ISLOCALNAME),
new AID("otherAgents2", AID.ISLOCALNAME),
...
};
这将为您提供现有的代理。第一个参数是创建它们时给出的代理名称。
如何将代理作为参数传递给另一个代理?
我知道我们应该将参数作为对象传递给 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();
}
}
这是需要参数的搜索器代理:
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));
}
}
不需要将 Agent 作为参数传递,因为您可以像这样从任何 Agent class 访问其他代理:
final AID[] otherAgents=
{
new AID("otherAgents0", AID.ISLOCALNAME),
new AID("otherAgents1", AID.ISLOCALNAME),
new AID("otherAgents2", AID.ISLOCALNAME),
...
};
这将为您提供现有的代理。第一个参数是创建它们时给出的代理名称。