Skip to content

Instantly share code, notes, and snippets.

@ashshak ashshak/MyFsm.cs
Last active Nov 5, 2015

Embed
What would you like to do?
namespace AkkaFsm
{
class MyFsm : FSM<MyState, MyData>
{
public MyFsm()
{
MyStates myStates = new MyStates();
StartWith(myStates.Ready, new MyData());
When(myStates.Ready, @event =>
{
State<MyState, MyData> nextState=null;
@event.FsmEvent.Match()
.With<MyEvents.Calculate>(calculate =>
{
StateData.Item1 = calculate.Item1;
StateData.Item2 = calculate.Item2;
nextState = GoTo(myStates.Calculating, StateData);
})
.Default(o =>
{
nextState = Stay();
});
return nextState;
});
When(myStates.Calculating, @event =>
{
StateData.Sum = StateData.Item1 + StateData.Item2;
return GoTo(myStates.Calculated, StateData);
});
Initialize();
}
}
class MyState
{
}
class ReadyState : MyState
{
}
class Calculating : MyState
{
}
class Calculated : MyState
{
}
class MyStates
{
public MyStates()
{
Ready = new ReadyState();
Calculating = new Calculating();
Calculated = new Calculated();
}
public ReadyState Ready { get; }
public Calculating Calculating { get; }
public Calculated Calculated { get; }
}
class MyData
{
public int Item1 { get; set; }
public int Item2 { get; set; }
public int Sum { get; set; }
}
class MyEvents
{
public class Calculate
{
public Calculate(int item1, int item2)
{
Item1 = item1;
Item2 = item2;
}
public int Item1 { get; }
public int Item2 { get; }
}
}
}
class Program
{
static void Main(string[] args)
{
var system = ActorSystem.Create("FsmTest");
var fsmActor = system.ActorOf(Props.Create(() => new MyFsm()));
fsmActor.Tell(new MyEvents.Calculate(1,2));
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.