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