ctkWorkflow_p.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.commontk.org/LICENSE
  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. #ifndef __ctkWorkflow_p_h
  15. #define __ctkWorkflow_p_h
  16. // Qt includes
  17. #include <QObject>
  18. #include <QString>
  19. #include <QList>
  20. #include <QMap>
  21. // CTK includes
  22. #include "ctkWorkflow.h"
  23. #include "ctkWorkflowTransitions.h"
  24. class QStateMachine;
  25. class ctkWorkflowStep;
  26. //class ctkWorkflow;
  27. //enum ctkWorkflow::TransitionDirectionality;
  28. //-----------------------------------------------------------------------------
  29. struct forwardAndBackwardSteps
  30. {
  31. QList<ctkWorkflowStep*> forwardSteps()
  32. {
  33. return this->ForwardSteps;
  34. }
  35. QList<ctkWorkflowStep*> backwardSteps()
  36. {
  37. return this->BackwardSteps;
  38. }
  39. QList<QString> forwardBranchIds()
  40. {
  41. return this->ForwardBranchIds;
  42. }
  43. QList<QString> backwardBranchIds()
  44. {
  45. return this->BackwardBranchIds;
  46. }
  47. void appendForwardStep(ctkWorkflowStep* step, QString id)
  48. {
  49. this->ForwardSteps.append(step);
  50. this->ForwardBranchIds.append(id);
  51. }
  52. void appendBackwardStep(ctkWorkflowStep* step, QString id)
  53. {
  54. this->BackwardSteps.append(step);
  55. this->BackwardBranchIds.append(id);
  56. }
  57. QString firstForwardBranchId()
  58. {
  59. if (this->ForwardBranchIds.isEmpty())
  60. {
  61. return QString();
  62. }
  63. else
  64. {
  65. return this->ForwardBranchIds.first();
  66. }
  67. }
  68. ctkWorkflowStep* forwardStep(QString branchId)
  69. {
  70. int index = this->ForwardBranchIds.indexOf(branchId);
  71. if (index != -1)
  72. {
  73. return ForwardSteps.at(index);
  74. }
  75. else
  76. {
  77. return 0;
  78. }
  79. }
  80. QString backwardBranchId(ctkWorkflowStep* step)
  81. {
  82. int index = this->BackwardSteps.indexOf(step);
  83. if (index != -1)
  84. {
  85. return BackwardBranchIds.at(index);
  86. }
  87. else
  88. {
  89. return QString();
  90. }
  91. }
  92. QString forwardBranchId(ctkWorkflowStep* step)
  93. {
  94. int index = this->ForwardSteps.indexOf(step);
  95. if (index != -1)
  96. {
  97. return ForwardBranchIds.at(index);
  98. }
  99. else
  100. {
  101. return QString();
  102. }
  103. }
  104. private:
  105. QList<ctkWorkflowStep*> ForwardSteps;
  106. QList<ctkWorkflowStep*> BackwardSteps;
  107. QList<QString> ForwardBranchIds;
  108. QList<QString> BackwardBranchIds;
  109. };
  110. // --------------------------------------------------------------------------
  111. class ctkWorkflowPrivate : public QObject
  112. {
  113. Q_OBJECT
  114. Q_DECLARE_PUBLIC(ctkWorkflow);
  115. protected:
  116. ctkWorkflow* const q_ptr;
  117. public:
  118. ctkWorkflowPrivate(ctkWorkflow& object);
  119. ~ctkWorkflowPrivate();
  120. /// \brief Add a step to the workflow
  121. ///
  122. /// \note The step's components will be automatically be added to the state machine
  123. /// (i.e. the processingState state, validationState state, validationTransition transition
  124. /// and validationFailedtransition transition.
  125. ///
  126. /// \return True or False indicating whether the method was successful.
  127. void addStep(ctkWorkflowStep* step);
  128. /// \brief Returns whether a transition has been previously added with the same origin,
  129. /// destination and directionality
  130. bool hasDuplicateTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  131. const ctkWorkflow::TransitionDirectionality directionality);
  132. /// \brief Returns whether a transition has been previously added with the same origin and branch
  133. /// id (for forward transitions) or with the same destination and branch id (for backward transitions
  134. bool hasTransitionWithSameBranchId(ctkWorkflowStep* origin, ctkWorkflowStep* destination,
  135. const QString& branchId,
  136. const ctkWorkflow::TransitionDirectionality directionality);
  137. /// \brief Creates a transition from the origin to the destinatio.
  138. ///
  139. /// More specifically, the transition is from the \a origin's validation state to the \a
  140. /// destination's processing state, and is of type ctkWorkflowTransition::TransitionToNextStep
  141. ///
  142. /// The destination step should semantically be a next step, i.e. from a workflow perspective, the
  143. /// \a destination step is meant to appear after the \a origin step.
  144. ///
  145. /// Returns true/false indicating whether the method was successful.
  146. void createTransitionToNextStep(ctkWorkflowStep* origin,
  147. ctkWorkflowStep* destination,
  148. const QString& branchId = "");
  149. /// \brief Creates a transition from the destination to the origin
  150. ///
  151. /// More specifically, the transition is from the \a destination's processing state to the \a
  152. /// origin's processing state, and is of type ctkWorkflowTransition::TransitionToPreviousStep
  153. ///
  154. /// The destination step should semantically be a next step, i.e. from a workflow perspective, the
  155. /// \a destination step is meant to appear after the \a origin step.
  156. ///
  157. /// Returns true/false indicating whether the method was successful.
  158. void createTransitionToPreviousStep(ctkWorkflowStep* origin,
  159. ctkWorkflowStep* destination,
  160. const QString& branchId = "");
  161. /// \brief Creates a transition from the goTo step to the step from which the attempt to go to the
  162. /// goTo step was initiated.
  163. ///
  164. /// More specifically, the transition is from the \a goTo step's processing state to the \a
  165. /// starting step's processing state, and is of type ctkWorkflowTransition::TransitionToPreviousStartingStep
  166. ///
  167. /// Returns true/false indicating whether the method was successful.
  168. void createTransitionToPreviousStartingStep(ctkWorkflowStep* startingStep,
  169. ctkWorkflowStep* currentStep);
  170. ///
  171. void validateInternal(ctkWorkflowStep* step);
  172. /// \brief Performs computation required when entering this step.
  173. ///
  174. /// Does some sanity checks and then either calls onEntry() or emits the invokeOnEntryCommand(),
  175. /// depending on whether the user indicates that there is an onEntryCommand.
  176. void onEntryInternal(ctkWorkflowStep* step, ctkWorkflowStep* comingFrom,
  177. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType);
  178. /// \brief Performs computation required when exiting this step.
  179. ///
  180. /// Does some sanity checks and then either calls onExit() or emits the invokeOnExitCommand(),
  181. /// depending on whether the user indicates that there is an onExitCommand.
  182. void onExitInternal(ctkWorkflowStep* step, ctkWorkflowStep* goingTo,
  183. const ctkWorkflowInterstepTransition::InterstepTransitionType& transitionType);
  184. /// Get the step in the workflow with a given id.
  185. ctkWorkflowStep* stepFromId(const QString& id)const;
  186. /// Get the step that a state belongs to (if any)
  187. ctkWorkflowStep* stepFromState(const QAbstractState* state);
  188. /// Get the number of forward steps from the given step
  189. int numberOfForwardSteps(ctkWorkflowStep* step);
  190. /// Get the number of backward steps from the given step
  191. int numberOfBackwardSteps(ctkWorkflowStep* step);
  192. /// Get the ids of the steps that directly follow the given step.
  193. QList<QString> forwardBranchIds(ctkWorkflowStep* step)const;
  194. /// Get the ids of the steps that directly preceed the given step.
  195. QList<QString> backwardBranchIds(ctkWorkflowStep* step)const;
  196. /// Determines whether there exists a path from the origin step (the current step by default) to
  197. /// the step with the given goalId
  198. bool pathExists(const QString& goalId, ctkWorkflowStep* origin = 0)const;
  199. /// Determines whether there exists a path from the current step's next step (as given by the
  200. /// branchId) to the step with the given goalId
  201. bool pathExistsFromNextStep(const QString& goalId, const QString& branchId)const;
  202. public slots:
  203. /// \brief Workflow processing executed after a step's onEntry function is run.
  204. void processingAfterOnEntry();
  205. /// \brief Workflow processing executed after a step's onExit function is run.
  206. void processingAfterOnExit();
  207. public:
  208. QStateMachine* StateMachine;
  209. // Maintain a list of pointers to the steps in the workflow,
  210. // along with their forward and backward transitions
  211. QMap<ctkWorkflowStep*, forwardAndBackwardSteps*> StepToForwardAndBackwardStepMap;
  212. // ... and its associated convenient typedef
  213. typedef QMap<ctkWorkflowStep*, forwardAndBackwardSteps*> StepToForwardAndBackwardStepMapType;
  214. typedef QList<ctkWorkflowStep*> StepListType;
  215. // Maintain a map of <state, step> key/value pairs, to find the step
  216. // that a given state belongs to
  217. typedef QMap<const QAbstractState*, ctkWorkflowStep*> StateToStepMapType;
  218. typedef QMap<const QAbstractState*, ctkWorkflowStep*>::iterator StateToStepMapIterator;
  219. StateToStepMapType StateToStepMap;
  220. ctkWorkflowStep* InitialStep;
  221. ctkWorkflowStep* CurrentStep;
  222. QMap<ctkWorkflowStep*, ctkWorkflowStep*> StepToPreviousStepMap;
  223. // Used when performing a transition
  224. ctkWorkflowStep* OriginStep;
  225. ctkWorkflowStep* DestinationStep;
  226. ctkWorkflowInterstepTransition::InterstepTransitionType TransitionType;
  227. QString DesiredBranchId; // Desired branchId specified when invoking goForward
  228. ctkWorkflowStep* GoToStep; // Desired step when attempting to go to a finish step
  229. ctkWorkflowStep* StartingStep; // Current step when we began the attempt to go to the desired finish step
  230. // Temporary transition after successfully going to finish step, to get us back to the starting step
  231. ctkWorkflowInterstepTransition* TransitionToPreviousStartingStep;
  232. QString ARTIFICIAL_BRANCH_ID_PREFIX;
  233. };
  234. #endif