java.lang.Object
software.amazon.smithy.rulesengine.logic.bdd.Bdd

public final class Bdd extends Object
Binary Decision Diagram (BDD) with complement edges for efficient rule evaluation.

This class represents a pure BDD structure without any knowledge of the specific conditions or results it represents. The interpretation of condition indices and result indices is left to the caller.

Reference Encoding:

  • 0: Invalid/unused reference (never appears in valid BDDs)
  • 1: TRUE terminal
  • -1: FALSE terminal
  • 2, 3, ...: Node references (points to nodes array at index ref-1)
  • -2, -3, ...: Complement node references (logical NOT)
  • 100_000_000+: Result terminals (100_000_000 + resultIndex)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Result reference encoding offset.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Bdd(int rootRef, int conditionCount, int resultCount, int nodeCount, int[] nodes)
    Creates a BDD by streaming nodes directly into the structure.
    Bdd(int rootRef, int conditionCount, int resultCount, int nodeCount, Consumer<BddNodeConsumer> nodeHandler)
    Creates a BDD by streaming nodes directly into the structure.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    int
    Evaluates the BDD using the provided condition evaluator.
    int
    Gets the number of conditions.
    int
    getHigh(int nodeIndex)
    Gets the high (true) reference for a node.
    int
    getLow(int nodeIndex)
    Gets the low (false) reference for a node.
    int
    Gets the number of nodes in the BDD.
    void
    Write all nodes to the consumer.
    int
    Gets the number of results.
    int
    Gets the root node reference.
    int
    getVariable(int nodeIndex)
    Gets the variable index for a node.
    int
     
    static boolean
    isComplemented(int ref)
    Checks if a reference is complemented (negative).
    static boolean
    isNodeReference(int ref)
    Checks if a reference points to a node (not a terminal or result).
    static boolean
    Checks if a reference points to a result.
    static boolean
    isTerminal(int ref)
    Checks if a reference is a terminal (TRUE or FALSE).
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • RESULT_OFFSET

      public static final int RESULT_OFFSET
      Result reference encoding offset.
      See Also:
  • Constructor Details

    • Bdd

      public Bdd(int rootRef, int conditionCount, int resultCount, int nodeCount, Consumer<BddNodeConsumer> nodeHandler)
      Creates a BDD by streaming nodes directly into the structure.
      Parameters:
      rootRef - the root reference
      conditionCount - the number of conditions
      resultCount - the number of results
      nodeCount - the exact number of nodes
      nodeHandler - a handler that will provide nodes via a consumer
    • Bdd

      public Bdd(int rootRef, int conditionCount, int resultCount, int nodeCount, int[] nodes)
      Creates a BDD by streaming nodes directly into the structure.
      Parameters:
      rootRef - the root reference
      conditionCount - the number of conditions
      resultCount - the number of results
      nodeCount - the exact number of nodes
      nodes - BDD nodes array where the condition, high, and low are all in succession.
  • Method Details

    • getConditionCount

      public int getConditionCount()
      Gets the number of conditions.
      Returns:
      condition count
    • getResultCount

      public int getResultCount()
      Gets the number of results.
      Returns:
      result count
    • getNodeCount

      public int getNodeCount()
      Gets the number of nodes in the BDD.
      Returns:
      the node count
    • getRootRef

      public int getRootRef()
      Gets the root node reference.
      Returns:
      root reference
    • getVariable

      public int getVariable(int nodeIndex)
      Gets the variable index for a node.
      Parameters:
      nodeIndex - the node index (0-based)
      Returns:
      the variable index
    • getHigh

      public int getHigh(int nodeIndex)
      Gets the high (true) reference for a node.
      Parameters:
      nodeIndex - the node index (0-based)
      Returns:
      the high reference
    • getLow

      public int getLow(int nodeIndex)
      Gets the low (false) reference for a node.
      Parameters:
      nodeIndex - the node index (0-based)
      Returns:
      the low reference
    • getNodes

      public void getNodes(BddNodeConsumer consumer)
      Write all nodes to the consumer.
      Parameters:
      consumer - the consumer to receive the integers
    • evaluate

      public int evaluate(ConditionEvaluator ev)
      Evaluates the BDD using the provided condition evaluator.
      Parameters:
      ev - the condition evaluator
      Returns:
      the result index, or -1 for no match
    • isNodeReference

      public static boolean isNodeReference(int ref)
      Checks if a reference points to a node (not a terminal or result).
      Parameters:
      ref - the reference to check
      Returns:
      true if this is a node reference
    • isResultReference

      public static boolean isResultReference(int ref)
      Checks if a reference points to a result.
      Parameters:
      ref - the reference to check
      Returns:
      true if this is a result reference
    • isTerminal

      public static boolean isTerminal(int ref)
      Checks if a reference is a terminal (TRUE or FALSE).
      Parameters:
      ref - the reference to check
      Returns:
      true if this is a terminal reference
    • isComplemented

      public static boolean isComplemented(int ref)
      Checks if a reference is complemented (negative).
      Parameters:
      ref - the reference to check
      Returns:
      true if the reference is complemented
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object