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
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
from simpleruleengine.conditional.when_all import WhenAll
from simpleruleengine.conditional.when_any import WhenAny
from simpleruleengine.expression.expression import Expression
from simpleruleengine.operator.greater_than_equal import Gte
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__":
applicant_age_gte_35 = Expression(
NumericToken("applicant_age"),
Gte(35)
)
business_owned_by_self_family = Expression(
StringToken("business_ownership"),
In("Owned by Self", "Owned by Family")
)
applicant_owned_by_self_family = Expression(
StringToken("applicant_ownership"),
In("Owned by Self", "Owned by Family")
)
rule_row_decision_go = RuleRowDecision(
WhenAll(
applicant_age_gte_35,
WhenAny(
business_owned_by_self_family,
applicant_owned_by_self_family
)
),
"GO"
)
rule_set_decision = RuleSetDecision(rule_row_decision_go)
fact_go = dict(
applicant_age=42,
applicant_ownership="Not Owned",
business_ownership="Owned by Self"
)
assert rule_set_decision.evaluate(fact_go) == "GO"
fact_no_go_1 = dict(
applicant_age=42,
applicant_ownership="Not Owned",
business_ownership="Not Owned"
)
assert rule_set_decision.evaluate(fact_no_go_1) != "GO"
fact_no_go_2 = dict(
applicant_age=25,
applicant_ownership="Owned by Self",
business_ownership="Owned by Self"
)
assert rule_set_decision.evaluate(fact_no_go_2) != "GO"