x) Move some stuff around.

x) Make an immutable and non-immutable FSM
This commit is contained in:
Marc Hernandez 2023-11-19 14:29:53 -08:00
parent c95a809bfd
commit aa0bbc3b2d
3 changed files with 136 additions and 24 deletions

View File

@ -8,33 +8,30 @@ namespace fsm;
public record class Context : imm.Recorded<Context>
{
public class Context{
}
public record class State<T, CTX> : imm.Recorded<State<T, CTX>>
where T: State<T, CTX>
where CTX: Context
public class State<T, CTX>
where T : State<T, CTX>
where CTX : Context
{
virtual public (CTX, T) onEnter(CTX ctx, State<T, CTX> oldState)
virtual public void onEnter(CTX ctx, State<T, CTX> oldState)
{
return (ctx, (T)this);
}
virtual public (CTX, T) onExit(CTX ctx, State<T, CTX> newState)
virtual public void onExit(CTX ctx, State<T, CTX> newState)
{
return (ctx, (T)this);
}
}
public record class FSM<T, CTX, ST> : imm.Recorded<FSM<T, CTX, ST>>
where T: FSM<T, CTX, ST>
where CTX: Context
where ST: State<ST, CTX>
public class FSM<T, CTX, ST>
where T : FSM<T, CTX, ST>
where CTX : Context
where ST : State<ST, CTX>
{
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<T, CTX, ST> : imm.Recorded<FSM<T, CTX, ST>>
State = state;
}
public FSM<T, CTX, ST> 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,
};
}

61
imm/FSM.cs Normal file
View File

@ -0,0 +1,61 @@

using System;
namespace imm;
public record class Context : imm.Recorded<Context>
{
}
public record class State<T, CTX> : imm.Recorded<State<T, CTX>>
where T : State<T, CTX>
where CTX : Context
{
virtual public (CTX, T) onEnter(CTX ctx, State<T, CTX> oldState)
{
return (ctx, (T)this);
}
virtual public (CTX, T) onExit(CTX ctx, State<T, CTX> newState)
{
return (ctx, (T)this);
}
}
public record class FSM<T, CTX, ST> : imm.Recorded<FSM<T, CTX, ST>>
where T : FSM<T, CTX, ST>
where CTX : Context
where ST : State<ST, CTX>
{
public CTX Context { get; private set; }
public ST State { get; private set; }
public FSM(CTX context, ST state)
{
Context = context;
State = state;
}
public FSM<T, CTX, ST> Transition(ST newState)
{
var (newOldCTX, oldState) = State.onExit(Context, newState);
var (newCTX, storeState) = newState.onEnter(newOldCTX, oldState);
return this with
{
Context = newCTX,
State = storeState,
};
}
}

63
net/FSM.cs Normal file
View File

@ -0,0 +1,63 @@

using System;
namespace net;
public class Context{
}
public class State<T, CTX>
where T : State<T, CTX>
where CTX : Context
{
virtual public void onEnter(CTX ctx, State<T, CTX> oldState)
{
}
virtual public void onExit(CTX ctx, State<T, CTX> newState)
{
}
}
public class PotentialState<T, CTX> : State<T, CTX>
where T : State<T, CTX>
where CTX : Context
{
virtual public void onEnter(CTX ctx, State<T, CTX> oldState)
{
}
virtual public void onExit(CTX ctx, State<T, CTX> newState)
{
}
}
public class FSM<T, CTX, ST>
where T : FSM<T, CTX, ST>
where CTX : Context
where ST : State<ST, CTX>
{
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)
{
}
}