Examples

A simple decision tree involving facts

Decision matrix

Bureau Score

between 650 and 800

Marital Status

in [Married, Unspecified]

Business Ownership

in [Owned by Self, Owned by Family]

Decision

GO

Rule specification

from simpleruleengine.conditional.when_all import WhenAll
from simpleruleengine.expression.expression import Expression
from simpleruleengine.operator.between import Between
from simpleruleengine.operator.string_in import In
from simpleruleengine.rulerow.rule_row_decision import RuleRowDecision
from simpleruleengine.ruleset.rule_set_decision import RuleSetDecision
from simpleruleengine.token.numeric_token import NumericToken
from simpleruleengine.token.string_token import StringToken

if __name__ == "__main__":
    cibil_score_between_650_800 = Expression(
        NumericToken("cibil_score"),
        Between(floor=650, ceiling=800)
    )
    marital_status_in_married_unspecified = Expression(
        StringToken("marital_status"),
        In("Married", "Unspecified")
    )
    business_owned_by_self_family = Expression(
        StringToken("business_ownership"),
        In("Owned by Self", "Owned by Family")
    )

    rule_row_decision_go = RuleRowDecision(
        WhenAll(
            cibil_score_between_650_800,
            marital_status_in_married_unspecified,
            business_owned_by_self_family
        ),
        "GO"
    )
    rule_set_decision = RuleSetDecision(rule_row_decision_go)

    # Evaluate a fact set against this rule and assert the results.
    fact = dict(
        cibil_score=700,
        marital_status="Married",
        business_ownership="Owned by Self"
    )
    assert rule_set_decision.evaluate(fact) == "GO"

A complex decision tree involving multiple AND and OR conditions

Decision matrix

Applicant Age

>=35

Applicant Ownership

in [Owned by Self, Owned by Family]

Business Ownership

in [Owned by Self, Owned by Family]

Decision

GO

Applicant Age

>=35

Applicant Ownership

in [Owned by Self, Owned by Family]

Business Ownership

in [Rented]

Decision

GO

Applicant Age

>=35

Applicant Ownership

in [Rented]

Business Ownership

in [Owned by Self, Owned by Family]

Decision

GO

Applicant Age

>=35

Applicant Ownership

in [Rented]

Business Ownership

in [Rented]

Decision

NO GO

Applicant Age

<35

Applicant Ownership

in [Rented]

Business Ownership

in [Rented]

Decision

NO GO

Applicant Age

<35

Applicant Ownership

in [Owned by Self, Owned by Family]

Business Ownership

in [Rented]

Decision

NO GO

Applicant Age

<35

Applicant Ownership

in [Rented]

Business Ownership

in [Owned by Self, Owned by Family]

Decision

NO GO

Applicant Age

<35

Applicant Ownership

in [Owned by Self, Owned by Family]

Business Ownership

in [Owned by Self, Owned by Family]

Decision

GO

  • when the applicant age is >=35, either of applicant ownership or business ownership must be Owned.

  • When the applicant age is <35, both the applicant ownership and business ownership must be Owned.

Rule specification

A scoring rule involving multiple parameters

Rule set Name
Weightage

no_of_running_bl_pl

0.5

last_loan_drawn_in_months

0.5

no_of_running_bl_pl

Condition
Score

no_of_running_bl_pl >= 7

-100

no_of_running_bl_pl >= 4

-40

no_of_running_bl_pl >= 2

30

no_of_running_bl_pl >= 0

100

no_of_running_bl_pl is none

100

last_loan_drawn_in_months

Condition
Score

last_loan_drawn_in_months == 0

30

last_loan_drawn_in_months <3

-30

last_loan_drawn_in_months <= 12

40

last_loan_drawn_in_months >12

100

last_loan_drawn_in_months is none

100

Rule Specification

A nested rule that involves another rule for evaulation

Rule

  • If cibil score is between 650 and 800, score is 100

  • If cibil score is less than 650, score is 0

  • Decide GO if pet in [dog, cat] and cibil score is greater than 0

Rule Specification

Last updated

Was this helpful?