首页 > 软件编程 > JADE Agent Parameters

JADE Agent Parameters

2010年12月12日 发表评论 阅读评论

Agent Parameters

It is often useful to be able to pass parameters to agents. This is described in the Jade Programming Tutorial (sect. 3.4). On a command line, the parameters are placed in a list seperated by spaces after the “name:class” agent specifier. For example:

% java jade.Boot fred:ParamAgent(3 “Hi there”)

Here fred will be passed 2 parameters: an integer and a string. In standard Java, we are used to retrieving command arguments from a String array parameter to the static method main. With JADE, retrieving arguments is a bit different because the same mechanism is used both to pass arguments from a command line and to pass arguments to agents created by programming.

In Jade, the arguments are obtained by calling the method getArguments which returns an array of Objects which must be cast to Strings (in this case). Here is an example of a ParamAgent which can retrieve the arguments “3” and “Hi there” shown above.

/**************************************************************   
     ParamAgent.java:  Retrieving parameters
 **************************************************************/   
 
     import jade.core.Agent;
 
     public class ParamAgent extends Agent 
     { 
        protected void setup() 
        { 
            Object[] args = getArguments();
            String s;
            if (args != null) {
                for (int i = 0; i<args.length; i++) {
                    s = (String) args[i];
                    System.out.println("p" + i + ": " + s);
                }
 
                // Extracting the integer.
                int i = Integer.parseInt( (String) args[0] );
                System.out.println("i*i= " + i*i);
            }
        }
     }

The command line shown above doesn’t work under UNIX or Mac OSX.

jean% java jade.Boot fred:ParamAgent(1 “Hi there”)

tcsh: Badly placed ()’s.

With these systems, each agent specifier (name, class & argument list) must be quoted.

% java jade.Boot ‘fred:ParamAgent(3 “fred toto”)’

p0: 3

p1: fred toto

i*i= 9

Reversing single and double quotes can give surprising results; in this case: 3 arguments !!!

jean% java jade.Boot “fred:ParamAgent(3 ‘fred toto’)”

p0: 3

p1: ‘fred

p2: toto’

i*i= 9

Passing arguments to newly created Agents

Similarly, an array of arguments can be provided for new agents in the third parameter of createNewAgent. Here is how we could create a ParamAgent with the same name and arguments as in our earlier example. Note that the arguments are passed as Objects; thus, simple types must be converted to Strings or Wrapper classes.

Object [] args = new Object[2];
args[0] = "3";
args[1] = "Allo there";
 
String name = "Fred" ;
AgentContainer c = getContainerController();
try {
       AgentController a = c.createNewAgent( name, "ParamAgent", args );
       a.start();
     }
catch (Exception e){}

Responder Agent

// ------------------------------------------------------------
//   ParamAgent:   An Agent receiving parameters             
//
//   Usage:    % javac ParamAgent.java
//             % java jade.Boot  fred:ParamAgent(3 "Allo there")
//
// ... on UNIX, the agent specifier and arguments must be quoted:
//
//             % java jade.Boot 'fred:ParamAgent(3 "Allo there")'
// ------------------------------------------------------------
 
 import jade.core.Agent;
 
 public class ParamAgent extends Agent 
 { 
	protected void setup() 
	{ 
		Object[] args = getArguments();
		String s;
		if (args != null) {
			for (int i = 0; i<args.length; i++) {
				s = (String) args[i];
				System.out.println("p" + i + ": " + s);
			}
 
			int i = Integer.parseInt( (String) args[0] );
			s     = (String) args[1];
 
			System.out.println("i*i= " + i*i);
			System.exit(1);
		}
	}
 }

Reference:

此文系静风说原创,转载请注明出处: http://www.jingfengshuo.com/archives/212.html

分类: 软件编程 标签: 浏览量:1,536 次阅读
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.