A decisions node is designed to manage and execute decision table, which is composed of a set of rules. Each rules will be evaluated in order from top to bottom and the result of the first successful rule will be returned as the final result.

Rule

Each rule includes two parts:
  • conditions: A set of conditions to evaluate.
  • actions: The results to return when all conditions are satisfied.

Condition

Each condition includes a key and a value expression. To simplify condition building and ensure correct syntax, use the DecisionsCondition class. The following example checks whether the value of revenue_quote_ratio falls within the range from 0 (inclusive) to 50 (exclusive).
[Python]
"conditions": {
  "revenue_quote_ratio": "[0:50)"
}

Action

An action defines the result when all conditions in a rule are satisfied. You can also define a default action to return when no rules match.

Locale

You can specify a locale string (e.g., en-US, fr-FR, es-MX) to control how dates are parsed. Currently, locale only affects date parsing.

Example

In this example (get_insurance_rate), you define a set of rules to calculate the insurance rate based on credit grade and loan amount:
[python]
def build_decisions(aflow: Flow) -> DecisionsNode:
    """
    Build a Decisions Table programmatically as a node.
    e.g. read from an Excel spreadsheet or CSV and populate the decision table.
    """
    rules = []
    ## We will be build the rules programmatically.
    rule1 = DecisionsRule()
    rule1.condition("grade", DecisionsCondition().equal("A")).condition("loan_amount", DecisionsCondition().less_than(100000))
    rule1.action("insurance_required", False)
    rules.append(rule1)
    rule2 = DecisionsRule()
    rule2.condition("grade", DecisionsCondition().equal("A")).condition("loan_amount", DecisionsCondition().in_range(100000, 300000, True, False))
    rule2.action("insurance_required", True).action("insurance_rate", 0.001)
    rules.append(rule2)
    rule3 = DecisionsRule()
    rule3.condition("grade", DecisionsCondition().equal("A")).condition("loan_amount", DecisionsCondition().in_range(300000, 600000, True, False))
    rule3.action("insurance_required", True).action("insurance_rate", 0.003)
    rules.append(rule3)
    rule4 = DecisionsRule()
    rule4.condition("grade", DecisionsCondition().equal("A")).condition("loan_amount", DecisionsCondition().greater_than_or_equal(600000))
    rule4.action("insurance_required", True).action("insurance_rate", 0.005)
    rules.append(rule4)
    ## We will be build the rules programmatically.
    rule5 = DecisionsRule()
    rule5.condition("grade", DecisionsCondition().equal("B")).condition("loan_amount", DecisionsCondition().less_than(100000))
    rule5.action("insurance_required", False)
    rules.append(rule5)
    rule6 = DecisionsRule()
    rule6.condition("grade", DecisionsCondition().equal("B")).condition("loan_amount", DecisionsCondition().in_range(100000, 300000, True, False))
    rule6.action("insurance_required", True).action("insurance_rate", 0.0025)
    rules.append(rule6)
    rule7 = DecisionsRule()
    rule7.condition("grade", DecisionsCondition().equal("B")).condition("loan_amount", DecisionsCondition().in_range(300000, 600000, True, False))
    rule7.action("insurance_required", True).action("insurance_rate", 0.005)
    rules.append(rule7)
    rule8 = DecisionsRule()
    rule8.condition("grade", DecisionsCondition().equal("A")).condition("loan_amount", DecisionsCondition().greater_than_or_equal(600000))
    rule8.action("insurance_required", True).action("insurance_rate", 0.0075)
    rules.append(rule8)
    node = aflow.decisions(
        name="assess_insurance_rate",
        display_name="Assess insurance rate.",
        description="Based on credit rate and loan amount, assess insurance rate.",
        rules=rules,
        default_actions={
            "assessment_error": "Not assessed. Incorrect data submitted."
        },
        input_schema=AssessmentData,
        output_schema=Assessment
    )
    return node