Joshua
Globals in drools are designed to be a way for applications to expose
time-constant values to rules, and also a way to make application call backs
available to rules (like service methods, etc). It is not designed to be a
data sharing mechanism between rules, as we believe if you need to reason
over data, the data should be inserted into the working memory as facts.
Having said that, I agree that for the use case you are describing, the
management of globals is not straight forward. For instance, I do believe we
need to work around technical limitations to make globals available to
functions. What I am not so sure is if we can or should do is allow rules to
change global values and cause the engine to re-test constraints using the
globals, since in this case, globals would behave exactly like facts, and as
so, why not use facts instead of globals?
Regards,
Edson
2007/11/2, Joshua Undesser <jundesse(a)cdmtech.com>:
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.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
Software Engineer - JBoss Rules Core Developer
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @
www.jboss.com