[rules-users] Fact Instance "lifetime" in knowledge session

Wolfgang Laun wolfgang.laun at gmail.com
Tue Mar 6 09:56:09 EST 2012


Don't worry - all Accounts are there at the end.

It is a basic (!) feature of Drools (and similar systems) to execute
matching rules only once, and then again only after one (or more)
contributing facts of the rule's activation have changed, or new data
has appeared, etc.

Please consider reading more of the documentation.

Wolfgang


On 06/03/2012, Anne Fiore <a.fiore at docboxinc.com> wrote:
> Hi,
>
> I'm very new to drools and am trying to understand how "facts" are managed
> by the knowledge session.  I've been experimenting with a simple example
> using drools 5.3.0.  I created a simple Account class that has a single
> "balance" attribute.  My rule tests the balance and prints a message if it
> is less than 100. I am creating a new instance of Account in my main method
> and inserting it into a stateful knowledge session.   I put this into a
> loop that runs 3 times and never dispose of the session.  Each of my
> Account instances has a balance less that 100.  Here is a portion of the
> code:
>
>     public static final void main(String[] args) throws InterruptedException
>     {
>         KnowledgeBase knowledgeBase = createKnowledgeBase();
>         StatefulKnowledgeSession session =
> knowledgeBase.newStatefulKnowledgeSession();
>
>         for (int i = 0; i < 3; i++)
>         {
>             try
>             {
>                 Account account = new Account();
>                 switch (i)
>                 {
>                    case 0: account.setBalance(95);
>                            break;
>                    case 1: account.setBalance(85);
>                            break;
>                    case 2: account.setBalance(80);
>                            break;
>                    default: account.setBalance(80);
>                 }
>                 session.insert(account);
>                 session.fireAllRules();
>             }
>             catch (Exception e)
>             {
>                 System.out.println("Exception: " + e.toString());
>             }
>             Thread.sleep(1000);
>         }
>     }
>
> My rule is:
> rule "basic rule"
> when $a : Account (balance < 100) // condition
> then System.out.println("Account balance " + $a.getBalance() + " is less
> than 100");  // consequence
> end
>
> What I see is that each time the loop executes only the last instance of
> Account seems to get triggered.  I expected that each time I call
> "fireAllRules" it would examine all the facts in the session and execute
> them. So I thought the first time the loop runs I would get 1 activation,
> the second time the loop runs I get 2 activations, etc.
>
> Are all 3 instances of Account still in the working memory or is the latest
> instance replacing the existing one?  Or, once a fact triggers the rules
> engine, does it not trigger it again?
>
> Thanks for any insights,
> Anne
>



More information about the rules-users mailing list