<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 <rules-users@lists.jboss.org>
<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 <<a href="javascript: internSendMess('igrachev@abv.bg')">igrachev@abv.bg</a>> wrote:<br>
> Hello, for the first time I have the need to define a global variables in a<br>
> DRL file, and from what it seems from the documentation it shall be a<br>
> variable visible to all my rules in the<br>
> DRL file. However I don't see what I do wrong, but the modifications made by<br>
> one rule are not visible to the others. Here is illustration of what I<br>
> have:<br>
><br>
> [Fact model code]<br>
> public class TestPOJO {          private int fieldOne;          private String<br>
> fieldTwo;                   public int getFieldOne() {...}          public void<br>
> setFieldOne(int fieldOne) {...}          public String getFieldTwo() {...}         <br>
> public void setFieldTwo(String fieldTwo) {...} }<br>
> [DRL rule code]<br>
> global java.lang.Double myGlobal; rule "Your First Rule"          salience 2<br>
><br>
> when<br>
><br>
><br>
><br>
> TestPOJO(fieldOne > 10)<br>
><br>
> then<br>
><br>
><br>
><br>
> System.out.println("This is rule #1");<br>
><br>
><br>
><br>
> System.out.println(myGlobal);<br>
><br>
><br>
><br>
> myGlobal = 0.03; end rule "Your Second Rule"          salience 1<br>
><br>
> when<br>
><br>
><br>
><br>
> TestPOJO(fieldTwo == "true")<br>
><br>
> then<br>
><br>
><br>
><br>
> System.out.println("This is rule #2");<br>
><br>
><br>
><br>
> System.out.println(myGlobal); end<br>
> [Call to Drools]<br>
> TestPOJO myTestFact = new TestPOJO(); myTestFact.setFieldOne(15);<br>
> myTestFact.setFieldTwo("true");<br>
> KieServices ks = KieServices.Factory.get(); KieContainer kContainer =<br>
> ks.getKieClasspathContainer(); KieSession kSession =<br>
> kContainer.newKieSession("ksession-rules");<br>
><br>
>          kSession.insert(myTestFact); kSession.fireAllRules();<br>
> kSession.dispose();<br>
> [Output]<br>
> This is rule #1 null This is rule #2 null<br>
><br>
><br>
> [What I expect to have as output] This is rule #1 null This is rule #2<br>
> 0.03<br>
> I used the salience to set rule order to be sure that rule one is executed<br>
> before rule two, but my global value is never updated.<br>
> Can you please tell me where I do wrong? Can my expectations be met somehow,<br>
> I need such type of internal rule variables to calculate temporary<br>
> values and pass them between rules?<br>
><br>
> Thank you!!!<br>
><br>
> 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>