Skip to content

Commit 7223486

Browse files
authored
Merge pull request #590 from mclift/bugfix/reentry-transition-missing-function-in-dotgraph
Show state entry function in re-entry transition in dot graph
2 parents c896d66 + 6b2cb9f commit 7223486

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

src/Stateless/Graph/GraphStyleBase.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,11 @@ public virtual List<string> FormatAllTransitions(List<Transition> transitions)
7777
line = FormatOneTransition(stay.SourceState.NodeName, stay.Trigger.UnderlyingTrigger.ToString(),
7878
null, stay.SourceState.NodeName, stay.Guards.Select(x => x.Description));
7979
}
80-
else if (stay.SourceState.EntryActions.Count == 0)
81-
{
82-
line = FormatOneTransition(stay.SourceState.NodeName, stay.Trigger.UnderlyingTrigger.ToString(),
83-
null, stay.SourceState.NodeName, stay.Guards.Select(x => x.Description));
84-
}
8580
else
8681
{
87-
// There are entry functions into the state, so call out that this transition
88-
// does invoke them (since normally a transition back into the same state doesn't)
8982
line = FormatOneTransition(stay.SourceState.NodeName, stay.Trigger.UnderlyingTrigger.ToString(),
90-
stay.SourceState.EntryActions, stay.SourceState.NodeName, stay.Guards.Select(x => x.Description));
83+
stay.DestinationEntryActions.Select(x => x.Method.Description),
84+
stay.SourceState.NodeName, stay.Guards.Select(x => x.Description));
9185
}
9286
}
9387
else

src/Stateless/Graph/StateGraph.cs

+10
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ void AddTransitions(StateMachineInfo machineInfo)
141141
Transitions.Add(stay);
142142
fromState.Leaving.Add(stay);
143143
fromState.Arriving.Add(stay);
144+
145+
// If the reentrant transition causes the state's entry action to be executed, this is shown
146+
// explicity in the state graph by adding it to the DestinationEntryActions list.
147+
if (stay.ExecuteEntryExitActions)
148+
{
149+
foreach (var action in stateInfo.EntryActions.Where(a => a.FromTrigger is null))
150+
{
151+
stay.DestinationEntryActions.Add(action);
152+
}
153+
}
144154
}
145155
else
146156
{

test/Stateless.Tests/DotGraphFixture.cs

+59
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,65 @@ public void Initial_State_Not_Changed_After_Trigger_Fired()
582582
Assert.Equal(expected, dotGraph);
583583
}
584584

585+
[Fact]
586+
public void Reentrant_Transition_Shows_Entry_Action_When_Action_Is_Configured_With_OnEntryFrom()
587+
{
588+
var expected = Prefix(Style.UML)
589+
+ Box(Style.UML, "A")
590+
+ Box(Style.UML, "B")
591+
+ Line("A", "B", "X / OnEntry")
592+
+ Line("B", "B", "X / OnEntry")
593+
+ suffix;
594+
595+
var sm = new StateMachine<State, Trigger>(State.A);
596+
597+
sm.Configure(State.A)
598+
.Permit(Trigger.X, State.B);
599+
600+
var list = new List<string>();
601+
sm.Configure(State.B)
602+
.OnEntryFrom(Trigger.X, OnEntry, entryActionDescription: "OnEntry")
603+
.PermitReentry(Trigger.X);
604+
605+
string dotGraph = UmlDotGraph.Format(sm.GetInfo());
606+
607+
#if WRITE_DOTS_TO_FOLDER
608+
System.IO.File.WriteAllText(DestinationFolder + "Reentrant_Transition_Shows_Entry_Action_When_Action_Is_Configured_With_OnEntryFrom.dot", dotGraph);
609+
#endif
610+
611+
Assert.Equal(expected, dotGraph);
612+
}
613+
614+
[Fact]
615+
public void Reentrant_Transition_Shows_Entry_Action_When_Action_Is_Configured_With_OnEntryFrom_And_Trigger_Has_Parameter()
616+
{
617+
var expected = Prefix(Style.UML)
618+
+ Box(Style.UML, "A")
619+
+ Box(Style.UML, "B")
620+
+ Line("A", "B", "X / LogTrigger")
621+
+ Line("B", "B", "X / LogTrigger")
622+
+ suffix;
623+
624+
var sm = new StateMachine<State, Trigger>(State.A);
625+
var triggerX = sm.SetTriggerParameters<string>(Trigger.X);
626+
627+
sm.Configure(State.A)
628+
.Permit(Trigger.X, State.B);
629+
630+
var list = new List<string>();
631+
sm.Configure(State.B)
632+
.OnEntryFrom(triggerX, list.Add, entryActionDescription: "LogTrigger")
633+
.PermitReentry(Trigger.X);
634+
635+
string dotGraph = UmlDotGraph.Format(sm.GetInfo());
636+
637+
#if WRITE_DOTS_TO_FOLDER
638+
System.IO.File.WriteAllText(DestinationFolder + "Reentrant_Transition_Shows_Entry_Action_When_Action_Is_Configured_With_OnEntryFrom_And_Trigger_Has_Parameter.dot", dotGraph);
639+
#endif
640+
641+
Assert.Equal(expected, dotGraph);
642+
}
643+
585644
private void TestEntryAction() { }
586645
private void TestEntryActionString(string val) { }
587646
private State DestinationSelector() { return State.A; }

0 commit comments

Comments
 (0)