-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FSM core components and example usage
- Loading branch information
akshita.dixit
committed
Dec 2, 2024
1 parent
da92348
commit b7acab0
Showing
24 changed files
with
138 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
name: "SimpleFSM" | ||
states: | ||
Idle: | ||
on_enter: "Entering Idle" | ||
on_exit: "Exiting Idle" | ||
on_enter: "Entering Idle hehe" | ||
on_exit: "Exiting Idle hehe" | ||
Active: | ||
on_enter: "Entering Active" | ||
on_exit: "Exiting Active" | ||
on_enter: "Entering Active ehehe" | ||
on_exit: "Exiting Active ehehe" | ||
transitions: | ||
- source: "Idle" | ||
target: "Active" | ||
condition: "lambda: True" | ||
action: "lambda: print('Activating')" | ||
action: "print('====Activating====')" | ||
- source: "Active" | ||
target: "Idle" | ||
condition: "lambda: True" | ||
action: "lambda: print('Deactivating')" | ||
action: "print('====Deactivating====')" | ||
events: | ||
Activate: | ||
transitions: | ||
- source: "Idle" | ||
target: "Active" | ||
terminal: | ||
- "Active" | ||
Deactivate: | ||
transitions: | ||
- source: "Active" | ||
target: "Idle" | ||
terminal: | ||
- "Idle" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from ..flowfsm.config.parser import parse_fsm_config | ||
from ..flowfsm.config.loader import load_fsm_from_config | ||
from ..flowfsm.runtime.executor import Executor | ||
|
||
# Load FSM configuration | ||
config = parse_fsm_config("./config_example.yml") | ||
|
||
# Create FSM | ||
workflow = load_fsm_from_config(config) | ||
|
||
print(f"Workflow '{workflow.name}' initialized with states: {workflow.states}") | ||
|
||
# Execute FSM | ||
executor = Executor(workflow) | ||
executor.run() |
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .base import FSMBase | ||
from .state import StateRegistry | ||
from .transition import Transition | ||
from .event import EventRegistry | ||
|
||
|
||
class Workflow(FSMBase): | ||
"""Workflow class extending FSMBase.""" | ||
def __init__(self, name, states, transitions, events): | ||
super().__init__(name) | ||
|
||
self.states = states | ||
self.current_state = self.states[0][1] | ||
self.current_state().enter() | ||
self.transitions = transitions | ||
self.events = events |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Executor: | ||
"""The executor runs the FSM based on events and transitions.""" | ||
def __init__(self, workflow): | ||
self.workflow = workflow | ||
|
||
def run(self): | ||
"""Run the FSM, processing events and triggering transitions.""" | ||
print(f"Starting FSM: {self.workflow.name}") | ||
while True: | ||
# Wait for events to trigger transitions | ||
event_name = "Activate" #input("Enter event name: ").strip() | ||
if event_name in self.workflow.events: | ||
event = self.workflow.events[event_name] | ||
self.workflow.trigger_event(event) | ||
else: | ||
print(f"Event '{event_name}' not found.") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def print_params_decorator(func): | ||
def wrapper(*args, **kwargs): | ||
print(f"Parameters passed to {func.__name__}: args={args}, kwargs={kwargs}") | ||
return func(*args, **kwargs) | ||
return wrapper |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from flowfsm.config.parser import parse_fsm_config | ||
from flowfsm.config.loader import load_fsm_from_config | ||
from flowfsm.runtime.executor import Executor | ||
|
||
|
||
# Load FSM configuration | ||
config = parse_fsm_config("./examples/config_example.yml") | ||
|
||
# Create FSM | ||
workflow = load_fsm_from_config(config) | ||
|
||
# Execute FSM | ||
executor = Executor(workflow) | ||
executor.run() |