//----------------------------------------------------------------------------- int ctkWorkflowTest2(int argc, char * argv [] ) { QApplication app(argc, argv); int defaultTime = 100; // create the steps and the workflow ctkWorkflow *workflow = new ctkWorkflow(); ctkWorkflowStep *step1 = new ctkWorkflowStep(workflow, "Step 1"); step1->setName("Step 1"); step1->setDescription("Description for step 1"); ctkWorkflowStep *step2 = new ctkWorkflowStep(workflow, "Step 2"); step2->setName("Step 2"); step2->setDescription("Description for step 2"); ctkWorkflowStep *step3 = new ctkWorkflowStep(workflow, "Step 3"); step3->setName("Step 3"); step3->setDescription("Description for step 3"); ctkWorkflowStep *step4 = new ctkWorkflowStep(workflow, "Step 4"); step4->setName("Step 4"); step4->setDescription("Description for step 4"); // create the qObjects that implement the required functions, and // communicate with the workflow using signals and slots ctkExampleWorkflowStepUsingSignalsAndSlots* qObject1 = new ctkExampleWorkflowStepUsingSignalsAndSlots; ctkExampleWorkflowStepUsingSignalsAndSlots* qObject2 = new ctkExampleWorkflowStepUsingSignalsAndSlots; ctkExampleWorkflowStepUsingSignalsAndSlots* qObject3 = new ctkExampleWorkflowStepUsingSignalsAndSlots; ctkExampleWorkflowStepUsingSignalsAndSlots* qObject4 = new ctkExampleWorkflowStepUsingSignalsAndSlots; // use the qObjects for validation QObject::connect(step1->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject1, SLOT(validate(const QString&))); QObject::connect(qObject1, SIGNAL(validationComplete(bool, const QString&)), workflow, SLOT(evaluateValidationResults(bool, const QString&))); QObject::connect(step2->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject2, SLOT(validate(const QString&))); QObject::connect(qObject2, SIGNAL(validationComplete(bool, const QString&)), workflow, SLOT(evaluateValidationResults(bool, const QString&))); // step 3's validation will always fail QObject::connect(step3->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject3, SLOT(validateFails())); QObject::connect(qObject3, SIGNAL(validationComplete(bool, const QString&)), workflow, SLOT(evaluateValidationResults(bool, const QString&))); QObject::connect(step4->ctkWorkflowStepQObject(), SIGNAL(invokeValidateCommand(const QString&)), qObject4, SLOT(validate(const QString&))); QObject::connect(qObject4, SIGNAL(validationComplete(bool, const QString&)), workflow, SLOT(evaluateValidationResults(bool, const QString&))); // use the qObjects for entry processing QObject::connect(step1->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject1, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject1, SIGNAL(onEntryComplete()), workflow, SLOT(processingAfterOnEntry())); QObject::connect(step2->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject2, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject2, SIGNAL(onEntryComplete()), workflow, SLOT(processingAfterOnEntry())); QObject::connect(step3->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject3, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject3, SIGNAL(onEntryComplete()), workflow, SLOT(processingAfterOnEntry())); QObject::connect(step4->ctkWorkflowStepQObject(), SIGNAL(invokeOnEntryCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject4, SLOT(onEntry(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject4, SIGNAL(onEntryComplete()), workflow, SLOT(processingAfterOnEntry())); // use the qObjects for exit processing QObject::connect(step1->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject1, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject1, SIGNAL(onExitComplete()), workflow, SLOT(processingAfterOnExit())); QObject::connect(step2->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject2, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject2, SIGNAL(onExitComplete()), workflow, SLOT(processingAfterOnExit())); QObject::connect(step3->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject3, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject3, SIGNAL(onExitComplete()), workflow, SLOT(processingAfterOnExit())); QObject::connect(step4->ctkWorkflowStepQObject(), SIGNAL(invokeOnExitCommand(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType)), qObject4, SLOT(onExit(const ctkWorkflowStep*, const ctkWorkflowInterstepTransition::InterstepTransitionType))); QObject::connect(qObject4, SIGNAL(onExitComplete()), workflow, SLOT(processingAfterOnExit())); step1->setHasValidateCommand(1); step2->setHasValidateCommand(1); step3->setHasValidateCommand(1); step4->setHasValidateCommand(1); step1->setHasOnEntryCommand(1); step2->setHasOnEntryCommand(1); step3->setHasOnEntryCommand(1); step4->setHasOnEntryCommand(1); step1->setHasOnExitCommand(1); step2->setHasOnExitCommand(1); step3->setHasOnExitCommand(1); step4->setHasOnExitCommand(1); // set the initial step (which sets the initial state) workflow->setInitialStep(step1); // add the first and second steps if (!workflow->addTransition(step1, step2)) { std::cerr << "could not add 1st and 2nd step" << std::endl; return EXIT_FAILURE; } // add the third step if (!workflow->addTransition(step2, step3)) { std::cerr << "could not add 3rd step" << std::endl; return EXIT_FAILURE; } // add the fourth step if (!workflow->addTransition(step3, step4)) { std::cerr << "could not add 4rd step" << std::endl; return EXIT_FAILURE; } // start the workflow if (!testStartWorkflow(workflow, defaultTime, app, 1, step1, qObject1, true, 0, qObject2, 0, 0, qObject3, 0, 0, qObject4, 0, 0)) { std::cerr << "error starting workflow"; return EXIT_FAILURE; } // trigger transition to the next step workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step2, qObject1, 1, 1, qObject2, 1, 0, qObject3, 0, 0, qObject4, 0, 0)) { std::cerr << "error transitioning to step 2"; return EXIT_FAILURE; } // trigger transition to the next step workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step3, qObject1, 1, 1, qObject2, 1, 1, qObject3, 1, 0, qObject4, 0, 0)) { std::cerr << "error transitioning to step 3"; return EXIT_FAILURE; } // trigger transition to the next state (this should fail!) workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step3, qObject1, 1, 1, qObject2, 1, 1, qObject3, 1, 0, qObject4, 0, 0)) { std::cerr << "error after transition failure at step 3"; return EXIT_FAILURE; } // make sure the workflow stops properly if (!testStopWorkflow(workflow, defaultTime, app, qObject1, 1, 1, qObject2, 1, 1, qObject3, 1, 1, qObject4, 0, 0)) { std::cerr << "error after stopping workflow"; return EXIT_FAILURE; } // handles deletions of the workflow, steps, states and transitions delete workflow; return EXIT_SUCCESS; }
//----------------------------------------------------------------------------- int ctkWorkflowTest1(int argc, char * argv [] ) { QApplication app(argc, argv); int defaultTime = 100; // create two steps and the workflow ctkWorkflow *workflow = new ctkWorkflow(); ctkExampleDerivedWorkflowStep *step1 = new ctkExampleDerivedWorkflowStep(workflow, "Step 1"); step1->setName("Step 1"); step1->setDescription("Description for step 1"); ctkExampleDerivedWorkflowStep *step2 = new ctkExampleDerivedWorkflowStep(workflow, "Step 2"); step2->setName("Step 2"); step2->setDescription("Description for step 2"); // -------------------------------------------------------------------------- // try to add a transition for a step with the same id ctkExampleDerivedWorkflowStep *step1Duplicated = new ctkExampleDerivedWorkflowStep(workflow, "Step 1"); if (workflow->addTransition(step1, step1Duplicated)) { std::cerr << "workflow connected two steps with the same id"; return EXIT_FAILURE; } // try to add a transition from a step to itself if (workflow->addTransition(step1, step1)) { std::cerr << "workflow connected two steps with the same id"; return EXIT_FAILURE; } // -------------------------------------------------------------------------- // workflow with no steps // try erroneously starting with no steps if (!testStartWorkflow(workflow, defaultTime, app, 0)) { std::cerr << "empty workflow is running after start()"; return EXIT_FAILURE; } // add the first step if (!workflow->addTransition(step1, 0)) { std::cerr << "could not add first step"; return EXIT_FAILURE; } // try erroneously starting with no initial step if (!testStartWorkflow(workflow, defaultTime, app, 0)) { std::cerr << "workflow is running after start() with no initial step"; return EXIT_FAILURE; } // -------------------------------------------------------------------------- // workflow with one step // set the initial step (which sets the initial state) workflow->setInitialStep(step1); // try starting with one step if (!testStartWorkflow(workflow, defaultTime, app, 1, step1, step1, 1, 0, step2, 0, 0)) { std::cerr << "workflow is not running after start() with a single step"; return EXIT_FAILURE; } // triggering ValidationTransition and TransitionToPreviousStep // should keep us in the same step, when there is only one step workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step1, step1, 1, 0, step2, 0, 0)) { std::cerr << "error in validation transition in a workflow with a single step"; return EXIT_FAILURE; } // transition to the previous step workflow->goBackward(); if (!transitionTest(workflow, defaultTime, app, step1, step1, 1, 0, step2, 0, 0)) { std::cerr << "error after transition to previous step in a workflow with a single step"; return EXIT_FAILURE; } // stop the workflow if (!testStopWorkflow(workflow, defaultTime, app, step1, 1, 1, step2, 0, 0)) { std::cerr << "workflow with one step still running after stop"; return EXIT_FAILURE; } // -------------------------------------------------------------------------- // workflow with two steps // add the second step if (!workflow->addTransition(step1, step2)) { std::cerr << "could not add second step"; return EXIT_FAILURE; } // start the workflow if (!testStartWorkflow(workflow, defaultTime, app, 1, step1, step1, 2, 1, step2, 0, 0)) { std::cerr << "workflow is not running after start() with two steps"; return EXIT_FAILURE; } // make sure the workflow has the steps if (!workflow->hasStep(step1->id())) { std::cerr << "Step1 not added to workflow"; return EXIT_FAILURE; } if (!workflow->hasStep(step2->id())) { std::cerr << "Step2 not added to workflow"; return EXIT_FAILURE; } // if (workflow->numberOfSteps() != 2) // { // std::cerr << "workflow has " << workflow->numberOfSteps() << " steps, not 2"; // return EXIT_FAILURE; // } // Test that the workflow transitions from processing to validation state workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step2, step1, 2, 2, step2, 1, 0)) { std::cerr << "error transitioning to next step in workflow with two steps"; return EXIT_FAILURE; } // Test that the workflow transitions back to the previous step workflow->goBackward(); if (!transitionTest(workflow, defaultTime, app, step1, step1, 3, 2, step2, 1, 1)) { std::cerr << "error transitioning to previous step in workflow with step steps"; return EXIT_FAILURE; } // make sure the workflow stops properly if (!testStopWorkflow(workflow, defaultTime, app, step1, 3, 3, step2, 1, 1)) { std::cerr << "workflow with two steps is running after stop()"; return EXIT_FAILURE; } // -------------------------------------------------------------------------- // workflow with three steps // add a third step manually ctkExampleDerivedWorkflowStep *step3 = new ctkExampleDerivedWorkflowStep(workflow, "Step 3"); step3->setName("Step 3"); step3->setDescription("Description for step 3"); if (!workflow->addTransition(step2, step3, "", ctkWorkflow::Forward)) { std::cerr << "could not add step 3 with forward transition"; return EXIT_FAILURE; } if (!workflow->addTransition(step2, step3, "", ctkWorkflow::Backward)) { std::cerr << "could not add next transition between step2 and step 3"; return EXIT_FAILURE; } if (workflow->forwardSteps(step1).length() != 1 || workflow->forwardSteps(step1).first() != step2 || workflow->forwardSteps(step2).length() != 1 || workflow->forwardSteps(step2).first() != step3 || workflow->forwardSteps(step3).length() != 0) { std::cerr << "error in list of forward steps" << std::endl; return EXIT_FAILURE; } if (workflow->backwardSteps(step1).length() != 0 || workflow->backwardSteps(step2).length() != 1 || workflow->backwardSteps(step2).first() != step1 || workflow->backwardSteps(step3).length() != 1 || workflow->backwardSteps(step3).first() != step2) { std::cerr << "error in list of backward steps" << std::endl; return EXIT_FAILURE; } if (!workflow->hasStep(step3->id())) { std::cerr << "Step3 not added to workflow"; return EXIT_FAILURE; } // if (workflow->numberOfSteps() != 3) // { // std::cerr << "workflow has " << workflow->numberOfSteps() << " steps, not 2"; // return EXIT_FAILURE; // } // now that we've stopped and restarted the state machine, we should // be back in the initial step (step 1) if (!testStartWorkflow(workflow, defaultTime, app, 1, step1, step1, 4, 3, step2, 1, 1, step3, 0, 0)) { std::cerr << "workflow is not running after start() with three steps"; return EXIT_FAILURE; } // test to make sure our lists of forward and backwards steps is correct if (!workflow->canGoForward(step1) || workflow->canGoBackward(step1) || !workflow->canGoForward(step2) || !workflow->canGoBackward(step2) || workflow->canGoForward(step3) || !workflow->canGoBackward(step3)) { std::cerr << "error in can go forward/backward" << std::endl; return EXIT_FAILURE; } // Test that the workflow transitions from step1 to step 2 to step 3 workflow->goForward(); QTimer::singleShot(defaultTime, &app, SLOT(quit())); app.exec(); workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step3, step1, 4, 4, step2, 2, 2, step3, 1, 0)) { std::cerr << "error transitioning to step3 in workflow with three steps"; return EXIT_FAILURE; } // Test that the workflow transitions back to the previous step workflow->goBackward(); if (!transitionTest(workflow, defaultTime, app, step2, step1, 4, 4, step2, 3, 2, step3, 1, 1)) { std::cerr << "error transitioning to previous step in workflow with three steps"; return EXIT_FAILURE; } // make sure the workflow stops properly if (!testStopWorkflow(workflow, defaultTime, app, step1, 4, 4, step2, 3, 3, step3, 1, 1)) { std::cerr << "error stopping workflow with three steps"; return EXIT_FAILURE; } // -------------------------------------------------------------------------- // workflow with a finish step (step 3) // restart the workflow if (!testStartWorkflow(workflow, defaultTime, app, 1, step1, step1, 5, 4, step2, 3, 3, step3, 1, 1)) { std::cerr << "workflow with finish step is not running after start()"; return EXIT_FAILURE; } // try to go automatically to step 3 workflow->goToStep("Step 3"); if (!transitionTest(workflow, defaultTime, app, step1, step1, 6, 5, step2, 4, 4, step3, 2, 2)) { std::cerr << "error after going to finish step"; return EXIT_FAILURE; } // try to go automatically to step 3 again workflow->goToStep("Step 3"); if (!transitionTest(workflow, defaultTime, app, step1, step1, 7, 6, step2, 5, 5, step3, 3, 3)) { std::cerr << "error after going to finish step the second time"; return EXIT_FAILURE; } // stop workflow if (!testStopWorkflow(workflow, defaultTime, app, step1, 7, 7, step2, 5, 5, step3, 3, 3)) { std::cerr << "error stopping workflow with finish step"; return EXIT_FAILURE; } // -------------------------------------------------------------------------- // workflow with two finishing steps (step3 and step4) ctkExampleDerivedWorkflowStep *step4 = new ctkExampleDerivedWorkflowStep(workflow, "Step 4"); step4->setName("Step 4"); step4->setDescription("Description for step 4"); workflow->addTransition(step3, step4); // restart the workflow if (!testStartWorkflow(workflow, defaultTime, app, 1, step1, step1, 8, 7, step2, 5, 5, step3, 3, 3, step4, 0, 0)) { std::cerr << "workflow with two finish steps is not running after start()"; return EXIT_FAILURE; } // try to go automatically to step 3 workflow->goToStep("Step 3"); if (!transitionTest(workflow, defaultTime, app, step1, step1, 9, 8, step2, 6, 6, step3, 4, 4, step4, 0, 0)) { std::cerr << "error going to the first finish step of two"; return EXIT_FAILURE; } // try to go automatically to step 4 workflow->goToStep("Step 4"); if (!transitionTest(workflow, defaultTime, app, step1, step1, 10, 9, step2, 7, 7, step3, 5, 5, step4, 1, 1)) { std::cerr << "error going to the second finish step of two"; return EXIT_FAILURE; } // go to step 3 (a finish step) workflow->goForward(); QTimer::singleShot(defaultTime, &app, SLOT(quit())); app.exec(); workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step3, step1, 10, 10, step2, 8, 8, step3, 6, 5, step4, 1, 1)) { std::cerr << "error going from step1 to step3"; return EXIT_FAILURE; } // try to go automatically to step 4 (another goTo step) workflow->goToStep("Step 4"); if (!transitionTest(workflow, defaultTime, app, step3, step1, 10, 10, step2, 8, 8, step3, 7, 6, step4, 2, 2)) { std::cerr << "error going from the first finish step to the second finish step"; return EXIT_FAILURE; } // go to step 4, and then go forward (should not let you go past last step) workflow->goForward(); QTimer::singleShot(defaultTime, &app, SLOT(quit())); app.exec(); workflow->goForward(); if (!transitionTest(workflow, defaultTime, app, step4, step1, 10, 10, step2, 8, 8, step3, 7, 7, step4, 3, 2)) { std::cerr << "error going forward past last step - shouldn't let you"; return EXIT_FAILURE; } // now try to go from step 4 to step 4 (should loop) workflow->goToStep("Step 4"); if (!transitionTest(workflow, defaultTime, app, step4, step1, 10, 10, step2, 8, 8, step3, 7, 7, step4, 4, 3)) { std::cerr << "error looping from step 4 to step 4"; return EXIT_FAILURE; } // go back to step 3, and then go from step 3 to step 3 (should loop without hitting step4) workflow->goBackward(); QTimer::singleShot(defaultTime, &app, SLOT(quit())); app.exec(); workflow->goToStep("Step 3"); if (!transitionTest(workflow, defaultTime, app, step3, step1, 10, 10, step2, 8, 8, step3, 9, 8, step4, 4, 4)) { std::cerr << "error looping from step 3 to step 3"; return EXIT_FAILURE; } // handles deletions of the workflow, steps, states and transitions delete workflow; return EXIT_SUCCESS; }