[rules-users] Instatiating Global Variables

Joshua Undesser jundesse at cdmtech.com
Fri Nov 2 17:47:48 EDT 2007


I am having a few issues with Global Variables and was wondering if someone
could possibly explain them to me.....

I am coming from a CLIPS/JESS background where globals are very straight
forward.  ie (global ?*FAMILY_NAME* = "SMITH") and you can use it everywhere
after that.   You can declare them anywhere you want(ie outside/inside
rules, inside a function call), use them whenever you want (within the
LHS/RHS or in a function) and modify them whenever you want (although you
shouldn't).

In Drools however, they seem overly complicated.

For instance.....I can't declare and initialize a global in the same line.  
IE you can't do this....  global String FAMILY_NAME = "SMITH";
Instead you have to just declare the global as.....global String
FAMILY_NAME.

Then after it's declared it can be set.    Although for the life of me,
setting it is not straight forward either.  In a different thread on this
list someone suggested doing the following....ie making sure the salience
number is the highest of any rule forcing it to always fire first....

rule "Initialize Rule" salience 100

when
	eval(FAMILY_NAME == null);
then
	WorkingMemory wm = drools.getWorkingMemory();
	String familyName = "SMITH";
	wm.setGlobal("FAMILY_NAME", familyName);
	System.out.println("FAMILY_NAME =  " + FAMILY_NAME);
end

this does not seem to set the Global Variable value.    The result
is...FAMILY_NAME = null.   

and any other rule that I have in place that utilizes this global doesn't
fire.

ie
rule "First Rule to Fire"
	when
		eval(FAMILY_NAME != null)
	then
		.....
end

This never fires....



However, if through my java application (not in a DRL file), I call 


......code.....
workingMemory.setGlobal("FAMILY_NAME", familyName);
fireAllRules();
....more code....

then my "Firest Rule To Fire" rule fires as expected.


There has to be something I am not fully understanding here because it
doesn't seem right that I can't create and set the Global all in one step,
or the fact that I can't set the variable value in a rule, but only through
a java app.   

I also don't understand why the Globals can't be used directly by functions
and why they have to be passed in as a variable to the function.  If the
function and the Global are defined within a specific working memory, then
it should be available to all rules and functions within that same working
memory.  Similar to Java Class Member Data.   The member data is available
anywhere in that class.   

If anyone can help shed some light on the subject I would be very grateful!   


Joshua


-- 
View this message in context: http://www.nabble.com/Instatiating-Global-Variables-tf4740897.html#a13556883
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list