when writing drools rules, do I separate the checkings into more rules or group them all in one rule to have more ifs in the ‘then’ part? Not considering any management issues, just performance (e.g. execution time, memory usuage).
Say I have multiple tasks that their conditions and stuff-to-do are 90% similar, just 10% left are specific to each of them. Do you write
rule A
when (90% conditions) and (task 1 or task 2 or task n)
then
if (10% conditions for task 1) ...(10% stuff-to-do for task 1)
else if (10% conditions for task 2) ...(10% stuff-to-do for task 2)
else if (10% conditions for task n) ...(10% stuff-to-do for task n)
(90% common stuff-to-do)
end
or
rule B-1
when (90% conditions) and (10% conditions for task 1)
then
(90% common stuff-to-do) + (10% stuff-to-do for task 1)
end
rule B-2
when (90% conditions) and (10% conditions for task 2)
then
(90% common stuff-to-do) + (10% stuff-to-do for task 2)
end
rule B-n
when (90% conditions) and (10% conditions for task n)
then
(90% common stuff-to-do) + (10% stuff-to-do for task n)
end
Thank you!
Instead of grouping the conditions and tasks together, you need to separate them into individual rules, like in the following example. Execution time will not matter much, thanks to RETE and memory usage mostly depends on your facts, not rules.
By separating your conditional logic into separate rules, you will gain testable and manageable code base. Putting everything into a big rule is not much different from using if-else statements in plain java code, in that case Drools will just add complexity to your project without any benefits.