Search code examples
javadrools

Drools traversal object tree


How can I traversal object tree using Drools (independent of depth of tree)?

My POJO:

public class Node {

    int ID;
    String name;
    String value;
    List<Node> nodes;
}

I need to log situation when value from Node is lower than sum of values from nodes list. Every node could have own nodes list:

Node1
   - Node1_1
   - Node1_2
        -Node1_2_1
        -Node1_2_2
   .....

Conditions:

Node1.value < Node1_1.value + Node1_2.value 
and 
Node1_2 < Node1_2_1.value + Node1_2_2.value

Solution

  • Writing a rule to monitor all Nodes according to this rule is not a good idea. I have two objections to using a rule.

    First, the rule would trigger for all Node objects in WM, and cause the recursive computation of the sum of the children. This means every node at every level will cause this computation - shudder!

    Second, what is the requirement once you have established a difference for a Node? What is the sum for the next higher level: the one computed from the stored value or the one using the computed value?

    After clarifying the second point: write a Java method passing through the tree so that it can compute the values and mark or correct the nodes that don't match.