ctkWorkflowTest3.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <QCoreApplication>
  16. #include <QTimer>
  17. // CTK includes
  18. #include "ctkBranchingWorkflowStep.h"
  19. #include "ctkExampleDerivedWorkflowStep.h"
  20. #include "ctkWorkflow.h"
  21. // STD includes
  22. #include <cstdlib>
  23. #include <iostream>
  24. //-----------------------------------------------------------------------------
  25. /* "simple" s3----s4
  26. // / \
  27. // s0----s1----s2 s7----s8
  28. // \ /
  29. // "advanced" s5----s6
  30. */
  31. //-----------------------------------------------------------------------------
  32. int ctkWorkflowTest3(int argc, char * argv [] )
  33. {
  34. QCoreApplication app(argc, argv);
  35. int defaultTime = 100;
  36. // create two steps and the workflow
  37. ctkWorkflow *workflow = new ctkWorkflow();
  38. ctkExampleDerivedWorkflowStep* s0 = new ctkExampleDerivedWorkflowStep("Step 0");
  39. ctkExampleDerivedWorkflowStep* s1 = new ctkExampleDerivedWorkflowStep("Step 1");
  40. ctkBranchingWorkflowStep* s2 = new ctkBranchingWorkflowStep("Step 2");
  41. ctkExampleDerivedWorkflowStep* s3 = new ctkExampleDerivedWorkflowStep("Step 3");
  42. ctkExampleDerivedWorkflowStep* s4 = new ctkExampleDerivedWorkflowStep("Step 4");
  43. ctkExampleDerivedWorkflowStep* s5 = new ctkExampleDerivedWorkflowStep("Step 5");
  44. ctkExampleDerivedWorkflowStep* s6 = new ctkExampleDerivedWorkflowStep("Step 6");
  45. ctkExampleDerivedWorkflowStep* s7 = new ctkExampleDerivedWorkflowStep("Step 7");
  46. ctkExampleDerivedWorkflowStep* s8 = new ctkExampleDerivedWorkflowStep("Step 8");
  47. workflow->addTransition(s0, s1);
  48. workflow->addTransition(s1, s2);
  49. workflow->addTransition(s2, s3, "simple");
  50. workflow->addTransition(s3, s4);
  51. workflow->addTransition(s4, s7);
  52. workflow->addTransition(s7, s8);
  53. workflow->addTransition(s2, s5, "advanced");
  54. workflow->addTransition(s5, s6);
  55. workflow->addTransition(s6, s7);
  56. workflow->setInitialStep(s0);
  57. // test error handling for branching workflows:
  58. if (workflow->addTransition(s6, s7))
  59. {
  60. std::cout << "should not be able to add duplicates of the same transition" << std::endl;
  61. return EXIT_FAILURE;
  62. }
  63. if (workflow->addTransition(s6, s6))
  64. {
  65. std::cout << "currently do not support transitions from a step to itself" << std::endl;
  66. return EXIT_FAILURE;
  67. }
  68. if (workflow->addTransition(s2, s5, "simple"))
  69. {
  70. std::cout << "should not be able to add multiple transitions with same id" << std::endl;
  71. return EXIT_FAILURE;
  72. }
  73. // test that the transitions are occuring properly
  74. workflow->start();
  75. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  76. app.exec();
  77. // transition to s1
  78. workflow->goForward();
  79. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  80. app.exec();
  81. if (workflow->currentStep() != s1)
  82. {
  83. std::cerr << "error transitioning s0->s1" << std::endl;
  84. return EXIT_FAILURE;
  85. }
  86. // transition to s2
  87. workflow->goForward();
  88. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  89. app.exec();
  90. if (workflow->currentStep() != s2)
  91. {
  92. std::cerr << "error transitioning s1->s2" << std::endl;
  93. return EXIT_FAILURE;
  94. }
  95. // transition to s3
  96. s2->setBranchId("simple");
  97. workflow->goForward();
  98. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  99. app.exec();
  100. if (workflow->currentStep() != s3)
  101. {
  102. std::cerr << "*** branch *** error transitioning s2->s3" << std::endl;
  103. return EXIT_FAILURE;
  104. }
  105. // transition to s4
  106. workflow->goForward();
  107. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  108. app.exec();
  109. if (workflow->currentStep() != s4)
  110. {
  111. std::cerr << "error transitioning s3->s4" << std::endl;
  112. return EXIT_FAILURE;
  113. }
  114. // transition to s7
  115. workflow->goForward();
  116. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  117. app.exec();
  118. if (workflow->currentStep() != s7)
  119. {
  120. std::cerr << "error transitioning s4->s7" << std::endl;
  121. return EXIT_FAILURE;
  122. }
  123. // transition to s8
  124. workflow->goForward();
  125. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  126. app.exec();
  127. if (workflow->currentStep() != s8)
  128. {
  129. std::cerr << "error transitioning s7->s8" << std::endl;
  130. return EXIT_FAILURE;
  131. }
  132. // transition back to s7
  133. workflow->goBackward();
  134. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  135. app.exec();
  136. if (workflow->currentStep() != s7)
  137. {
  138. std::cerr << "error transitioning s8->s7" << std::endl;
  139. return EXIT_FAILURE;
  140. }
  141. // transition back to s4
  142. workflow->goBackward();
  143. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  144. app.exec();
  145. if (workflow->currentStep() != s4)
  146. {
  147. std::cerr << "*** reverse branch *** error transitioning s7->s4" << std::endl;
  148. return EXIT_FAILURE;
  149. }
  150. // transition back to s3
  151. workflow->goBackward();
  152. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  153. app.exec();
  154. if (workflow->currentStep() != s3)
  155. {
  156. std::cerr << "error transitioning s4->s3" << std::endl;
  157. return EXIT_FAILURE;
  158. }
  159. // transition back to s2
  160. workflow->goBackward();
  161. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  162. app.exec();
  163. if (workflow->currentStep() != s2)
  164. {
  165. std::cerr << "error transitioning s3->s2" << std::endl;
  166. return EXIT_FAILURE;
  167. }
  168. // transition to s5
  169. s2->setBranchId("advanced");
  170. workflow->goForward();
  171. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  172. app.exec();
  173. if (workflow->currentStep() != s5)
  174. {
  175. std::cerr << "*** branch *** error transitioning s2->s5" << std::endl;
  176. return EXIT_FAILURE;
  177. }
  178. // transition to s6
  179. workflow->goForward();
  180. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  181. app.exec();
  182. if (workflow->currentStep() != s6)
  183. {
  184. std::cerr << "error transitioning s5->s6" << std::endl;
  185. return EXIT_FAILURE;
  186. }
  187. // transition to s7
  188. workflow->goForward();
  189. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  190. app.exec();
  191. if (workflow->currentStep() != s7)
  192. {
  193. std::cerr << "error transitioning s6->s7" << std::endl;
  194. return EXIT_FAILURE;
  195. }
  196. // transition to s8
  197. workflow->goForward();
  198. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  199. app.exec();
  200. if (workflow->currentStep() != s8)
  201. {
  202. std::cerr << "error transitioning s7->s8" << std::endl;
  203. return EXIT_FAILURE;
  204. }
  205. // transition back to s7
  206. workflow->goBackward();
  207. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  208. app.exec();
  209. if (workflow->currentStep() != s7)
  210. {
  211. std::cerr << "error transitioning s8->s7" << std::endl;
  212. return EXIT_FAILURE;
  213. }
  214. // transition back to s6
  215. workflow->goBackward();
  216. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  217. app.exec();
  218. if (workflow->currentStep() != s6)
  219. {
  220. std::cerr << "error transitioning s7->s6" << std::endl;
  221. std::cerr << qPrintable(workflow->currentStep()->id());
  222. return EXIT_FAILURE;
  223. }
  224. // transition back to s5
  225. workflow->goBackward();
  226. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  227. app.exec();
  228. if (workflow->currentStep() != s5)
  229. {
  230. std::cerr << "error transitioning s6->s5" << std::endl;
  231. return EXIT_FAILURE;
  232. }
  233. // transition back to s2
  234. workflow->goBackward();
  235. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  236. app.exec();
  237. if (workflow->currentStep() != s2)
  238. {
  239. std::cerr << "error transitioning s5->s2" << std::endl;
  240. return EXIT_FAILURE;
  241. }
  242. // transition back to s1
  243. workflow->goBackward();
  244. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  245. app.exec();
  246. if (workflow->currentStep() != s1)
  247. {
  248. std::cerr << "error transitioning s2->s1" << std::endl;
  249. return EXIT_FAILURE;
  250. }
  251. workflow->stop();
  252. int d = workflow->backwardDistanceToStep(s7);
  253. if (d != 5)
  254. {
  255. std::cerr << "error distance between s7->s0, got"<< d << std::endl;
  256. return EXIT_FAILURE;
  257. }
  258. QTimer::singleShot(defaultTime, &app, SLOT(quit()));
  259. app.exec();
  260. // handles deletions of the workflow, steps, states and transitions
  261. delete workflow;
  262. return EXIT_SUCCESS;
  263. }