[rules-users] Newbie help for rule that won't fire. What am I missing?

Bagwell, Allen F afbagwe at sandia.gov
Tue Apr 22 10:18:31 EDT 2008


Drools newbie here in need of help.  I'm missing something fundamental here, and I just can't see it.  The following doesn't work for me:

In my rule file:

---------------------------------
rule "timer goes off"
        when
                $rt : RuleTimer(timedOut == true)
        then
                System.out.println( $rt.getUniqueName() + " went off.");
end
--------------------------------

Here's what I'm doing in the code after all the usual setup of compiling the rule file (which drools reports having no errors).
Additionally I can report that yes I HAVE loaded the compiled rule file into the session, because I have several other simple rules in it, and they
do activate and fire when the conditions are met.  I've just isolated the above rule which is part of the problem.

----------------------------------------------------------
// make a timer lasting two seconds.
RuleTimer timer1 = new RuleTimer("Timer1",0,0,2);
// make a timer lasting five seconds.
RuleTimer timer2 = new RuleTimer("Timer2",0,0,5);

// insert my RuleTimers to be dynamically updated.
session.insert( timer1, true );
session.insert( timer2, true );

// sleep 2.5 seconds, timer 1 should time out
Thread.sleep(2500);

timer1.calculateTimeLeft();
timer2.calculateTimeLeft();

session.fireAllRules();

//sleep another 2.750 seconds, timer 2 should time out.
Thread.sleep(2750);

timer2.calculateTimeLeft();

session.fireAllRules();
logger.writeToDisk();
session.dispose();
------------------------------------------------

For reference, here's some of the methods being called in the RuleTimer class...

-------------------------------------------------

public void calculateTimeLeft()
{
        if (expireInterval >= 0)
            timeLeft = expireInterval - (Tut.getWallTime() - timeBegin);

        if (timeLeft <= 0)
        {
            setTimedOut(true);
        }
}

public void setTimedOut(boolean b)
{
        boolean oldTimedOut = timedOut;
        timedOut = b;
        changes.firePropertyChange("timedOut", oldTimedOut, timedOut);
}

public final boolean isTimedOut()
{
        return timedOut;
}

---------------------------------------------------

And finally here's the audit log output for the drools session. The objects were inserted. They
were dynamically updated. But their was no rule activation (see the rule at top of post):

---------------------------------------------------

Object inserted (1):
uniqueName: Timer1
timedOut: false

Object inserted (2):
uniqueName: Timer2
timedOut: false

Object updated (1):
uniqueName: Timer1
timedOut: true

Object updated (2):
uniqueName: Timer2
timedOut: true

------------------------------------------------

So can any sharp eye tell me what I'm doing wrong here? It seems pretty simple and straightforward, but I'm confounded as to why I get no rule activation.

thanks!
Allen





More information about the rules-users mailing list