/// <summary> /// Initializes the state machine /// </summary> private void InitializeStateMachine() { _stateMachine = new ActiveStateMachine <NaoState, NaoCommand>(); for (int i = 0; i < this.StateCommands.Count - 1; i++) { var state = this.StateCommands.GetItem(i); var nextState = this.StateCommands.GetItem(i + 1); if (state.Value.Count > 1) {// if a state contains multiple tasks then do not pass to the next state unless it's the last task in the list for (int j = 0; j < state.Value.Count - 1; j++) { var command = state.Value.GetItem(j); _stateMachine.In(state.Key).On(command.Key).Goto(state.Key).Execute <KeyValuePair <NaoCommand, Dictionary <string, string> > >(ExecuteCommand); } } if (state.Value.Count > 0) {// move to the next state for the last task var command = state.Value.GetItem(state.Value.Count - 1); _stateMachine.In(state.Key).On(command.Key).Goto(nextState.Key).Execute <KeyValuePair <NaoCommand, Dictionary <string, string> > >(ExecuteCommand); } } _stateMachine.TransitionBegin += _stateMachine_TransitionBegin; _stateMachine.TransitionCompleted += _stateMachine_TransitionCompleted; _stateMachine.TransitionDeclined += _stateMachine_TransitionDeclined; }
/// <summary> /// Setup a new bot with some details. /// </summary> /// <param name="details"></param> /// <param name="extensions">any extensions you want on the state machine.</param> public LobbyBot(SteamUser.LogOnDetails details, params IExtension<States, Events>[] extensions) { reconnect = true; this.details = details; log = LogManager.GetLogger("LobbyBot " + details.Username); log.Debug("Initializing a new LobbyBot, username: " + details.Username); reconnectTimer.Elapsed += (sender, args) => { reconnectTimer.Stop(); fsm.Fire(Events.AttemptReconnect); }; fsm = new ActiveStateMachine<States, Events>(); foreach (var ext in extensions) fsm.AddExtension(ext); fsm.DefineHierarchyOn(States.Connecting) .WithHistoryType(HistoryType.None); fsm.DefineHierarchyOn(States.Connected) .WithHistoryType(HistoryType.None) .WithInitialSubState(States.Dota); fsm.DefineHierarchyOn(States.Dota) .WithHistoryType(HistoryType.None) .WithInitialSubState(States.DotaConnect) .WithSubState(States.DotaMenu) .WithSubState(States.DotaLobby); fsm.DefineHierarchyOn(States.Disconnected) .WithHistoryType(HistoryType.None) .WithInitialSubState(States.DisconnectNoRetry) .WithSubState(States.DisconnectRetry); fsm.DefineHierarchyOn(States.DotaLobby) .WithHistoryType(HistoryType.None); fsm.In(States.Connecting) .ExecuteOnEntry(InitAndConnect) .On(Events.Connected).Goto(States.Connected) .On(Events.Disconnected).Goto(States.DisconnectRetry) .On(Events.LogonFailSteamDown).Execute(SteamIsDown) .On(Events.LogonFailSteamGuard).Goto(States.DisconnectNoRetry) //.Execute(() => reconnect = false) .On(Events.LogonFailBadCreds).Goto(States.DisconnectNoRetry); fsm.In(States.Connected) .ExecuteOnExit(DisconnectAndCleanup) .On(Events.Disconnected).If(ShouldReconnect).Goto(States.Connecting) .Otherwise().Goto(States.Disconnected); fsm.In(States.Disconnected) .ExecuteOnEntry(DisconnectAndCleanup) .ExecuteOnExit(ClearReconnectTimer) .On(Events.AttemptReconnect).Goto(States.Connecting); fsm.In(States.DisconnectRetry) .ExecuteOnEntry(StartReconnectTimer); fsm.In(States.Dota) .ExecuteOnExit(DisconnectDota); fsm.In(States.DotaConnect) .ExecuteOnEntry(ConnectDota) .On(Events.DotaGCReady).Goto(States.DotaMenu); fsm.In(States.DotaMenu) .ExecuteOnEntry(SetOnlinePresence); fsm.In(States.DotaLobby) .ExecuteOnEntry(EnterLobbyChat) .ExecuteOnEntry(EnterBroadcastChannel) .On(Events.DotaLeftLobby).Goto(States.DotaMenu).Execute(LeaveChatChannel); fsm.Initialize(States.Connecting); }
public void EventsQueueing( IStateMachine <string, int> machine, AutoResetEvent signal) { const int FirstEvent = 0; const int SecondEvent = 1; "establish an active state machine with transitions"._(() => { signal = new AutoResetEvent(false); machine = new ActiveStateMachine <string, int>(); machine.In("A").On(FirstEvent).Goto("B"); machine.In("B").On(SecondEvent).Goto("C"); machine.In("C").ExecuteOnEntry(() => signal.Set()); machine.Initialize("A"); }); "when firing an event onto the state machine"._(() => { machine.Fire(FirstEvent); machine.Fire(SecondEvent); machine.Start(); }); "it should queue event at the end"._(() => signal.WaitOne(1000).Should().BeTrue("state machine should arrive at destination state")); }
/// <summary> /// Disposes the current object /// </summary> public void Dispose() { // dispose the robot _connection = null; _commandExecutionEngine.Dispose(); _stateMachine.Stop(); _stateMachine = null; }
public void CustomFactory( ActiveStateMachine <string, int> machine, StandardFactory <string, int> factory) { "establish a custom factory"._(() => { factory = A.Fake <StandardFactory <string, int> >(); }); "when creating an active state machine"._(() => { machine = new ActiveStateMachine <string, int>("_", factory); machine.In("initial").On(42).Goto("answer"); }); "it should use custom factory to create internal instances"._(() => A.CallTo(factory).MustHaveHappened()); }
public void CustomFactory( ActiveStateMachine<string, int> machine, StandardFactory<string, int> factory) { "establish a custom factory"._(() => { factory = A.Fake<StandardFactory<string, int>>(); }); "when creating an active state machine"._(() => { machine = new ActiveStateMachine<string, int>("_", factory); machine.In("initial").On(42).Goto("answer"); }); "it should use custom factory to create internal instances"._(() => A.CallTo(factory).MustHaveHappened()); }
public void DefaultStateMachineName( ActiveStateMachine <string, int> machine, StateMachineNameReporter reporter) { "establish an instantiated active state machine"._(() => { machine = new ActiveStateMachine <string, int>(); }); "establish a state machine reporter"._(() => { reporter = new StateMachineNameReporter(); }); "when the state machine report is generated"._(() => machine.Report(reporter)); "it should use the type of the state machine as name for state machine"._(() => reporter.StateMachineName .Should().Be("Appccelerate.StateMachine.ActiveStateMachine<System.String,System.Int32>")); }
public void DefaultStateMachineName( ActiveStateMachine<string, int> machine, StateMachineNameReporter reporter) { "establish an instantiated active state machine"._(() => { machine = new ActiveStateMachine<string, int>(); }); "establish a state machine reporter"._(() => { reporter = new StateMachineNameReporter(); }); "when the state machine report is generated"._(() => machine.Report(reporter)); "it should use the type of the state machine as name for state machine"._(() => reporter.StateMachineName .Should().Be("Appccelerate.StateMachine.ActiveStateMachine<System.String,System.Int32>")); }
private void Initialize() { _stateMachine = new ActiveStateMachine <NaoState, NaoCommand>(); _stateMachine.In(NaoState.Initialized) .On(NaoCommand.WalkToCheckpoint).Goto(NaoState.AtCheckpoint).Execute(WalkToMark) .On(NaoCommand.Stop).Goto(NaoState.Initialized); _stateMachine.In(NaoState.AtCheckpoint) .On(NaoCommand.GoToGrabLocation).Goto(NaoState.AtGrabLocation).Execute(GoToGrabLocation); _stateMachine.In(NaoState.AtGrabLocation) .On(NaoCommand.GoToLiftPosition).Goto(NaoState.InLiftPosition).Execute(GoToLiftPosition); _stateMachine.In(NaoState.InLiftPosition) .On(NaoCommand.LiftObject).Goto(NaoState.InWalkPosition).Execute(LiftObject) .On(NaoCommand.Stop).Goto(NaoState.InLiftPosition).Execute(Stop); _stateMachine.In(NaoState.InWalkPosition) .On(NaoCommand.WalkWithObject).Goto(NaoState.Terminated).Execute(WalkWithObject) .On(NaoCommand.Stop).Goto(NaoState.InWalkPosition); _stateMachine.TransitionBegin += _stateMachine_TransitionBegin; _stateMachine.TransitionCompleted += _stateMachine_TransitionCompleted; _stateMachine.TransitionDeclined += _stateMachine_TransitionDeclined; }
public void CustomStateMachineName( ActiveStateMachine <string, int> machine, StateMachineNameReporter reporter) { const string Name = "custom name"; "establish an instantiated active state machine with custom name"._(() => { machine = new ActiveStateMachine <string, int>(Name); }); "establish a state machine reporter"._(() => { reporter = new StateMachineNameReporter(); }); "when the state machine report is generated"._(() => machine.Report(reporter)); "it should use custom name for state machine"._(() => reporter.StateMachineName .Should().Be(Name)); }
public void CustomStateMachineName( ActiveStateMachine<string, int> machine, StateMachineNameReporter reporter) { const string Name = "custom name"; "establish an instantiated active state machine with custom name"._(() => { machine = new ActiveStateMachine<string, int>(Name); }); "establish a state machine reporter"._(() => { reporter = new StateMachineNameReporter(); }); "when the state machine report is generated"._(() => machine.Report(reporter)); "it should use custom name for state machine"._(() => reporter.StateMachineName .Should().Be(Name)); }
private void Start() { driver = GetComponent <CompDriver>(); shooter = GetComponent <CompShooter>(); stateMachine = GetComponent <ActiveStateMachine>(); health = stateMachine.states.Length; //最后一个状态用于加Buff if (isPlayer) { health--; } #if UNITY_EDITOR //编辑器模式自检 if (driver == null || shooter == null || stateMachine == null) { Debug.LogError("坦克组件不全!"); } if (health == 0) { Debug.LogError("请添加坦克状态!"); } #endif }
public void CustomStateMachineName( ActiveStateMachine <string, int> machine, StateMachineNameReporter reporter) { const string Name = "custom name"; "establish an instantiated active state machine with custom name".x(() => machine = new StateMachineDefinitionBuilder <string, int>() .WithInitialState("initial") .Build() .CreateActiveStateMachine(Name)); "establish a state machine reporter".x(() => { reporter = new StateMachineNameReporter(); }); "when the state machine report is generated".x(() => machine.Report(reporter)); "it should use custom name for state machine".x(() => reporter.StateMachineName .Should().Be(Name)); }
public void Destroy() { manager = null; if (fsm != null) { fsm.Stop(); fsm.ClearExtensions(); fsm = null; } reconnect = false; DisconnectAndCleanup(); user = null; client = null; friends = null; dota = null; manager = null; log.Debug("Bot destroyed."); }
private void Start() { asm = GetComponent <ActiveStateMachine>(); asm.ChangeState(id); }
public void PriorityEventsQueueing( IStateMachine<string, int> machine, AutoResetEvent signal) { const int FirstEvent = 0; const int SecondEvent = 1; "establish an active state machine with transitions"._(() => { signal = new AutoResetEvent(false); machine = new ActiveStateMachine<string, int>(); machine.In("A").On(SecondEvent).Goto("B"); machine.In("B").On(FirstEvent).Goto("C"); machine.In("C").ExecuteOnEntry(() => signal.Set()); machine.Initialize("A"); }); "when firing a priority event onto the state machine"._(() => { machine.Fire(FirstEvent); machine.FirePriority(SecondEvent); machine.Start(); }); "it should queue event at the front"._(() => signal.WaitOne(1000).Should().BeTrue("state machine should arrive at destination state")); }