From aa0bbc3b2d850f44f7297c832ea3904fc0e1a711 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 19 Nov 2023 14:29:53 -0800 Subject: [PATCH] x) Move some stuff around. x) Make an immutable and non-immutable FSM --- fsm/FSM.cs | 36 +++++++++++-------------------- imm/FSM.cs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ net/FSM.cs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 24 deletions(-) create mode 100644 imm/FSM.cs create mode 100644 net/FSM.cs diff --git a/fsm/FSM.cs b/fsm/FSM.cs index e13c147..afe3cec 100644 --- a/fsm/FSM.cs +++ b/fsm/FSM.cs @@ -8,33 +8,30 @@ namespace fsm; -public record class Context : imm.Recorded -{ +public class Context{ } -public record class State : imm.Recorded> - where T: State - where CTX: Context +public class State + where T : State + where CTX : Context { - virtual public (CTX, T) onEnter(CTX ctx, State oldState) + virtual public void onEnter(CTX ctx, State oldState) { - return (ctx, (T)this); } - virtual public (CTX, T) onExit(CTX ctx, State newState) + virtual public void onExit(CTX ctx, State newState) { - return (ctx, (T)this); } } -public record class FSM : imm.Recorded> - where T: FSM - where CTX: Context - where ST: State +public class FSM + where T : FSM + where CTX : Context + where ST : State { public CTX Context { get; private set; } - public ST State { get; private set; } + public ST State { get; private set; } public FSM(CTX context, ST state) { @@ -42,17 +39,8 @@ public record class FSM : imm.Recorded> State = state; } - public FSM Transition(ST newState) + public void Transition(ST newState) { - var (newOldCTX, oldState) = State.onExit(Context, newState); - - var (newCTX, storeState) = newState.onEnter(newOldCTX, oldState); - - return this with - { - Context = newCTX, - State = storeState, - }; } diff --git a/imm/FSM.cs b/imm/FSM.cs new file mode 100644 index 0000000..44bf500 --- /dev/null +++ b/imm/FSM.cs @@ -0,0 +1,61 @@ + + +using System; + + + +namespace imm; + + + +public record class Context : imm.Recorded +{ + +} + +public record class State : imm.Recorded> + where T : State + where CTX : Context +{ + virtual public (CTX, T) onEnter(CTX ctx, State oldState) + { + return (ctx, (T)this); + } + + virtual public (CTX, T) onExit(CTX ctx, State newState) + { + return (ctx, (T)this); + } +} + +public record class FSM : imm.Recorded> + where T : FSM + where CTX : Context + where ST : State +{ + public CTX Context { get; private set; } + public ST State { get; private set; } + + public FSM(CTX context, ST state) + { + Context = context; + State = state; + } + + public FSM Transition(ST newState) + { + var (newOldCTX, oldState) = State.onExit(Context, newState); + + var (newCTX, storeState) = newState.onEnter(newOldCTX, oldState); + + return this with + { + Context = newCTX, + State = storeState, + }; + } + + + +} + diff --git a/net/FSM.cs b/net/FSM.cs new file mode 100644 index 0000000..2f3a050 --- /dev/null +++ b/net/FSM.cs @@ -0,0 +1,63 @@ + + +using System; + + + +namespace net; + + + +public class Context{ + +} + +public class State + where T : State + where CTX : Context +{ + virtual public void onEnter(CTX ctx, State oldState) + { + } + + virtual public void onExit(CTX ctx, State newState) + { + } +} + +public class PotentialState : State + where T : State + where CTX : Context +{ + virtual public void onEnter(CTX ctx, State oldState) + { + } + + virtual public void onExit(CTX ctx, State newState) + { + } +} + + +public class FSM + where T : FSM + where CTX : Context + where ST : State +{ + public CTX Context { get; private set; } + public ST State { get; private set; } + + public FSM(CTX context, ST state) + { + Context = context; + State = state; + } + + public void Transition(ST newState) + { + } + + + +} +