Jade 创建agent时传参数

1 篇文章 0 订阅

通过Agent创建Agent

AgentContainer agentContainer=agent.getContainerController();
		AgentController agentController;
		try {
			for (Guardinformation guardinformation:map.getGuardinfos()){
				Object[] objects = new Object [3];
				objects[0]=guardinformation.getX();
				objects[1]=guardinformation.getY();
				objects[2]=guardinformation.getDirection();
				agentController=agentContainer.createNewAgent(guardinformation.getName(), "com.gc.agent.GuardAgent", objects);
				agentController.start();
		}
		} catch (StaleProxyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

关键是创建的时候不会立即获得参数,而是在创建后才能获得参数,而且即使存在带参数的构造函数也是不会执行的。jade只会执行不带参数的构造函数。

获得参数

	protected void setup() {
		//接收参数
		Object[] objs=getArguments();//获得参数
		this.x=(int) objs[0];
		this.y=(int) objs[1];
		this.direction=(int) objs[2];
		// TODO Auto-generated method stub
		addBehaviour(new Guardheartbeat(this, 1000));
		addBehaviour(new GuardReceive(this));
	}
注意构造函数里面是不能获得参数的。


更多关于参数的内容

How to use arguments or properties to configure your agent.

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值