ResetStrategy::ResetStrategy(const NSFString& name, NSFCompositeState* parentState) : NSFStateMachine(name, parentState), hardwareReset(false), commReady(false), hardwareResetDelayTime(1000), commReadyDelayTime(1000), // State Machine Components // Define and initialize in the order: // 1) Events // 2) Regions and states, from outer to inner // 3) Transitions, ordered internal, local, external // 4) Group states and transitions within a region together. // Maintain the same order of declaration and initialization. // Events commReadyEvent("CommReady", this), hardwareResetEvent("HardwareReset", this), // Regions and states, from outer to inner // Initial state construtors take the form (name, parent) initialResetStrategyState("InitialResetStrategy", this), // Composite state construtors take the form (name, parent, entry action, exit action) resetHardwareState("ResetHardware", this, NSFAction(this, &ResetStrategy::resetHardwareEntryActions), NULL), reestablishCommunicationsState("ReestablishCommunications", this, NSFAction(this, &ResetStrategy::reestablishCommunicationsEntryActions), NULL), readyState("Ready", this, NULL, NULL), // Transitions, ordered internal, local, external // External transition construtors take the form (name, source, target, trigger, guard, action) initialResetStrategyToResetHardwareTransition("InitialToResetHardware", &initialResetStrategyState, &resetHardwareState, NULL, NULL, NSFAction(this, &ResetStrategy::resetVariables)), resetHardwareToReestablishCommunicationsTransition("ResetHardwareToReestablishCommunications", &resetHardwareState, &reestablishCommunicationsState, NULL, NSFGuard(this, &ResetStrategy::isHardwareReset), NULL), reestablishCommunicationsStateToReadyTransition("ReestablishCommunicationsStateToReady", &reestablishCommunicationsState, &readyState, NULL, NSFGuard(this, &ResetStrategy::isCommReady), NULL), // Actions resetHardwareAction("ReadHardware", NSFAction(this, &ResetStrategy::resetHardware), getEventThread()), readyCommAction("ResetComm", NSFAction(this, &ResetStrategy::resetComm), getEventThread()) { }
void NSFState::construct() { if (parentRegion != NULL) { parentRegion->addSubstate(this); } EntryActions.setExceptionAction(NSFAction(this, &NSFState::handleEntryActionException)); ExitActions.setExceptionAction(NSFAction(this, &NSFState::handleExitActionException)); }
NSFThread::NSFThread(const NSFString& name, int priority) : NSFTaggedObject(name), osThread(NSFOSThread::create(name, NSFAction(this, &NSFThread::threadEntry), priority)), threadMutex(NSFOSMutex::create()), terminationStatus(ThreadReady) { NSFEnvironment::getEnvironment().addThread(this); }
StrategyTest::StrategyTest(const NSFString& name) : NSFStateMachine(name, new NSFEventThread(name)), // Events event1("Event1", this), event2("Event2", this), // Regions and states, from outer to inner initialState("InitialTest15", this), state1("State1", this, NULL, NULL), state2("State2", this, NSFAction(this, &StrategyTest::state5EntryActions), NULL), state3("State3", this, NULL, NULL), //Transitions initialToState1Transition("InitialToState1", &initialState, &state1, NULL, NULL, NULL), state1ToState2Transition("State1ToState2", &state1, &state2, &event1, NULL, NULL), state2ToState3Transition("State2ToState3", &state2, &state3, &event2, NULL, NULL), state3ToState2Transition("State3ToState2", &state3, &state2, &event1, NULL, NULL), state2ToState1Transition("State2ToState1", &state2, &state1, &event1, NULL, NULL), state2Strategy("State2Strategy", &state2) { state2Strategy.getState5().EntryActions += NSFAction(&event2, &NSFEvent::queueEvent); }
StateMachineRestartTest::StateMachineRestartTest(const NSFString& name) : NSFStateMachine(name, new NSFEventThread(name)), // Events event1("Event1", this), event2("Event2", this), // Regions and states, from outer to inner initialState("InitialTest10", this), state1("State1", this, NULL, NULL), state2("State2", this, NULL, NULL), state3("State3", this, NULL, NULL), // Transitions, ordered internal, local, external test1ReactionToEvent2("Test1ReactionToEvent2", this, &event2, NULL, NSFAction(this, &StateMachineRestartTest::test1ReactionToEvent2Actions)), state2ReactionToEvent1("State2ReactionToEvent1", &state2, &event1, NULL, NSFAction(this, &StateMachineRestartTest::state2ReactionToEvent1Actions)), initialToState1Transition("InitialToState1", &initialState, &state1, NULL, NULL, NULL), state1ToState2Transition("State1ToState2", &state1, &state2, &event1, NULL, NULL), state2ToState3Transition("State2ToState3", &state2, &state3, &event2, NULL, NULL), state3ToState2Transition("State3ToState2", &state3, &state2, &event1, NULL, NULL), state2ToState1Transition("State2ToState1", &state2, &state1, &event1, NULL, NULL) { }
TransitionOrderTest::TransitionOrderTest(const NSFString& name) : NSFStateMachine(name, new NSFEventThread(name)), // Events event1("Event1", this), event2("Event2", this), event3("Event3", this), // Regions and states, from outer to inner initialTest15("InitialTest16", this), state1("State1", this, NULL, NULL), state2("State2", this, NULL, NULL), intialState2("IntialState2", &state2), state2_1("State2_1", &state2, NULL, NSFAction(this, &TransitionOrderTest::state2_1ExitActions)), state3("State3", this, NULL, NULL), initialState3("InitialState3", &state3), state3_1("State3_1", &state3, NULL, NSFAction(this, &TransitionOrderTest::state3_1ExitActions)), state4("State4", this, NULL, NULL), errorState("Error", this, NULL, NULL), // Transitions, ordered internal, local, external initialTest15ToState1Transition("InitialTest15ToState1", &initialTest15, &state1, NULL, NULL, NULL), state1ToState2Transition("State1ToState2", &state1, &state2, &event2, NULL, NULL), state1ReactionToEvent1("State1ReactionToEvent1", &state1, &event1, NULL, NSFAction(this, &TransitionOrderTest::state1ReactionToEvent1Actions)), state1ToErrorTransition("State1ToError", &state1, &errorState, &event1, NULL, NULL), state2ToState3Transition("State2ToState3Transition", &state2, &state3, &event2, NULL, NULL), state2ToErrorTransition("State2ToErrorTransition", &state2, &errorState, &event1, NULL, NULL), state2ToState2Transition("State2ToState2Transition", &state2, &state2, &event1, NULL, NULL), intialState2ToState2_1Transition("intialState2ToState2_1Transition", &intialState2, &state2_1, NULL, NULL, NULL), state3ToState4Transition("State3ToState4Transition", &state3, &state4, &event2, NULL, NULL), state3ToState3Transition("State3ToState3Transition", &state3, &state3, &event1, NULL, NULL), state3ReactionToEvent1("state3ReactionToEvent1", &state3, &event1, NULL, NSFAction(this, &TransitionOrderTest::state3ReactionToEvent1Actions)), initialState3ToState3_1Transition("InitialState3ToState3_1Transition", &initialState3, &state3_1, NULL, NULL, NULL), state3ToErrorTransition("State3ToErrorTransition", &state3, &errorState, &event3, NULL, NULL) { }
bool TimerAccuracyTest::runTest(NSFString& errorMessage) { eventHandler.addEventReaction(&testEvent, NSFAction(this, &TimerAccuracyTest::testEventAction)); // Schedule test event to fire every millisecond testEvent.schedule(0, 1); // Wait for test to end NSFOSThread::sleep(1000); // Unschedule test event testEvent.unschedule(); // Add results to name for test visibility name += "; Min / Max Delta Time = " + toString(minDeltaTime) + " / " + toString(maxDeltaTime) + " mS"; return true; }
CycleAB::CycleAB(const NSFString& name) : NSFStateMachine(name, new NSFEventThread(name)), // State Machine Components // Define and initialize in the order: // 1) Events // 2) Regions and states, from outer to inner // 3) Transitions, ordered internal, local, external // 4) Group states and transitions within a region together. // Maintain the same order of declaration and initialization. // Events // Event constructors take the form (name, parent) cycleEvent("CycleEvent", this), aReadyEvent("AReadyEvent", this), bReadyEvent("BReadyEvent", this), aCompleteEvent("ACompleteEvent", this), bCompleteEvent("BCompleteEvent", this), // Regions and states, from outer to inner systemRegion("SystemRegion", this), subsystemARegion("SubsystemARegion", this), subsystemBRegion("SubsystemBRegion", this), initializeForkJoin("InitializeForkJoin", this), cycleForkJoin("CycleForkJoin", this), completeForkJoin("CompleteForkJoin", this), // System Region // Regions and states, from outer to inner // Initial state construtors take the form (name, parent) systemInitialState("SystemInitial", &systemRegion), // Composite state construtors take the form (name, parent, entry actions, exit actions) waitForCycleEventState("WaitForCycleEvent", &systemRegion, NULL, NULL), // Transitions, ordered internal, local, external // External transition construtors take the form (name, source, target, trigger, guard, action) systemInitialToInitializeForkJoinTransition("SystemInitialToInitializeForkJoin", &systemInitialState, &initializeForkJoin, NULL, NULL, NULL), initializeForkJoinToWaitForCycleEventTransition("InitializeForkJoinToWaitForCycleEvent", &initializeForkJoin, &waitForCycleEventState, NULL, NULL, NULL), waitForCycleEventToCycleForkJoinTransition("WaitForCycleEventToCycleForkJoin", &waitForCycleEventState, &cycleForkJoin, &cycleEvent, NULL, NULL), cycleForkJoinToCompleteForkJoinTransiiton("CycleForkJoinToCompleteForkJoin", &cycleForkJoin, &completeForkJoin, &systemRegion, NULL), completeForkJoinToWaitForCycleEventTransition("CompleteForkJoinToWaitForCycleEvent", &completeForkJoin, &waitForCycleEventState, NULL, NULL, NULL), // Subystem A Region // Regions and states, from outer to inner // Initial state construtors take the form (name, parent) subsystemAInitialState("SubsystemAInitial", &subsystemARegion), // Composite state construtors take the form (name, parent, entry actions, exit actions) initializeAState("InitializeA", &subsystemARegion, NSFAction(this, &CycleAB::initializeAEntryActions), NULL), cycleAState("CycleA", &subsystemARegion, NSFAction(this, &CycleAB::cycleAEntryActions), NULL), // Transitions, ordered internal, local, external // External transition construtors take the form (name, source, target, trigger, guard, action) subsystemAInitialToInitializeATransition("SubsystemAInitialToInitializeA", &subsystemAInitialState, &initializeAState, NULL, NULL, NULL), initializeAToInitializeForkJoinTransition("InitializeAToInitializeForkJoin", &initializeAState, &initializeForkJoin, &aReadyEvent, NULL, NULL), initializeForkJoinToCycleForkJoinARegionTransition("InitializeForkJoinToCycleForkJoinARegion", &initializeForkJoin, &cycleForkJoin, &subsystemARegion, NULL), cycleForkJoinToCycleATransition("CycleForkJoinToCycleA", &cycleForkJoin, &cycleAState, NULL, NULL, NULL), cycleAToCompleteForkJoinTransition("CycleAToCompleteForkJoin", &cycleAState, &completeForkJoin, &aCompleteEvent, NULL, NULL), // Subystem B Region // Regions and states, from outer to inner // Initial state construtors take the form (name, parent) subsystemBInitialState("SubsystemBInitial", &subsystemBRegion), // Composite state construtors take the form (name, parent, entry actions, exit actions) initializeBState("InitializeB", &subsystemBRegion, NSFAction(this, &CycleAB::initializeBEntryActions), NULL), cycleBState("CycleB", &subsystemBRegion, NSFAction(this, &CycleAB::cycleBEntryActions), NULL), // Transitions, ordered internal, local, external // External transition construtors take the form (name, source, target, trigger, guard, action) subsystemBInitialToInitializeBTransition("SubsystemBInitialToInitializeB", &subsystemBInitialState, &initializeBState, NULL, NULL, NULL), initializeBToInitializeForkJoinTransition("InitializeBToInitializeForkJoin", &initializeBState, &initializeForkJoin, &bReadyEvent, NULL, NULL), initializeForkJoinToCycleForkJoinBRegionTransition("InitializeForkJoinToCycleForkJoinBRegion", &initializeForkJoin, &cycleForkJoin, &subsystemBRegion, NULL), cycleForkJoinToCycleBTransition("CycleForkJoinToCycleB", &cycleForkJoin, &cycleBState, NULL, NULL, NULL), cycleBToCompleteForkJoinTransition("CycleBToCompleteForkJoin", &cycleBState, &completeForkJoin, &bCompleteEvent, NULL, NULL) { }
ChoiceStateTest::ChoiceStateTest(const NSFString& name) : NSFStateMachine(name, new NSFEventThread(name)), value (0), // Events evaluateEvent("EvaluateEvent", this), waitEvent("WaitEvent", this), // Regions and states, from outer to inner initialChoiceStateTestState("InitialChoiceStateTest", this), waitToEvaluateState("WaitToEvaluate", this, NULL, NULL), evaluateValueState("EvaluateValue", this), evaluatedState("Evaluated", this, NULL, NULL), initialEvaluatedState("InitialEvaluatedState", &evaluatedState), valueLowState("ValueLow", &evaluatedState, NULL, NULL), valueMiddleState("ValueMiddle", &evaluatedState, NULL, NULL), valueHighState("ValueHigh", &evaluatedState, NULL, NULL), // Transitions, ordered internal, local, external initialChoiceStateTestToWaitToEvaluateTransition("InitialChoiceStateTestToWaitToEvaluate", &initialChoiceStateTestState, &waitToEvaluateState, NULL, NULL, NULL), waitToEvaluateToEvaluateValueTransition("WaitToEvaluateToEvaluateValueTransition", &waitToEvaluateState, &evaluateValueState, &evaluateEvent, NULL, NULL), initialEvaluatedToValueLowTransition("InitialEvaluatedToValueLowTransition", &initialEvaluatedState, &valueLowState, NULL, NULL, NULL), evaluateValueToValueLowTransition("EvaluateValueToValueLowTransition", &evaluateValueState, &valueLowState, NULL, NSFGuard(this, &ChoiceStateTest::isValueLow), NULL), evaluateValueToValueMiddleTransition("EvaluateValueToValueMiddleTransition", &evaluateValueState, &valueMiddleState, NULL, Else, NULL), evaluateValueToValueHighTransition("EvaluateValueToValueHighTransition", &evaluateValueState, &valueHighState, NULL, NSFGuard(this, &ChoiceStateTest::isValueHigh), NULL), evaluatedToWaitToEvaluateTransition("EvaluatedToWaitToEvaluateTransition", &evaluatedState, &waitToEvaluateState, &waitEvent, NULL, NSFAction(this, &ChoiceStateTest::addValue)) { }