Skip to content

Commit

Permalink
change name
Browse files Browse the repository at this point in the history
  • Loading branch information
jiowchern committed Feb 28, 2024
1 parent d41aa24 commit 01f490d
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 92 deletions.
50 changes: 25 additions & 25 deletions Docs/README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace PinionCyber.StateManagement.Sample1
{
class StateA : PinionCyber.StateManagement.IState
{
void ISwitch.End()
void IActivable.Disbale()
{
// State ends
// State Disbales
}

void ISwitch.Start()
void IActivable.Enable()
{
// State starts
// State Enables
}

void IUpdate.Update()
Expand All @@ -40,14 +40,14 @@ namespace PinionCyber.StateManagement.Sample1

var machine = new PinionCyber.StateManagement.StateMachine();

// Switching states
// Changing states
var stateA = new StateA();
machine.Switch(stateA);
machine.Change(stateA);
var stateB = new StateB(); // Another state
machine.Switch(stateB);
machine.Change(stateB);

// Updating state
machine.Active().Update();
machine.Activer().Update();

// Clearing states
machine.Empty();
Expand All @@ -57,8 +57,8 @@ namespace PinionCyber.StateManagement.Sample1
## Cookbook
State machines are a design pattern. The above code handles the core part of a state machine. To fulfill a project's requirements, some control handling is needed. Below are the ways to use a state machine.

### State Switching
Use events to output the result of a state. Below is the switching between StateA and StateB.
### State Changeing
Use events to output the result of a state. Below is the Changeing between StateA and StateB.

```csharp
class StateA : IState
Expand All @@ -77,7 +77,7 @@ class StateB : IState
public event System.Action DoneEvent;
}
```
Switch between two states using ```DoneEvent```.
Change between two states using ```DoneEvent```.

```csharp
class Sample
Expand All @@ -86,22 +86,22 @@ class Sample
public Sample()
{
_Machine = new PinionCyber.StateManagement.StateMachine();
// First switch to StateA
// First Change to StateA
_ToStateA();
}

void _ToStateA()
{
var state = new StateA();
state.DoneEvent += _ToStateB; // When StateA completes, switch to StateB
_Machine.Switch(state);
state.DoneEvent += _ToStateB; // When StateA completes, Change to StateB
_Machine.Change(state);
}

void _ToStateB()
{
var state = new StateB();
state.DoneEvent += _ToStateA; // When StateB completes, switch back to StateA
_Machine.Switch(state);
state.DoneEvent += _ToStateA; // When StateB completes, Change back to StateA
_Machine.Change(state);
}
}
```
Expand Down Expand Up @@ -156,27 +156,27 @@ class Sample
_Machine = new PinionCyber.StateManagement.StateMachine();

}
// This method makes it easy to start the initial state after event registration
// This method makes it easy to Enable the initial state after event registration
// Or the class Sample itself can also be a state.
public void Start()
public void Enable()
{
// First switch to StateA
// First Change to StateA
_ToStateA();
}
void _ToStateA()
{
var state = new StateA();
state.DoneEvent += _ToStateB; // When StateA completes, switch to StateB
_Machine.Switch(state);
state.DoneEvent += _ToStateB; // When StateA completes, Change to StateB
_Machine.Change(state);
// State notification
AccessibleEvent?.Invoke(state);
}

void _ToStateB()
{
var state = new StateB();
state.DoneEvent += _ToStateA; // When StateB completes, switch back to StateA
_Machine.Switch(state);
state.DoneEvent += _ToStateA; // When StateB completes, Change back to StateA
_Machine.Change(state);
// State notification
GetterEvent?.Invoke(state);
}
Expand Down Expand Up @@ -205,9 +205,9 @@ class Controller
_CommandHandler = _CreateHandler(getter);
};
}
public void Start()
public void Enable()
{
_Sample.Start();
_Sample.Enable();
}
// Get state
public SampleState GetSampleState()
Expand Down
22 changes: 11 additions & 11 deletions Docs/README-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace PinionCyber.StateManagement.Sample1
{
class StateA : PinionCyber.StateManagement.IState
{
void ISwitch.End()
void IActivable.Disable()
{
// 狀態結束
}

void ISwitch.Start()
void IActivable.Enable()
{
// 狀態起始
}
Expand All @@ -41,9 +41,9 @@ namespace PinionCyber.StateManagement.Sample1

// 狀態切換
var stateA = new StateA();
machine.Switch(stateA);
machine.Change(stateA);
var stateB = new StateB(); // 另一個狀態
machine.Switch(stateB);
machine.Change(stateB);

// 狀態更新
machine.Activer().Update();
Expand Down Expand Up @@ -91,14 +91,14 @@ class Sample
{
var state = new StateA();
state.DoneEvent += _ToStateB; // 當 StateA 完成則切換到 StateB
_Machine.Switch(state);
_Machine.Change(state);
}

void _ToStateB()
{
var state = new StateB();
state.DoneEvent += _ToStateA;// 當 StateB 完成則切換到 StateA
_Machine.Switch(state);
_Machine.Change(state);
}
}
```
Expand Down Expand Up @@ -153,7 +153,7 @@ class Sample
}
// 創建這著方法方便事件註冊完後啟動初始狀態
// 或者 class Sample 本身也可以是個狀態.
public void Start()
public void Enable()
{
// 首先先切到 StateA
_ToStateA();
Expand All @@ -162,7 +162,7 @@ class Sample
{
var state = new StateA();
state.DoneEvent += _ToStateB; // 當 StateA 完成則切換到 StateB
_Machine.Switch(state);
_Machine.Change(state);
// 狀態通知
AccessibleEvent(state);
}
Expand All @@ -171,7 +171,7 @@ class Sample
{
var state = new StateB();
state.DoneEvent += _ToStateA;// 當 StateB 完成則切換到 StateA
_Machine.Switch(state);
_Machine.Change(state);
// 狀態通知
GetterEvent(state);
}
Expand Down Expand Up @@ -199,9 +199,9 @@ class Controller
_CommandHandler = _CreateHandler(getter);
};
}
public void Start()
public void Enable()
{
_Sample.Start();
_Sample.Enable();
}
// 取得狀態
public SampleState GetSampleState()
Expand Down
6 changes: 3 additions & 3 deletions PinionCyber.StateManagement.Tests/TestCallSwitch.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace PinionCyber.StateManagement.Tests
{
class TestCallSwitch : ISwitch
class TestCallSwitch : IActivable
{
public int EndCallCount;
public int StartCallCount;

void ISwitch.End()
void IActivable.Disable()
{
EndCallCount++;
}

void ISwitch.Start()
void IActivable.Enable()
{
StartCallCount++;
}
Expand Down
12 changes: 6 additions & 6 deletions PinionCyber.StateManagement.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ public void Setup()
}

[Test]
public void Switch()
public void ActiverChanger()
{
var state = new TestCallSwitch();
var controller = new Switcher<ISwitch>(new TestCallSwitch());
controller.Switch(state);
controller.Switch(new TestCallSwitch());
var controller = new ActiverChanger<IActivable>(new TestCallSwitch());
controller.Change(state);
controller.Change(new TestCallSwitch());


Assert.That(1,Is.EqualTo(state.EndCallCount));
Assert.That(1, Is.EqualTo(state.StartCallCount));
}

[Test]
public void StateWithEmpty()
public void StateMachine()
{
var state = new TestCallState();
var machine = new StateMachine();
machine.Switch(state);
machine.Change(state);
machine.Activer().Update();
machine.Empty();
Assert.That(1, Is.EqualTo(state.EndCallCount));
Expand Down
40 changes: 40 additions & 0 deletions PinionCyber.StateManagement/ActiverChanger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

namespace PinionCyber.StateManagement
{
public class ActiverChanger<T> where T : IActivable
{
T _Activable;
/// <summary>
/// An empty implementation is usually passed here.
/// This constructor calls the IActivable.Enable on the parameter.
/// </summary>
/// <param name="first"></param>
public ActiverChanger(T first)
{
_Activable = first;
_Activable.Enable();
}

/// <summary>
/// Changes the current activable object to the provided one and manages their active states.
/// It disables the current activable object before switching to the new one, which is then enabled.
/// </summary>
/// <param name="activer">The new activable object to switch to.</param>
public void Change(T activer)
{
_Activable.Disable();
_Activable = activer;
_Activable.Enable();
}

/// <summary>
/// Returns the current activable object.
/// </summary>
/// <returns>The current activable object.</returns>
public T Activer()
{
return _Activable;
}
}
}
8 changes: 8 additions & 0 deletions PinionCyber.StateManagement/IActivable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PinionCyber.StateManagement
{
public interface IActivable
{
void Enable();
void Disable();
}
}
2 changes: 1 addition & 1 deletion PinionCyber.StateManagement/IState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PinionCyber.StateManagement
{
public interface IState : ISwitch , IUpdate
public interface IState : IActivable , IUpdate
{
}
}
8 changes: 0 additions & 8 deletions PinionCyber.StateManagement/ISwitch.cs

This file was deleted.

12 changes: 8 additions & 4 deletions PinionCyber.StateManagement/PinionCyber.StateManagement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<Version>0.1.0</Version>
<Version>0.1.1</Version>
<Authors>jc</Authors>
<Company>PinionCyber</Company>
<Description>A very simple state machine.</Description>
<PackageTags>StateMachine</PackageTags>
<RepositoryUrl>https://github.com/jiowchern/PinionCyber.StateManagement</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
<None Remove="README.md" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>PinionCyber.StateManagement.Tests</_Parameter1>
Expand Down
2 changes: 0 additions & 2 deletions PinionCyber.StateManagement/README.md

This file was deleted.

8 changes: 4 additions & 4 deletions PinionCyber.StateManagement/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace PinionCyber.StateManagement
{

public class StateMachine : Switcher<IState>
public class StateMachine : ActiverChanger<IState>
{
class EmptyState : IState
{
void ISwitch.End()
void IActivable.Disable()
{

}

void ISwitch.Start()
void IActivable.Enable()
{

}
Expand All @@ -32,7 +32,7 @@ public StateMachine(IState first) : base(first)
}
public void Empty()
{
Switch(new EmptyState());
Change(new EmptyState());
}
}

Expand Down
Loading

0 comments on commit 01f490d

Please sign in to comment.