Skip to content

Commit

Permalink
Transition changes
Browse files Browse the repository at this point in the history
Added a method to return a transition to a completion transition.
Added a When overload that can evaluate just the context.
Do not trigger a model bootstrap when changing transition predicates
(as they do not need it).
  • Loading branch information
mesmo committed Oct 11, 2014
1 parent 68572b5 commit 2398b2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Welcome to state.cs

The current stable release is 5.0.0.
The current stable release is 5.0.1.

If you're using state.cs I'd love to hear about it; please e-mail me at [email protected]

Expand Down
36 changes: 28 additions & 8 deletions src/Transition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,55 @@ internal Transition( Vertex<TContext> source, Vertex<TContext> target ) {

this.Source = source;
this.Target = target;
this.Predicate = ( context, message ) => message == this.Source;
// this.Predicate = ( context, message ) => message == this.Source;

this.Completion();
}

/// <summary>
/// Adds a typed guard condition to a transition that can evaluate both the state machine context and the triggering message.
/// </summary>
/// <typeparam name="TMessage">The type of the message that can trigger the transition.</typeparam>
/// <param name="guard">The guard condition that must evaluate true for the transition to be traversed.</param>
/// <param name="guard">The guard condition taking both the state machine context and the message that must evaluate true for the transition to be traversed.</param>
/// <returns>Returns the transition.</returns>
public Transition<TContext> When<TMessage>( Func<TContext, TMessage, Boolean> guard ) where TMessage : class {
this.Predicate = ( context, message ) => message is TMessage && guard( context, message as TMessage );

this.Source.Root.Clean = false;

return this;
}

/// <summary>
/// Adds a typed guard condition to a transition that can evaluate the triggering message.
/// </summary>
/// <typeparam name="TMessage">The type of the message that can trigger the transition.</typeparam>
/// <param name="guard">The guard condition that must evaluate true for the transition to be traversed.</param>
/// <param name="guard">A guard condition taking the message that must evaluate true for the transition to be traversed.</param>
/// <returns>Returns the transition.</returns>
public Transition<TContext> When<TMessage>( Func<TMessage, Boolean> guard ) where TMessage : class {
this.Predicate = ( context, message ) => message is TMessage && guard( message as TMessage );

this.Source.Root.Clean = false;
return this;
}

/// <summary>
/// Adds a guard condition to a transition that can evaluate the triggering message.
/// </summary>
/// <param name="guard">A guard condition, taking just the state machine context, that must evaluate true for the transition to be traversed.</param>
/// <returns>Returns the transition.</returns>
public Transition<TContext> When( Func<TContext, Boolean> guard ) {
this.Predicate = ( context, message ) => guard( context );

return this;
}

/// <summary>
/// Turns a transition into a completion transition
/// </summary>
/// <returns>Returns the transition.</returns>
/// <remarks>
/// All transitions are completion tansitions when initially created; this method can be used to return a transition to be a completion transition if prior calls to When or Else have been made.
/// </remarks>
public Transition<TContext> Completion() {
this.Predicate = ( context, message ) => message == this.Source;

return this;
}
Expand All @@ -137,8 +159,6 @@ public Transition<TContext> Else() {

this.Predicate = Transition<TContext>.IsElse;

this.Source.Root.Clean = false;

return this;
}

Expand Down

0 comments on commit 2398b2a

Please sign in to comment.