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:
- 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
- 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.
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.