Drools 3.1M中global的使用问题

做了个小实验:想周期性的去Ping一个服务器,然后计算服务器可用率,比方说两次中ping通了一次,可用率就是0.5。但这需要在drl文件中针对不同情况(on/off)对变量进行设置,并且保证在每次ping的过程中变量值可以累积。这怎么做呀??

我想这样的:在调用的主程序中assert一个含有俩变量的类对象,
ComputeUsage usage = new ComputeUsage();
		mem.assertObject(usage);


然后在drl中声明它为global
global ComputeUsage usage

在对应的rule中设置usage的值,
rule "Server On"
	salience 2000
	when
		#conditions
		server : Server(status == true)
	then 
		#actions
		System.out.println(server);
		//usage.setAvail(usage.getAvail()+1);
		//usage.setSum(usage.getSum()+1);
		computeUsage(server);
end

rule "Server Off"
	#include attributes such as "salience" here...
	salience 2000
	when
		#conditions
		server : Server(status == false)
	then 
		#actions
		System.out.println(server);
		//usage.setSum(usage.getSum()+1);
		computeUsage(server);
		// Inform the mangers
end


最后用一个函数对其进行计算并输出。
function void computeUsage(Server server) {
	System.out.println("The usage rate of the server ["+server.getHost()+"] is {"(usage.getAvail()/usage.getSum())"}\n");
	//System.err.println();
}


可是老是不正确。有如下错误:
Server [127.0.0.1]'s status is [false];    The Response time is (0)ms
java.lang.NullPointerException
	at audion.check.Rule_Server_Off_0.consequence(Rule_Server_Off_0.java:9)
	at audion.check.Rule_Server_Off_0ConsequenceInvoker.evaluate(Rule_Server_Off_0ConsequenceInvoker.java:23)
	at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:467)
	at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:431)
	at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:360)
	at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:341)
Chief Manager Haibin has been informed about the failure of server [127.0.0.1].

	at audion.checkserver.PingServer.main(PingServer.java:62)
Exception in thread "main" org.drools.spi.ConsequenceException: java.lang.NullPointerException
	at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:471)
	at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:431)
	at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:360)
	at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:341)
	at audion.checkserver.PingServer.main(PingServer.java:62)
Caused by: java.lang.NullPointerException
	at audion.check.Rule_Server_Off_0.consequence(Rule_Server_Off_0.java:9)
	at audion.check.Rule_Server_Off_0ConsequenceInvoker.evaluate(Rule_Server_Off_0ConsequenceInvoker.java:23)
	at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:467)
	... 4 more

不知道怎么回事啊?这个global和function到底怎么用啊?谁可以给个例子啊?谢谢啦!

PS:我只是个新手,不大会。还有这个例子当然可以不用Drools做,可能会更简单。不过我只是想试试这种工具。

你可能感兴趣的:(java,应用服务器,网络应用,脚本,360)