-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
84 lines (67 loc) · 2.24 KB
/
core.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
#encoding=utf-8
import logging
import sys
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
MODEPASSALL = 1
MODEMATCHONCE = 2
class DefaultRulesEngine(object):
def __init__(self, mode=MODEPASSALL):
"""
:param mode: "pass_all", "match_once"
"""
self.mode=mode
def fire(self, rules, facts):
matched_times = 0
for rule in rules.rules_list:
logger.debug("rule {name} triggered".format(
name=rule.name
))
condition_result = rule.condition(**facts.facts_dict)
logger.debug("rule {name} condition test result: {result}".format(
name=rule.name,
result=bool(condition_result)
))
if condition_result:
matched_times += 1
if self.mode == MODEMATCHONCE and matched_times >= 2:
break
for action in rule.get_actions():
action(**facts.facts_dict)
class Facts(object):
def __init__(self):
self.facts_dict = dict()
def put(self, key, value):
self.facts_dict[key] = value
class Rule(object):
def __init__(self):
self.name = "rule"
self.description = "description"
self.priority = sys.maxint - 1
self._actions = list()
def condition(self, **kwargs):
return False
def action(self, **kwargs):
return True
def add_action(self, action):
assert callable(action)
if not hasattr(self, "_action"):
self._actions = list()
else:
self._actions.append(action)
def get_actions(self):
if not hasattr(self, "_action"):
res = list()
else:
res = self._actions
for method in dir(self):
if callable(getattr(self, method)) and method.startswith("action_"):
res.append(getattr(self, method))
return res
class Rules(object):
def __init__(self):
self.rules_list = list()
def register(self, rule):
assert isinstance(rule, Rule)
self.rules_list.append(rule)
self.rules_list.sort(key=lambda x: x.priority)