Skip to content

Commit

Permalink
Tidy up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Oct 29, 2015
1 parent 57f76dd commit d66b561
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 56 deletions.
9 changes: 4 additions & 5 deletions src/StateMechanic/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Event : IEvent, IEventInternal<IInvokableTransition>
/// <summary>
/// Gets the name assigned to this event
/// </summary>
public string Name => this.innerEvent.Name;
public string Name { get; }

/// <summary>
/// Gets the state machine associated with this event. This event can be used to trigger transitions on its parent state machine, or any of its child state machines
Expand All @@ -22,16 +22,17 @@ public class Event : IEvent, IEventInternal<IInvokableTransition>

internal Event(string name, IEventDelegate parentStateMachine)
{
this.Name = name;
this.parentStateMachine = parentStateMachine;
this.innerEvent = new EventInner<Event, IInvokableTransition>(name, parentStateMachine);
this.innerEvent = new EventInner<Event, IInvokableTransition>();
}

internal void AddTransition(IState state, IInvokableTransition transition)
{
this.innerEvent.AddTransition(state, transition);
}

internal List<IInvokableTransition> GetTransitionsForState(IState state)
internal IEnumerable<IInvokableTransition> GetTransitionsForState(IState state)
{
return this.innerEvent.GetTransitionsForState(state);
}
Expand All @@ -49,7 +50,6 @@ internal List<IInvokableTransition> GetTransitionsForState(IState state)
public bool TryFire()
{
return this.parentStateMachine.RequestEventFireFromEvent(this, EventFireMethod.TryFire);
// return this.innerEvent.Fire(transition => transition.TryInvoke(), this, EventFireMethod.TryFire);
}

/// <summary>
Expand All @@ -65,7 +65,6 @@ public bool TryFire()
public void Fire()
{
this.parentStateMachine.RequestEventFireFromEvent(this, EventFireMethod.Fire);
// this.innerEvent.Fire(transition => transition.TryInvoke(), this, EventFireMethod.Fire);
}

/// <summary>
Expand Down
41 changes: 5 additions & 36 deletions src/StateMechanic/EventInner.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using System.Collections.Generic;
using System.Linq;

namespace StateMechanic
{
internal class EventInner<TEvent, TTransition>
{
private readonly Dictionary<IState, List<TTransition>> transitions = new Dictionary<IState, List<TTransition>>();

public readonly string Name;
public readonly IEventDelegate parentStateMachine;

public EventInner(string name, IEventDelegate parentStateMachine)
{
this.Name = name;
this.parentStateMachine = parentStateMachine;
}

public void AddTransition(IState state, TTransition transitionInvocation)
{
List<TTransition> transitions;
Expand All @@ -27,36 +19,13 @@ public void AddTransition(IState state, TTransition transitionInvocation)
transitions.Add(transitionInvocation);
}

public List<TTransition> GetTransitionsForState(IState state)
public IEnumerable<TTransition> GetTransitionsForState(IState state)
{
List<TTransition> transitions;
this.transitions.TryGetValue(state, out transitions);
if (!this.transitions.TryGetValue(state, out transitions))
return Enumerable.Empty<TTransition>();

return transitions;
}

//public bool Fire(Func<TTransition, bool> transitionInvoker, IEvent parentEvent, EventFireMethod eventFireMethod)
//{
// this.parentStateMachine.RequestEventFireFromEvent()

// return this.parentStateMachine.RequestEventFireFromEvent(parentEvent, state =>
// {
// List<TTransition> transitions;
// if (!this.transitions.TryGetValue(state, out transitions))
// return false;

// // Keep trying until one works (i.e. has a guard that lets it execute)
// bool anyFound = false;
// foreach (var transition in transitions)
// {
// if (transitionInvoker(transition))
// {
// anyFound = true;
// break;
// }
// }

// return anyFound;
// }, eventFireMethod);
//}
}
}
9 changes: 4 additions & 5 deletions src/StateMechanic/Event`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Event<TEventData> : IEvent
/// <summary>
/// Gets the name assigned to this event
/// </summary>
public string Name => this.innerEvent.Name;
public string Name { get; }

/// <summary>
/// Gets the state machine associated with this event. This event can be used to trigger transitions on its parent state machine, or any of its child state machines
Expand All @@ -24,15 +24,16 @@ public class Event<TEventData> : IEvent
internal Event(string name, IEventDelegate parentStateMachine)
{
this.parentStateMachine = parentStateMachine;
this.innerEvent = new EventInner<Event<TEventData>, IInvokableTransition<TEventData>>(name, parentStateMachine);
this.Name = name;
this.innerEvent = new EventInner<Event<TEventData>, IInvokableTransition<TEventData>>();
}

internal void AddTransition(IState state, IInvokableTransition<TEventData> transition)
{
this.innerEvent.AddTransition(state, transition);
}

internal List<IInvokableTransition<TEventData>> GetTransitionsForState(IState state)
internal IEnumerable<IInvokableTransition<TEventData>> GetTransitionsForState(IState state)
{
return this.innerEvent.GetTransitionsForState(state);
}
Expand All @@ -51,7 +52,6 @@ internal List<IInvokableTransition<TEventData>> GetTransitionsForState(IState st
public bool TryFire(TEventData eventData)
{
return this.parentStateMachine.RequestEventFireFromEvent(this, eventData, EventFireMethod.TryFire);
// return this.innerEvent.Fire(transition => transition.TryInvoke(eventData), this, EventFireMethod.TryFire);
}

/// <summary>
Expand All @@ -68,7 +68,6 @@ public bool TryFire(TEventData eventData)
public void Fire(TEventData eventData)
{
this.parentStateMachine.RequestEventFireFromEvent(this, eventData, EventFireMethod.Fire);
// this.innerEvent.Fire(transition => transition.TryInvoke(eventData), this, EventFireMethod.Fire);
}

void IEvent.Fire()
Expand Down
12 changes: 2 additions & 10 deletions src/StateMechanic/TransitionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public EventTransitionInvoker(Event @event, EventFireMethod eventFireMethod)

public bool TryInvoke(TState sourceState)
{
var transitions = this.@event.GetTransitionsForState(sourceState);
if (transitions == null)
return false;

foreach (var transition in transitions)
foreach (var transition in this.@event.GetTransitionsForState(sourceState))
{
if (transition.TryInvoke())
return true;
Expand All @@ -53,11 +49,7 @@ public EventTransitionInvokerWithData(Event<TEventData> @event, EventFireMethod

public bool TryInvoke(TState sourceState)
{
var transitions = this.@event.GetTransitionsForState(sourceState);
if (transitions == null)
return false;

foreach (var transition in transitions)
foreach (var transition in this.@event.GetTransitionsForState(sourceState))
{
if (transition.TryInvoke(this.eventData))
return true;
Expand Down

0 comments on commit d66b561

Please sign in to comment.