alex sundukovskiy
alex sundukovskiy

Reputation: 493

Is it expensive to create a new Drools KnowledgeBase every time a rule needs to be evaluated?

I have a situation where for each product I have a different rule.

Thus, I will have 1 drl per each product.

Consequently, as far I understand I have a choice:

  1. add all those knowledge packages into a single KnowledgeBase.

and then let Drools match the right rule using the id of the product.

when 

avs : AvailabilityStatus( available == true, quantity <= 50, productId = 7899 ) 

then    

avs.setDiscountRate("0.65");

end 
  1. create a new KnowledgeBase for each product i.e. for 50 products, 50 KnowledgeBases with just one drl loaded for each.

In my web app each request requires a new evaluation of the rules for the product.

I don't know which approach is more efficient.

Upvotes: 1

Views: 3387

Answers (3)

ali k&#246;ksal
ali k&#246;ksal

Reputation: 1227

If you need to write a rule per your product it is likely that you have multiple rules that have the same kind of LHS with only parameter changes, eg

when 
  avs : AvailabilityStatus( available == true, quantity <= 50, productId = 7899 ) 
then
  //update

then you can consider using a decision table. Drools can generate rules itself. As stated in users guide

Upvotes: 2

salaboy
salaboy

Reputation: 4133

I suggest you to create just one knowledge base and store the binary packages in the file system. When you want to add a new rule, you regenerate the binary package. Don't create a drl per rule, that doesn't make any sense. Cheers

Upvotes: 0

Augusto
Augusto

Reputation: 29927

The KnowledgeBase is a very, very expensive object to instantiate, so I wouldn't create one each time a rule needs to be evaluated.

I think the first approach you mentioned is better (to have all rules into one drl). This also leaves the option open to do rules across products (imagine if you want to add price rules, and you want to model a buy 2, get the 3rd free).

There's a third approach, in which you can still have one drl per product, but you load all of them in the same knowledge base. This is similar to the sigle responsibility principle, but applied to the rules.

Upvotes: 2

Related Questions