<HTML><BODY>
Hello I tried what you proposed, it didn't do the trick. Then i reread the documentation and discovered:<br>"Globals are not designed to share data between rules and they should never be used for that purpose. <br>Rules always reason and react to the working memory state, so if you want to pass data from rule to rule, <br>assert the data as facts into the working memory."<div><br></div><div>Sadly, I was trying to do just that...<br><br>I am sorry for the question in first place.<br>I want to ask something (just to be sure).<br><br>If I want to share data between rules (in my case transitional calculation results, that are missing in my model),</div><div>the only way to do so is to pass them into some temporary facts?<br><br>Thanks again</div><div><br></div><div>Best Regards,<br>Milen<br><br>




 -------- Оригинално писмо --------
<br>От: Wolfgang Laun wolfgang.laun@gmail.com
<br>Относно: Re: [rules-users] Where is my understanding of Drools global
 variables wrong?
<br>До: Rules Users List &lt;rules-users@lists.jboss.org&gt;
<br>Изпратено на: Понеделник, 2014, Февруари 3 18:09:26 EET
<br><br>The typical misunderstanding is that a global is a "variable" in the<br>
usual sense of the word. It is not.<br>
<br>
When you set a global, you store an object in a map, using the<br>
global's name as a key. Using a global in DRL code perfidiously and<br>
conveniently hides the way the global is made available to the code in<br>
a rule when you use the global's name just like any other variable:<br>
You write the global's name, but you use a local copy.<br>
<br>
If you intend to change the global existence of a global you'll have<br>
to use the API that's used to set up a global in the first place: get<br>
the session object, call setGlobal...<br>
<br>
Cheers<br>
Wolfgang<br>
<br>
<br>
<br>
<br>
On 03/02/2014, milen igrachev &lt;<a href="javascript: internSendMess('igrachev@abv.bg')">igrachev@abv.bg</a>&gt; wrote:<br>
&gt; Hello, for the first time I have the need to define a global variables in a<br>
&gt; DRL file, and from what it seems from the documentation it shall be a<br>
&gt; variable visible to all my rules in the<br>
&gt; DRL file. However I don't see what I do wrong, but the modifications made by<br>
&gt; one rule are not visible to the others.  Here is illustration of what I<br>
&gt; have:<br>
&gt;<br>
&gt; [Fact model code]<br>
&gt;  public class TestPOJO {            private int fieldOne;            private String<br>
&gt; fieldTwo;                        public int getFieldOne() {...}            public void<br>
&gt; setFieldOne(int fieldOne) {...}            public String getFieldTwo() {...}           <br>
&gt; public void setFieldTwo(String fieldTwo) {...}  }<br>
&gt;   [DRL rule code]<br>
&gt;  global java.lang.Double myGlobal;  rule "Your First Rule"            salience 2<br>
&gt;<br>
&gt;  when<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  TestPOJO(fieldOne &gt; 10)<br>
&gt;<br>
&gt;  then<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  System.out.println("This is rule #1");<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  System.out.println(myGlobal);<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  myGlobal = 0.03;  end  rule "Your Second Rule"            salience 1<br>
&gt;<br>
&gt;  when<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  TestPOJO(fieldTwo == "true")<br>
&gt;<br>
&gt;  then<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  System.out.println("This is rule #2");<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;  System.out.println(myGlobal);  end<br>
&gt;   [Call to Drools]<br>
&gt;  TestPOJO myTestFact = new TestPOJO();  myTestFact.setFieldOne(15);<br>
&gt; myTestFact.setFieldTwo("true");<br>
&gt;    KieServices ks = KieServices.Factory.get();  KieContainer kContainer =<br>
&gt; ks.getKieClasspathContainer();  KieSession kSession =<br>
&gt; kContainer.newKieSession("ksession-rules");<br>
&gt;<br>
&gt;              kSession.insert(myTestFact);  kSession.fireAllRules();<br>
&gt; kSession.dispose();<br>
&gt;   [Output]<br>
&gt;  This is rule #1  null  This is rule #2  null<br>
&gt;<br>
&gt;<br>
&gt; [What I expect to have as output]    This is rule #1  null  This is rule #2<br>
&gt; 0.03<br>
&gt;   I used the salience to set rule order to be sure that rule one is executed<br>
&gt; before rule two, but my global value is never updated.<br>
&gt; Can you please tell me where I do wrong? Can my expectations be met somehow,<br>
&gt; I need such type of internal rule variables to calculate temporary<br>
&gt; values and pass them between rules?<br>
&gt;<br>
&gt;   Thank you!!!<br>
&gt;<br>
&gt; Best Regards,  Milen<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="javascript: internSendMess('rules-users@lists.jboss.org')">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</div></BODY></HTML>