本文整理汇总了C#中StateMechanic.StateMachine.CreateState方法的典型用法代码示例。如果您正苦于以下问题:C# StateMachine.CreateState方法的具体用法?C# StateMachine.CreateState怎么用?C# StateMachine.CreateState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StateMechanic.StateMachine
的用法示例。
在下文中一共展示了StateMachine.CreateState方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CancellationTokenCancelsOperation
public void CancellationTokenCancelsOperation()
{
var sm = new StateMachine("sm");
var initial = sm.CreateInitialState("initial");
var doingOperation = sm.CreateState("doingOperation");
var completedOperation = sm.CreateState("completedOperation");
var evt = new Event("evt");
initial.TransitionOn(evt).To(doingOperation);
doingOperation.TransitionOn(evt).To(completedOperation);
var cts = new CancellationTokenSource();
var op = new Operation<State>(evt, doingOperation, completedOperation);
var task = op.TryFireAsync(cts.Token);
Assert.False(task.IsCompleted);
Assert.False(task.IsCanceled);
cts.Cancel();
Assert.True(task.IsCanceled);
// Check nothing explodes
evt.Fire();
}
示例2: IndicatesWhetherInState
public void IndicatesWhetherInState()
{
var sm = new StateMachine("State Machine");
var state1 = sm.CreateInitialState("State 1");
var state2 = sm.CreateState("State 2");
var state3 = sm.CreateState("State 3");
var evt = sm.CreateEvent("Event");
var group1 = new StateGroup("Group 1");
var group2 = new StateGroup("Group 2");
state1.AddToGroup(group1);
state2.AddToGroup(group2);
state3.AddToGroup(group2);
state1.TransitionOn(evt).To(state2);
state2.TransitionOn(evt).To(state3);
Assert.True(group1.IsCurrent);
Assert.False(group2.IsCurrent);
evt.Fire();
Assert.False(group1.IsCurrent);
Assert.True(group2.IsCurrent);
evt.Fire();
Assert.False(group1.IsCurrent);
Assert.True(group2.IsCurrent);
}
示例3: TransitionAddedToState
public void TransitionAddedToState()
{
var stateMachine = new StateMachine("State Machine");
var state1 = stateMachine.CreateState("State 1");
var state2 = stateMachine.CreateState("State 2");
var evt = stateMachine.CreateEvent("Event");
state1.TransitionOn(evt).To(state2);
Assert.AreEqual(1, state1.Transitions.Count);
Assert.AreEqual(state1, state1.Transitions[0].From);
Assert.AreEqual(state2, state1.Transitions[0].To);
Assert.AreEqual(evt, state1.Transitions[0].Event);
Assert.False(state1.Transitions[0].IsInnerTransition);
}
示例4: ForcefullyTransitioningFromChildStateMachineCallsExitHandlerOnCurrentState
public void ForcefullyTransitioningFromChildStateMachineCallsExitHandlerOnCurrentState()
{
StateHandlerInfo<State> state22ExitInfo = null;
var sm = new StateMachine("State Machine");
var evt1 = new Event("Event 1");
var state1 = sm.CreateInitialState("State 1");
var state2 = sm.CreateState("State 2");
var subSm = state2.CreateChildStateMachine();
var state21 = subSm.CreateInitialState("State 2.1");
var state22 = subSm.CreateState("State 2.2").WithExit(i => state22ExitInfo = i);
state1.TransitionOn(evt1).To(state2);
state21.TransitionOn(evt1).To(state22);
// Enter state2, and start child state machine
evt1.Fire();
// Enter State 2.2
evt1.Fire();
// Transition from state2 to state1, exiting the child state machine
sm.ForceTransition(state1, evt1);
Assert.NotNull(state22ExitInfo);
Assert.AreEqual(state22, state22ExitInfo.From);
Assert.AreEqual(state1, state22ExitInfo.To);
Assert.AreEqual(evt1, state22ExitInfo.Event);
}
示例5: FirstRegisteredTransitionWins
public void FirstRegisteredTransitionWins()
{
var sm = new StateMachine("state machine");
var initial = sm.CreateInitialState("initial");
var state1 = sm.CreateState("state1");
var state2 = sm.CreateState("state2");
var evt = new Event("evt");
initial.TransitionOn(evt).To(state2).WithGuard(i => true);
initial.TransitionOn(evt).To(state1);
evt.Fire();
Assert.AreEqual(state2, sm.CurrentState);
}
示例6: NoEventsAreQueuedIfAGuardThrowsAnException
public void NoEventsAreQueuedIfAGuardThrowsAnException()
{
var sm = new StateMachine("sm");
var state1 = sm.CreateInitialState("state1");
var state2 = sm.CreateState("state2");
var evt = new Event("evt");
var evt2 = new Event("evt2");
var exception = new Exception("foo");
state1.TransitionOn(evt).To(state2).WithGuard(i =>
{
evt.Fire();
throw exception;
});
state1.TransitionOn(evt2).To(state2);
state2.TransitionOn(evt).To(state1);
Assert.Throws<Exception>(() => evt.Fire());
// Make sure that an event wasn't queued
evt2.Fire();
Assert.AreEqual(state2, sm.CurrentState);
}
示例7: TransitionNotFoundRaisedWhenTransitionNotFoundOnChild
public void TransitionNotFoundRaisedWhenTransitionNotFoundOnChild()
{
var sm = new StateMachine("sm");
var initial = sm.CreateInitialState("initial");
var state1 = sm.CreateState("state 1");
var child = initial.CreateChildStateMachine();
var childInitial = child.CreateInitialState("childInitial");
var evt = new Event("evt");
state1.InnerSelfTransitionOn(evt);
TransitionNotFoundEventArgs<State> ea = null;
sm.TransitionNotFound += (o, e) =>
{
ea = e;
};
evt.TryFire();
Assert.NotNull(ea);
Assert.AreEqual(evt, ea.Event);
Assert.AreEqual(initial, ea.From);
Assert.AreEqual(sm, ea.StateMachine);
Assert.AreEqual(EventFireMethod.TryFire, ea.EventFireMethod);
}
示例8: CurrentChildStateReflectsCurrentStateOfChild
public void CurrentChildStateReflectsCurrentStateOfChild()
{
var sm = new StateMachine("state machine");
var initialState = sm.CreateInitialState("initial state");
var state1 = sm.CreateState("state1");
var childSm = state1.CreateChildStateMachine();
var childInitialState = childSm.CreateInitialState("childInitialState");
var childState1 = childSm.CreateState("childState1");
var evt = new Event("evt");
var evt2 = new Event("evt2");
initialState.TransitionOn(evt).To(state1);
childInitialState.TransitionOn(evt).To(childState1);
state1.TransitionOn(evt2).To(initialState);
Assert.AreEqual(initialState, sm.CurrentChildState);
evt.Fire();
Assert.AreEqual(childInitialState, sm.CurrentChildState);
evt.Fire();
Assert.AreEqual(childState1, sm.CurrentChildState);
evt2.Fire();
Assert.AreEqual(initialState, sm.CurrentChildState);
}
示例9: FormatsStateMachinesWithNoNames
public void FormatsStateMachinesWithNoNames()
{
var sm = new StateMachine();
var state1 = sm.CreateInitialState();
var state2 = sm.CreateState();
var child1 = state1.CreateChildStateMachine();
var state11 = child1.CreateInitialState();
var state12 = child1.CreateState();
var child2 = state2.CreateChildStateMachine();
var state21 = child2.CreateInitialState();
var state22 = child2.CreateState();
var evt1 = new Event();
var evt2 = new Event<string>();
state1.InnerSelfTransitionOn(evt1);
state1.TransitionOn(evt2).To(state2);
state11.TransitionOn(evt2).ToDynamic(_ => state12);
state12.TransitionOn(evt1).To(state11);
state21.TransitionOn(evt1).To(state22);
var dot = sm.FormatDot();
var dgml = sm.FormatDgml();
}
示例10: FormatsStateMachines
public void FormatsStateMachines()
{
var sm = new StateMachine("sm");
var state1 = sm.CreateInitialState("state1");
var state2 = sm.CreateState("state2");
var child1 = state1.CreateChildStateMachine("child1");
var state11 = child1.CreateInitialState("state11");
var state12 = child1.CreateState("state12");
var child2 = state2.CreateChildStateMachine("child2");
var state21 = child2.CreateInitialState("state21");
var state22 = child2.CreateState("state22");
var evt1 = new Event("evt1");
var evt2 = new Event<string>("evt2");
state1.InnerSelfTransitionOn(evt1);
state1.TransitionOn(evt2).To(state2);
state11.TransitionOn(evt2).ToDynamic(_ => state12);
state12.TransitionOn(evt1).To(state11);
state21.TransitionOn(evt1).To(state22);
state21.TransitionOn(evt2).To(state22);
var dot = sm.FormatDot(colorize: true);
var dgml = sm.FormatDgml(colorize: true);
}
示例11: FireThrowsIfEventFireThrows
public void FireThrowsIfEventFireThrows()
{
var sm = new StateMachine("sm");
var initial = sm.CreateInitialState("initial");
var operation = sm.CreateState("state2");
var success = sm.CreateState("success");
var evt = new Event("evt");
operation.TransitionOn(evt).To(operation);
var op = new Operation<State>(evt, operation, success);
var task = op.FireAsync();
Assert.True(task.IsFaulted);
Assert.Throws<TransitionNotFoundException>(() => task.GetAwaiter().GetResult());
}
示例12: StateMachineStartsInInitialState
public void StateMachineStartsInInitialState()
{
var sm = new StateMachine("state machine");
var initialState = sm.CreateInitialState("initial state");
var state1 = sm.CreateState("state1");
Assert.AreEqual(initialState, sm.CurrentState);
Assert.AreEqual(initialState, sm.InitialState);
}
示例13: EventFireInTransitionHandlerIsQueued
public void EventFireInTransitionHandlerIsQueued()
{
var sm = new StateMachine("sm");
var initial = sm.CreateInitialState("initial");
var state1 = sm.CreateState("state1");
var state2 = sm.CreateState("state2");
var state3 = sm.CreateState("state3");
var evt = sm.CreateEvent("evt");
var evt2 = sm.CreateEvent("evt2");
initial.TransitionOn(evt).To(state1).WithHandler(i => evt2.Fire());
initial.TransitionOn(evt2).To(state2);
state1.TransitionOn(evt2).To(state3);
evt.Fire();
Assert.AreEqual(state3, sm.CurrentState);
}
示例14: ThrowsIfForcedTransitionOnAChildEvent
public void ThrowsIfForcedTransitionOnAChildEvent()
{
var sm = new StateMachine("State Machine");
var state1 = sm.CreateInitialState("Initial State");
var state2 = sm.CreateState("State 2");
var subSm = state1.CreateChildStateMachine();
var evt = subSm.CreateEvent("Event");
Assert.Throws<InvalidEventTransitionException>(() => sm.ForceTransition(state2, evt));
}
示例15: TryFireReturnsFalseIfTransitionNotFound
public void TryFireReturnsFalseIfTransitionNotFound()
{
var sm = new StateMachine("sm");
var initialState = sm.CreateInitialState("initialState");
var state1 = sm.CreateState("state1");
var evt = new Event("evt");
state1.InnerSelfTransitionOn(evt);
Assert.False(evt.TryFire());
}