public void TestConstructor() { // Prepare datas Trigger[] setting_MonitoredTriggers = new Trigger[3] { new Trigger(), new Trigger(), new Trigger() }; Trigger[] expected_MonitoredTriggers = setting_MonitoredTriggers; Trigger[] expected_elements_MonitoredTriggers = (Trigger[])setting_MonitoredTriggers.Clone(); // Execute NormalState normal_state = new NormalState(setting_MonitoredTriggers); // Get results FieldInfo field_info = normal_state.GetType().GetField("MonitoredTriggers", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); Trigger[] actual_MonitoredTriggers = (Trigger[])field_info.GetValue(normal_state); // Validata Assert.Equal(expected_MonitoredTriggers, actual_MonitoredTriggers); Assert.Equal(expected_elements_MonitoredTriggers.Length, actual_MonitoredTriggers.Length); for (int i = 0; i < expected_elements_MonitoredTriggers.Length; i++) { Assert.Equal(expected_elements_MonitoredTriggers[i], actual_MonitoredTriggers[i]); } }
public void TestConstructor() { // Prepare datas Type type = typeof(Trigger); const bool expected_HasOccured = false; FieldInfo field_info = type.GetField("CountOfTrigger", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Static); uint expected_CountOfTrigger = (uint)field_info.GetValue(null) + 1; uint expected_Id = (uint)field_info.GetValue(null); // Execute Trigger trigger = new Trigger(); // Get result field_info = type.GetField("_HasOccured", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); bool actual_HasOccured = (bool)field_info.GetValue(trigger); field_info = type.GetField("<Id>k__BackingField", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); uint actual_Id = (uint)field_info.GetValue(trigger); field_info = type.GetField("CountOfTrigger", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Static); uint actual_CountOfTrigger = (uint)field_info.GetValue(null); // Validate Assert.Equal(expected_HasOccured, actual_HasOccured); Assert.Equal(expected_Id, actual_Id); Assert.Equal(expected_CountOfTrigger, actual_CountOfTrigger); }
/// <summary> /// 状態が<paramref name="currentState"/>のときにトリガ<paramref name="trigger"/>が発生した場合の, /// 起こる遷移を取得する. /// </summary> /// <param name="currentState">現在の状態</param> /// <param name="trigger">発生したトリガ</param> /// <returns><paramref name="currentState"/>で<paramref name="trigger"/>が発生したときに起こる遷移. /// 起こる遷移が無ければ, nullが返る.</returns> /// <exception cref="ArgumentNullException">引数<paramref name="currentState"/>がnull, /// または引数<paramref name="trigger"/>がnull.</exception> /// <exception cref="ArgumentOutOfRangeException">引数<paramref name="currentState"/>が, /// 状態遷移表に存在しない.</exception> public ITransition SearchTransition(State currentState, Trigger trigger) { if (currentState == null) { throw new ArgumentNullException("currentState"); } if (trigger == null) { throw new ArgumentNullException("trigger"); } if (MatrixData.ContainsKey(currentState) == false) { throw new ArgumentOutOfRangeException("currentState"); } if (MatrixData[currentState].ContainsKey(trigger)) { return MatrixData[currentState][trigger]; } return null; }
/// <summary> /// このステートマシンにトリガーを入力する /// </summary> /// <remarks> /// ステートマシンにトリガーを入力すると, /// このステートマシンが持っている状態遷移表に従って遷移が発生し, /// 現在の状態が遷移後の状態に更新される. /// トリガーに対する遷移が定義されていない場合は, 何もしない. /// </remarks> /// <param name="trigger">入力するトリガー</param> /// <exception cref="ArgumentNullException">引数<paramref name="trigger"/>がnull.</exception> /// <exception cref="InvalidOperationException">現在の状態が状態遷移表に存在しない. /// 状態遷移表の設定ミスなどによる.</exception> public void InputTrigger(Trigger trigger) { if (trigger == null) { throw new ArgumentNullException("trigger"); } System.Diagnostics.Debug.Assert(CurrentState != null); ITransition transition; try { transition = TransitionMatrixData.SearchTransition(CurrentState, trigger); } catch (ArgumentOutOfRangeException e) { throw new InvalidOperationException("現在の状態は, 状態遷移表に登録されていません.", e); } if (transition != null) { CurrentState = transition.Execute(); } }
public NormalState(Trigger[] triggers) : base(triggers) { }
public State(Trigger[] triggers) { MonitoredTriggers = triggers; }
public void TestConstructor() { // Prepare datas Trigger[] all_triggers = new Trigger[5] { new Trigger(), new Trigger(), new Trigger(), new Trigger(), new Trigger() }; TriggerSet1 = new Trigger[3] { all_triggers[0], all_triggers[1], all_triggers[2] }; TriggerSet2 = new Trigger[3] { all_triggers[0], all_triggers[2], all_triggers[4] }; TriggerSet3 = new Trigger[3] { all_triggers[2], all_triggers[3], all_triggers[4] }; State[] states = new State[3] { new NormalState(TriggerSet1), new NormalState(TriggerSet2), new NormalState(TriggerSet3) }; var input_transitionMatrix = new Dictionary<State, Dictionary<Trigger, ITransition>>() { { states[0], new Dictionary<Trigger, ITransition>() { { TriggerSet1[0], new Transition(states[2], null) }, { TriggerSet1[1], new Transition(states[2], null) }, { TriggerSet1[2], new Transition(states[1], null) } } }, { states[1], new Dictionary<Trigger, ITransition>() { { TriggerSet2[0], new Transition(states[0], null) }, { TriggerSet2[1], new Transition(states[1], null) }, { TriggerSet2[2], new Transition(states[2], null) } } }, { states[2], new Dictionary<Trigger, ITransition>() { { TriggerSet3[0], new Transition(states[2], null) }, { TriggerSet3[1], new Transition(states[2], null) }, { TriggerSet3[2], new Transition(states[0], null) } } } }; var expected_MatrixData = new Dictionary<State, Dictionary<Trigger, ITransition>>(input_transitionMatrix); // Execute TransitionMatrix transition_matrix = new TransitionMatrix(input_transitionMatrix); // Get result FieldInfo field_info = transition_matrix.GetType().GetField("MatrixData", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); var actual_MatrixData = (Dictionary<State, Dictionary<Trigger, ITransition>>)field_info.GetValue(transition_matrix); // Validate Assert.Same(input_transitionMatrix, actual_MatrixData); Assert.Equal(expected_MatrixData, actual_MatrixData); }
public void TestSearchTransitionUnregisteredState() { // Prepare datas Trigger[] TestTriggerSet = new Trigger[2] { All_Triggers[0], All_Triggers[3] }; State input_State = new NormalState(TestTriggerSet); Trigger input_Trigger = All_Triggers[0]; // Execute & Validate Assert.Throws<ArgumentOutOfRangeException>( "currentState", () => TransitionMatrixInstance.SearchTransition(input_State, input_Trigger)); }
public RegularInstanceTest() { All_Triggers = new Trigger[5] { new Trigger(), new Trigger(), new Trigger(), new Trigger(), new Trigger() }; Trigger[] TriggerSet1 = new Trigger[3] { All_Triggers[0], All_Triggers[1], All_Triggers[2] }; Trigger[] TriggerSet2 = new Trigger[3] { All_Triggers[0], All_Triggers[2], All_Triggers[4] }; Trigger[] TriggerSet3 = new Trigger[3] { All_Triggers[2], All_Triggers[3], All_Triggers[4] }; All_States = new State[3] { new NormalState(TriggerSet1), new NormalState(TriggerSet2), new NormalState(TriggerSet3) }; setting_MatrixData = new Dictionary<State, Dictionary<Trigger, ITransition>>() { { All_States[0], new Dictionary<Trigger, ITransition>() { { TriggerSet1[0], new Transition(All_States[2], null) }, { TriggerSet1[1], new Transition(All_States[2], null) }, { TriggerSet1[2], new Transition(All_States[1], null) } } }, { All_States[1], new Dictionary<Trigger, ITransition>() { { TriggerSet2[0], new Transition(All_States[0], null) }, { TriggerSet2[1], new Transition(All_States[1], null) }, { TriggerSet2[2], new Transition(All_States[2], null) } } }, { All_States[2], new Dictionary<Trigger, ITransition>() { { TriggerSet3[0], new Transition(All_States[2], null) }, { TriggerSet3[1], new Transition(All_States[2], null) }, { TriggerSet3[2], new Transition(All_States[0], null) } } } }; TransitionMatrixInstance = new TransitionMatrix(setting_MatrixData); }
public SingleInstanceTest() { trigger = new Trigger(); }