-
Notifications
You must be signed in to change notification settings - Fork 0
/
rulesEngine.py
64 lines (48 loc) · 2.08 KB
/
rulesEngine.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
import datetime
import json
from business_rules import export_rule_data
from business_rules import run_all
from business_rules.actions import rule_action, BaseActions
from business_rules.fields import FIELD_NUMERIC
from business_rules.variables import BaseVariables, string_rule_variable, numeric_rule_variable
class Credit:
def __init__(self, current_balance, credit_limit, start_date, maximum_amount_owed):
self.current_balance = current_balance
self.credit_limit = credit_limit
self.start_date = start_date
self.maximum_amount_owed = maximum_amount_owed
self.initial_score = 0
class CreditVariables(BaseVariables):
def __init__(self, credit):
self.credit = credit
@numeric_rule_variable
def current_balance(self):
return self.credit.current_balance
@numeric_rule_variable
def credit_limit(self):
return self.credit.credit_limit
@string_rule_variable()
def start_date(self):
return self.credit.start_date
@numeric_rule_variable
def maximum_amount_owed(self):
return self.credit.maximum_amount_owed
class CreditActions(BaseActions):
def __init__(self, credit):
self.credit = credit
@rule_action(params={'score_positive': FIELD_NUMERIC})
def current_balance_less(self, score_positive):
print("current_balance_less previus score_positive" + str(self.credit.initial_score))
self.credit.initial_score += score_positive
print("current_balance_less previus score_positive" + str(self.credit.initial_score))
with open("rules.json") as data_file:
rules = json.loads(data_file.read())
listOfCredits = [Credit(120120, 1020, datetime.datetime.now(), 2120210)]
for product in listOfCredits:
rule_was_triggered = run_all(rule_list=rules,
defined_variables=CreditVariables(product),
defined_actions=CreditActions(product),
stop_on_first_trigger=True
)
json = export_rule_data(CreditVariables, CreditActions)
print(json)