You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enter A1 by (_machine.Configure(State.A.ToString()) .InitialTransition(SubAStatueA.A1.ToString()))
not by Trigger the result is A1 outer A1 -----------------------bad
Input initial state
A1
A1-0
input command
EA2 A2 in A -----------------------good A2 in A2 -----------------------good
it can be trigger:EA3
it can be trigger:EB
A2-0
input command
EA3
it can be trigger:EC
B-0
input command
EC
it can be trigger:EA
C-0
input command
EA A1 in A -----------------------good A1 outer A1 -----------------------bad
it can be trigger:EA1
it can be trigger:EA2
it can be trigger:EB
A1-0
input command
Code Sample
using System;using System.Threading.Tasks;using Stateless;namespaceConsoleApp30{internalenumTrigger{EA,EB,EC}publicenumState{A,B,C}publicenumSubAStatueA{A1,A2}internalenumSubTriggerA{EA1,EA2,EA3}internalclassObjState{publicEnumState{get;set;}publicintValue{get;set;}publicoverridestringToString(){return$"{State}-{Value}";}}publicclassTest{privatereadonlyStateMachine<string,string>_machine;privatereadonlyObjState_objState;publicTest(EnumpreCommand){if(preCommand==null)thrownew ArgumentNullException(nameof(preCommand));_objState=new ObjState {State=preCommand};_machine=newStateMachine<string,string>(()=> _objState.State.ToString(),s =>{ _objState.State = StrToEnum(s);});vardoCTriggerSetPara= _machine.SetTriggerParameters<int>(Trigger.EC.ToString());//A - B
_machine.Configure(State.A.ToString())//------------- may be bug --------------------------.InitialTransition(SubAStatueA.A1.ToString()).Permit(Trigger.EB.ToString(), State.B.ToString());//A1(A) - B or A1(A) -A2
_machine.Configure(SubAStatueA.A1.ToString()).SubstateOf(State.A.ToString()).OnEntryAsync(p => Task.Run(()=>{vartmp1= _machine.IsInState(State.A.ToString())?"in":"outer"; Console.WriteLine("A1 "+tmp1+" A ");vartmp2= _machine.IsInState(SubAStatueA.A1.ToString())?"in":"outer"; Console.WriteLine("A1 "+tmp2+"A1 ");})).Permit(SubTriggerA.EA1.ToString(), State.B.ToString()).Permit(SubTriggerA.EA2.ToString(), SubAStatueA.A2.ToString());//A2(A) - B
_machine.Configure(SubAStatueA.A2.ToString()).SubstateOf(State.A.ToString()).OnEntryAsync(p => Task.Run(()=>{vartmp1= _machine.IsInState(State.A.ToString())?"in":"outer"; Console.WriteLine("A2 "+tmp1+" A ");vartmp2= _machine.IsInState(SubAStatueA.A2.ToString())?"in":"outer"; Console.WriteLine("A2 "+tmp2+" A2 ");})).Permit(SubTriggerA.EA3.ToString(), State.B.ToString());//B - C
_machine.Configure(State.B.ToString()).Permit(Trigger.EC.ToString(), State.C.ToString());//C - A
_machine.Configure(State.C.ToString()).OnEntryFromAsync(doCTriggerSetPara,p => Task.Run(()=>{ _objState.Value =p;})).Permit(Trigger.EA.ToString(), State.A.ToString());
_machine.OnUnhandledTrigger((state,trigger)=>{});}publicstatic Enum StrToEnum(stringstr){Enumfoo=null;if(Enum.TryParse(str,out State state))foo=state;elseif(Enum.TryParse(str,out SubAStatueA subAStatueA))foo=subAStatueA;returnfoo;}publicstaticasync Task DoAsync(){
Console.WriteLine("Input initial state");varpreCommand= Console.ReadLine();varfoo= StrToEnum(preCommand);varbar=new Test(foo);while(true){
Console.WriteLine(bar.GetState());
Console.WriteLine("input command");varcommand= Console.ReadLine();await bar._machine.FireAsync(command);foreach(var machinePermittedTrigger in bar._machine.PermittedTriggers)
Console.WriteLine("it can be trigger:"+machinePermittedTrigger);}}publicstringGetState(){return _objState?.ToString();}}}
The API to test is IsInState
Let's look at the test results
Enter A1 by (_machine.Configure(State.A.ToString()) .InitialTransition(SubAStatueA.A1.ToString()))
not by Trigger the result is A1 outer A1 -----------------------bad
Code Sample
The text was updated successfully, but these errors were encountered: