-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40.DesignWorkflowSystem.py
More file actions
81 lines (57 loc) · 2.85 KB
/
40.DesignWorkflowSystem.py
File metadata and controls
81 lines (57 loc) · 2.85 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
# Design a lightweight workflow/state-machine system that supports defining states,
# allowed transitions, and executing workflow instances. The system must validate
# transitions, maintain history, ensure thread safety, and run entirely
# in a single Python file without any external database.
import threading
class State:
def __init__(self, name):
self.name = name
class WorkflowDefinition:
def __init__(self):
self.states = {}
self.transitions = {}
def add_state(self, name):
self.states[name] = State(name)
def add_transition(self, from_state, to_state):
if from_state not in self.transitions:
self.transitions[from_state] = []
self.transitions[from_state].append(to_state)
def is_valid_transition(self, from_state, to_state):
return to_state in self.transitions.get(from_state, [])
class WorkFlowInstance:
def __init__(self, definition, start_state):
self.definition = definition
self.current_state = definition.states[start_state]
self.history = [start_state]
self.lock = threading.Lock()
def move_to(self, next_state_name):
with self.lock:
if self.definition.is_valid_transition(self.current_state.name, next_state_name):
self.current_state = self.definition.states[next_state_name]
self.history.append(next_state_name)
print(f"Transitioned to: {next_state_name}")
else:
print(f"Invalid Transition: {next_state_name}")
def get_available_states(self):
return self.definition.transitions.get(self.current_state.name, [])
def get_history(self):
return self.history
if __name__ == "__main__":
# Step 1: Create workflow definition
w = WorkflowDefinition()
# Add states to the workflow
for s in ["START", "IN_REVIEW", "APPROVED", "REJECTED"]:
w.add_state(s) # Register each state
# Define allowed transitions (state → next possible states)
w.add_transition("START", "IN_REVIEW") # START → IN_REVIEW
w.add_transition("IN_REVIEW", "APPROVED") # IN_REVIEW → APPROVED
w.add_transition("IN_REVIEW", "REJECTED") # IN_REVIEW → REJECTED
# Step 2: Create a workflow instance beginning at "START"
inst = WorkFlowInstance(w, "START")
# Step 3: Demo transitions
print("\nAvailable next:", inst.get_available_states()) # Show next possible moves
inst.move_to("IN_REVIEW") # Move to IN_REVIEW
print("Available next:", inst.get_available_states()) # Display next possible moves
inst.move_to("APPROVED") # Move to APPROVED (valid)
# Step 4: End by printing the transition history
print("\nTransition History:", inst.get_history())