Creating Agents without XML
Todd Wright
twright at bbn.com
Wed Mar 26 17:47:42 EDT 2008
Christian Bernardt wrote:
> Hi there,
>
> I am trying to figure out, how to create an agent society without
> defining the agents in a xml file. This way of managing societies is
> described on page 18 of the CAD. (section 3.2.1 Initialization). I have
> created a number of empty Nodes representing the spaces, the agents
> shall be created in. I would like to create agents in these societies,
> while cougaar is running.
> 1.)How can I add/remove agents to/from the nodes during runtime?
You can use the blackboard-based "AddTicket" approach, as discussed in:
http://www.cougaar.org/pipermail/cougaar-developers/2007-May/002349.html
This approach works from any agent or even a remote node.
Or, if you only want to add an agent to the local node, you can call the
"addAgent" method. This requires a plugin in the node agent. The above
"AddTicket" code uses the "addAgent" method. Here's an example:
------------------------------------------------------------------------------
package whatever;
import org.cougaar.core.agent.AgentContainer;
import org.cougaar.core.component.ComponentSupport;
import org.cougaar.core.mts.MessageAddress;
import org.cougaar.core.node.NodeControlService;
import org.cougaar.core.service.LoggingService;
import org.cougaar.core.service.ThreadService;
import org.cougaar.core.thread.Schedulable;
public class AddAgent extends ComponentSupport {
private LoggingService log;
private ThreadService ts;
private AgentContainer ac;
public void setLoggingService(LoggingService log) { this.log = log; }
public void setThreadService(ThreadService ts) { this.ts = ts; }
public void setNodeControlService(NodeControlService ncs) {
ac = (ncs == null ? null : ncs.getRootContainer());
}
public void start() {
super.start();
// add our agent after 5 seconds. For threading info see:
//
http://www.cougaar.org/pipermail/cougaar-developers/2004-October/001491.html
Runnable r = new Runnable() {
public void run() {
log.shout("Adding agent Foo");
ac.addAgent(MessageAddress.getMessageAddress("Foo"));
}
};
Schedulable s = ts.getThread(this, r);
log.shout("Waiting 5 seconds");
s.schedule(5*1000);
}
}
------------------------------------------------------------------------------
"MyNode.xml":
------------------------------------------------------------------------------
<?xml version='1.0'?>
<node name="Node1">
<component class="whatever.AddAgent"/> <!-- must be in the node agent -->
</node>
------------------------------------------------------------------------------
"Foo.xml":
------------------------------------------------------------------------------
<?xml version='1.0'?>
<agent name="Foo">
<!-- anything, e.g. the hello-world plugin in the demo-hello.zip -->
<component class="org.cougaar.demo.hello.HelloPlugin"/>
</agent>
------------------------------------------------------------------------------
Logging output:
------------------------------------------------------------------------------
2008-03-26 17:17:01,410 SHOUT [XMLComponentInitializerServiceProvider] -
Initializing node "Node1" from XML file "MyNode.xml"
2008-03-26 17:17:11,427 WARN [AgentLoader] - Node1: Node Node1 contains zero
agents
2008-03-26 17:17:11,444 SHOUT [AddAgent] - Node1: Waiting 5 seconds
2008-03-26 05:17:16,441 SHOUT [DOTS] - .
2008-03-26 17:17:16,449 SHOUT [AddAgent] - Node1: Adding agent Foo
2008-03-26 17:17:16,694 SHOUT [HelloPlugin] - Foo: Hello, world!
------------------------------------------------------------------------------
Note that you must have a "Foo.xml" file in the Cougaar config path (e.g. the
current directory). There's an enhancement request to allow an in-memory
specification of what plugins to put in the agent, but that hasn't been
implemented yet.
> 2.)Can I create such agents from outside of a node? If, how?
You'll need some way to tell the node to create the agent, e.g. a servlet.
From there you can use either of the above techniques:
1) Use the blackboard-based "AddTicket"
or
2) Use the service-based "addAgent" method
> 3.)How can I address the nodes that I have initialized in the xml file?
You name the agents when you create them, e.g. the above "Foo" example. You
can send blackboard relays to them.
Todd
>
> I am totally new to cougaar, can anybody give me a jump start on how to
> achieve these things?
>
> Thanks in advance. Chris
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Cougaar-developers mailing list
> Cougaar-developers at cougaar.org
> http://cougaar.org/mailman/listinfo/cougaar-developers
More information about the Cougaar-developers
mailing list