Does Drools keeps track of the matches of the previous run ?

73 Views Asked by At

Let me describe an abstraction of my use case so the intention of the question will be clear.

Use Case:

(i). If there is already an object objA of type A, create another obj2A of type 
     A.
(ii).If there are more than one object of type A, create an object objB of type 
     B.

I want to use Drools only for checking the matches in the working memory, so precisely the query part only. The object creation part is not controlled by Drools. I only ask Drools which one of (i) or (ii) matches (or both), and then decide what to do.

So at first I create an objA of type A and put it in the working memory.

Now I query Drools for the match.

state 0) There will be match for (i) only, and I will create another object of type A and put in the working memory.

state 1) Now the situation arrives where both of the case (i) and (ii) matches. Suppose I work only with (ii) and create objB and put in the working memory.

state 2) At this stage, will Drools again do the computation to check whether condition (i) getting matched, or it will use the match information from the previously ran query ?

In theoretical term I want to know how Drools manages incremental pattern matching.

  • Also how can I ask Drools to give me the pattern match and use match(es) from the previous run ?
  • If Drools returns me a match and I don't use it, such as match (i) in state 1, can Drools keep the match 'aside' and skip the re-computation ?

Kindly let me know if I should provide additional information.

1

There are 1 best solutions below

3
laune On

You have to describe your use case in a more rigorous language, e.g., logic.

The rule

rule another_A1 when A() then insert( new A() ); end

might check your use case (i) but it is extremely dangerous, since the newly created A will trigger this rule right away and you'll create another A and so on, and so on. But this one

rule another_A2 when exists A() then insert( new A() ); end

might be better: existence of one or more A is what you are looking for.

Use case (ii) is similar, and again it depends on how you check for the presence of two or more A facts.

The engine of a Production Rule System will assess the state of facts in working memory, fire matching rules and constantly re-assess the situation. Evaluations of left hand sides are optimized in good systems, and even partial matches are kept in the network for further use. You don't have to request anything - this is the standard procedure as long as you retain the session. A new session, however, is a new game.