Markos
2016-08-30 14:49:27 UTC
Hi,
I would like to listen the advice of more experienced Tcl programmers about the strategy I'm using.
I'm trying to use the hierarchical State Machine Technique according to the model:
Loading Image...
And to represent this model in a "tree" structure as:
Loading Image...
To do this I'm using a variable of type dictionary called "state" in which the keys and values represent the nodes of the tree as shown in partial diagram:
Loading Image...
I'm using "dict set" command to create the tree structure within the state variable in which each "dict set" define a branch from the root to a leaf as shown in the diagrams:
Loading Image...
Loading Image...
And to find the nodes I'm using the recursion technique with walkState procedure in which from a state and an event (input) I can find the next state.
(state_tree is the dictionary variable)
(state is a node in tree)
(input is an event)
The procedure search on the tree (state_tree) if "input" is a child node of the node "state".
proc walkState { state_tree state input } {
global next_state
if { [ catch { set k [dict keys $state] } ] } {
if { $state_tree == $state } {
puts "$state is a LEAF"
}
return
}
foreach sub_state [dict keys $state_tree] {
puts "sub_state -> $sub_state\n"
if {$sub_state == $state} {
set child_node [dict keys [dict get $state_tree $sub_state]]
puts "Child node of $sub_state -> $child_node"
if { $child_node == $input } {
puts "$input is an event compatible with $sub_state\n"
set next_state [dict get $state_tree $sub_state $child_node]
puts "Next state -> $next_state"
} else {
puts "$input isn't compatible with the state $sub_state"
}
}
if {[dict exists $state_tree $sub_state]} {
set sub_state [dict get $state_tree $sub_state]
walkState $sub_state $state $input
}
}
return $next_state
}
Any comment about the strategy I'm following?
Is goog, is bad?
Thanks for the tips,
Markos
I would like to listen the advice of more experienced Tcl programmers about the strategy I'm using.
I'm trying to use the hierarchical State Machine Technique according to the model:
Loading Image...
And to represent this model in a "tree" structure as:
Loading Image...
To do this I'm using a variable of type dictionary called "state" in which the keys and values represent the nodes of the tree as shown in partial diagram:
Loading Image...
I'm using "dict set" command to create the tree structure within the state variable in which each "dict set" define a branch from the root to a leaf as shown in the diagrams:
Loading Image...
Loading Image...
And to find the nodes I'm using the recursion technique with walkState procedure in which from a state and an event (input) I can find the next state.
(state_tree is the dictionary variable)
(state is a node in tree)
(input is an event)
The procedure search on the tree (state_tree) if "input" is a child node of the node "state".
proc walkState { state_tree state input } {
global next_state
if { [ catch { set k [dict keys $state] } ] } {
if { $state_tree == $state } {
puts "$state is a LEAF"
}
return
}
foreach sub_state [dict keys $state_tree] {
puts "sub_state -> $sub_state\n"
if {$sub_state == $state} {
set child_node [dict keys [dict get $state_tree $sub_state]]
puts "Child node of $sub_state -> $child_node"
if { $child_node == $input } {
puts "$input is an event compatible with $sub_state\n"
set next_state [dict get $state_tree $sub_state $child_node]
puts "Next state -> $next_state"
} else {
puts "$input isn't compatible with the state $sub_state"
}
}
if {[dict exists $state_tree $sub_state]} {
set sub_state [dict get $state_tree $sub_state]
walkState $sub_state $state $input
}
}
return $next_state
}
Any comment about the strategy I'm following?
Is goog, is bad?
Thanks for the tips,
Markos