I have a question about how conditions are evaluated for Drools Decision Tables. I had thought the conditions were evaluated left to right and if the left most column it was checking for a given rule was false, it would not check the remaining conditions.
One reason this is important to me is the concept of putting the conditions that narrow scope the most furthest to the left. This would mean objects would kick out sooner, rather than most of the objects satisfying a broad condition first and continuing to check additional conditions.
However, this is not the behavior I witnessed in a unit test, which I will outline below.
This example is simple and isn't intended to demonstrate early narrowing of scope.
|------------------|-------------------| |Condition |Condition | |------------------|-------------------| |myObject |myObject | |------------------|-------------------| |isNameEq("$param")|isValueEq("$param")| |------------------|-------------------| |A |1 | |A |2 | |A |3 | |A |4 | |A |5 | |B |4 | |B |5 | |B |6 | |B |7 | |------------------|-------------------|
In this example, isNameEq and isValueEq are functions from the java object myObject. Please ignore any slight Drools error/lack of declared imports since I know my test works fine and this illustration is an approximation to convey the scenario.
The two functions include some simple logging to show when they are called. For an object with name=A and value=3 I would have expected the isValueEq function to never get called for the rules with B in the name (left most) column because the object fails this condition.
However, the logging indicates function calls were made in the following order:
- isNameEq(A)
- isNameEq(B)
- isValueEq(1)
- isValueEq(2)
- isValueEq(3)
- isValueEq(4)
- isValueEq(5)
- isValueEq(6)
- isValueEq(7)
Does this sound correct? Was I simply mistaken in my assumption? Is this part of the rete algorithm and caching evaluations (nodes?) since it didn't call isValueEq for (B, 4), (B,5)?
Thanks to anyone who can shed some light on this for me!
Each row of your spreadsheet is translated into a single rule, e.g. row number 1:
and another one with "A" and "2", and so on. Each inserted fact is evaluated against these rules, one by one.
The benefit of the algorithm (Rete or whatever) may not become effective when functions hide the access to fact data. You might try the simple constraints (name == "A", value == "1") and log the access by a modification to the getter; this will not affect the way the evaluation network is constructed.