-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_core.py
More file actions
111 lines (78 loc) · 2.56 KB
/
engine_core.py
File metadata and controls
111 lines (78 loc) · 2.56 KB
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
# engine_core.py
# Prototype: Deterministic Logic + State + Workflow Engine
class State:
def __init__(self, initial=None):
self.data = initial or {}
self.history = []
def update(self, key, value):
self.data[key] = value
self.history.append((key, value))
def snapshot(self):
return dict(self.data)
class Rule:
def __init__(self, name, condition, action):
self.name = name
self.condition = condition # function(state) -> bool
self.action = action # function(state) -> None
def apply(self, state):
if self.condition(state):
self.action(state)
return True
return False
class Step:
def __init__(self, name, rules):
self.name = name
self.rules = rules # list of Rule objects
class Workflow:
def __init__(self, steps):
self.steps = steps
self.current = 0
def next_step(self):
if self.current < len(self.steps):
step = self.steps[self.current]
self.current += 1
return step
return None
class Engine:
def __init__(self, state, workflow):
self.state = state
self.workflow = workflow
self.trace = []
def run(self):
while True:
step = self.workflow.next_step()
if not step:
break
step_trace = {"step": step.name, "rules": []}
for rule in step.rules:
fired = rule.apply(self.state)
step_trace["rules"].append({
"rule": rule.name,
"fired": fired,
"state": self.state.snapshot()
})
self.trace.append(step_trace)
return self.trace
# -------------------------
# Demo / Example Usage
# -------------------------
if __name__ == "__main__":
# --- Define rules ---
def needs_approval(state):
return state.data.get("amount", 0) > 1000
def approve(state):
state.update("approved", True)
def finalize(state):
state.update("status", "completed")
rule_check = Rule("CheckAmount", needs_approval, approve)
rule_finalize = Rule("Finalize", lambda s: True, finalize)
# --- Define workflow ---
step1 = Step("Validation", [rule_check])
step2 = Step("Completion", [rule_finalize])
workflow = Workflow([step1, step2])
# --- Run engine ---
state = State({"amount": 1500})
engine = Engine(state, workflow)
trace = engine.run()
import json
print(json.dumps(trace, indent=2))