Class Bdd
java.lang.Object
software.amazon.smithy.rulesengine.logic.bdd.Bdd
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 terminal2, 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
FieldsModifier and TypeFieldDescriptionstatic final intResult reference encoding offset. -
Constructor Summary
ConstructorsConstructorDescriptionBdd(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 TypeMethodDescriptionbooleanintEvaluates the BDD using the provided condition evaluator.intGets the number of conditions.intgetHigh(int nodeIndex) Gets the high (true) reference for a node.intgetLow(int nodeIndex) Gets the low (false) reference for a node.intGets the number of nodes in the BDD.voidgetNodes(BddNodeConsumer consumer) Write all nodes to the consumer.intGets the number of results.intGets the root node reference.intgetVariable(int nodeIndex) Gets the variable index for a node.inthashCode()static booleanisComplemented(int ref) Checks if a reference is complemented (negative).static booleanisNodeReference(int ref) Checks if a reference points to a node (not a terminal or result).static booleanisResultReference(int ref) Checks if a reference points to a result.static booleanisTerminal(int ref) Checks if a reference is a terminal (TRUE or FALSE).toString()
-
Field Details
-
RESULT_OFFSET
public static final int RESULT_OFFSETResult 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 referenceconditionCount- the number of conditionsresultCount- the number of resultsnodeCount- the exact number of nodesnodeHandler- 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 referenceconditionCount- the number of conditionsresultCount- the number of resultsnodeCount- the exact number of nodesnodes- 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
Write all nodes to the consumer.- Parameters:
consumer- the consumer to receive the integers
-
evaluate
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
-
hashCode
public int hashCode() -
toString
-