Decision Trees
What is a Decision Tree?
A Decision Tree is a piece of Python code that can be used in the conditional_chain_flow rule to dynamically determine which flow should be chained based on the current state of the flow.
Decision trees allow you to implement branching logic that is evaluated at runtime. Rather than hard-coding which child flow to trigger in a rule, you configure a decision tree that inspects the current flow properties and sets the ID of the flow to execute next.
There are two kinds of decision trees in the platform:
- Flow decision trees: Used in the
conditional_chain_flowrule to determine which flow to chain to. - Rule decision trees: Used within rule code blobs to encapsulate conditional logic that can be reused across rules.
This page focuses on flow decision trees.
How flow decision trees work
When a conditional_chain_flow rule is configured with a Decision Tree Id, the platform executes the decision tree before chaining. The decision tree inspects the current flow context and sets the output variable to the ID of the flow that should be triggered next.
The decision tree has access to:
flow_properties— any values set by preceding rules in the parent flow.log()— a logging function for writing audit entries.
Creating a decision tree
Decision trees are managed under Flow Management → Decision Trees in the platform.
To create a new decision tree:
- Go to [Flow Management → Decision Trees].
- Click [+ Create Decision Tree].
- Fill in the Technical Name and Display Name.
- Paste your Python code in the Source Code field.
- Click [Save].
Decision tree code structure
A decision tree is a standalone Python script — not a class. The platform makes flow_properties and log() directly available. The decision tree must set the variable output to the flow configuration ID that should be chained, or raise a FlowCancelException to abort the chain entirely.
from energyworx.domain import FlowCancelException
log('Flow properties {}'.format(flow_properties), audit=True)
if "verb" not in flow_properties:
raise FlowCancelException("To decide whether to trigger an association or dissociation flow, the 'verb' must be in the flow_properties.")
verb = flow_properties['verb']
if verb.lower() == "created":
# GO to create flow
output = "5634436741726208"
elif verb.lower() == "changed":
# DO NOTHING ON CHANGE
log('changed verb found: no need to chain', audit=True)
else:
raise FlowCancelException("The 'verb' must be either 'created', 'changed'.")
Notes:
- Set
outputto the flow configuration ID (as a string) to trigger that flow. - If
outputis never set (e.g. thechangedbranch above), no flow is chained. - Raise
FlowCancelExceptionto stop the flow with an error, useful for enforcing requiredflow_propertiesvalues. - Use
log(..., audit=True)to write entries that appear in the datasource's Audit Events.
Configuring a decision tree in a conditional chain flow
Once the decision tree is created in the platform, reference it in the conditional chain flow rule:
- In your parent flow, add a conditional chain flow rule to a sequence.
- In the rule parameters of the conditional chain flow rule, set the Decision Tree Id field to the ID of your decision tree.
Relationship with skip_chain_flow
There are two complementary ways to control whether a conditional chain flow triggers:
skip_chain_flowflag (set in a rule'sflow_properties): Ifflow_properties['skip_chain_flow']isTrue, the chain will be skipped entirely without even running the decision tree.- Decision tree not setting
output: If the decision tree completes without assigningoutput, no flow is chained.
Both mechanisms can be used together. The skip_chain_flow flag is evaluated first; if it is True, the decision tree is not invoked.
Updating the chained flows list
Since a decision tree can set different flow IDs dynamically, make sure that all possible flow IDs are listed in the Flow configuration ids parameter of the conditional chain flow rule. This list acts as the allowed set of flows that can be chained — any flow ID assigned to output that is not in this list will not be triggered.