protected internal override void exit(NSFStateMachineContext context) { // Additional behavior if (!active) { return; } // Set all associated regions' active substates to null state foreach (NSFTransition incomingTransition in incomingTransitions) { // Simple case of transition source is a state if (incomingTransition.Source.ParentRegion != null) { incomingTransition.Source.ParentRegion.setActiveSubstate(NSFState.NullState); } else // A region can also be specified as part of a fork-join to fork-join transition { NSFForkJoinTransition forkJoinTransition = incomingTransition as NSFForkJoinTransition; if ((forkJoinTransition != null) && (forkJoinTransition.ForkJoinRegion != null)) { forkJoinTransition.ForkJoinRegion.setActiveSubstate(NSFState.NullState); } } } // Remove all completed transitions completedTransitions.Clear(); // Base class behavior base.exit(context); }
protected internal override void enter(NSFStateMachineContext context, bool useHistory) { // Additional behavior // Enter parent state, if necessary if (!parentState.isActive()) { parentState.enter(context, false); } // Add transition to list of completed transitions completedTransitions.Add(context.Transition); // Set associated region's active substate to this // Simple case of transition source is a state if (context.Transition.Source.ParentRegion != null) { context.Transition.Source.ParentRegion.setActiveSubstate(this); } else // A region can also be specified as part of a fork-join to fork-join transition { NSFForkJoinTransition forkJoinTransition = context.Transition as NSFForkJoinTransition; if ((forkJoinTransition != null) && (forkJoinTransition.ForkJoinRegion != null)) { forkJoinTransition.ForkJoinRegion.setActiveSubstate(this); } } base.enter(context, useHistory); }
protected internal override void enter(NSFStateMachineContext context, bool useHistory) { // Additional behavior // Enter parent state, if necessary if (!parentState.isActive()) { parentState.enter(context, false); } // Add transition to list of completed transitions completedTransitions.Add(context.Transition); // Set associated region's active substate to this // Simple case of transition source is a state if (context.Transition.Source.ParentRegion != null) { context.Transition.Source.ParentRegion.setActiveSubstate(this); } else // A region can also be specified as part of a fork-join to fork-join transition { NSFForkJoinTransition forkJoinTransition = context.Transition as NSFForkJoinTransition; if ((forkJoinTransition != null) && (forkJoinTransition.ForkJoinRegion != null)) { forkJoinTransition.ForkJoinRegion.setActiveSubstate(this); } } base.enter(context, useHistory); }
/// <summary> /// Enters the state. /// </summary> /// <param name="context">The event arguments pertaining to the transition into the state.</param> /// <param name="useHistory">Used by history states to reconstitute state history.</param> /// <remarks> /// This method is for use only by the North State Framework's internal logic. /// </remarks> protected internal virtual void enter(NSFStateMachineContext context, bool useHistory) { active = true; if (parentRegion != null) { parentRegion.setActiveSubstate(this); if (!parentRegion.isActive()) { parentRegion.enter(context, false); } } if (TopStateMachine.LoggingEnabled && LogEntry) { NSFTraceLog.PrimaryTraceLog.addTrace(NSFTraceTags.StateEnteredTag, NSFTraceTags.StateMachineTag, TopStateMachine.Name, NSFTraceTags.StateTag, Name); } // Update context to indicate entering this state context.EnteringState = this; context.ExitingState = null; EntryActions.execute(context); NSFStateMachine parentStateMachine = ParentStateMachine; if (parentStateMachine != null) { parentStateMachine.executeStateChangeActions(context); } }
protected internal override void exit(NSFStateMachineContext context) { // Additional behavior exitRegions(context); // Base class behavior base.exit(context); }
protected internal override void enter(NSFStateMachineContext context, bool useHistory) { // Base class behavior base.enter(context, useHistory); // Additional behavior enterRegions(context, useHistory); }
/// <summary> /// Exits the region. /// </summary> /// <param name="context">Additional contextual information.</param> internal void exit(NSFStateMachineContext context) { active = false; if (activeSubstate != NSFState.NullState) { activeSubstate.exit(context); } }
private void retrySend(NSFStateMachineContext context) { NSFDebugUtility.PrimaryDebugUtility.writeLineToConsole("Retry send ..."); ++retries; // Code to re-send the command goes here // ... // Schedule timeout event, in case no response received responseTimeoutEvent.schedule(responseTimeout, 0); }
/// <summary> /// Exits all the regions of the composite state. /// </summary> internal void exitRegions(NSFStateMachineContext context) { foreach (NSFRegion region in regions) { if (region.isActive()) { region.exit(context); } } }
/// <summary> /// Enters all the regions of the composite state. /// </summary> /// <param name="context">Additional contextual information.</param> /// <param name="useHistory">Flag indicating if regions should be entered using the history substate.</param> internal void enterRegions(NSFStateMachineContext context, bool useHistory) { foreach (NSFRegion region in regions) { if (!region.isActive()) { region.enter(context, useHistory); } } }
/// <summary> /// Executes the actions in the state change actions list. /// </summary> internal void executeStateChangeActions(NSFStateMachineContext context) { StateChangeActions.execute(context); NSFStateMachine parentStateMachine = ParentStateMachine; if (parentStateMachine != null) { parentStateMachine.executeStateChangeActions(context); } }
protected internal override void enter(NSFStateMachineContext context, bool useHistory) { // Base class behavior base.enter(context, false); // Additional behavior // Enter history substate, not using recursive history NSFState historySubstate = parentRegion.HistorySubstate; if (historySubstate != NSFState.NullState) { historySubstate.enter(context, false); } }
protected internal override void enter(NSFStateMachineContext context, bool useHistory) { // Base class behavior base.enter(context, false); // Additional behavior // Enter history substate, using recursive history NSFState historySubstate = parentRegion.HistorySubstate; if (historySubstate != NSFState.NullState) { historySubstate.enter(context, true); } }
/// <summary> /// Exits the state. /// </summary> /// <param name="context">The event arguments pertaining to the transition out of the state.</param> /// <remarks> /// This method is for use only by the North State Framework's internal logic. /// </remarks> protected internal virtual void exit(NSFStateMachineContext context) { active = false; // Update context to indicate exiting this state context.ExitingState = this; context.EnteringState = null; ExitActions.execute(context); if (parentRegion != null) { parentRegion.setActiveSubstate(NSFState.NullState); } }
protected override void fireTransition(NSFStateMachineContext context) { compositeSource.exitRegions(context); // Reset context possibly changed by exiting states context.EnteringState = null; context.ExitingState = null; Actions.execute(context); if (Source != Target) { Target.enter(context, false); } else { compositeSource.enterRegions(context, false); } }
protected override void fireTransition(NSFStateMachineContext context) { Source.exit(context); // Exit parent states until common parent is found NSFState parentState = Source.ParentState; while ((parentState != null) && (!parentState.isParent(Target))) { parentState.exit(context); parentState = parentState.ParentState; } // Reset context possibly changed by exiting states context.EnteringState = null; context.ExitingState = null; Actions.execute(context); Target.enter(context, false); }
protected override void fireTransition(NSFStateMachineContext context) { Source.exit(context); // Exit parent states until common parent is found NSFState parentState = Source.ParentState; while ((parentState != null) && (!parentState.isParent(Target))) { parentState.exit(context); parentState = parentState.ParentState; } // Reset context possibly changed by exiting states context.EnteringState = null; context.ExitingState = null; Actions.execute(context); Target.enter(context, false); }
/// <summary> /// Enters the region. /// </summary> /// <param name="context">Additional contextual information.</param> /// <param name="useHistory">Flag indicating whether or not to use the history state as entry point.</param> internal void enter(NSFStateMachineContext context, bool useHistory) { active = true; if (!parentState.isActive()) { parentState.enter(context, false); } if (useHistory) { if (historySubstate != NSFState.NullState) { historySubstate.enter(context, useHistory); } } if (activeSubstate == NSFState.NullState) { initialState.enter(context, false); } }
/// <summary> /// Processes an event, evaluating if the event results in the transition firing. /// </summary> /// <remarks> /// This method is for use only by the North State Framework's internal logic. /// </remarks> protected internal NSFEventStatus processEvent(NSFEvent nsfEvent) { bool transitionTriggered = false; if (triggers.Count == 0) { transitionTriggered = true; } else { foreach (NSFEvent trigger in Triggers) { if (trigger.Id == nsfEvent.Id) { transitionTriggered = true; break; } } } if (!transitionTriggered) { return(NSFEventStatus.NSFEventUnhandled); } NSFStateMachineContext newContext = new NSFStateMachineContext(Source.TopStateMachine, null, null, this, nsfEvent); if (!Guards.execute(newContext)) { return(NSFEventStatus.NSFEventUnhandled); } fireTransition(newContext); return(NSFEventStatus.NSFEventHandled); }
private void errorEntryActions(NSFStateMachineContext context) { // Code to handle the error goes here // ... }
void terminateTest(NSFStateMachineContext context) { readyToTerminate = true; }
/// <summary> /// Exits the region. /// </summary> /// <param name="context">Additional contextual information.</param> internal void exit(NSFStateMachineContext context) { active = false; if (activeSubstate != NSFState.NullState) { activeSubstate.exit(context); } }
/// <summary> /// Enters the region. /// </summary> /// <param name="context">Additional contextual information.</param> /// <param name="useHistory">Flag indicating whether or not to use the history state as entry point.</param> internal void enter(NSFStateMachineContext context, bool useHistory) { active = true; if (!parentState.isActive()) { parentState.enter(context, false); } if (useHistory) { if (historySubstate != NSFState.NullState) { historySubstate.enter(context, useHistory); } } if (activeSubstate == NSFState.NullState) { initialState.enter(context, false); } }
/// <summary> /// Fires the transition. /// </summary> /// <param name="context">The state machine context associated with the transition firing.</param> /// <remarks> /// This method is for use only by the North State Framework's internal logic. /// </remarks> protected abstract void fireTransition(NSFStateMachineContext context);
private bool isReady(NSFStateMachineContext context) { // Code to check if hardware is reset properly goes here // ... return true; }
private void sendCommand(NSFStateMachineContext context) { String commandString = commandQueue.Peek(); // Code to send the command goes here // ... // Log trace of command sent // Trace Format: // <MessageSent> // <Source>name</Source> // <Message>message</Message> // </MessageSent> NSFTraceLog.PrimaryTraceLog.addTrace(NSFTraceTags.MessageSentTag, NSFTraceTags.SourceTag, Name, NSFTraceTags.MessageTag, commandString); }
private void queueCommand(NSFStateMachineContext context) { // Add data to command queue. // If the state machine is in the waitForCommandState, then the // run to completion step will result in transitioning to the // waitForResponse state, sending the command during the transtion. commandQueue.Enqueue(((NSFDataEvent<String>)(context.Trigger)).Data); }
private void handleTransition(NSFStateMachineContext context) { NSFDebugUtility.PrimaryDebugUtility.writeLineToConsole("Transitioning from " + context.Transition.Source.Name + " to " + context.Transition.Target.Name); }
/// <summary> /// Queues the event to its destination. /// </summary> /// <remarks> /// This method is provided to allow event queuing to be registered as an entry, exit, or transition action. /// The event source will be changed to the context source for logging purposes during execution of this method. /// This method is not thread safe from a logging perspective, so that two threads calling this method on the same /// event can result in indeterminate logging of the source. /// </remarks> public void queueEvent(NSFStateMachineContext context) { Destination.queueEvent(this, context.Source); }
/// <summary> /// Provides a syntactical method for specifying "Never" as a transition guard, effectively blocking the transition from ever occuring. /// </summary> public bool Never(NSFStateMachineContext context) { return(false); }
protected override void fireTransition(NSFStateMachineContext context) { compositeSource.exitRegions(context); // Reset context possibly changed by exiting states context.EnteringState = null; context.ExitingState = null; Actions.execute(context); if (Source != Target) { Target.enter(context, false); } else { compositeSource.enterRegions(context, false); } }
/// <summary> /// Queues the event to its destination. /// </summary> /// <remarks> /// This method is provided to allow event queuing to be registered as an entry, exit, or transition action. /// The event source will be changed to the context source for logging purposes during execution of this method. /// This method is not thread safe from a logging perspective, so that two threads calling this method on the same /// event can result in indeterminate logging of the source. /// </remarks> public void queueEvent(NSFStateMachineContext context) { Destination.queueEvent(this, context.Source); }
private void handleResponse(NSFStateMachineContext context) { // Code to handle the response goes here // ... // Log trace of response received // Trace Format: // <MessageReceived> // <Source>name</Source> // <Message>message</Message> // </MessageReceived> NSFTraceLog.PrimaryTraceLog.addTrace(NSFTraceTags.MessageReceivedTag, NSFTraceTags.SourceTag, Name, NSFTraceTags.MessageTag, ((NSFDataEvent<String>)(context.Trigger)).Data); }
private bool hasCommand(NSFStateMachineContext context) { return (commandQueue.Count != 0); }
private void waitForResponseExitActions(NSFStateMachineContext context) { // Unschedule the timeout event responseTimeoutEvent.unschedule(); commandQueue.Dequeue(); }
private bool isResponse(NSFStateMachineContext context) { // Code to verify that the resposne is correct for the command goes here. // ... return true; }
private void state3_1ExitActions(NSFStateMachineContext context) { queueEvent(event3); }
private void resetEntryActions(NSFStateMachineContext context) { // Code to reset hardware goes here // ... }
/// <summary> /// Executes the actions in the state change actions list. /// </summary> internal void executeStateChangeActions(NSFStateMachineContext context) { StateChangeActions.execute(context); NSFStateMachine parentStateMachine = ParentStateMachine; if (parentStateMachine != null) { parentStateMachine.executeStateChangeActions(context); } }
private void waitForResponseEntryActions(NSFStateMachineContext context) { // Schedule timeout event, in case no response received responseTimeoutEvent.schedule(responseTimeout, 0); }
private void addValue(NSFStateMachineContext context) { value += 10; }
private void state3ReactionToEvent1Actions(NSFStateMachineContext context) { queueEvent(event2); }
private bool isValueHigh(NSFStateMachineContext context) { return (value > 10); }
private void waitForResponseEntryActions(NSFStateMachineContext context) { retries = 0; }
private bool isValueLow(NSFStateMachineContext context) { return (value < 10); }
private void handleStateExited(NSFStateMachineContext context) { NSFDebugUtility.PrimaryDebugUtility.writeLineToConsole("Exiting " + context.ExitingState.Name); }
private bool canRetry(NSFStateMachineContext context) { return (retries < maxRetries); }
protected override void fireTransition(NSFStateMachineContext context) { Actions.execute(context); }
/// <summary> /// Provides a syntactical method for specifying "Never" as a transition guard, effectively blocking the transition from ever occuring. /// </summary> public bool Never(NSFStateMachineContext context) { return false; }