Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/PersistenceExample/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>

<supportedRuntime version="v2.0.50727"/></startup>
</configuration>
64 changes: 64 additions & 0 deletions src/PersistenceExample/PersistenceExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3C45B29F-222C-4914-A4F8-3775438A4070}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PersistenceExample</RootNamespace>
<AssemblyName>PersistenceExample</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Stateless\Stateless.csproj">
<Project>{a15eec02-60ec-4705-a58b-a6ecd55a9628}</Project>
<Name>Stateless</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
51 changes: 51 additions & 0 deletions src/PersistenceExample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Stateless;

namespace PersistenceExample
{
static class Program
{
static void Main()
{
try
{
const string on = "On";
const string off = "Off";
const char space = ' ';

Console.WriteLine("Press <space> to toggle the switch. Any other key will raise an error.");

var savedState = off;
Func<string> loadMethod = () =>
{
Console.WriteLine("(Persistence: state {0} has been loaded)", savedState);
return savedState;
};
Action<string> saveMethod = state =>
{
savedState = state;
Console.WriteLine("(Persistence: state {0} has been saved)", state);
};

var initialState = loadMethod();
var onOffSwitch = new StateMachine<string, char>(initialState, saveMethod);

onOffSwitch.Configure(off).Permit(space, on);
onOffSwitch.Configure(on).Permit(space, off);

while (true)
{
Console.WriteLine("Switch is in state: " + onOffSwitch.State);
var pressed = Console.ReadKey(true).KeyChar;
onOffSwitch.Fire(pressed);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
}
}
36 changes: 36 additions & 0 deletions src/PersistenceExample/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PersistenceExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PersistenceExample")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2a02c2e2-2c9a-433f-8e35-1e1e72e3625c")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
25 changes: 25 additions & 0 deletions src/Stateless.Tests/StateMachineFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ public void StateCanBeStoredExternally()
Assert.AreEqual(State.C, state);
}

[Test]
public void StateIsWrittenJustOnceOnEveryStateTransition()
{
var state = State.B;
var saveCount = 0;
Action<State> stateMutator = s =>
{
saveCount++;
state = s;
};
var sm = new StateMachine<State, Trigger>(State.B, stateMutator);
sm.Configure(State.B).Permit(Trigger.X, State.C);
sm.Configure(State.C).Permit(Trigger.Y, State.B);
Assert.AreEqual(State.B, sm.State);
Assert.AreEqual(State.B, state);
sm.Fire(Trigger.X);
Assert.AreEqual(State.C, sm.State);
Assert.AreEqual(State.C, state);
Assert.AreEqual(1, saveCount);
sm.Fire(Trigger.Y);
Assert.AreEqual(State.B, sm.State);
Assert.AreEqual(State.B, state);
Assert.AreEqual(2, saveCount);
}

[Test]
public void SubstateIsIncludedInCurrentState()
{
Expand Down
12 changes: 10 additions & 2 deletions src/Stateless.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stateless", "Stateless\Stateless.csproj", "{A15EEC02-60EC-4705-A58B-A6ECD55A9628}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stateless.Tests", "Stateless.Tests\Stateless.Tests.csproj", "{70AD8860-1F5E-4F78-9276-CAD498EDDF73}"
Expand All @@ -11,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelephoneCallExample", "Tel
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BugTrackerExample", "BugTrackerExample\BugTrackerExample.csproj", "{3A56311E-C3AC-4E34-A1DF-CA5042A31039}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersistenceExample", "PersistenceExample\PersistenceExample.csproj", "{3C45B29F-222C-4914-A4F8-3775438A4070}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -37,6 +41,10 @@ Global
{3A56311E-C3AC-4E34-A1DF-CA5042A31039}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A56311E-C3AC-4E34-A1DF-CA5042A31039}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A56311E-C3AC-4E34-A1DF-CA5042A31039}.Release|Any CPU.Build.0 = Release|Any CPU
{3C45B29F-222C-4914-A4F8-3775438A4070}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C45B29F-222C-4914-A4F8-3775438A4070}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C45B29F-222C-4914-A4F8-3775438A4070}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C45B29F-222C-4914-A4F8-3775438A4070}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions src/Stateless/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public StateMachine(TState initialState)
_stateMutator = s => reference.State = s;
}

/// <summary>
/// Construct a state machine with external storage (saving only).
/// </summary>
/// <param name="initialState">The initial state.</param>
/// <param name="stateMutator">An action that will be called to write new state values.</param>
public StateMachine(TState initialState, Action<TState> stateMutator)
{
var reference = new StateReference { State = initialState };
_stateAccessor = () => reference.State;
_stateMutator = s =>
{
reference.State = s;
stateMutator(s);
};
}

/// <summary>
/// The current state.
/// </summary>
Expand Down