x) Move some stuff around.
x) Make an immutable and non-immutable FSM
This commit is contained in:
parent
c95a809bfd
commit
aa0bbc3b2d
36
fsm/FSM.cs
36
fsm/FSM.cs
@ -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>>
|
public class State<T, CTX>
|
||||||
where T: State<T, CTX>
|
where T : State<T, CTX>
|
||||||
where CTX: Context
|
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>>
|
public class FSM<T, CTX, ST>
|
||||||
where T: FSM<T, CTX, ST>
|
where T : FSM<T, CTX, ST>
|
||||||
where CTX: Context
|
where CTX : Context
|
||||||
where ST: State<ST, CTX>
|
where ST : State<ST, CTX>
|
||||||
{
|
{
|
||||||
public CTX Context { get; private set; }
|
public CTX Context { get; private set; }
|
||||||
public ST State { get; private set; }
|
public ST State { get; private set; }
|
||||||
|
|
||||||
public FSM(CTX context, ST state)
|
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;
|
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
61
imm/FSM.cs
Normal 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
63
net/FSM.cs
Normal 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user