[rules-users] Avoid caching method results within non fact objects

Greg Barton greg_barton at yahoo.com
Fri Aug 3 00:06:38 EDT 2012


The alternative you're attempting using "from" is potentially less efficient than the first one you tried. :)

Can you alter the CurrentFact objects to hold current and previous state?  That way you avoid the matching step altogether.

rule "Interesting Event"
when
$currentFact: CurrentFact(state=="somethingNew", previousState=="somethingOld")

then
//do something
//update currentFact.previousState to reflect the state of the newFact
end


----- Original Message -----
From: rogelio_sevilla1 <rogelio.sevilla1 at gmail.com>
To: rules-users at lists.jboss.org
Cc: 
Sent: Thursday, August 2, 2012 7:19 PM
Subject: [rules-users] Avoid caching method results within non fact objects

Good day everyone, I'm having a problem here and I'm not sure if I'm using
the right approach.

Currently I have a couple of rules where I inject A LOT of objects as facts.
These objects change their states quite commonly and I have to make
decisions when the previous state has a particular value comparing it to the
current state.  Something like this (just an example, sorry if there are
typos)

rule "Interesting Event"
when
$currentFact: CurrentFact(state=="somethingNew")
$previousFact: PreviousFact($currentFact.id == id && state=="somethingOld")
then
//do something
//update previousFact to reflect the state of the newFact
end


The problem that I had with this is that I had SOOO much facts within the
rules session (thousands of objects with a lot of data that I need within
the session too) that the fireAllRules method started to take too much time
to complete.

So, what I did is to store the previous facts of the objects within a
Wrapper class that contains a hashmap with all of these states.

Something like:


PreviousStateHolder{

private static HashMap<String,PreviousFact> previousFacts;

public void insertPreviousState(){
...
}

public boolean containsPreviousState(CurrentState currentState){
...
}

public boolean deletePreviousState(){
...
}

.. other management methods


}



And my new rules look like




rule "Interesting Event"
when
$currentFact: CurrentFact(state=="somethingNew")
$previousFact: PreviousFact(state=="somethingOld")
                     from
PreviousStateHolder.retrievePreviousState($currentFact)
then
//do something
//update previousFact to reflect the state of the newFact
end



The problem is that drools caches the result returned by the
retrievePreviousState()  method, so, it does not matter if the
PreviousStateHolder member changes; drools always caches the result. I have
tried to put my invocations within eval() but I still get the same problem
(cached results).

I know they are being cached because I put a breakpoint within the methods,
and the first time the fireAllRules gets executed, they get hit. But then,
the consequence of these rules keep getting executed even if the
retrievePreviousState() does not reflect the value needed to do so.

Am I doing something wrong?? Is there another approach that does not involve
inserting all of these states as facts within my session??


Thanks a lot in advance 



--
View this message in context: http://drools.46999.n3.nabble.com/Avoid-caching-method-results-within-non-fact-objects-tp4019005.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




More information about the rules-users mailing list