[rules-users] How to reuse a result of a rule in an other rule

Swindells, Thomas TSwindells at nds.com
Fri Jul 22 05:26:20 EDT 2011


The latter approach is particularly powerful we have a very simple but amazingly flexible class called Affirmation which just have 3 fields - String name, Object subject, Object value. This however gives most flexibility that you need

Eg


 rule "Hungry"
   when
     $person : Person(hoursSinceLunch>2 || donutsOnDesk==0 ||
  candybarsInDrawer==0)
not Affirmation(name == "PersonIsHungry", subject == $person)
   then
      insert(new Affirmation("PersonIsHungry", $person, true);
 end

 rule "Food"
   when
     $person : Person()
     Affirmation(name == "PersonIsHungry", subject == $person, value == true)
     not Affirmation(name == "PersonHasLookedForFood", subject == $person)
   then
   modify($person) {
       lookForFood()
   }
   insert(new Affirmation("PersonHasLookedForFood", $person));
 end

- I;ve also updated the rules so they are less likely to retrigger multiple times if some other rules modify person

Thomas

> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of mjohansen
> Sent: 21 July 2011 22:18
> To: rules-users at lists.jboss.org
> Subject: Re: [rules-users] How to reuse a result of a rule in an other rule
>
> Basically, you can either update an object with the results of the first rule, or
> create a new object.
>
> Like:
>
> rule "Hungry"
>   when
>     $person : Person(hoursSinceLunch>2 || donutsOnDesk==0 ||
> candybarsInDrawer==0)
>   then
>     $person.setHungry(true);
>     update($person);
> end
>
> rule "Food"
>   when
>     $person : Person(hungry==true)
>   then
>     $person.lookForFood()
> end
>
> or
>
> rule "Hungry"
>   when
>     $person : Person(hoursSinceLunch>2 || donutsOnDesk==0 ||
> candybarsInDrawer==0)
>   then
>      $hungry=new Hungry();
>      $hungry.setPerson($person);
>      insert($hungry);
> end
>
> rule "Food"
>   when
>     $person : Person()
>     $hungry : Hungry(person==$person)
>   then
>     $person.lookForFood()
> end
>
>
> --
> View this message in context: http://drools.46999.n3.nabble.com/How-to-
> reuse-a-result-of-a-rule-in-an-other-rule-tp3182344p3189730.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users


**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************




More information about the rules-users mailing list