Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

java - Drools: Why does a retracted fact still show up in collect

We have the following code from an inherited project and i'm trying to understand why it executes the way it does.

We have the following 2 drools rules:

// The purpose of this rule is to update a UI counter with the number of complete activities
rule "Activity Complete"
    dialect "java"
    no-loop
    when
        $log : Logger()
        [...]
        $acts : List( $numTransferred : size ) from collect( ActivityInstance(complete==true, [some more filtering stuff]))
    then
        $log.debug([dump information about all activities in $acts])
        [call necessary java code updating the UI counter to display $numTransferred]
end

rule "Purge Complete Activity Instances"
salience( -100 )
    when
        $log : Logger()
        $a : ActivityInstance(complete == true )
    then
       $log.debug([dump information about activity being retracted]);
       retract($a)
    end

The result of these rules running in the project is that the counter is steadily incremented. The logging output is:

Activity Complete: $acts has size 1, contains Activity1
Purge: Activity1 has been retracted
Activity Complete: $acts has size 2, contains Activity1, Activity2
Purge: Activity2 has been retracted
...

I'm not very familiar with drools, but my understanding is that collect collects facts from working memory, while retract removes facts from working memory. My expectation would be that this code wouldn't work as intended, and the counter should be forever stuck at 1, with the lower salience rule always purging a completed activity from working memory after it is used in the default salience rule.

Any help in understanding why this code produces these results would be appreciated, thanks!

question from:https://stackoverflow.com/questions/65842266/drools-why-does-a-retracted-fact-still-show-up-in-collect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...