Drools: Using globals defined at session creation for newly inserted rules
Right, I'm running Drools in a ordering program I'm making. I was asked to make rules loadable during run of the program via JMX. I take these in as a String and add them to a StatefulKnowledgeSession's knowledgebase. So far all is well. I'm getting a simple test rule to load just fine and without incident.
However, when I start up the program I load a number of fixed global variables, for example a Logger. If I attempt to load a rule referencing these globals via JMX it says
com/hipdm/messaging/Rule_Test_0.java (7:704) : logger cannot be resolved
Is there a way to make this global visible to newly loaded rules? I have entered it into the same package as the rules loaded at startup.
The full rule:
package com.hipdm.messaging
import com.hipdm.messaging.model.DummyOrderMessage
rule "Test"
when $msg : DummyOrderMessage( processed == false ) from entry-point "Message stream"
then
modify($msg) {process()};
logger.info("Order proc开发者_开发问答essed by rule engine");
end
Any ideas what may be wrong and how I can make my globals visible after the initial session creation?
As far as I know the globals must be 'imported' into the rule file to make them visible. There exists a special keyword in DRL: global. To do that add the following line after the import line in your rules.
global <full-class-name> <name-of-global-variable>
So if you are using a logger (assuming it is a log4j-logger) you would add:
global org.apache.log4j.Logger logger
Of course on the other end in the Java code the global must be set to the working memory, but I guess you managed to do that. In any case:
workingMemory.setGlobal("logger", someLogger);
Note that the 'logger' keyword must be exactly the same in the Java code and in your rules.
精彩评论