-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_custom_decision_rule_lark.py
139 lines (115 loc) · 3.28 KB
/
test_custom_decision_rule_lark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pytest
from lark import Lark
from services.adapter.simple_rule_engine_lark_tree_adapter import SimpleRuleEngineLarkTreeAdapter
@pytest.fixture
def decision_rule_grammar():
with open("./decision_rule.lark") as rule_grammar_file:
rule_grammar = rule_grammar_file.read()
return rule_grammar
def test_rule_simple_decision(decision_rule_grammar):
parser = Lark(decision_rule_grammar)
custom_rule = """
my_rule {
when {
cibil_score > 650 and
age > 35 and
house_ownership in (owned, rented) and
(pet == dog or pet == parrot)
}
then true
when {
cibil_score < 650
}
then false
}
"""
tree = parser.parse(custom_rule)
print(tree.pretty())
decision_rule = SimpleRuleEngineLarkTreeAdapter(tree).get_rule()
facts = dict(
cibil_score=700,
age=40,
house_ownership="owned",
pet="parrot"
)
assert decision_rule.execute(facts) is True
facts = dict(
cibil_score=700,
age=40,
house_ownership="owned",
pet="pig"
)
assert decision_rule.execute(facts) is not True
facts = dict(
cibil_score=500,
age=40,
house_ownership="owned",
pet="dog"
)
assert decision_rule.execute(facts) is not True
def test_rule_complex_decision(decision_rule_grammar):
parser = Lark(decision_rule_grammar)
custom_rule = """
my_rule {
when {
cibil_score between 650 and 750 and
age > 35 and
house_ownership in (owned, rented) and
(
total_overdue_amount == 0 or
number_of_overdue_loans < 2 or
(
number_of_overdue_loans >= 2 and
big_shot == true
)
) and
pet == dog
}
then true
when {
cibil_score < 650
}
then false
}
"""
tree = parser.parse(custom_rule)
print(tree.pretty())
decision_rule = SimpleRuleEngineLarkTreeAdapter(tree).get_rule()
# Evaluate the Decision Rule by passing data
facts = dict(
cibil_score=700,
age=40,
house_ownership="owned",
total_overdue_amount=0,
pet="dog"
)
assert decision_rule.execute(token_dict=facts) is True
facts = dict(
cibil_score=700,
age=40,
house_ownership="owned",
total_overdue_amount=100,
number_of_overdue_loans=1,
pet="dog"
)
assert decision_rule.execute(token_dict=facts) is True
facts = dict(
cibil_score=700,
age=40,
house_ownership="owned",
total_overdue_amount=100,
number_of_overdue_loans=2,
big_shot=True,
pet="dog"
)
assert decision_rule.execute(token_dict=facts) is True
facts = dict(
cibil_score=600,
age=40,
house_ownership="owned",
total_overdue_amount=100,
number_of_overdue_loans=2,
big_shot=False,
pet="dog"
)
assert decision_rule.execute(token_dict=facts) is False