[rules-users] Where is my understanding of Drools global variables wrong?

Wolfgang Laun wolfgang.laun at gmail.com
Tue Feb 4 01:43:33 EST 2014


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

Hi Milen,

the answer to your question is: yes, if this data is intended to
influence the logic of other rules dynamically. The engine does not
react to any changes of data not inserted as a fact.

You may, of course, provide data to rules using a global (or a static
variable) as long as this data is there before you insert facts, and
you may collect results in a global container.

-W






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



More information about the rules-users mailing list