Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/get transitions from state #8

Merged
Merged
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
5 changes: 5 additions & 0 deletions nTransition/Transition Machine/TransitionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public IEnumerable<TState> States
}
}

public IEnumerable<Transition<TState>> StateTransitions
{
get { return _stateTransitions.Distinct(); }
}

public TState Between(TState current, TState newState)
{
var stateTransitions = _stateTransitions.Where(s => s.FromState.CompareTo(current) == 0 && s.ToState.CompareTo( newState) == 0 );
Expand Down
19 changes: 19 additions & 0 deletions nTransition/Transition Machine/TransitionMachine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace eStateMachine
{
Expand Down Expand Up @@ -35,5 +36,23 @@ public TState Between(TState fromState, TState toState)
{
return Configuration.Between(fromState, toState);
}


/// <summary>
/// Get possible states that can be transitioned to
/// </summary>
/// <param name="fromState">State to ne transitioned from</param>
/// <returns>IEnumerable of states that can be transitioned to</returns>
public IEnumerable<TState> GetTransitionsFromState(TState fromState)
{
//I'm not sure how efficient this is
foreach (var stateTransition in Configuration.StateTransitions)
{
if (stateTransition.FromState.Equals(fromState))
{
yield return stateTransition.ToState;
}
}
}
}
}
18 changes: 18 additions & 0 deletions nTransitionTests/UseATransitionMachineTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using eStateMachine.Interfaces;
using NUnit.Framework;
Expand Down Expand Up @@ -64,5 +65,22 @@ public void SimpleCircularMachineDoesNotThrow()
t.State = 1;
});
}

[Test]
public void CanGetPossibleTransitionsFromState()
{
var TestMachine = new TransitionMachine<int>(c =>
{
c.From(1).To(2).Done();
c.From(1).To(3).Done();
c.From(2).To(4).Done();
c.From(3).To(4).Done();
c.From(4).To(1).Done();
});
var possibleTransitions = TestMachine.GetTransitionsFromState(1).ToArray();
possibleTransitions.Length.ShouldBe(2);
possibleTransitions[0].ShouldBe(2);
possibleTransitions[1].ShouldBe(3);
}
}
}
2 changes: 1 addition & 1 deletion nTransitionTests/nTransitionTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\eStateMachine\nTransition.csproj">
<ProjectReference Include="..\nTransition\nTransition.csproj">
<Project>{78fe77df-239c-4288-92f3-5a9aa05aa848}</Project>
<Name>nTransition</Name>
</ProjectReference>
Expand Down