diff --git a/vlib/datatypes/fsm/fsm.v b/vlib/datatypes/fsm/fsm.v index bb439264a0..43d63b64a4 100644 --- a/vlib/datatypes/fsm/fsm.v +++ b/vlib/datatypes/fsm/fsm.v @@ -31,10 +31,12 @@ mut: current_state string } +// StateMachine static method returns a new StateMachine instance. pub fn new() StateMachine { return StateMachine{} } +// set_state sets the current state of the state machine to the given state by `name`. pub fn (mut s StateMachine) set_state(name string) ! { if name in s.states { s.current_state = name @@ -43,10 +45,13 @@ pub fn (mut s StateMachine) set_state(name string) ! { } } +// get_state returns the current state of the state machine. pub fn (mut s StateMachine) get_state() string { return s.current_state } +// add_state adds a new state to the state machine. +// It takes the `name` of the state, and three event handlers: `entry`, `run`, and `exit`. pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run EventHandlerFn, exit EventHandlerFn) { s.states[name] = State{ entry_handler: entry @@ -58,6 +63,8 @@ pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run Eve } } +// add_transition adds a new transition to the state machine. +// It takes the `from` and `to` states, and a condition handler. pub fn (mut s StateMachine) add_transition(from string, to string, condition_handler ConditionFn) { t := Transition{ to: to @@ -70,6 +77,7 @@ pub fn (mut s StateMachine) add_transition(from string, to string, condition_han s.transitions[from] = [t] } +// run runs the state machine. It takes a `receiver` argument that is passed to the event handlers. pub fn (mut s StateMachine) run(receiver voidptr) ! { from_state := s.current_state mut to_state := s.current_state diff --git a/vlib/datatypes/fsm/tools/fsm_graph.v b/vlib/datatypes/fsm/tools/fsm_graph.v index 26368aa526..db516cb8b7 100644 --- a/vlib/datatypes/fsm/tools/fsm_graph.v +++ b/vlib/datatypes/fsm/tools/fsm_graph.v @@ -1,6 +1,7 @@ import os import flag +// read_file reads a file and returns the lines as a list of strings. It takes the file path as an argument. pub fn read_file(file string) ![]string { if os.is_file(file) { text := os.read_lines(file) or { @@ -12,6 +13,7 @@ pub fn read_file(file string) ![]string { return [''] } +// extract_transitions extracts the transitions from a line and returns it as a formatted string. pub fn extract_transitions(line string) ?string { mut result := ' ' first_comma := line.index(',')? @@ -24,12 +26,14 @@ pub fn extract_transitions(line string) ?string { return result + from + ' -> ' + to + ' [label=' + condition + '];' } +// get_transitions gets the transitions from a line and returns it as a formatted string using `extract_transitions`. pub fn get_transitions(line string) ?string { mut raw_text := line[line.index_any('(') + 1..line.index_any(')')] raw_text = raw_text.replace("'", '').replace(' ', '') return extract_transitions(raw_text) } +// main reads a file and generates a graph from the transitions in the file. pub fn main() { mut fp := flag.new_flag_parser(os.args) file := fp.string('file', `f`, '', 'input V file with transitions to generate graph from.')