I noticed that you have a switch statement everytime you want to convert an event to the call to the state machine for state transition. i.e. case event1: state_map.event1(data). If the number of potential events is large, then you could have very large switch statement with potential slow performance due to linear time search. Any possible way to implement so that I can avoid the switch statement?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I assume that you are talking about the SMC Manual, section 7, "Queuing Up". This section presents sample code which you might write. I used a switch statement for simplicity. In reality, I would use a callback table containing either member function pointers (C++) or Method objects (Java).
In fact, this is exactly what I did in the Java example. I created a hash table which maps transition names to transition methods. You can do something similar to this in C++. Create member function array and use the Transitions enum as the index into the array. This member function array then replaces the switch statement.
Note: I assume that you realize that transition queuing is rarely needed and to be avoided if at all possible. I consider this to be advanced use of SMC. I added section 7 only to help users who can't get around the issue.
Let me know if you have any further questions. I would be glad to help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In the previous thread Mr. C. Wrapp is talking about mapping Transition events, which in my application are Keys, to Methods defined in "AppClass.sm" using a hash table to avoid clumsy "Case" statements.
Could you please include an example how to do this in VB.Net.
I would like to include SMC in PBX-Console application controlling
6 telephone loops so 6 State Machines Instances (All loops follow the same transitions)
where an integer (0-5) indicates de appropriate instance.
There are about 15 states and about 20 events (transitions).
Any help would be greatly appriciated.
Regards,
KlaasK.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your issue is not one of mapping events to transitions but of storing you application instances into any array.
1. You define a class TelephoneLoop: Public Class TelephoneLoop
2. You define the FSM TelephoneLoop.sm and set "%class TelephoneLoop
3. The TelephoneLoop class contains the FSM: Private _fsm As TelephoneLoopContext = New TelephoneLoopContext(Me)
4. You have a TelephoneLoop array which contains 6 TelephoneLoop instances: Dim TelephoneLoops(6) As TelephoneLoop
Note that only TelephoneLoop may access its private FSM and so only TelephoneLoop may execute transitions. The way transitions are issued are through TelephoneLoop's public interface:
Public Sub OffHook()
_fsm.OffHook()
End Sub
When you want to inform telephone loop 4 that an off-hook was detected, then you call TelephoneLoops(4).OffHook()
There is another issue how to code this call to OffHook and other TelephoneLoop methods. Do you use a condition? If Event == OffHookEvent Then TelephoneLoops(LoopIndex).OffHook() End If
Or do you use reflection? Or do you pass the event to TelephoneLoop and let that class decide?
I hope this answer helps you to find a solution.
C. W. Rapp
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I noticed that you have a switch statement everytime you want to convert an event to the call to the state machine for state transition. i.e. case event1: state_map.event1(data). If the number of potential events is large, then you could have very large switch statement with potential slow performance due to linear time search. Any possible way to implement so that I can avoid the switch statement?
James,
I assume that you are talking about the SMC Manual, section 7, "Queuing Up". This section presents sample code which you might write. I used a switch statement for simplicity. In reality, I would use a callback table containing either member function pointers (C++) or Method objects (Java).
In fact, this is exactly what I did in the Java example. I created a hash table which maps transition names to transition methods. You can do something similar to this in C++. Create member function array and use the Transitions enum as the index into the array. This member function array then replaces the switch statement.
Note: I assume that you realize that transition queuing is rarely needed and to be avoided if at all possible. I consider this to be advanced use of SMC. I added section 7 only to help users who can't get around the issue.
Let me know if you have any further questions. I would be glad to help.
Hello,
In the previous thread Mr. C. Wrapp is talking about mapping Transition events, which in my application are Keys, to Methods defined in "AppClass.sm" using a hash table to avoid clumsy "Case" statements.
Could you please include an example how to do this in VB.Net.
I would like to include SMC in PBX-Console application controlling
6 telephone loops so 6 State Machines Instances (All loops follow the same transitions)
where an integer (0-5) indicates de appropriate instance.
There are about 15 states and about 20 events (transitions).
Any help would be greatly appriciated.
Regards,
KlaasK.
Klaas,
Your issue is not one of mapping events to transitions but of storing you application instances into any array.
1. You define a class TelephoneLoop: Public Class TelephoneLoop
2. You define the FSM TelephoneLoop.sm and set "%class TelephoneLoop
3. The TelephoneLoop class contains the FSM: Private _fsm As TelephoneLoopContext = New TelephoneLoopContext(Me)
4. You have a TelephoneLoop array which contains 6 TelephoneLoop instances: Dim TelephoneLoops(6) As TelephoneLoop
Note that only TelephoneLoop may access its private FSM and so only TelephoneLoop may execute transitions. The way transitions are issued are through TelephoneLoop's public interface:
Public Sub OffHook()
_fsm.OffHook()
End Sub
When you want to inform telephone loop 4 that an off-hook was detected, then you call TelephoneLoops(4).OffHook()
There is another issue how to code this call to OffHook and other TelephoneLoop methods. Do you use a condition? If Event == OffHookEvent Then TelephoneLoops(LoopIndex).OffHook() End If
Or do you use reflection? Or do you pass the event to TelephoneLoop and let that class decide?
I hope this answer helps you to find a solution.
C. W. Rapp