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

Wolfgang Laun wolfgang.laun at gmail.com
Fri Aug 3 02:30:48 EDT 2012


Given that the rules looking for an "interesting event" use the fact
id and the old and new state constraints looking for distinct values
(i.e., "==") even several thousands of facts should not cause
excessive processing time.

So I suspect that there are other causes for the delay you noticed.

Make sure to distinguish between the time it takes to *insert* facts and
the time spent executing the consequences.

t0 = ...;
insert(...);....
t1 = ...;
fireAllRules();
t2 = ...;

If t1-t0 takes a long time, then it is the conditions that cause the
delay; if t2-t1 takes too long, the actions are the culprits.

It's more  difficult when inserts occur on right hand sides, or when
you modify existing facts, which is what you do, according to your
sample rule. This might cause additional processing time, because the
modification might result in yet another rule. To avoid unnecessary
work for the Engine, make sure to retract the CurrentFact as soon as
it has been processed and *before the PreviousFact is updated* (!).
You continue to have access to the retracted fact via bound variables.

rule "Interesting Event"
when
$currentFact: CurrentFact( $newState: state=="somethingNew")
$previousFact: PreviousFact($currentFact.id == id && state=="somethingOld")
then
retract( $currentFact );
//do something
modify( $previousFact ){ setState( $newState ) }
end

-W



On 03/08/2012, rogelio_sevilla1 <rogelio.sevilla1 at gmail.com> wrote:
> 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