G
Guest
This post is intended to verify that true value semantics DO NOT EXIST for the
Enum class (relative to boolean operations).
If this is true then (thus and therefore) you can not design state machines in
C# where the machine can use the enumerated 'state' to execute (thus the term
'machine' as in 'state machine') the machine operations as this requires
boolean operations to be performed on the state.
.....
I would like to design a simple enumerated state machine
with a 'switcher' and an enumerated 'bit map' ([flags] enum) to hold the
state.
The 'switcher' is a replacement for a switch statement when state/operational
mapping's are from 10 to 100 (or more). This high operational cardinality is
commonly seen in metric analysis probes whose engine is based on a 'state
machine'.
Smaller operational cardinalities (1 to 10) are common for process engines
where the state controls a workflow process.
Thus a simple elegant state machine is a handy item to have around...
.....
The state machine would support any enum type (specified during construction).
So I need to perform boolean iteration and operations (switcher aspect
delegate
execution semantics) as well as enumerated state operations and iteration
(state
aspect of the machine - set, get, test, state semantics).
The 'switcher' maps various states to delegates. It executes the current
state based
on one of several execution algorithms.
An example of use (for the state machine) is a workflow process machine with
the
sequence of enumerated process steps defined in the current state of the
bitmap.
The question (for verification) is as follows.
---- You can do boolean operations on enum types but NOT on Enum types ----
Therefore it is IMPOSSIBLE to design a 'SIMPLE' enumerated state machine
WHEREBY the machine state has value semantics (for boolean operations)
and state semantics (meaningful state via enumeration (VS meaningless
boolean values that lack state semantics useful to programmers).
It is possible to design a COMPLEX and convoluted state machine in C#
(reminds me of early Java) where state and operational (delegate command
and control) are separate. Enums as keys to collections, Enums translated
to BitArrays comes to mind.
See the compiler error CS0019 below for an example of the poor value
semantics of Enum.
Since state machines are so fundamental I'm hoping I'm missing something
and you can do value semantics on Enum (it IS a structure).
If anyone has a better approach that would be very useful.
Thanks ahead of time for any replies or ideas. I've looked at various state
machine patterns (on the Internet) but none of the good ones use C# for this
reason (I suspect .
Shawnk
PS. The switcher needs to be configurable for various delegate execution
modes
(Value mapped sequential, bit mapped sequential, heuristic sequence/rule
engine,
etc). Thus the boolean operational aspect of the enumerated (state semantics)
machine execution semantics.
---------
using System;
public class Demo
{
[Flags]
private enum portState
{
Unknown = 0
, Open = 1
, Active = 2
, Secure = 4
}
static void Main(string[] args)
{
portState l_ps = portState.Unknown;
l_ps |= portState.Secure | portState.Open;
Console.WriteLine(l_ps.ToString());
// output is Open, Secure
Enum l_state_enm;
int l_initial_value = 3;
l_state_enm = (Enum) Enum.ToObject( typeof (portState),
l_initial_value );
Console.WriteLine( l_state_enm.GetType().Name );
Console.WriteLine( l_state_enm );
// output is portState
l_state_enm = portState.Unknown;
// Can not use boolean operations on Enumeration
l_state_enm |= portState.Secure | portState.Open; // Compiler Error CS0019
}
Enum class (relative to boolean operations).
If this is true then (thus and therefore) you can not design state machines in
C# where the machine can use the enumerated 'state' to execute (thus the term
'machine' as in 'state machine') the machine operations as this requires
boolean operations to be performed on the state.
.....
I would like to design a simple enumerated state machine
with a 'switcher' and an enumerated 'bit map' ([flags] enum) to hold the
state.
The 'switcher' is a replacement for a switch statement when state/operational
mapping's are from 10 to 100 (or more). This high operational cardinality is
commonly seen in metric analysis probes whose engine is based on a 'state
machine'.
Smaller operational cardinalities (1 to 10) are common for process engines
where the state controls a workflow process.
Thus a simple elegant state machine is a handy item to have around...
.....
The state machine would support any enum type (specified during construction).
So I need to perform boolean iteration and operations (switcher aspect
delegate
execution semantics) as well as enumerated state operations and iteration
(state
aspect of the machine - set, get, test, state semantics).
The 'switcher' maps various states to delegates. It executes the current
state based
on one of several execution algorithms.
An example of use (for the state machine) is a workflow process machine with
the
sequence of enumerated process steps defined in the current state of the
bitmap.
The question (for verification) is as follows.
---- You can do boolean operations on enum types but NOT on Enum types ----
Therefore it is IMPOSSIBLE to design a 'SIMPLE' enumerated state machine
WHEREBY the machine state has value semantics (for boolean operations)
and state semantics (meaningful state via enumeration (VS meaningless
boolean values that lack state semantics useful to programmers).
It is possible to design a COMPLEX and convoluted state machine in C#
(reminds me of early Java) where state and operational (delegate command
and control) are separate. Enums as keys to collections, Enums translated
to BitArrays comes to mind.
See the compiler error CS0019 below for an example of the poor value
semantics of Enum.
Since state machines are so fundamental I'm hoping I'm missing something
and you can do value semantics on Enum (it IS a structure).
If anyone has a better approach that would be very useful.
Thanks ahead of time for any replies or ideas. I've looked at various state
machine patterns (on the Internet) but none of the good ones use C# for this
reason (I suspect .
Shawnk
PS. The switcher needs to be configurable for various delegate execution
modes
(Value mapped sequential, bit mapped sequential, heuristic sequence/rule
engine,
etc). Thus the boolean operational aspect of the enumerated (state semantics)
machine execution semantics.
---------
using System;
public class Demo
{
[Flags]
private enum portState
{
Unknown = 0
, Open = 1
, Active = 2
, Secure = 4
}
static void Main(string[] args)
{
portState l_ps = portState.Unknown;
l_ps |= portState.Secure | portState.Open;
Console.WriteLine(l_ps.ToString());
// output is Open, Secure
Enum l_state_enm;
int l_initial_value = 3;
l_state_enm = (Enum) Enum.ToObject( typeof (portState),
l_initial_value );
Console.WriteLine( l_state_enm.GetType().Name );
Console.WriteLine( l_state_enm );
// output is portState
l_state_enm = portState.Unknown;
// Can not use boolean operations on Enumeration
l_state_enm |= portState.Secure | portState.Open; // Compiler Error CS0019
}