Author: Dick Cowan, Menehune Software Inc.
Email: rm.cowan@verizon.net
Last update: April 15, 2010.
Java platform: Java Standard Edition version 5 or later
JADE version 4.0 and above.
JADE provides a simple mechanism to pass arguments to agents via the command-line or via the in-process interface.
For instance, the following command line passes 3 arguments to the agent FooAgent:
java jade.Boot foo:FooAgent(1,arg2,argument3)
the three arguments (separated by commas ',') can then be extracted by the
FooAgent code via a simple call to the Agent method getArguments()
that returns an array of type java.lang.Object
.
public void setup() { Object[] args = getArguments(); String arg1 = args[0].toString(); // this returns the String "1" String arg2 = args[1].toString(); // this returns the String "arg2" String arg3 = args[2].toString(); // this returns the String "argument3" }
The same arguments can be passed via the in-process interface as follows:
Object args = new Object[3]; args[0] = "1"; args[1] = "arg2"; args[3] = "argument3"; AgentController dummy = ac.createNewAgent("foo", "FooAgent", args);
It should be noticed that when using the in-process interface the type of arguments is not limited to String and whatever Java Object can be passed.
The command line can also be effectively used in order to pass general properties to all the agents in the system. For instance, the following command line sets to 3 the value of the property named verbosity:
java jade.Boot -verbosity 3 foo:FooAgent(1,arg2,argument3)
The same property can be set also via the in-process interface, in particular by setting that value in the Profile object:
Profile p = new ProfileImpl(); p.setParameter("verbosity","3"); AgentContainer ac = rt.createAgentContainer(p);
In both cases, all agents can then get that value via the getProperty
method call provided by the jade.core.Agent
class:
String verbosity = getProperty("verbosity", "0");
However, attempting to pass other than a few simple arguments via the command line is difficult. Particularly trying to do so in a platform neutral manner.
To overcome this limitation the Agents for Mobility group at HP Labs created a small framework of properties oriented classes that enable a richer and more flexible method of passing arguments to agents. Since version 4.0 this class framework has been simplified so that all extended properties management features have been embedded into a single class called ExtendedProperties.
The ExtendedProperties class extends the usual Properties class adding the following features
1) Support for referencing System as well as Environment Variables into properties. For instance let's assume you have defined the CLASSPATH environment variable to c:\myProject\classes;c:\jade\lib\jade.jar. If you insert the following property in a properties file
classpath-message = Classpath is ${CLASSPATH}
and you load this property file into an ExtendedProperties object, getting the value of the classpath-message property would result in
Classpath is c:\myProject\classes;c:\jade\lib\jade.jar
2) Similarly it is possible to reference other properties in the same properties file as exemplified below:
argument = 3
agents = a1:myPackage.MyClass(${argument}) \
a2:myPackage.MyClass(${argument}) \
a3:myPackage.MyClass(${argument}) \
a4:myPackage.MyClass(${argument}) \
a5:myPackage.MyClass(${argument}) \
a6:myPackage.MyClass(${argument}) \
allows you to change the argument to pass to all your agents by just modifying the value of the argument property.
3) Suppport for importing properties specified in another properties file. This can be done by means of the import property as exemplified below
File props1.txt
--------------------
port = 1111
host = avalon.tilab.com
# Import properties from file props2.txt
import = props2.txt
File props2.txt
--------------------
services = jade.core.replication.MainReplicationService
backupmain = true
The above situation is fully equivalent to having a single property file with the following content:
port = 1111
host = avalon.tilab.com
services = jade.core.replication.MainReplicationService
backupmain = true
4) Support for extracting prefixed properties. Given an ExtendedProperties object, by means of the extractSubset() method, It is possible to copy in a new ExtendedProperties object all properties whose keys start with a given prefix. The prefix is automatically removed. For instance let's assume you have the following properties
port = 1111
host = avalon.tilab.com
server.foo = x
server.bar = y
Calling the extractSubset() method would create a new ExtendedProperties object including the following properties:
foo = x
bar = y