ctkWorkflow.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QStateMachine>
  17. #include <QState>
  18. #include <QQueue>
  19. // CTK includes
  20. #include "ctkWorkflow.h"
  21. #include "ctkWorkflowStep.h"
  22. #include "ctkWorkflowStep_p.h"
  23. #include "ctkWorkflowTransitions.h"
  24. #include "ctkLogger.h"
  25. // STD includes
  26. #include <iostream>
  27. //--------------------------------------------------------------------------
  28. static ctkLogger logger("org.commontk.libs.core.ctkWorkflow");
  29. //--------------------------------------------------------------------------
  30. // --------------------------------------------------------------------------
  31. // ctkWorkflowPrivate methods
  32. // --------------------------------------------------------------------------
  33. ctkWorkflowPrivate::ctkWorkflowPrivate(ctkWorkflow& object)
  34. :q_ptr(&object)
  35. {
  36. this->InitialStep = 0;
  37. this->CurrentStep = 0;
  38. this->OriginStep = 0;
  39. this->DestinationStep = 0;
  40. this->GoToStep = 0;
  41. this->StartingStep = 0;
  42. this->TransitionToPreviousStartingStep = 0;
  43. // By default, go back to the origin step upon success of the goToStep(targetId) attempt.
  44. this->GoBackToOriginStepUponSuccess = true;
  45. this->ARTIFICIAL_BRANCH_ID_PREFIX = "ctkWorkflowArtificialBranchId_";
  46. }
  47. // --------------------------------------------------------------------------
  48. ctkWorkflowPrivate::~ctkWorkflowPrivate()
  49. {
  50. }
  51. // --------------------------------------------------------------------------
  52. bool ctkWorkflowPrivate::addStep(ctkWorkflowStep* step)
  53. {
  54. Q_Q(ctkWorkflow);
  55. Q_ASSERT(step);
  56. Q_ASSERT(!q->hasStep(step->id()));
  57. Q_ASSERT(!this->StateMachine->isRunning());
  58. if (!step->workflow())
  59. {
  60. step->setWorkflow(q);
  61. }
  62. if (step->workflow() != q)
  63. {
  64. // Check if steps are not already associated with a different workflow
  65. QString msg("addStep - step [%1] already associated with a different workfow !");
  66. logger.error(msg.arg(step->id()));
  67. return false;
  68. }
  69. if (!this->RegisteredSteps.contains(step))
  70. {
  71. this->RegisteredSteps << step;
  72. emit q->stepRegistered(step);
  73. }
  74. // Add the states, creating them if necessary
  75. this->StateMachine->addState(step->processingState());
  76. this->StateMachine->addState(step->validationState());
  77. // Update the map of steps to transitions and the <state,step> map
  78. this->StepToForwardAndBackwardStepMap.insert(step, new forwardAndBackwardSteps);
  79. this->StateToStepMap[step->processingState()] = step;
  80. this->StateToStepMap[step->validationState()] = step;
  81. // Setup the signal/slot that triggers the attempt to go to the next step
  82. QObject::connect(step->validationState(), SIGNAL(entered()),
  83. q, SLOT(attemptToGoToNextStep()));
  84. // Setup the signal/slot that triggers the evaluation of the validation results
  85. // after validate(const QString&) is called
  86. this->connect(
  87. step->ctkWorkflowStepQObject(), SIGNAL(validationComplete(bool,QString)),
  88. q, SLOT(evaluateValidationResults(bool,QString)));
  89. this->connect(
  90. step->ctkWorkflowStepQObject(), SIGNAL(onEntryComplete()),
  91. SLOT(processingAfterOnEntry()));
  92. this->connect(
  93. step->ctkWorkflowStepQObject(), SIGNAL(onExitComplete()),
  94. SLOT(processingAfterOnExit()));
  95. return true;
  96. }
  97. // --------------------------------------------------------------------------
  98. bool ctkWorkflowPrivate::hasDuplicateTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  99. const ctkWorkflow::TransitionDirectionality directionality)
  100. {
  101. Q_Q(ctkWorkflow);
  102. Q_ASSERT(origin);
  103. Q_ASSERT(destination);
  104. Q_ASSERT(directionality == ctkWorkflow::Forward || ctkWorkflow::Backward);
  105. ctkWorkflowPrivate::StepListType stepList;
  106. ctkWorkflowStep* targetStep = 0;
  107. if (directionality == ctkWorkflow::Forward)
  108. {
  109. stepList = q->forwardSteps(origin);
  110. targetStep = destination;
  111. }
  112. else if (directionality == ctkWorkflow::Backward)
  113. {
  114. stepList = q->backwardSteps(destination);
  115. targetStep = origin;
  116. }
  117. foreach(ctkWorkflowStep * step, stepList)
  118. {
  119. if (step == targetStep)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. // --------------------------------------------------------------------------
  127. bool ctkWorkflowPrivate::hasTransitionWithSameBranchId(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  128. const QString& branchId,
  129. const ctkWorkflow::TransitionDirectionality directionality)
  130. {
  131. Q_ASSERT(origin);
  132. Q_ASSERT(destination);
  133. Q_ASSERT(directionality == ctkWorkflow::Forward || ctkWorkflow::Backward);
  134. Q_ASSERT(!branchId.isEmpty());
  135. QList<QString> branchIdList;
  136. if (directionality == ctkWorkflow::Forward)
  137. {
  138. branchIdList = this->forwardBranchIds(origin);
  139. }
  140. else if (directionality == ctkWorkflow::Backward)
  141. {
  142. branchIdList = this->backwardBranchIds(destination);
  143. }
  144. foreach(QString id, branchIdList)
  145. {
  146. if (QString::compare(id, branchId, Qt::CaseInsensitive) == 0)
  147. {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. // --------------------------------------------------------------------------
  154. void ctkWorkflowPrivate::createTransitionToNextStep(ctkWorkflowStep* origin,
  155. ctkWorkflowStep* destination,
  156. const QString& branchId)
  157. {
  158. Q_Q(ctkWorkflow);
  159. Q_ASSERT(origin);
  160. Q_ASSERT(destination);
  161. Q_ASSERT(!q->hasTransition(origin, destination, branchId, ctkWorkflow::Forward));
  162. QString id;
  163. // create an artificial branchId if one is not given
  164. if (branchId.isEmpty())
  165. {
  166. id.setNum(this->numberOfForwardSteps(origin));
  167. id.prepend(this->ARTIFICIAL_BRANCH_ID_PREFIX);
  168. }
  169. else
  170. {
  171. id = branchId;
  172. }
  173. // Create the transition
  174. ctkWorkflowInterstepTransition* transition = new ctkWorkflowInterstepTransition(ctkWorkflowInterstepTransition::TransitionToNextStep, id);
  175. transition->setTargetState(destination->processingState());
  176. origin->validationState()->addTransition(transition);
  177. // Update the step to transitions map
  178. this->StepToForwardAndBackwardStepMap.value(origin)->appendForwardStep(destination, id);
  179. // Setup the signal/slot that shows and hides the steps' user interfaces
  180. // on transition to the next step
  181. QObject::connect(transition, SIGNAL(triggered()), q, SLOT(performTransitionBetweenSteps()));
  182. }
  183. // --------------------------------------------------------------------------
  184. void ctkWorkflowPrivate::createTransitionToPreviousStep(ctkWorkflowStep* origin,
  185. ctkWorkflowStep* destination,
  186. const QString& branchId)
  187. {
  188. Q_Q(ctkWorkflow);
  189. Q_ASSERT(origin);
  190. Q_ASSERT(destination);
  191. Q_ASSERT(!q->hasTransition(origin, destination, branchId, ctkWorkflow::Backward));
  192. QString id;
  193. // create an artificial branchId if one is not given
  194. if (branchId.isEmpty())
  195. {
  196. id.setNum(this->numberOfBackwardSteps(destination));
  197. id.prepend(this->ARTIFICIAL_BRANCH_ID_PREFIX);
  198. }
  199. else
  200. {
  201. id = branchId;
  202. }
  203. ctkWorkflowInterstepTransition* transition = new ctkWorkflowInterstepTransition(ctkWorkflowInterstepTransition::TransitionToPreviousStep, id);
  204. transition->setTargetState(origin->processingState());
  205. destination->processingState()->addTransition(transition);
  206. // Update the step to transitions map
  207. this->StepToForwardAndBackwardStepMap.value(destination)->appendBackwardStep(origin, id);
  208. // Setup the signal/slot that shows and hides the steps' user
  209. // interfaces on transition to the previous step
  210. QObject::connect(transition, SIGNAL(triggered()), q, SLOT(performTransitionBetweenSteps()));
  211. }
  212. // --------------------------------------------------------------------------
  213. void ctkWorkflowPrivate::createTransitionToPreviousStartingStep(ctkWorkflowStep* startingStep,
  214. ctkWorkflowStep* currentStep)
  215. {
  216. Q_Q(ctkWorkflow);
  217. Q_ASSERT(startingStep);
  218. Q_ASSERT(currentStep);
  219. if (!this->TransitionToPreviousStartingStep)
  220. {
  221. ctkWorkflowInterstepTransition* transition = new ctkWorkflowInterstepTransition(
  222. ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  223. // Setup the signal/slot that shows and hides the steps' user interfaces
  224. // on transition to the previous step
  225. QObject::connect(transition, SIGNAL(triggered()), q, SLOT(performTransitionBetweenSteps()));
  226. this->TransitionToPreviousStartingStep = transition;
  227. }
  228. QState* currentState;
  229. // looping on the finish step
  230. if (startingStep == currentStep)
  231. {
  232. currentState = currentStep->validationState();
  233. }
  234. else
  235. {
  236. currentState = currentStep->processingState();
  237. }
  238. this->TransitionToPreviousStartingStep->setTargetState(startingStep->processingState());
  239. currentState->addTransition(this->TransitionToPreviousStartingStep);
  240. }
  241. // --------------------------------------------------------------------------
  242. ctkWorkflowStep* ctkWorkflowPrivate::stepFromId(const QString& id)const
  243. {
  244. foreach(ctkWorkflowStep* step, this->StepToForwardAndBackwardStepMap.keys())
  245. {
  246. Q_ASSERT(step);
  247. if (QString::compare(step->id(), id, Qt::CaseInsensitive) == 0)
  248. {
  249. return step;
  250. }
  251. }
  252. return 0;
  253. }
  254. // --------------------------------------------------------------------------
  255. QList<QString> ctkWorkflowPrivate::forwardBranchIds(ctkWorkflowStep* step)const
  256. {
  257. Q_ASSERT(step);
  258. return this->StepToForwardAndBackwardStepMap.value(step)->forwardBranchIds();
  259. }
  260. // --------------------------------------------------------------------------
  261. QList<QString> ctkWorkflowPrivate::backwardBranchIds(ctkWorkflowStep* step)const
  262. {
  263. Q_ASSERT(step);
  264. return this->StepToForwardAndBackwardStepMap.value(step)->backwardBranchIds();
  265. }
  266. //---------------------------------------------------------------------------
  267. void ctkWorkflowPrivate::validateInternal(ctkWorkflowStep* step)
  268. {
  269. Q_ASSERT(step);
  270. logger.debug(QString("validateInternal - validating input from %1").arg(step->name()));
  271. if (step->hasValidateCommand())
  272. {
  273. step->invokeValidateCommand(this->DesiredBranchId);
  274. }
  275. else
  276. {
  277. step->validate(this->DesiredBranchId);
  278. }
  279. }
  280. // --------------------------------------------------------------------------
  281. void ctkWorkflowPrivate::onEntryInternal(
  282. ctkWorkflowStep* step,
  283. ctkWorkflowStep* comingFrom,
  284. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType)
  285. {
  286. Q_ASSERT(step);
  287. logger.debug(QString("onEntryInternal - entering input from %1").arg(step->name()));
  288. //Ensure we are transitioning between steps or starting the workflow
  289. Q_ASSERT(transitionType == ctkWorkflowInterstepTransition::TransitionToNextStep
  290. || transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStep
  291. || transitionType == ctkWorkflowInterstepTransition::StartingWorkflow
  292. || transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  293. if (step->hasOnEntryCommand())
  294. {
  295. step->invokeOnEntryCommand(comingFrom, transitionType);
  296. }
  297. else
  298. {
  299. step->onEntry(comingFrom, transitionType);
  300. }
  301. }
  302. // --------------------------------------------------------------------------
  303. void ctkWorkflowPrivate::processingAfterOnEntry()
  304. {
  305. Q_Q(ctkWorkflow);
  306. logger.debug("processingAfterOnEntry");
  307. if (!this->DestinationStep)
  308. {
  309. logger.error("processingAfterOnEntry - Called processingAfterOnEntry without "
  310. "having set a destination step");
  311. return;
  312. }
  313. // Update the currentStep and previous step
  314. this->CurrentStep = this->DestinationStep;
  315. // Reset the pointers used internally for performing a transition
  316. this->OriginStep = 0;
  317. this->DestinationStep = 0;
  318. // // Reset the pointers used internally for performing a transition
  319. // // back to the starting step
  320. // if (d->TransitionToPreviousStartingStep)
  321. // {
  322. // std::cout << "TRANSITION TO PREVIOUS STARTING STEP EXISTS" << std::endl;
  323. // //d->TransitionToPreviousStartingStep->sourceState()->removeTransition(d->TransitionToPreviousStartingStep);
  324. // //std::cout << "removed" << std::endl;
  325. // // d->TransitionToPreviousStartingStep = 0;
  326. // //destination->processingState()->removeTransition(d->TransitionToPreviousStartingStep);
  327. // //delete d->TransitionToPreviousStartingStep;
  328. // // d->TransitionToPreviousStartingStep = 0;
  329. // std::cout << "here" << std::endl;
  330. // }
  331. // If we are trying to get to the finish step, then check if we are
  332. // finished.
  333. if (this->GoToStep)
  334. {
  335. if (this->CurrentStep == this->GoToStep)
  336. {
  337. q->goToStepSucceeded();
  338. }
  339. // if we're not finished, continue transitioning to the next step
  340. else
  341. {
  342. q->goForward();
  343. }
  344. }
  345. else
  346. {
  347. emit q->currentStepChanged(this->CurrentStep);
  348. }
  349. }
  350. // --------------------------------------------------------------------------
  351. void ctkWorkflowPrivate::onExitInternal(
  352. ctkWorkflowStep* step,
  353. ctkWorkflowStep* goingTo,
  354. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType)
  355. {
  356. Q_ASSERT(step);
  357. logger.debug(QString("onExitInternal - exiting %1").arg(step->name()));
  358. // Ensure we are transitioning between steps or starting the workflow
  359. Q_ASSERT (transitionType == ctkWorkflowInterstepTransition::TransitionToNextStep ||
  360. transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStep ||
  361. transitionType == ctkWorkflowInterstepTransition::StoppingWorkflow ||
  362. transitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  363. if (step->hasOnExitCommand())
  364. {
  365. step->invokeOnExitCommand(goingTo, transitionType);
  366. }
  367. else
  368. {
  369. step->onExit(goingTo, transitionType);
  370. }
  371. }
  372. // --------------------------------------------------------------------------
  373. void ctkWorkflowPrivate::processingAfterOnExit()
  374. {
  375. // enter the destination step if we have one
  376. if (this->DestinationStep)
  377. {
  378. this->onEntryInternal(this->DestinationStep, this->OriginStep, this->TransitionType);
  379. }
  380. // reset the pointers used internally for performing a transition if we're done
  381. else
  382. {
  383. this->OriginStep = 0;
  384. this->DestinationStep = 0;
  385. // we've exited the CurrentStep and haven't gone into another step, so we no longer have a
  386. // currentStep.
  387. this->CurrentStep = 0;
  388. }
  389. }
  390. // --------------------------------------------------------------------------
  391. ctkWorkflowStep* ctkWorkflowPrivate::stepFromState(const QAbstractState* state)
  392. {
  393. if (state)
  394. {
  395. return this->StateToStepMap.value(state);
  396. }
  397. return 0;
  398. }
  399. // --------------------------------------------------------------------------
  400. int ctkWorkflowPrivate::numberOfForwardSteps(ctkWorkflowStep* step)
  401. {
  402. Q_Q(ctkWorkflow);
  403. return q->forwardSteps(step).length();
  404. }
  405. // --------------------------------------------------------------------------
  406. int ctkWorkflowPrivate::numberOfBackwardSteps(ctkWorkflowStep* step)
  407. {
  408. Q_Q(ctkWorkflow);
  409. return q->backwardSteps(step).length();
  410. }
  411. // --------------------------------------------------------------------------
  412. bool ctkWorkflowPrivate::pathExists(const QString& goalId, ctkWorkflowStep* origin)const
  413. {
  414. Q_Q(const ctkWorkflow);
  415. Q_ASSERT(!goalId.isEmpty());
  416. Q_ASSERT(this->CurrentStep);
  417. QString originId;
  418. if (origin)
  419. {
  420. originId = origin->id();
  421. }
  422. else
  423. {
  424. originId = this->CurrentStep->id();
  425. }
  426. // there exists a path from the origin to the goal if:
  427. // - there is a goal AND
  428. // - either:
  429. // - the origin is already the goal
  430. // - there is a path from at least one of the origin's successors to the goal
  431. return (q->hasStep(goalId)
  432. && ((QString::compare(goalId, originId, Qt::CaseInsensitive) == 0)
  433. || (q->canGoForward(origin)))); // <-- TODO insert logic here for looking at graph
  434. }
  435. // --------------------------------------------------------------------------
  436. bool ctkWorkflowPrivate::pathExistsFromNextStep(const QString& goalId, const QString& branchId)const
  437. {
  438. Q_ASSERT(!goalId.isEmpty());
  439. Q_ASSERT(!branchId.isEmpty());
  440. Q_ASSERT(this->CurrentStep);
  441. // return whether there exists a path from the the step that will be followed (given the branchId) to the goal
  442. ctkWorkflowStep* nextStep = this->StepToForwardAndBackwardStepMap.value(this->CurrentStep)->forwardStep(branchId);
  443. if (!nextStep)
  444. {
  445. return false;
  446. }
  447. else
  448. {
  449. return this->pathExists(goalId, nextStep);
  450. }
  451. }
  452. // --------------------------------------------------------------------------
  453. // ctkWorkflow methods
  454. // --------------------------------------------------------------------------
  455. ctkWorkflow::ctkWorkflow(QObject* _parent) : Superclass(_parent)
  456. , d_ptr(new ctkWorkflowPrivate(*this))
  457. {
  458. Q_D(ctkWorkflow);
  459. d->StateMachine = new QStateMachine(this);
  460. }
  461. // --------------------------------------------------------------------------
  462. ctkWorkflow::~ctkWorkflow()
  463. {
  464. Q_D(ctkWorkflow);
  465. if (d->StateMachine->isRunning())
  466. {
  467. d->StateMachine->stop();
  468. }
  469. // Clean registered step
  470. while (!d->RegisteredSteps.isEmpty())
  471. {
  472. delete d->RegisteredSteps.takeFirst();
  473. }
  474. }
  475. // --------------------------------------------------------------------------
  476. bool ctkWorkflow::addTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  477. const QString& branchId,
  478. const ctkWorkflow::TransitionDirectionality directionality)
  479. {
  480. Q_D(ctkWorkflow);
  481. if (d->StateMachine->isRunning())
  482. {
  483. logger.warn("addTransition - Cannot add a transition while the workflow is started !");
  484. return false;
  485. }
  486. // Set origin id if empty
  487. if (origin && origin->id().isEmpty())
  488. {
  489. origin->setId(QString("step%1").arg(d->StepToForwardAndBackwardStepMap.count()));
  490. }
  491. // cannot currently create a transition between two steps of the same id, which is equivalent to
  492. // adding a transition from a step to itself
  493. if (origin && destination && (QString::compare(origin->id(), destination->id(), Qt::CaseInsensitive) == 0))
  494. {
  495. logger.error("addTransition - Workflow does not currently support a transition"
  496. " from a step to itself. Use GoToStep instead !");
  497. return false;
  498. }
  499. // add the origin step if it doesn't exist in the workflow yet
  500. if (origin && !this->hasStep(origin->id()))
  501. {
  502. bool ok = d->addStep(origin);
  503. if (!ok)
  504. {
  505. return false;
  506. }
  507. }
  508. // Set destination id if empty
  509. if (destination && destination->id().isEmpty())
  510. {
  511. destination->setId(QString("step%1").arg(d->StepToForwardAndBackwardStepMap.count()));
  512. }
  513. // add the destination step if it doesn't exist in the workflow yet
  514. if (destination && !this->hasStep(destination->id()))
  515. {
  516. bool ok = d->addStep(destination);
  517. if (!ok)
  518. {
  519. return false;
  520. }
  521. }
  522. if (origin && destination)
  523. {
  524. // ensure we haven't already added a transition with the same origin, destination and directionality
  525. if (this->hasTransition(origin, destination, branchId, directionality))
  526. {
  527. logger.warn("addTransition - Cannot create a transition that matches a "
  528. "previously created transition");
  529. return false;
  530. }
  531. // create the forward transition
  532. if (directionality == ctkWorkflow::Forward
  533. || directionality == ctkWorkflow::Bidirectional)
  534. {
  535. //qDebug() << "addTransition" << origin->id() << "->" << destination->id();
  536. d->createTransitionToNextStep(origin, destination, branchId);
  537. }
  538. // create the backward transition
  539. if (directionality == ctkWorkflow::Backward
  540. || directionality == ctkWorkflow::Bidirectional)
  541. {
  542. //qDebug() << "addTransition" << origin->id() << "<-" << destination->id();
  543. d->createTransitionToPreviousStep(origin, destination, branchId);
  544. }
  545. }
  546. // Set initialStep if needed
  547. if (origin && d->StepToForwardAndBackwardStepMap.count() == 2 && !this->initialStep())
  548. {
  549. this->setInitialStep(origin);
  550. }
  551. return true;
  552. }
  553. // --------------------------------------------------------------------------
  554. bool ctkWorkflow::hasTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  555. const QString& branchId,
  556. const ctkWorkflow::TransitionDirectionality directionality)
  557. {
  558. Q_D(ctkWorkflow);
  559. // we have a bidirectional transition if we have both a forward and a backward transition
  560. if (directionality == ctkWorkflow::Bidirectional)
  561. {
  562. return this->hasTransition(origin, destination, branchId, ctkWorkflow::Forward)
  563. && this->hasTransition(origin, destination, branchId, ctkWorkflow::Backward);
  564. }
  565. else
  566. {
  567. if (branchId.isEmpty())
  568. {
  569. return d->hasDuplicateTransition(origin, destination, directionality);
  570. }
  571. else
  572. {
  573. return d->hasDuplicateTransition(origin, destination, directionality)
  574. || d->hasTransitionWithSameBranchId(origin, destination, branchId, directionality);
  575. }
  576. }
  577. }
  578. // --------------------------------------------------------------------------
  579. QList<ctkWorkflowStep*> ctkWorkflow::forwardSteps(ctkWorkflowStep* step)const
  580. {
  581. Q_D(const ctkWorkflow);
  582. // use the given step if provided, otherwise use the workflow's current step
  583. if (step && d->StepToForwardAndBackwardStepMap.contains(step))
  584. {
  585. return d->StepToForwardAndBackwardStepMap.value(step)->forwardSteps();
  586. }
  587. else if (d->CurrentStep && d->StepToForwardAndBackwardStepMap.contains(d->CurrentStep))
  588. {
  589. return d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->forwardSteps();
  590. }
  591. else
  592. {
  593. return QList<ctkWorkflowStep*>();
  594. }
  595. }
  596. // --------------------------------------------------------------------------
  597. QList<ctkWorkflowStep*> ctkWorkflow::backwardSteps(ctkWorkflowStep* step)const
  598. {
  599. Q_D(const ctkWorkflow);
  600. // use the current step if provided, otherwise use the workflow's current step
  601. if (step && d->StepToForwardAndBackwardStepMap.contains(step))
  602. {
  603. return d->StepToForwardAndBackwardStepMap.value(step)->backwardSteps();
  604. }
  605. else if (d->CurrentStep && d->StepToForwardAndBackwardStepMap.contains(d->CurrentStep))
  606. {
  607. return d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->backwardSteps();
  608. }
  609. else
  610. {
  611. return QList<ctkWorkflowStep*>();
  612. }
  613. }
  614. // --------------------------------------------------------------------------
  615. QList<ctkWorkflowStep*> ctkWorkflow::finishSteps()const
  616. {
  617. Q_D(const ctkWorkflow);
  618. // iterate through our list of steps, and keep the steps that don't have anything following them
  619. QList<ctkWorkflowStep*> finishSteps;
  620. foreach (ctkWorkflowStep* step, d->StepToForwardAndBackwardStepMap.keys())
  621. {
  622. if (!this->canGoForward(step))
  623. {
  624. finishSteps.append(step);
  625. }
  626. }
  627. return finishSteps;
  628. }
  629. // --------------------------------------------------------------------------
  630. QList<ctkWorkflowStep*> ctkWorkflow::steps()const
  631. {
  632. Q_D(const ctkWorkflow);
  633. return d->RegisteredSteps;
  634. }
  635. // --------------------------------------------------------------------------
  636. bool ctkWorkflow::canGoForward(ctkWorkflowStep* step)const
  637. {
  638. return (!this->forwardSteps(step).isEmpty());
  639. }
  640. // --------------------------------------------------------------------------
  641. bool ctkWorkflow::canGoBackward(ctkWorkflowStep* step)const
  642. {
  643. return (!this->backwardSteps(step).isEmpty());
  644. }
  645. // --------------------------------------------------------------------------
  646. bool ctkWorkflow::canGoToStep(const QString& targetId, ctkWorkflowStep* step)const
  647. {
  648. Q_D(const ctkWorkflow);
  649. return d->pathExists(targetId, step);
  650. }
  651. // --------------------------------------------------------------------------
  652. bool ctkWorkflow::hasStep(const QString& id)const
  653. {
  654. Q_D(const ctkWorkflow);
  655. return d->stepFromId(id);
  656. }
  657. // --------------------------------------------------------------------------
  658. // Convenience method to set the QStateMachine's initialState to a
  659. // specific step's processing state.
  660. CTK_GET_CPP(ctkWorkflow, ctkWorkflowStep*, initialStep, InitialStep);
  661. CTK_SET_CPP(ctkWorkflow, ctkWorkflowStep*, setInitialStep, InitialStep);
  662. // --------------------------------------------------------------------------
  663. CTK_GET_CPP(ctkWorkflow, ctkWorkflowStep*, currentStep, CurrentStep);
  664. // --------------------------------------------------------------------------
  665. void ctkWorkflow::start()
  666. {
  667. Q_D(ctkWorkflow);
  668. if (!d->InitialStep)
  669. {
  670. logger.warn("start - Cannot start workflow without an initial step");
  671. return;
  672. }
  673. // Setup to do the entry processing for the initial setp
  674. d->StateMachine->setInitialState(d->InitialStep->processingState());
  675. d->OriginStep = 0;
  676. d->DestinationStep = d->InitialStep;
  677. d->TransitionType = ctkWorkflowInterstepTransition::StartingWorkflow;
  678. d->onEntryInternal(d->DestinationStep, d->OriginStep, d->TransitionType);
  679. d->StateMachine->start();
  680. }
  681. // --------------------------------------------------------------------------
  682. bool ctkWorkflow::isRunning()const
  683. {
  684. Q_D(const ctkWorkflow);
  685. return d->StateMachine->isRunning();
  686. }
  687. // --------------------------------------------------------------------------
  688. void ctkWorkflow::stop()
  689. {
  690. Q_D(ctkWorkflow);
  691. if (!d->StateMachine->isRunning())
  692. {
  693. return;
  694. }
  695. // Setup to do the exit processing for the current step
  696. if (d->CurrentStep)
  697. {
  698. d->OriginStep = d->CurrentStep;
  699. d->DestinationStep = 0;
  700. d->TransitionType = ctkWorkflowInterstepTransition::StoppingWorkflow;
  701. d->onExitInternal(d->OriginStep, d->DestinationStep, d->TransitionType);
  702. }
  703. d->StateMachine->stop();
  704. }
  705. // --------------------------------------------------------------------------
  706. void ctkWorkflow::goForward(const QString& desiredBranchId)
  707. {
  708. Q_D(ctkWorkflow);
  709. if (!this->isRunning())
  710. {
  711. logger.warn("goForward - The workflow is not running !");
  712. return;
  713. }
  714. // if we're just going to the next step and not to a 'goTo' step, then check to make sure that
  715. // there exists a step following the current step
  716. if (!d->GoToStep)
  717. {
  718. if (!this->canGoForward())
  719. {
  720. logger.warn("goForward - Attempt to goForward from a finish step !");
  721. return;
  722. }
  723. }
  724. d->DesiredBranchId = desiredBranchId;
  725. logger.info("goForward - posting ValidationTransition");
  726. d->StateMachine->postEvent(
  727. new ctkWorkflowIntrastepTransitionEvent(ctkWorkflowIntrastepTransition::ValidationTransition));
  728. }
  729. // --------------------------------------------------------------------------
  730. void ctkWorkflow::goBackward(const QString& desiredBranchId)
  731. {
  732. Q_D(ctkWorkflow);
  733. if (!this->isRunning())
  734. {
  735. logger.warn("goBackward - The workflow is not running !");
  736. return;
  737. }
  738. if (!this->canGoBackward())
  739. {
  740. logger.warn("goBackward - Attempt to goBackward from first step !");
  741. return;
  742. }
  743. ctkWorkflowStep* previousStep = d->StepToPreviousStepMap[d->CurrentStep];
  744. Q_ASSERT(previousStep);
  745. QString branchId = d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->backwardBranchId(previousStep);
  746. Q_ASSERT(!branchId.isEmpty());
  747. d->DesiredBranchId = desiredBranchId;
  748. logger.info("goBackward - posting TransitionToPreviousStep");
  749. d->StateMachine->postEvent(
  750. new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToPreviousStep, branchId));
  751. }
  752. // --------------------------------------------------------------------------
  753. CTK_GET_CPP(ctkWorkflow, bool, goBackToOriginStepUponSuccess, GoBackToOriginStepUponSuccess);
  754. CTK_SET_CPP(ctkWorkflow, bool, setGoBackToOriginStepUponSuccess, GoBackToOriginStepUponSuccess);
  755. // --------------------------------------------------------------------------
  756. void ctkWorkflow::goToStep(const QString& targetId)
  757. {
  758. Q_D(ctkWorkflow);
  759. if (!this->isRunning())
  760. {
  761. logger.warn("goToStep - The workflow is not running !");
  762. return;
  763. }
  764. // TODO currently returns true only if the workflow is running - need logic here
  765. if (!this->canGoToStep(targetId))
  766. {
  767. logger.warn(QString("goToStep - Cannot goToStep %1 ").arg(targetId));
  768. return;
  769. }
  770. #ifndef QT_NO_DEBUG
  771. ctkWorkflowStep* step = d->stepFromId(targetId);
  772. Q_ASSERT(step);
  773. #endif
  774. logger.info(QString("goToStep - Attempting to go to finish step %1").arg(targetId));
  775. // if (step == d->CurrentStep)
  776. // {
  777. // qDebug() << "we are already in the desired finish step";
  778. // return;
  779. // }
  780. d->GoToStep = d->stepFromId(targetId);
  781. d->StartingStep = d->CurrentStep;
  782. this->goForward();
  783. }
  784. // --------------------------------------------------------------------------
  785. void ctkWorkflow::attemptToGoToNextStep()
  786. {
  787. logger.info("attemptToGoToNextStep - Attempting to go to the next step ");
  788. Q_D(ctkWorkflow);
  789. Q_ASSERT(d->CurrentStep);
  790. //Q_ASSERT(this->canGoForward(d->CurrentStep));
  791. d->validateInternal(d->CurrentStep);
  792. }
  793. // --------------------------------------------------------------------------
  794. void ctkWorkflow::evaluateValidationResults(bool validationSucceeded, const QString& branchId)
  795. {
  796. if (validationSucceeded)
  797. {
  798. this->goToNextStepAfterSuccessfulValidation(branchId);
  799. }
  800. else
  801. {
  802. this->goToProcessingStateAfterValidationFailed();
  803. }
  804. }
  805. // --------------------------------------------------------------------------
  806. // if ctkWorkflowStep::validationComplete() did not provide a branchId, then:
  807. // - if there is one step following the current step, we will follow that transition
  808. // - if there are multiple steps following the current step, then we will follow the first
  809. // transition that was added
  810. // (either way this corresponds to following the first forwardBranchId we've recorded)
  811. // if ctkWorkflowStep::validationComplete() provided a branchId, then:
  812. // - if there is one transition following the current step that was not created using a branchId,
  813. // then we will follow it
  814. // - otherwise do a conditional branching based on the branchId provided by validationComplete()
  815. void ctkWorkflow::goToNextStepAfterSuccessfulValidation(const QString& branchId)
  816. {
  817. Q_D(ctkWorkflow);
  818. logger.debug("goToNextStepAfterSuccessfulValidation - Calidation succeeded");
  819. logger.info("goToNextStepAfterSuccessfulValidation - Posting TransitionToNextStep");
  820. // we may already be in the 'goTo' step - i.e. looping on a finish step
  821. if (d->GoToStep && d->CurrentStep == d->GoToStep)
  822. {
  823. this->goToStepSucceeded();
  824. return;
  825. }
  826. QString transitionBranchId;
  827. // these values are helpful for the logic below
  828. QString firstForwardBranchId = d->StepToForwardAndBackwardStepMap.value(d->CurrentStep)->firstForwardBranchId();
  829. int numberOfForwardSteps = d->numberOfForwardSteps(d->CurrentStep);
  830. Q_ASSERT(!firstForwardBranchId.isEmpty());
  831. Q_ASSERT(numberOfForwardSteps);
  832. // validationComplete() does not give us a branchId
  833. if (branchId.isEmpty())
  834. {
  835. transitionBranchId = firstForwardBranchId;
  836. if (numberOfForwardSteps > 1)
  837. {
  838. logger.warn("goToNextStepAfterSuccessfulValidation - ctkWorkflowStep::ValidatComplete() "
  839. "did not provide branchId at a branch in the workflow - will follow first "
  840. "transition that was created");
  841. }
  842. }
  843. // validationComplete() gives us a branchId
  844. else
  845. {
  846. if (numberOfForwardSteps == 1 && firstForwardBranchId.contains(d->ARTIFICIAL_BRANCH_ID_PREFIX))
  847. {
  848. transitionBranchId = firstForwardBranchId;
  849. logger.warn("goToNextStepAfterSuccessfulValidation - ctkWorkflowStep::ValidationComplete()"
  850. " returns a branchId, but was overridden by the workflow");
  851. }
  852. else
  853. {
  854. transitionBranchId = branchId;
  855. }
  856. }
  857. // if we are trying to go to a 'goTo' step, check that the selected branch will still take us along a path that leads to the 'goTo' step, and fail if not
  858. if (d->GoToStep && !d->pathExistsFromNextStep(d->GoToStep->id(), transitionBranchId))
  859. {
  860. this->goToProcessingStateAfterValidationFailed();
  861. return;
  862. }
  863. d->StateMachine->postEvent(new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToNextStep, transitionBranchId));
  864. }
  865. // --------------------------------------------------------------------------
  866. void ctkWorkflow::goToProcessingStateAfterValidationFailed()
  867. {
  868. logger.debug("goToNextStepAfterSuccessfulValidation - Validation failed");
  869. Q_D(ctkWorkflow);
  870. // Validation failed in the process of attempting to go to the finish step
  871. if (d->GoToStep)
  872. {
  873. this->goToStepFailed();
  874. }
  875. logger.info("goToNextStepAfterSuccessfulValidation - Posting ValidationFailedTransition");
  876. d->StateMachine->postEvent(new ctkWorkflowIntrastepTransitionEvent(ctkWorkflowIntrastepTransition::ValidationFailedTransition));
  877. }
  878. // --------------------------------------------------------------------------
  879. void ctkWorkflow::performTransitionBetweenSteps()
  880. {
  881. Q_D(ctkWorkflow);
  882. logger.debug("performTransitionBetweenSteps - Performing transition between steps");
  883. // Alternative: could find the origin and destination step based on
  884. // d->CurrentStep rather than QObject::sender(), but would require
  885. // keeping track of an origin step's destination step (and would be
  886. // tricky in an extension to branching workflows, unless we change
  887. // this method signature)
  888. ctkWorkflowInterstepTransition* transition = qobject_cast<ctkWorkflowInterstepTransition*>(QObject::sender());
  889. Q_ASSERT(transition);
  890. d->OriginStep = d->stepFromState(transition->sourceState());
  891. d->DestinationStep = d->stepFromState(transition->targetState());
  892. d->TransitionType = transition->transitionType();
  893. Q_ASSERT(d->TransitionType == ctkWorkflowInterstepTransition::TransitionToNextStep
  894. || d->TransitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStep
  895. || d->TransitionType == ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep);
  896. // update the map from the step to the previous step if we are going forward
  897. if (d->TransitionType == ctkWorkflowInterstepTransition::TransitionToNextStep)
  898. {
  899. d->StepToPreviousStepMap.insert(d->DestinationStep, d->OriginStep);
  900. }
  901. // exit the destination step
  902. d->onExitInternal(d->OriginStep, d->DestinationStep, d->TransitionType);
  903. }
  904. // --------------------------------------------------------------------------
  905. void ctkWorkflow::goToStepSucceeded()
  906. {
  907. Q_D(ctkWorkflow);
  908. logger.debug("goToStepSucceeded");
  909. // after success, go back to the step at which we begin looking for
  910. // the finish step (will exit the current step and enter the starting step)
  911. // only if the property goBackToOriginStepUponSuccess is true.
  912. if (this->goBackToOriginStepUponSuccess())
  913. {
  914. d->createTransitionToPreviousStartingStep(d->StartingStep, d->CurrentStep);
  915. }
  916. d->GoToStep = 0;
  917. d->StartingStep->setStatusText("Attempt to go to the finish step succeeded");
  918. d->StartingStep = 0;
  919. if (this->goBackToOriginStepUponSuccess())
  920. {
  921. this->goFromGoToStepToStartingStep();
  922. }
  923. }
  924. // --------------------------------------------------------------------------
  925. void ctkWorkflow::goFromGoToStepToStartingStep()
  926. {
  927. Q_D(ctkWorkflow);
  928. logger.info("goFromGoToStepToStartingStep - Posting TransitionToPreviousStartingStep");
  929. d->StateMachine->postEvent(new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep));
  930. }
  931. // --------------------------------------------------------------------------
  932. void ctkWorkflow::goToStepFailed()
  933. {
  934. // Abort attempt to get to the finish step
  935. Q_D(ctkWorkflow);
  936. d->GoToStep = 0;
  937. d->StartingStep = 0;
  938. // We don't need to transition between steps - leave the user at the
  939. // point of failure, so that they can try to continue manually
  940. // Emit the signal that we have changed the current step, since it wasn't emitted in the process
  941. // of going to the 'goTo' step.
  942. emit this->currentStepChanged(d->CurrentStep);
  943. // if (failedOnBranch)
  944. // {
  945. // this->goToProcessingStateAfterValidationFailed();
  946. // }
  947. }
  948. // --------------------------------------------------------------------------
  949. int ctkWorkflow::backwardDistanceToStep(ctkWorkflowStep* fromStep,
  950. ctkWorkflowStep* origin) const
  951. {
  952. if (!fromStep)
  953. {
  954. fromStep = this->currentStep();
  955. }
  956. if (!origin)
  957. {
  958. origin = this->initialStep();
  959. }
  960. if (!fromStep || !origin)
  961. {
  962. return -1;
  963. }
  964. QQueue< std::pair<ctkWorkflowStep*, int> > queue;
  965. queue.append(std::make_pair(fromStep, 0));
  966. while (! queue.isEmpty())
  967. {
  968. std::pair<ctkWorkflowStep*, int> p = queue.dequeue();
  969. ctkWorkflowStep* step = p.first;
  970. if (! step)
  971. {
  972. return -1;
  973. }
  974. if (step->id() == origin->id())
  975. {
  976. return p.second;
  977. }
  978. foreach(ctkWorkflowStep* previousStep, this->backwardSteps(step))
  979. {
  980. queue.append(std::make_pair(previousStep, p.second + 1));
  981. }
  982. }
  983. return -1;
  984. }