/protected/components/ezcomponents/Workflow/src/interfaces/node_conditional_branch.php

https://github.com/kamarulismail/kamarul-playground · PHP · 201 lines · 97 code · 20 blank · 84 comment · 13 complexity · 2a810ab33f643855bbe5238c5c60008f MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcWorkflowNodeConditionalBranch class.
  4. *
  5. * @package Workflow
  6. * @version 1.4.1
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Abstract base class for nodes that conditionally branch multiple threads of
  12. * execution.
  13. *
  14. * Most implementations only need to set the conditions for proper functioning.
  15. *
  16. * @package Workflow
  17. * @version 1.4.1
  18. */
  19. abstract class ezcWorkflowNodeConditionalBranch extends ezcWorkflowNodeBranch
  20. {
  21. /**
  22. * Constraint: The minimum number of conditional outgoing nodes this node
  23. * has to have. Set to false to disable this constraint.
  24. *
  25. * @var integer
  26. */
  27. protected $minConditionalOutNodes = false;
  28. /**
  29. * Constraint: The minimum number of conditional outgoing nodes this node
  30. * has to activate. Set to false to disable this constraint.
  31. *
  32. * @var integer
  33. */
  34. protected $minActivatedConditionalOutNodes = false;
  35. /**
  36. * Constraint: The maximum number of conditional outgoing nodes this node
  37. * may activate. Set to false to disable this constraint.
  38. *
  39. * @var integer
  40. */
  41. protected $maxActivatedConditionalOutNodes = false;
  42. /**
  43. * Holds the conditions of the out nodes.
  44. *
  45. * The key is the position of the out node in the array of out nodes.
  46. *
  47. * @var array( 'condition' => array( 'int' => ezcWorkflowCondtion ) )
  48. */
  49. protected $configuration = array(
  50. 'condition' => array(),
  51. 'else' => array()
  52. );
  53. /**
  54. * Adds the conditional outgoing node $outNode to this node with the
  55. * condition $condition. Optionally, an $else node can be specified that is
  56. * activated when the $condition evaluates to false.
  57. *
  58. * @param ezcWorkflowCondition $condition
  59. * @param ezcWorkflowNode $outNode
  60. * @param ezcWorkflowNode $else
  61. * @return ezcWorkflowNode
  62. */
  63. public function addConditionalOutNode( ezcWorkflowCondition $condition, ezcWorkflowNode $outNode, ezcWorkflowNode $else = null )
  64. {
  65. $this->addOutNode( $outNode );
  66. $this->configuration['condition'][ezcWorkflowUtil::findObject( $this->outNodes, $outNode )] = $condition;
  67. if ( !is_null( $else ) )
  68. {
  69. $this->addOutNode( $else );
  70. $key = ezcWorkflowUtil::findObject( $this->outNodes, $else );
  71. $this->configuration['condition'][$key] = new ezcWorkflowConditionNot( $condition );
  72. $this->configuration['else'][$key] = true;
  73. }
  74. return $this;
  75. }
  76. /**
  77. * Returns the condition for a conditional outgoing node
  78. * and false if the passed not is not a (unconditional)
  79. * outgoing node of this node.
  80. *
  81. * @param ezcWorkflowNode $node
  82. * @return ezcWorkflowCondition
  83. * @ignore
  84. */
  85. public function getCondition( ezcWorkflowNode $node )
  86. {
  87. $keys = array_keys( $this->outNodes );
  88. $numKeys = count( $keys );
  89. for ( $i = 0; $i < $numKeys; $i++ )
  90. {
  91. if ( $this->outNodes[$keys[$i]] === $node )
  92. {
  93. if ( isset( $this->configuration['condition'][$keys[$i]] ) )
  94. {
  95. return $this->configuration['condition'][$keys[$i]];
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Returns true when the $node belongs to an ELSE condition.
  107. *
  108. * @param ezcWorkflowNode $node
  109. * @return bool
  110. * @ignore
  111. */
  112. public function isElse( ezcWorkflowNode $node )
  113. {
  114. return isset( $this->configuration['else'][ezcWorkflowUtil::findObject( $this->outNodes, $node )] );
  115. }
  116. /**
  117. * Evaluates all the conditions, checks the constraints and activates any nodes that have
  118. * passed through both checks and condition evaluation.
  119. *
  120. * @param ezcWorkflowExecution $execution
  121. * @return boolean true when the node finished execution,
  122. * and false otherwise
  123. * @ignore
  124. */
  125. public function execute( ezcWorkflowExecution $execution )
  126. {
  127. $keys = array_keys( $this->outNodes );
  128. $numKeys = count( $keys );
  129. $nodesToStart = array();
  130. $numActivatedConditionalOutNodes = 0;
  131. if ( $this->maxActivatedConditionalOutNodes !== false )
  132. {
  133. $maxActivatedConditionalOutNodes = $this->maxActivatedConditionalOutNodes;
  134. }
  135. else
  136. {
  137. $maxActivatedConditionalOutNodes = $numKeys;
  138. }
  139. for ( $i = 0; $i < $numKeys && $numActivatedConditionalOutNodes <= $maxActivatedConditionalOutNodes; $i++ )
  140. {
  141. if ( isset( $this->configuration['condition'][$keys[$i]] ) )
  142. {
  143. // Conditional outgoing node.
  144. if ( $this->configuration['condition'][$keys[$i]]->evaluate( $execution->getVariables() ) )
  145. {
  146. $nodesToStart[] = $this->outNodes[$keys[$i]];
  147. $numActivatedConditionalOutNodes++;
  148. }
  149. }
  150. else
  151. {
  152. // Unconditional outgoing node.
  153. $nodesToStart[] = $this->outNodes[$keys[$i]];
  154. }
  155. }
  156. if ( $this->minActivatedConditionalOutNodes !== false && $numActivatedConditionalOutNodes < $this->minActivatedConditionalOutNodes )
  157. {
  158. throw new ezcWorkflowExecutionException(
  159. 'Node activates less conditional outgoing nodes than required.'
  160. );
  161. }
  162. return $this->activateOutgoingNodes( $execution, $nodesToStart );
  163. }
  164. /**
  165. * Checks this node's constraints.
  166. *
  167. * @throws ezcWorkflowInvalidWorkflowException if the constraints of this node are not met.
  168. */
  169. public function verify()
  170. {
  171. parent::verify();
  172. $numConditionalOutNodes = count( $this->configuration['condition'] );
  173. if ( $this->minConditionalOutNodes !== false && $numConditionalOutNodes < $this->minConditionalOutNodes )
  174. {
  175. throw new ezcWorkflowInvalidWorkflowException(
  176. 'Node has less conditional outgoing nodes than required.'
  177. );
  178. }
  179. }
  180. }
  181. ?>