<br> Joshua<br><br> 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.
<br><br> 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?
<br><br> Regards,<br> Edson<br><br> <br><br><div><span class="gmail_quote">2007/11/2, Joshua Undesser <<a href="mailto:jundesse@cdmtech.com">jundesse@cdmtech.com</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>I am having a few issues with Global Variables and was wondering if someone<br>could possibly explain them to me.....<br><br>I am coming from a CLIPS/JESS background where globals are very straight<br>forward. ie (global ?*FAMILY_NAME* = "SMITH") and you can use it everywhere
<br>after that. You can declare them anywhere you want(ie outside/inside<br>rules, inside a function call), use them whenever you want (within the<br>LHS/RHS or in a function) and modify them whenever you want (although you
<br>shouldn't).<br><br>In Drools however, they seem overly complicated.<br><br>For instance.....I can't declare and initialize a global in the same line.<br>IE you can't do this.... global String FAMILY_NAME = "SMITH";
<br>Instead you have to just declare the global as.....global String<br>FAMILY_NAME.<br><br>Then after it's declared it can be set. Although for the life of me,<br>setting it is not straight forward either. In a different thread on this
<br>list someone suggested doing the following....ie making sure the salience<br>number is the highest of any rule forcing it to always fire first....<br><br>rule "Initialize Rule" salience 100<br><br>when<br> eval(FAMILY_NAME == null);
<br>then<br> WorkingMemory wm = drools.getWorkingMemory();<br> String familyName = "SMITH";<br> wm.setGlobal("FAMILY_NAME", familyName);<br> System.out.println("FAMILY_NAME = " + FAMILY_NAME);
<br>end<br><br>this does not seem to set the Global Variable value. The result<br>is...FAMILY_NAME = null.<br><br>and any other rule that I have in place that utilizes this global doesn't<br>fire.<br><br>ie<br>rule "First Rule to Fire"
<br> when<br> eval(FAMILY_NAME != null)<br> then<br> .....<br>end<br><br>This never fires....<br><br><br><br>However, if through my java application (not in a DRL file), I call<br>
<br><br>......code.....<br>workingMemory.setGlobal("FAMILY_NAME", familyName);<br>fireAllRules();<br>....more code....<br><br>then my "Firest Rule To Fire" rule fires as expected.<br><br><br>There has to be something I am not fully understanding here because it
<br>doesn't seem right that I can't create and set the Global all in one step,<br>or the fact that I can't set the variable value in a rule, but only through<br>a java app.<br><br>I also don't understand why the Globals can't be used directly by functions
<br>and why they have to be passed in as a variable to the function. If the<br>function and the Global are defined within a specific working memory, then<br>it should be available to all rules and functions within that same working
<br>memory. Similar to Java Class Member Data. The member data is available<br>anywhere in that class.<br><br>If anyone can help shed some light on the subject I would be very grateful!<br><br><br>Joshua<br><br><br>--<br>
View this message in context: <a href="http://www.nabble.com/Instatiating-Global-Variables-tf4740897.html#a13556883">http://www.nabble.com/Instatiating-Global-Variables-tf4740897.html#a13556883</a><br>Sent from the drools - user mailing list archive at
<a href="http://Nabble.com">Nabble.com</a>.<br><br>_______________________________________________<br>rules-users mailing list<br><a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users">
https://lists.jboss.org/mailman/listinfo/rules-users</a><br></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> Software Engineer - JBoss Rules Core Developer<br> Office: +55 11 3529-6000<br> Mobile: +55 11 9287-5646
<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a>