Author: Sebastian Bergmann Date: 2007-05-03 18:43:33 +0200 (Thu, 03 May 2007) New Revision: 5027
Log: - Add verification visitor. Added: trunk/Workflow/src/visitors/verification.php Modified: trunk/Workflow/src/workflow.php trunk/Workflow/src/workflow_autoload.php Copied: trunk/Workflow/src/visitors/verification.php (from rev 5024, trunk/Workflow/src/visitors/dot.php) =================================================================== --- trunk/Workflow/src/visitors/dot.php 2007-05-03 15:12:46 UTC (rev 5024) +++ trunk/Workflow/src/visitors/verification.php 2007-05-03 16:43:33 UTC (rev 5027) @@ -0,0 +1,105 @@ +<?php +/** + * File containing the ezcWorkflowVisitorVerification class. + * + * @package Workflow + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * An implementation of the ezcWorkflowVisitor interface that + * verifies a workflow specification. + * + * @package Workflow + * @version //autogen// + */ +class ezcWorkflowVisitorVerification implements ezcWorkflowVisitor +{ + /** + * @var integer + */ + protected $numStartNodes = 0; + + /** + * @var integer + */ + protected $numEndNodes = 0; + + /** + * @var integer + */ + protected $numEndNodesReached = 0; + + /** + * @var array + */ + protected $visited = array(); + + /** + * @param ezcWorkflowVisitable $node + * @throws ezcWorkflowInvalidDefinitionException + */ + public function visit( ezcWorkflowVisitable $visitable ) + { + foreach ( $this->visited as $visited ) + { + if ( $visited === $visitable ) + { + return false; + } + } + + $this->visited[] = $visitable; + + if ( $visitable instanceof ezcWorkflow ) + { + foreach ( $visitable->getNodes() as $node ) + { + if ( $node instanceof ezcWorkflowNodeStart ) + { + $this->numStartNodes++; + + if ( $this->numStartNodes > 1 ) + { + throw new ezcWorkflowInvalidDefinitionException( + 'A workflow may have only one start node.' + ); + } + } + + if ( $node instanceof ezcWorkflowNodeEnd ) + { + $this->numEndNodes++; + } + } + } + + if ( $visitable instanceof ezcWorkflowNode ) + { + if ( $visitable instanceof ezcWorkflowNodeEnd ) + { + $this->numEndNodesReached++; + } + + $visitable->verify(); + } + + return true; + } + + /** + * @throws ezcWorkflowInvalidDefinitionException + */ + public function verify() + { + if ( $this->numEndNodes > $this->numEndNodesReached ) + { + throw new ezcWorkflowInvalidDefinitionException( + 'Not all end nodes are reachable.' + ); + } + } +} +?> Modified: trunk/Workflow/src/workflow.php =================================================================== --- trunk/Workflow/src/workflow.php 2007-05-03 16:00:50 UTC (rev 5026) +++ trunk/Workflow/src/workflow.php 2007-05-03 16:43:33 UTC (rev 5027) @@ -253,50 +253,17 @@ } /** - * Checks the constraints of this workflow's nodes. + * Verifies the specification of this workflow. * - * @throws ezcWorkflowInvalidDefinitionException if the constraints of this workflow's nodes are not met. + * @throws ezcWorkflowInvalidDefinitionException if the specification of this workflow is not correct. */ public function verify() { - $start = 0; - $end = 0; + $verifier = new ezcWorkflowVisitorVerification; - foreach ( $this->nodes as $node ) - { - if ( $node instanceof ezcWorkflowNodeStart ) - { - $start++; + $this->accept( $verifier ); - if ( $start > 1 ) - { - throw new ezcWorkflowInvalidDefinitionException( - 'A workflow can have only one start node.' - ); - } - } - - if ( $node instanceof ezcWorkflowNodeEnd ) - { - $end++; - } - - $node->verify(); - } - - if ( $start < 1 ) - { - throw new ezcWorkflowInvalidDefinitionException( - 'A workflow needs a start node.' - ); - } - - if ( $end < 1 ) - { - throw new ezcWorkflowInvalidDefinitionException( - 'A workflow needs at least one end node.' - ); - } + $verifier->verify(); } /** Modified: trunk/Workflow/src/workflow_autoload.php =================================================================== --- trunk/Workflow/src/workflow_autoload.php 2007-05-03 16:00:50 UTC (rev 5026) +++ trunk/Workflow/src/workflow_autoload.php 2007-05-03 16:43:33 UTC (rev 5027) @@ -76,6 +76,7 @@ 'ezcWorkflowRollbackableServiceObject' => 'Workflow/interfaces/rollbackable_service_object.php', 'ezcWorkflowUtil' => 'Workflow/util.php', 'ezcWorkflowVariableHandler' => 'Workflow/interfaces/variable_handler.php', + 'ezcWorkflowVisitorVerification' => 'Workflow/visitors/verification.php', 'ezcWorkflowVisitorVisualization' => 'Workflow/visitors/visualization.php', ); ?> -- svn-components mailing list [email protected] http://lists.ez.no/mailman/listinfo/svn-components
