private function replaceItemRunner(core_kernel_classes_Resource $workflow, $directory)
 {
     $authoringService = wfAuthoring_models_classes_ProcessService::singleton();
     // foreach activity in workflow
     foreach (wfEngine_models_classes_ProcessDefinitionService::singleton()->getAllActivities($workflow) as $activity) {
         foreach (wfEngine_models_classes_ActivityService::singleton()->getInteractiveServices($activity) as $service) {
             $serviceDefinition = $service->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CALLOFSERVICES_SERVICEDEFINITION));
             if ($serviceDefinition->getUri() == self::OLD_ITEMRUNNER_SERVICE) {
                 $this->out('to replace: ' . $service->getLabel());
                 // retrieve item
                 $item = $this->getItem($service);
                 // create service
                 $itemDirectory = $this->createNamedSubDirectory($directory, $activity);
                 $newService = $this->compileItem($item, $itemDirectory);
                 $newService->setLabel($service->getLabel());
                 $activity->removePropertyValue(new core_kernel_classes_Property(PROPERTY_ACTIVITIES_INTERACTIVESERVICES), $service);
                 //delete its related properties
                 $deleted = $authoringService->deleteActualParameters($service);
                 //delete call of service itself
                 $deleted = $service->delete(true);
                 $activity->setPropertyValue(new core_kernel_classes_Property(PROPERTY_ACTIVITIES_INTERACTIVESERVICES), $newService);
             }
         }
         $this->out('done activity: ' . $activity->getLabel());
     }
 }
 /**
  * Walk through the cloned process and replace the item runner placeholders
  *
  * @param core_kernel_classes_Resource $processDefinition            
  */
 protected function process(core_kernel_classes_Resource $processDefinition)
 {
     $report = new common_report_Report(common_report_Report::TYPE_SUCCESS);
     $activities = wfEngine_models_classes_ProcessDefinitionService::singleton()->getAllActivities($processDefinition);
     foreach ($activities as $activity) {
         $services = wfEngine_models_classes_ActivityService::singleton()->getInteractiveServices($activity);
         foreach ($services as $service) {
             $serviceDefinition = $service->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CALLOFSERVICES_SERVICEDEFINITION));
             if ($serviceDefinition->getUri() == INSTANCE_ITEMCONTAINER_SERVICE) {
                 $item = taoWfTest_models_classes_WfTestService::singleton()->getItemByService($service);
                 if (is_null($item)) {
                     $report->add($this->fail(__('No valid item found for service "%s"', $service->getLabel())));
                 } else {
                     $itemReport = $this->subCompile($item);
                     if ($itemReport->getType() == common_report_Report::TYPE_SUCCESS) {
                         $serviceCall = $itemReport->getData();
                         $storedServiceCall = $serviceCall->toOntology();
                         // remove old service
                         wfEngine_models_classes_InteractiveServiceService::singleton()->deleteInteractiveService($service);
                         $activity->removePropertyValue(new core_kernel_classes_Property(PROPERTY_ACTIVITIES_INTERACTIVESERVICES), $service);
                         // add new service
                         $activity->setPropertyValue(new core_kernel_classes_Property(PROPERTY_ACTIVITIES_INTERACTIVESERVICES), $storedServiceCall);
                     }
                     $report->add($itemReport);
                     if ($itemReport->getType() != common_report_Report::TYPE_SUCCESS) {
                         $report->setType($itemReport->getType());
                     }
                 }
             }
         }
     }
     return $report;
 }
 public function testCheckProcess()
 {
     $id = '_unit_pr_check_';
     $processChecker = new wfAuthoring_models_classes_ProcessChecker($this->proc);
     $activity1 = $this->authoringService->createActivity($this->proc, "{$id}Activity_1");
     $activity1->editPropertyValues(new core_kernel_classes_Property(PROPERTY_ACTIVITIES_ISINITIAL), GENERIS_TRUE);
     $connector1 = $this->authoringService->createConnector($activity1);
     $then1 = $this->authoringService->createConditionalActivity($connector1, 'then', null, "{$id}Activity_2");
     //create "Activity_2"
     $else1 = $this->authoringService->createConditionalActivity($connector1, 'else', null, '', true);
     //create another connector
     $this->assertEquals($connector1->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CONNECTORS_TYPE))->getUri(), INSTANCE_TYPEOFCONNECTORS_CONDITIONAL);
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $this->assertTrue($activityService->isActivity($then1));
     $connectorService = wfEngine_models_classes_ConnectorService::singleton();
     $this->assertTrue($connectorService->isConnector($else1));
     $transitionRule = $connector1->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CONNECTORS_TRANSITIONRULE));
     $this->assertEquals($then1->getUri(), $transitionRule->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_TRANSITIONRULES_THEN))->getUri());
     $this->assertEquals($else1->getUri(), $transitionRule->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_TRANSITIONRULES_ELSE))->getUri());
     //create a sequential a
     $connector2 = $this->authoringService->createConnector($then1);
     $lastActivity = $this->authoringService->createSequenceActivity($connector2, null, "{$id}Activity_3");
     //connector "else1": connect the "then" to the activity "then1" and the "else" to
     $then2 = $this->authoringService->createConditionalActivity($else1, 'then', $connector2);
     //connect to the activity $then1
     $else2 = $this->authoringService->createConditionalActivity($else1, 'else', $lastActivity);
     //connect to the connector of the activity $then1
     $this->assertEquals($then2->getUri(), $connector2->getUri());
     $this->assertEquals($else2->getUri(), $lastActivity->getUri());
     $this->assertTrue($processChecker->check());
 }
 /**
  * Builds a simple Diagram of the Process
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource process
  * @return string
  */
 public static function buildDiagramData(core_kernel_classes_Resource $process)
 {
     $returnValue = (string) '';
     common_Logger::i("Building diagram for " . $process->getLabel());
     $authoringService = wfAuthoring_models_classes_ProcessService::singleton();
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $connectorService = wfEngine_models_classes_ConnectorService::singleton();
     $activityCardinalityService = wfEngine_models_classes_ActivityCardinalityService::singleton();
     $activities = $authoringService->getActivitiesByProcess($process);
     $todo = array();
     foreach ($activities as $activity) {
         if ($activityService->isInitial($activity)) {
             $todo[] = $activity;
         }
     }
     $currentLevel = 0;
     $diagram = new wfAuthoring_models_classes_ProcessDiagram();
     $done = array();
     while (!empty($todo)) {
         $nextLevel = array();
         $posOnLevel = 0;
         foreach ($todo as $item) {
             $next = array();
             if ($activityService->isActivity($item)) {
                 // add this activity
                 $diagram->addActivity($item, 54 + 200 * $posOnLevel + 10 * $currentLevel, 35 + 80 * $currentLevel);
                 $next = array_merge($next, $activityService->getNextConnectors($item));
                 common_Logger::d('Activity added ' . $item->getUri());
             } elseif ($connectorService->isConnector($item)) {
                 // add this connector
                 $diagram->addConnector($item, 100 + 200 * $posOnLevel + 10 * $currentLevel, 40 + 80 * $currentLevel);
                 $next = array_merge($next, $connectorService->getNextActivities($item));
             } else {
                 common_Logger::w('unexpected ressource in process ' . $item->getUri());
             }
             //replace cardinalities
             foreach ($next as $key => $destination) {
                 if ($activityCardinalityService->isCardinality($destination)) {
                     // not represented on diagram
                     $next[$key] = $activityCardinalityService->getDestination($destination);
                 }
             }
             //add arrows
             foreach ($next as $destination) {
                 $diagram->addArrow($item, $destination);
             }
             $posOnLevel++;
             $nextLevel = array_merge($nextLevel, $next);
         }
         $done = array_merge($done, $todo);
         $todo = array_diff($nextLevel, $done);
         $currentLevel++;
     }
     $returnValue = $diagram->toJSON();
     return (string) $returnValue;
 }
 public function __construct()
 {
     //init services
     $this->activityService = wfEngine_models_classes_ActivityService::singleton();
     $this->processVariableService = wfEngine_models_classes_VariableService::singleton();
     $this->authoringService = wfAuthoring_models_classes_ProcessService::singleton();
     $this->activityExecutionService = wfEngine_models_classes_ActivityExecutionService::singleton();
     $this->connectorService = wfEngine_models_classes_ConnectorService::singleton();
     $this->processVariablesClass = new core_kernel_classes_Class(CLASS_PROCESSVARIABLES);
     $this->propertyIsSample = new core_kernel_classes_Property(PROPERTY_IS_SAMPLE);
 }
 protected function flattenProcessActivity(core_kernel_classes_Resource $activity)
 {
     $this->initCloningVariables();
     $services = wfEngine_models_classes_ActivityService::singleton()->getInteractiveServices($activity);
     // only replace single-service activities, with the service process runner
     if (count($services) == 1) {
         $serviceCall = current($services);
         $serviceDefinition = $serviceCall->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CALLOFSERVICES_SERVICEDEFINITION));
         if ($serviceDefinition->getUri() == INSTANCE_SERVICE_PROCESSRUNNER) {
             // found a wfEngine call, extract processDefnition
             $subProcess = $this->getSubProcess($serviceCall);
             if (empty($subProcess)) {
                 throw new common_exception_InconsistentData('Missing process uri in service call ' . $serviceCall->getUri());
             }
             // @todo test the process first
             // @todo clone sub process
             common_Logger::w('Should have cloned subprocess ' . $subProcess);
             $segment = $this->cloneProcessSegment($subProcess);
             $inActivity = $segment['in'];
             $firstout = current($segment['out']);
             //allow first acitvity only if the parent is
             if (!wfAuthoring_models_classes_ActivityService::singleton()->isInitial($activity)) {
                 $inActivity->editPropertyValues(new core_kernel_classes_Property(PROPERTY_ACTIVITIES_ISINITIAL), GENERIS_FALSE);
             }
             $this->addClonedActivity($inActivity, $activity, $firstout);
             $propProcessActivities = new core_kernel_classes_Property(PROPERTY_PROCESS_ACTIVITIES);
             foreach ($this->getClonedActivities() as $activityClone) {
                 $this->processDefinition->setPropertyValue($propProcessActivities, $activityClone->getUri());
             }
             //get the previous connector if exists and clone it
             $allConnectors = wfAuthoring_models_classes_ProcessService::singleton()->getConnectorsByActivity($activity);
             $connectors = array_merge($allConnectors['next'], $allConnectors['prev']);
             foreach ($connectors as $connector) {
                 //trick to reference previous and following connector: connector_prev -> activity_subprocess[activity1, activity2, etc.] -> connector_follow
                 $this->addClonedConnector($connector, $connector);
             }
             //glue segment:
             $glue = array_merge(array($activity), $allConnectors['prev']);
             foreach ($glue as $fragment) {
                 $this->linkClonedStep($fragment);
             }
             //delete all activity:
             $activity->delete(true);
             //recursive call:
             foreach ($this->getClonedActivities() as $activityClone) {
                 $this->flattenProcessActivity($activityClone);
             }
         }
     }
 }
 /**
  * Short description of method createConnector
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param core_kernel_classes_Resource sourceStep
  * @param string label
  * @throws Exception
  * @return core_kernel_classes_Resource
  */
 public function createConnector(core_kernel_classes_Resource $sourceStep, $label = '')
 {
     $returnValue = null;
     $label = empty($label) ? $sourceStep->getLabel() . "_c" : $label;
     $connectorClass = new core_kernel_classes_Class(CLASS_CONNECTORS);
     $returnValue = $connectorClass->createInstance($label, "created by ProcessService.Class");
     if (is_null($returnValue)) {
         throw new Exception("the connector cannot be created for the activity {$sourceStep->getUri()}");
     }
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $connectorService = wfEngine_models_classes_ConnectorService::singleton();
     //associate the connector to the activity
     $sourceStep->setPropertyValue(new core_kernel_classes_Property(PROPERTY_STEP_NEXT), $returnValue);
     //set the activity reference of the connector:
     $activityRefProp = new core_kernel_classes_Property(PROPERTY_CONNECTORS_ACTIVITYREFERENCE);
     if ($activityService->isActivity($sourceStep)) {
         $returnValue->setPropertyValue($activityRefProp, $sourceStep);
     } elseif ($connectorService->isConnector($sourceStep)) {
         $returnValue->setPropertyValue($activityRefProp, $sourceStep->getUniquePropertyValue($activityRefProp));
     } else {
         throw new Exception("invalid resource type for the activity parameter: {$sourceStep->getUri()}");
     }
     return $returnValue;
 }
 public function testCreateConditionalActivity()
 {
     $activity1 = $this->authoringService->createActivity($this->proc, 'myActivity');
     $connector1 = $this->authoringService->createConnector($activity1);
     $then = $this->authoringService->createConditionalActivity($connector1, 'then');
     //create "Activity_2"
     $else = $this->authoringService->createConditionalActivity($connector1, 'else', null, '', true);
     //create another connector
     $this->assertEquals($connector1->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CONNECTORS_TYPE))->getUri(), INSTANCE_TYPEOFCONNECTORS_CONDITIONAL);
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $this->assertTrue($activityService->isActivity($then));
     $connectorService = wfEngine_models_classes_ConnectorService::singleton();
     $this->assertTrue($connectorService->isConnector($else));
     $activity3 = $this->authoringService->createSequenceActivity($else, null, 'Act3');
     $this->assertEquals($activity3->getLabel(), 'Act3');
     $transitionRule = $connector1->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CONNECTORS_TRANSITIONRULE));
     $this->assertEquals($then->getUri(), $transitionRule->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_TRANSITIONRULES_THEN))->getUri());
     $this->assertEquals($else->getUri(), $transitionRule->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_TRANSITIONRULES_ELSE))->getUri());
     $myProcessVar1 = null;
     $myProcessVar1 = $this->variableService->getProcessVariable('myProcessVarCode1', true);
     $transitionRuleBis = $this->authoringService->createTransitionRule($connector1, '^myProcessVarCode1 == 1');
     $this->assertEquals($transitionRule->getUri(), $transitionRuleBis->getUri());
     $this->assertTrue($this->variableService->deleteProcessVariable('myProcessVarCode1'));
 }
 /**
  * Short description of method getConditionalConnectorNewActivities
  *
  * @access protected
  * @author Somsack Sipasseuth, <*****@*****.**>
  * @param  Resource processExecution
  * @param  Resource activityExecution
  * @param  Resource conditionalConnector
  * @return array
  */
 protected function getConditionalConnectorNewActivities(core_kernel_classes_Resource $processExecution, core_kernel_classes_Resource $activityExecution, core_kernel_classes_Resource $conditionalConnector)
 {
     $returnValue = array();
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $connectorService = wfEngine_models_classes_ConnectorService::singleton();
     $transitionRuleService = wfEngine_models_classes_TransitionRuleService::singleton();
     $transitionRule = $connectorService->getTransitionRule($conditionalConnector);
     if (is_null($transitionRule)) {
         return $returnValue;
     }
     $evaluationResult = $transitionRuleService->getExpression($transitionRule)->evaluate(array(VAR_PROCESS_INSTANCE => $activityExecution->getUri()));
     //		var_dump('transition rule '.$transitionRule->getLabel(), $evaluationResult);
     if ($evaluationResult) {
         // next activities = THEN
         $thenActivity = $transitionRuleService->getThenActivity($transitionRule);
         if (!is_null($thenActivity)) {
             if ($activityService->isActivity($thenActivity)) {
                 $thenActivity->getLabel();
                 $returnValue[] = $thenActivity;
             } else {
                 if ($activityService->isConnector($thenActivity)) {
                     $returnValue = $this->getNewActivities($processExecution, $activityExecution, $thenActivity);
                 }
             }
         } else {
             throw new wfEngine_models_classes_ProcessDefinitonException('no "then" activity found for the transition rule ' . $transitionRule->getUri());
         }
         //			var_dump('then', $returnValue);
     } else {
         // next activities = ELSE
         $elseActivity = $transitionRuleService->getElseActivity($transitionRule);
         if (!is_null($elseActivity)) {
             if ($activityService->isActivity($elseActivity)) {
                 $elseActivity->getLabel();
                 $returnValue[] = $elseActivity;
             } else {
                 $returnValue = $this->getNewActivities($processExecution, $activityExecution, $elseActivity);
             }
         } else {
             throw new wfEngine_models_classes_ProcessDefinitonException('no "else" activity found for the transition rule ' . $transitionRule->getUri());
         }
         //			var_dump('else', $returnValue);
     }
     return (array) $returnValue;
 }
 /**
  * Short description of method isActivityInitial
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource activity
  * @return boolean
  */
 public static function isActivityInitial(core_kernel_classes_Resource $activity)
 {
     $returnValue = (bool) false;
     $returnValue = wfEngine_models_classes_ActivityService::singleton()->isInitial($activity);
     return (bool) $returnValue;
 }
 protected function assertCorresponds(core_kernel_classes_Resource $step1, core_kernel_classes_Resource $step2)
 {
     //echo 'Compare '.$step1->getLabel().' and '.$step2->getLabel().'<br />';
     $services1 = wfEngine_models_classes_ActivityService::singleton()->getInteractiveServices($step1);
     $services2 = wfEngine_models_classes_ActivityService::singleton()->getInteractiveServices($step2);
     $this->assertEquals(count($services1), count($services2));
     if (count($services1) == count($services2)) {
         foreach ($services1 as $service1) {
             $service2 = array_shift($services2);
             $call1 = tao_models_classes_service_ServiceCall::fromResource($service1);
             $call2 = tao_models_classes_service_ServiceCall::fromResource($service2);
             $this->assertEquals($call1->serializeToString(), $call2->serializeToString());
         }
     }
 }
 /**
  * Test the tokens into a parallel process
  */
 public function testVirtualParallelJoinProcess()
 {
     error_reporting(E_ALL);
     $t_start = microtime(true);
     //init services
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $processVariableService = wfEngine_models_classes_VariableService::singleton();
     $authoringService = wfAuthoring_models_classes_ProcessService::singleton();
     $activityExecutionService = wfEngine_models_classes_ActivityExecutionService::singleton();
     $activityExecutionService->cache = (bool) self::SERVICE_CACHE;
     //process definition
     $processDefinitionClass = new core_kernel_classes_Class(CLASS_PROCESS);
     $processDefinition = $processDefinitionClass->createInstance('PJ processForUnitTest_' . date(DATE_ISO8601), 'created for the unit test of process execution');
     $this->assertNotNull($processDefinition);
     /*
     		                +---------------+
     		                |  activity 0   |
     		                +-------+-------+
     		                        |
     		                    +---v---+
     		                    |  c 0  |   split
     		                    +--+-+--+
     		                       | |
     		          3  +---------+ +---------+  unit_var_12345678
     		             |                     |
     		     +-------v--------+    +-------v------+
     		     |   activity 1   |    |  activity 2  |
     		     +-------+--------+    +--------+-----+
     		             |                      |
     		             +--------+    +--------+
     		                   +--v----v--+
     		                   |   c 2    |    join
     		                   +----+-----+
     		                        |
     		                +-------v--------+
     		                |  activity 3    |
     		                +----------------+
     */
     //activities definitions
     $activity0 = $authoringService->createActivity($processDefinition, 'activity0');
     $this->assertNotNull($activity0);
     $connector0 = null;
     $authoringService->setFirstActivity($processDefinition, $activity0);
     $connector0 = $authoringService->createConnector($activity0);
     $connectorParallele = new core_kernel_classes_Resource(INSTANCE_TYPEOFCONNECTORS_PARALLEL);
     $authoringService->setConnectorType($connector0, $connectorParallele);
     $this->assertNotNull($connector0);
     $parallelActivity1 = $authoringService->createActivity($processDefinition, 'activity1');
     $this->assertNotNull($parallelActivity1);
     $roleRestrictedUser = new core_kernel_classes_Resource(INSTANCE_ACL_ROLE_RESTRICTED_USER);
     $activityService->setAcl($parallelActivity1, $roleRestrictedUser, $this->testUserRole);
     //!!! it is mendatory to set the role restricted user ACL mode to make this parallel process test case work
     $connector1 = null;
     $connector1 = $authoringService->createConnector($parallelActivity1);
     $this->assertNotNull($connector1);
     $parallelActivity2 = $authoringService->createActivity($processDefinition, 'activity2');
     $this->assertNotNull($parallelActivity2);
     $activityService->setAcl($parallelActivity2, $roleRestrictedUser, $this->testUserRole);
     //!!! it is mendatory to set the role restricted user ACL mode to make this parallel process test case work
     $connector2 = null;
     $connector2 = $authoringService->createConnector($parallelActivity2);
     $this->assertNotNull($connector2);
     //define parallel activities, first branch with constant cardinality value, while the second listens to a process variable:
     $parallelCount1 = 3;
     $parallelCount2 = 5;
     $parallelCount2_processVar_key = 'unit_var_' . time();
     $parallelCount2_processVar = $processVariableService->createProcessVariable('Var for unit test', $parallelCount2_processVar_key);
     $prallelActivitiesArray = array($parallelActivity1->getUri() => $parallelCount1, $parallelActivity2->getUri() => $parallelCount2_processVar);
     $result = $authoringService->setParallelActivities($connector0, $prallelActivitiesArray);
     $this->assertTrue($result);
     //set several split variables:
     $splitVariable1_key = 'unit_split_var1_' . time();
     $splitVariable1 = $processVariableService->createProcessVariable('Split Var1 for unit test', $splitVariable1_key);
     $splitVariable2_key = 'unit_split_var2_' . time();
     $splitVariable2 = $processVariableService->createProcessVariable('Split Var2 for unit test', $splitVariable2_key);
     $splitVariablesArray = array($parallelActivity1->getUri() => array($splitVariable1), $parallelActivity2->getUri() => array($splitVariable1, $splitVariable2));
     $connectorService = wfAuthoring_models_classes_ConnectorService::singleton();
     $connectorService->setSplitVariables($connector0, $splitVariablesArray);
     $prallelActivitiesArray[$parallelActivity2->getUri()] = $parallelCount2;
     $joinActivity = $authoringService->createActivity($processDefinition, 'activity3');
     //join parallel Activity 1 and 2 to "joinActivity"
     $connector1 = wfAuthoring_models_classes_ConnectorService::singleton()->createJoin(array($parallelActivity1, $parallelActivity2), $joinActivity);
     /*
     $authoringService->createJoinActivity($connector1, $joinActivity, '', $parallelActivity1);
     $authoringService->createJoinActivity($connector2, $joinActivity, '', $parallelActivity2);
     */
     //run the process
     $processExecName = 'Test Parallel Process Execution';
     $processExecComment = 'created for processExecustionService test case by ' . __METHOD__;
     $processInstance = $this->service->createProcessExecution($processDefinition, $processExecName, $processExecComment);
     $this->assertTrue($this->service->checkStatus($processInstance, 'started'));
     $this->out(__METHOD__, true);
     $this->out("<strong>Forward transitions:</strong>", true);
     $previousActivityExecution = null;
     $numberActivities = 2 + $parallelCount1 + $parallelCount2;
     $createdUsers = array();
     $loginProperty = new core_kernel_classes_Property(PROPERTY_USER_LOGIN);
     for ($i = 1; $i <= $numberActivities; $i++) {
         $activitieExecs = $this->service->getCurrentActivityExecutions($processInstance);
         $countActivities = count($activitieExecs);
         $activity = null;
         $activityExecution = null;
         if ($countActivities > 1) {
             //select one of the available activities in the parallel branch:
             foreach ($activitieExecs as $activityExecUri => $activityExec) {
                 if (!$activityExecutionService->isFinished($activityExec)) {
                     $activityDefinition = $activityExecutionService->getExecutionOf($activityExec);
                     $activityUri = $activityDefinition->getUri();
                     if (isset($prallelActivitiesArray[$activityUri])) {
                         if ($prallelActivitiesArray[$activityUri] > 0) {
                             $prallelActivitiesArray[$activityUri]--;
                             $activityExecution = $activityExec;
                             $activity = $activityDefinition;
                             break;
                         }
                     }
                 }
             }
         } else {
             if ($countActivities == 1) {
                 $activityExecution = reset($activitieExecs);
                 $activity = $activityExecutionService->getExecutionOf($activityExecution);
             } else {
                 $this->fail('no current activity definition found for the iteration ' . $i);
             }
         }
         $this->out("<strong> Iteration {$i} :</strong>", true);
         $this->out("<strong>" . (is_null($activity) ? 'null' : $activity->getLabel()) . "</strong> (among {$countActivities})");
         //issue : no activity found for the last iteration...
         //init execution
         $activityExecution = $this->service->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser);
         $this->assertNotNull($activityExecution);
         if ($i == 1) {
             //set value of the parallel thread:
             $processVariableService->push($parallelCount2_processVar_key, $parallelCount2);
             //set some values to the split variables:
             $values1 = array();
             for ($j = 1; $j <= $parallelCount1; $j++) {
                 $values1[] = 'A' . $j;
             }
             $values2 = array();
             for ($j = 1; $j <= $parallelCount2; $j++) {
                 $values2[] = 'B' . $j;
             }
             $processVariableService->push($splitVariable1_key, serialize($values1));
             $processVariableService->push($splitVariable2_key, serialize($values2));
         } else {
             //check dispatched value:
             //				$value1 = $processVariableService->get($splitVariable1_key);
             //				$value2 = $processVariableService->get($splitVariable2_key);
             //				var_dump($value1, $value2);
         }
         $activityExecStatus = $activityExecutionService->getStatus($activityExecution);
         $this->assertNotNull($activityExecStatus);
         $this->assertEquals($activityExecStatus->getUri(), INSTANCE_PROCESSSTATUS_STARTED);
         //transition to next activity
         $this->out("current user: "******"' . $this->currentUser->getUri() . '"');
         $this->out("performing transition ...");
         //transition to next activity
         $performed = $this->service->performTransition($processInstance, $activityExecution);
         if (!$performed) {
             $this->out('transition failed.');
         }
         $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
         $this->out("process status: " . $this->service->getStatus($processInstance)->getLabel());
         //try undoing the transition:
         switch ($i) {
             case 1:
                 $this->assertTrue($this->service->undoForwardTransition($processInstance, $activityExecution));
                 $activitieExecs = $this->service->getCurrentActivityExecutions($processInstance);
                 $this->assertEquals(count($activitieExecs), 1);
                 $activityBis = $activityExecutionService->getExecutionOf(reset($activitieExecs));
                 $this->assertTrue($activity->getUri() == $activityBis->getUri());
                 $transitionResult = $this->service->performTransition($processInstance, $activityExecution);
                 break;
             case 1 + $parallelCount1:
                 $this->assertFalse($this->service->undoForwardTransition($processInstance, $activityExecution));
                 $history = $this->service->getExecutionHistory($processInstance);
                 $this->assertEquals(count($history), 2 * ($parallelCount1 + $parallelCount2) + 1);
                 //activity 1, 2(closed), 2, 3 and 4
                 $this->assertFalse($this->service->undoForwardTransition($processInstance, new core_kernel_classes_Resource(reset($history))));
                 $this->assertNotNull($previousActivityExecution);
                 $this->assertFalse($this->service->undoForwardTransition($processInstance, $previousActivityExecution));
                 break;
             case 1 + $parallelCount1 + $parallelCount2:
                 $this->assertTrue($this->service->undoForwardTransition($processInstance, $activityExecution));
                 $activitieExecs = $this->service->getCurrentActivityExecutions($processInstance);
                 $this->assertEquals(count($activitieExecs), $parallelCount1 + $parallelCount2);
                 $transitionResult = $this->service->performTransition($processInstance, $activityExecution);
                 break;
         }
         $previousActivityExecution = $activityExecution;
         if ($this->service->isPaused($processInstance)) {
             //Login another user to execute parallel branch
             $this->userService->logout();
             $this->out("logout " . $this->currentUser->getOnePropertyValue($loginProperty) . ' "' . $this->currentUser->getUri() . '"', true);
             list($usec, $sec) = explode(" ", microtime());
             $login = '******' . $i . '-' . $usec;
             $pass = '******';
             $userData = array(PROPERTY_USER_LOGIN => $login, PROPERTY_USER_PASSWORD => core_kernel_users_Service::getPasswordHash()->encrypt($pass), PROPERTY_USER_DEFLG => 'http://www.tao.lu/Ontologies/TAO.rdf#Lang' . DEFAULT_LANG, PROPERTY_USER_UILG => 'http://www.tao.lu/Ontologies/TAO.rdf#Lang' . DEFAULT_LANG, PROPERTY_USER_ROLES => INSTANCE_ROLE_WORKFLOW, RDFS_LABEL => $login);
             if (!$this->userService->loginAvailable($login)) {
                 $this->fail('test login already taken');
             }
             $otherUser = $this->testUserClass->createInstanceWithProperties($userData);
             $createdUsers[$otherUser->getUri()] = $otherUser;
             if ($this->userService->loginUser($login, $pass)) {
                 $this->currentUser = $this->userService->getCurrentUser();
                 $this->out("new user logged in: " . $this->currentUser->getOnePropertyValue($loginProperty) . ' "' . $this->currentUser->getUri() . '"');
             } else {
                 $this->fail("unable to login user {$login}");
             }
         }
     }
     $this->assertTrue($this->service->isFinished($processInstance));
     $this->assertTrue($this->service->resume($processInstance));
     $this->out("<strong>Backward transitions:</strong>", true);
     //		var_dump($this->service->getAllActivityExecutions($processInstance));
     $j = 0;
     $iterationNumber = 2;
     while ($j < $iterationNumber) {
         $activitieExecs = $this->service->getCurrentActivityExecutions($processInstance);
         $activityExecution = null;
         $activity = null;
         switch ($j) {
             case 0:
                 $this->assertEquals(count($activitieExecs), 1);
                 //check
                 $activityExecution = reset($activitieExecs);
                 $activity = $activityExecutionService->getExecutionOf($activityExecution);
                 break;
             case 1:
                 $this->assertEquals(count($activitieExecs), $parallelCount1 + $parallelCount2);
                 //check
                 $activity = $parallelActivity2;
                 foreach ($this->service->getCurrentActivityExecutions($processInstance, $activity) as $activityExec) {
                     if ($activityExecutionService->getActivityExecutionUser($activityExec)->getUri() == $this->currentUser->getUri()) {
                         $activityExecution = $activityExec;
                     }
                 }
                 if (is_null($activityExecution)) {
                     $activity = $parallelActivity1;
                     foreach ($this->service->getCurrentActivityExecutions($processInstance, $activity) as $activityExec) {
                         if ($activityExecutionService->getActivityExecutionUser($activityExec)->getUri() == $this->currentUser->getUri()) {
                             $activityExecution = $activityExec;
                         }
                     }
                 }
                 $this->assertNotNull($activityExecution);
                 break;
         }
         $this->out("<strong>" . $activity->getLabel() . "</strong>", true);
         //init execution
         $activityExecution = $this->service->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser);
         $this->assertNotNull($activityExecution);
         $activityExecStatus = $activityExecutionService->getStatus($activityExecution);
         $this->assertNotNull($activityExecStatus);
         $this->assertEquals($activityExecStatus->getUri(), INSTANCE_PROCESSSTATUS_RESUMED);
         $this->out("current user: "******"' . $this->currentUser->getUri() . '"');
         $this->out("performing transition ...");
         //transition to next activity
         $transitionResult = $this->service->performBackwardTransition($processInstance, $activityExecution);
         if ($j == 0) {
             $this->assertTrue(count($transitionResult) > 0);
         } else {
             if ($j == $iterationNumber - 1) {
                 //var_dump($transitionResult);
                 $this->assertFalse($transitionResult);
             }
         }
         $processStatus = $this->service->getStatus($processInstance);
         $this->assertNotNull($processStatus);
         $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
         $this->out("process status: " . $processStatus->getLabel());
         $this->assertEquals($processStatus->getUri(), INSTANCE_PROCESSSTATUS_PAUSED);
         $j++;
     }
     $this->out("<strong>Forward transitions again:</strong>", true);
     $currentActivityExecutions = $this->service->getCurrentActivityExecutions($processInstance);
     $currentActivityExecutionsCount = count($currentActivityExecutions);
     $this->assertEquals($currentActivityExecutionsCount, $parallelCount1 + $parallelCount2);
     for ($i = 0; $i < $currentActivityExecutionsCount; $i++) {
         $currentActivityExecution = array_pop($currentActivityExecutions);
         $user = $activityExecutionService->getActivityExecutionUser($currentActivityExecution);
         $activityDefinition = $activityExecutionService->getExecutionOf($currentActivityExecution);
         $this->assertNotNull($user);
         $this->assertNotNull($activityDefinition);
         if (!is_null($user) && !is_null($activityDefinition)) {
             $this->userService->logout();
             $this->out("logout " . $this->currentUser->getOnePropertyValue($loginProperty) . ' "' . $this->currentUser->getUri() . '"', true);
             $login = (string) $user->getUniquePropertyValue($loginProperty);
             $pass = '******';
             if ($this->userService->loginUser($login, $pass)) {
                 $this->currentUser = $this->userService->getCurrentUser();
                 $this->out("new user logged in: " . $this->currentUser->getOnePropertyValue($loginProperty) . ' "' . $this->currentUser->getUri() . '"');
             } else {
                 $this->fail("unable to login user {$login}<br>");
             }
             $iterationNo = $i + 1;
             $this->out("<strong>Iteration {$iterationNo}: " . $activityDefinition->getLabel() . "</strong>", true);
             //execute activity:
             $activityExecution = $this->service->initCurrentActivityExecution($processInstance, $currentActivityExecution, $this->currentUser);
             $this->assertNotNull($activityExecution);
             $activityExecStatus = $activityExecutionService->getStatus($activityExecution);
             $this->assertNotNull($activityExecStatus);
             $this->assertEquals($activityExecStatus->getUri(), INSTANCE_PROCESSSTATUS_RESUMED);
             //transition to next activity
             $this->out("current user: "******"' . $this->currentUser->getUri() . '"');
             $this->out("performing transition ...");
             //transition to next activity
             $this->service->performTransition($processInstance, $activityExecution);
             $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
             $this->out("process status: " . $this->service->getStatus($processInstance)->getLabel());
             //try undoing the transition:
             if ($i < $currentActivityExecutionsCount - 1) {
                 $this->assertFalse($this->service->undoForwardTransition($processInstance, $activityExecution));
             }
         }
     }
     //try undoing the transition:
     $this->assertTrue($this->service->undoForwardTransition($processInstance, $activityExecution));
     $activitieExecs = $this->service->getCurrentActivityExecutions($processInstance);
     $this->assertEquals(count($activitieExecs), $parallelCount1 + $parallelCount2);
     $transitionResult = $this->service->performTransition($processInstance, $activityExecution);
     $this->assertEquals(count($transitionResult), 1);
     $activitieExecs = $this->service->getCurrentActivityExecutions($processInstance);
     $this->assertEquals(count($activitieExecs), 1);
     $activityExecution = reset($activitieExecs);
     $activity = $activityExecutionService->getExecutionOf($activityExecution);
     $this->assertEquals($activity->getUri(), $joinActivity->getUri());
     $this->out("<strong>Executing last activity: " . $activity->getLabel() . "</strong>", true);
     //init execution
     $activityExecution = $this->service->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser);
     $this->assertNotNull($activityExecution);
     $activityExecStatus = $activityExecutionService->getStatus($activityExecution);
     $this->assertNotNull($activityExecStatus);
     $this->assertEquals($activityExecStatus->getUri(), INSTANCE_PROCESSSTATUS_STARTED);
     //transition to next activity
     $this->out("current user: "******"' . $this->currentUser->getUri() . '"');
     $this->out("performing transition ...");
     //transition to next activity
     $this->service->performTransition($processInstance, $activityExecution);
     $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
     $this->out("process status: " . $this->service->getStatus($processInstance)->getLabel());
     $this->assertTrue($this->service->isFinished($processInstance));
     $t_end = microtime(true);
     $duration = $t_end - $t_start;
     $this->out('Elapsed time: ' . $duration . 's', true);
     $this->out('deleting created resources:', true);
     //delete process exec:
     $this->assertTrue($this->service->deleteProcessExecution($processInstance));
     //delete processdef:
     $this->assertTrue($authoringService->deleteProcess($processDefinition));
     $parallelCount2_processVar->delete();
     //delete created users:
     foreach ($createdUsers as $createdUser) {
         $this->out('deleting ' . $createdUser->getLabel() . ' "' . $createdUser->getUri() . '"');
         $this->assertTrue($this->userService->removeUser($createdUser));
     }
     if (!is_null($this->currentUser)) {
         $this->userService->logout();
         $this->userService->removeUser($this->currentUser);
     }
 }
 public function index()
 {
     if (is_null($this->processExecution)) {
         common_Logger::w('ProcessBrowser invoked without processExecution');
         $this->redirectToMain();
         return;
     }
     if ($this->autoRedirecting) {
         $this->autoredirectToIndex();
         return;
     }
     /*
      * @todo: clean usage
      * known use of Session::setAttribute("processUri") in:
      * - taoDelivery_actions_ItemDelivery::runner()
      * - tao_actions_Api::createAuthEnvironment()
      */
     $this->setSessionAttribute("processUri", $this->processExecution->getUri());
     //user data for browser view
     $userViewData = wfEngine_helpers_UsersHelper::buildCurrentUserForView();
     $this->setData('userViewData', $userViewData);
     $browserViewData = array();
     // general data for browser view.
     //init services:
     $userService = wfEngine_models_classes_UserService::singleton();
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $interactiveServiceService = wfEngine_models_classes_InteractiveServiceService::singleton();
     //get current user:
     $currentUser = $userService->getCurrentUser();
     if (is_null($currentUser)) {
         throw new wfEngine_models_classes_ProcessExecutionException("No current user found!");
     }
     //get activity execution from currently available process definitions:
     $currentlyAvailableActivityExecutions = $this->processExecutionService->getAvailableCurrentActivityExecutions($this->processExecution, $currentUser, true);
     $activityExecution = null;
     if (count($currentlyAvailableActivityExecutions) == 0) {
         common_Logger::w('No available current activity exec found: no permission or issue in process execution');
         $this->pause();
         return;
     } else {
         if (!is_null($this->activityExecution) && $this->activityExecution instanceof core_kernel_classes_Resource) {
             foreach ($currentlyAvailableActivityExecutions as $availableActivityExec) {
                 if ($availableActivityExec->getUri() == $this->activityExecution->getUri()) {
                     $activityExecution = $this->processExecutionService->initCurrentActivityExecution($this->processExecution, $this->activityExecution, $currentUser);
                     break;
                 }
             }
             if (is_null($activityExecution)) {
                 //invalid choice of activity execution:
                 $this->activityExecution = null;
                 //					$invalidActivity = new core_kernel_classes_Resource($activityUri);
                 //					throw new wfEngine_models_classes_ProcessExecutionException("invalid choice of activity definition in process browser {$invalidActivity->getLabel()} ({$invalidActivity->getUri()}). \n<br/> The link may be outdated.");
                 $this->autoredirectToIndex();
                 return;
             }
         } else {
             if (count($currentlyAvailableActivityExecutions) == 1) {
                 $activityExecution = $this->processExecutionService->initCurrentActivityExecution($this->processExecution, reset($currentlyAvailableActivityExecutions), $currentUser);
                 if (is_null($activityExecution)) {
                     throw new wfEngine_models_classes_ProcessExecutionException('cannot initiate the activity execution of the unique next activity definition');
                 }
             } else {
                 //count > 1:
                 //parallel branch, ask the user to select activity to execute:
                 common_Logger::i('Ask the user to select activity');
                 $this->pause();
                 return;
             }
         }
     }
     if (!is_null($activityExecution)) {
         $this->activityExecution = $activityExecution;
         $browserViewData[''] = $this->processExecution->getUri();
         $browserViewData['activityExecutionUri'] = $activityExecution->getUri();
         $this->activityExecutionService->createNonce($this->activityExecution);
         $browserViewData['activityExecutionNonce'] = $this->activityExecutionService->getNonce($activityExecution);
         //get interactive services (call of services):
         $activityDefinition = $this->activityExecutionService->getExecutionOf($activityExecution);
         $interactiveServices = $activityService->getInteractiveServices($activityDefinition);
         $services = array();
         foreach ($interactiveServices as $interactiveService) {
             $serviceCallModel = tao_models_classes_service_ServiceCall::fromResource($interactiveService);
             $vars = $serviceCallModel->getRequiredVariables();
             $parameters = array();
             foreach ($vars as $variable) {
                 $key = (string) $variable->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_PROCESSVARIABLES_CODE));
                 $value = $activityExecution->getOnePropertyValue(new core_kernel_classes_Property($variable));
                 if ($value instanceof core_kernel_classes_Resource) {
                     $parameters[$key] = $value->getUri();
                 } elseif ($value instanceof core_kernel_classes_Literal) {
                     $parameters[$key] = (string) $value;
                 }
             }
             $serviceCallId = $activityExecution->getUri() . (count($interactiveServices) == 1 ? '' : $interactiveService->getUri());
             $jsServiceApi = tao_helpers_ServiceJavascripts::getServiceApi($serviceCallModel, $serviceCallId, $parameters);
             $services[] = array('style' => $interactiveServiceService->getStyle($interactiveService), 'api' => $jsServiceApi);
         }
         $this->setData('services', $services);
         //set activity control:
         $controls = $activityService->getControls($activityDefinition);
         $browserViewData['controls'] = array('backward' => isset($controls[INSTANCE_CONTROL_BACKWARD]) ? (bool) $controls[INSTANCE_CONTROL_BACKWARD] : false, 'forward' => isset($controls[INSTANCE_CONTROL_FORWARD]) ? (bool) $controls[INSTANCE_CONTROL_FORWARD] : false);
         // If paused, resume it:
         if ($this->processExecutionService->isFinished($this->processExecution)) {
             $this->processExecutionService->resume($this->processExecution);
         }
         //get process definition:
         $processDefinition = $this->processExecutionService->getExecutionOf($this->processExecution);
         // Browser view main data.
         $browserViewData['processLabel'] = $processDefinition->getLabel();
         $browserViewData['processExecutionLabel'] = $this->processExecution->getLabel();
         $browserViewData['activityLabel'] = $activityDefinition->getLabel();
         $browserViewData['processUri'] = $this->processExecution->getUri();
         $browserViewData['active_Resource'] = "'" . $activityDefinition->getUri() . "'";
         $browserViewData['isInteractiveService'] = true;
         $this->setData('browserViewData', $browserViewData);
         $this->setData('activity', $activityDefinition);
         /* <DEBUG> :populate the debug widget */
         if (DEBUG_MODE) {
             $this->setData('debugWidget', DEBUG_MODE);
             $servicesResources = array();
             foreach ($services as $service) {
                 $servicesResource = $service;
                 $servicesResource['input'] = $interactiveServiceService->getInputValues($interactiveService, $activityExecution);
                 $servicesResource['output'] = $interactiveServiceService->getOutputValues($interactiveService, $activityExecution);
                 $servicesResources[] = $servicesResource;
             }
             $variableService = wfEngine_models_classes_VariableService::singleton();
             $this->setData('debugData', array('Activity' => $activityDefinition, 'ActivityExecution' => $activityExecution, 'CurrentActivities' => $currentlyAvailableActivityExecutions, 'Services' => $servicesResources, 'VariableStack' => $variableService->getAll()));
         }
         /* </DEBUG> */
         $this->setData('activityExecutionUri', $browserViewData['activityExecutionUri']);
         $this->setData('processUri', $browserViewData['processUri']);
         $this->setData('activityExecutionNonce', $browserViewData['activityExecutionNonce']);
         $this->setData('client_config_url', $this->getClientConfigUrl());
         $this->setView('process_browser.tpl');
     }
 }
 /**
  * Short description of method cloneProcessSegment
  *
  * @access protected
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource process
  * @param  boolean addTransitionalActivity
  * @return core_kernel_classes_Array
  */
 protected function cloneProcessSegment(core_kernel_classes_Resource $process, $addTransitionalActivity = false)
 {
     $returnValue = null;
     $steps = $this->cloneProcessContent($process);
     $in = array();
     $out = array();
     foreach ($steps as $activity) {
         if (!$this->activityService->isActivity($activity)) {
             continue;
         }
         if (wfEngine_models_classes_ActivityService::singleton()->isInitial($activity)) {
             $in[] = $activity;
         }
         $next = $activity->getOnePropertyValue(new core_kernel_classes_Property(PROPERTY_STEP_NEXT));
         if (is_null($next)) {
             $out[] = $activity;
         }
     }
     if (count($in) != 1) {
         throw new common_exception_Error('Unsupported nr of initial activities ' . count($in) . ' for test ' . $process->getUri());
     }
     $initialActivity = array_shift($in);
     $newFinalActivities = $out;
     if (is_null($initialActivity)) {
         throw new Exception('no initial activity found to the defined process segment');
     }
     if (empty($newFinalActivities)) {
         //TODO: check that every connector has a following activity
         throw new Exception('no terminal activity found to the defined process segment');
     }
     $newInitialActivity = $initialActivity;
     if ($addTransitionalActivity) {
         //echo "adding transitionnal actiivties";
         //init the required properties:
         $propInitial = new core_kernel_classes_Property(PROPERTY_ACTIVITIES_ISINITIAL);
         $propHidden = new core_kernel_classes_Property(PROPERTY_ACTIVITIES_ISHIDDEN);
         $activityClass = new core_kernel_classes_Class(CLASS_ACTIVITIES);
         //build the $firstActivity:
         $firstActivity = $activityClass->createInstance("process_start ({$process->getLabel()})", "created by ProcessCloner.Class");
         $firstActivity->editPropertyValues($propInitial, GENERIS_TRUE);
         //do set it here, the property will be modified automatically by create "following" activity
         $firstActivity->editPropertyValues($propHidden, GENERIS_TRUE);
         $connector = $this->authoringService->createConnector($firstActivity);
         //get the clone of the intiial acitivty:
         if (is_null($newInitialActivity)) {
             throw new Exception("the intial activity has not been cloned: {$initialActivity->getLabel()}({$initialActivity->getUri()})");
         }
         $this->authoringService->createSequenceActivity($connector, $newInitialActivity);
         //this function also automatically set the former $iniitalAcitivty to "not initial"
         //TODO: rename the function createSequenceActivity to addSequenceActivity, clearer that way
         //build the last activity:
         $lastActivity = $activityClass->createInstance("process_end ({$process->getLabel()})", "created by ProcessCloner.Class");
         $lastActivity->editPropertyValues($propHidden, GENERIS_TRUE);
         foreach ($newFinalActivities as $newActivity) {
             //TODO: determine if there is need for merging multiple instances of a parallelized activity that has not been merged
             $connector = $this->authoringService->createConnector($newActivity);
             $this->authoringService->createSequenceActivity($connector, $lastActivity);
         }
         $newInitialActivity = $firstActivity;
         $newFinalActivities = $lastActivity;
         $this->addClonedActivity($firstActivity);
         $this->addClonedActivity($lastActivity);
     }
     $returnValue = array('in' => $newInitialActivity, 'out' => $newFinalActivities);
     return $returnValue;
 }
 /**
  * Test the sequential process execution:
  */
 public function testVirtualSequencialProcess()
 {
     error_reporting(E_ALL);
     try {
         $roleService = wfEngine_models_classes_RoleService::singleton();
         $authoringService = wfAuthoring_models_classes_ProcessService::singleton();
         $activityService = wfEngine_models_classes_ActivityService::singleton();
         $activityExecutionService = wfEngine_models_classes_ActivityExecutionService::singleton();
         $processExecutionService = wfEngine_models_classes_ProcessExecutionService::singleton();
         $processVariableService = wfEngine_models_classes_VariableService::singleton();
         //TEST PLAN :
         //INSTANCE_ACL_ROLE, $roleA
         //INSTANCE_ACL_ROLE_RESTRICTED_USER, $roleB
         //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB (assigned dynamically via process var $role_processVar in activity1)
         //INSTANCE_ACL_USER, $user2	(assigned dynamically via process var $user_processVar in activity2)
         //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB (assigned dynamically via process var $role_processVar in activity1)
         //INSTANCE_ACL_ROLE_RESTRICTED_USER_DELIVERY, $roleA
         //create roles and users:
         $wfRole = new core_kernel_classes_Resource(INSTANCE_ROLE_WORKFLOW);
         $roleA = $roleService->addRole('ACLTestCaseRoleA', $wfRole);
         $roleB = $roleService->addRole('ACLTestCaseRoleB', $wfRole);
         $roleC = $roleService->addRole('ACLTestCaseRoleC', $wfRole);
         list($usec, $sec) = explode(" ", microtime());
         $users = array();
         $users[0] = $usec;
         for ($i = 1; $i <= 6; $i++) {
             $users[] = 'ACLTestCaseUser' . $i . '-' . $usec;
         }
         $user1 = $this->createUser($users[1]);
         $user1->setLabel($users[1]);
         $user2 = $this->createUser($users[2]);
         $user2->setLabel($users[2]);
         $user3 = $this->createUser($users[3]);
         $user3->setLabel($users[3]);
         $user4 = $this->createUser($users[4]);
         $user4->setLabel($users[4]);
         $user5 = $this->createUser($users[5]);
         $user5->setLabel($users[5]);
         $user6 = $this->createUser($users[6]);
         $user6->setLabel($users[6]);
         $roleService->setRoleToUsers($roleA, array($user1->getUri(), $user2->getUri(), $user3->getUri()));
         $roleService->setRoleToUsers($roleB, array($user4->getUri(), $user5->getUri()));
         $roleService->setRoleToUsers($roleC, array($user6->getUri()));
         //create some process variables:
         $user_processVar_key = 'unit_var_user_' . time();
         $user_processVar = $processVariableService->createProcessVariable('Proc Var for user assignation', $user_processVar_key);
         $role_processVar_key = 'unit_var_role_' . time();
         $role_processVar = $processVariableService->createProcessVariable('Proc Var for role assignation', $role_processVar_key);
         //create a new process def
         $processDefinition = $authoringService->createProcess('ProcessForUnitTest', 'Unit test');
         $this->assertIsA($processDefinition, 'core_kernel_classes_Resource');
         //define activities and connectors
         //activity 1:
         $activity1 = $authoringService->createActivity($processDefinition, 'activity1');
         $this->assertNotNull($activity1);
         $authoringService->setFirstActivity($processDefinition, $activity1);
         $activityService->setAcl($activity1, new core_kernel_classes_Resource(INSTANCE_ACL_ROLE), $roleA);
         $connector1 = $authoringService->createConnector($activity1);
         $authoringService->setConnectorType($connector1, new core_kernel_classes_Resource(INSTANCE_TYPEOFCONNECTORS_SEQUENCE));
         $this->assertNotNull($connector1);
         //activity 2:
         $activity2 = $authoringService->createSequenceActivity($connector1, null, 'activity2');
         $this->assertNotNull($activity2);
         $activityService->setAcl($activity2, new core_kernel_classes_Resource(INSTANCE_ACL_ROLE_RESTRICTED_USER), $roleB);
         $connector2 = $authoringService->createConnector($activity2);
         $authoringService->setConnectorType($connector2, new core_kernel_classes_Resource(INSTANCE_TYPEOFCONNECTORS_SEQUENCE));
         $this->assertNotNull($connector2);
         //activity 3:
         $activity3 = $authoringService->createSequenceActivity($connector2, null, 'activity3');
         $this->assertNotNull($activity3);
         $activityService->setAcl($activity3, new core_kernel_classes_Resource(INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED), $role_processVar);
         $connector3 = $authoringService->createConnector($activity3);
         $authoringService->setConnectorType($connector3, new core_kernel_classes_Resource(INSTANCE_TYPEOFCONNECTORS_SEQUENCE));
         $this->assertNotNull($connector3);
         //activity 4:
         $activity4 = $authoringService->createSequenceActivity($connector3, null, 'activity4');
         $this->assertNotNull($activity4);
         $activityService->setAcl($activity4, new core_kernel_classes_Resource(INSTANCE_ACL_USER), $user_processVar);
         $connector4 = $authoringService->createConnector($activity4);
         $authoringService->setConnectorType($connector4, new core_kernel_classes_Resource(INSTANCE_TYPEOFCONNECTORS_SEQUENCE));
         $this->assertNotNull($connector4);
         //activity 5:
         $activity5 = $authoringService->createSequenceActivity($connector4, null, 'activity5');
         $this->assertNotNull($activity5);
         $activityService->setAcl($activity5, new core_kernel_classes_Resource(INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED), $role_processVar);
         $connector5 = $authoringService->createConnector($activity5);
         $authoringService->setConnectorType($connector5, new core_kernel_classes_Resource(INSTANCE_TYPEOFCONNECTORS_SEQUENCE));
         $this->assertNotNull($connector5);
         //activity 6:
         $activity6 = $authoringService->createSequenceActivity($connector5, null, 'activity6');
         $this->assertNotNull($activity6);
         $activityService->setAcl($activity6, new core_kernel_classes_Resource(INSTANCE_ACL_ROLE_RESTRICTED_USER_DELIVERY), $roleA);
         //run the process
         $processExecName = 'Test Process Execution';
         $processExecComment = 'created for processExecustionService test case by ' . __METHOD__;
         $processInstance = $processExecutionService->createProcessExecution($processDefinition, $processExecName, $processExecComment);
         $this->assertEquals($processDefinition->getUri(), $processExecutionService->getExecutionOf($processInstance)->getUri());
         $this->assertEquals($processDefinition->getUri(), $processExecutionService->getExecutionOf($processInstance)->getUri());
         $this->assertTrue($processExecutionService->checkStatus($processInstance, 'started'));
         $this->out(__METHOD__, true);
         $currentActivityExecutions = $processExecutionService->getCurrentActivityExecutions($processInstance);
         $this->assertEquals(count($currentActivityExecutions), 1);
         $this->assertEquals(strpos(array_pop($currentActivityExecutions)->getLabel(), 'Execution of activity1'), 0);
         $this->out("<strong>Forward transitions:</strong>", true);
         $loginProperty = new core_kernel_classes_Property(PROPERTY_USER_LOGIN);
         $iterationNumber = 6;
         $i = 1;
         while ($i <= $iterationNumber) {
             if ($i < $iterationNumber) {
                 //try deleting a process that is not finished
                 $this->assertFalse($processExecutionService->deleteProcessExecution($processInstance, true));
             }
             $activities = $processExecutionService->getAvailableCurrentActivityDefinitions($processInstance, $this->currentUser);
             $this->assertEquals(count($activities), 1);
             $activity = array_shift($activities);
             $this->out("<strong>" . $activity->getLabel() . "</strong>", true);
             $this->assertTrue($activity->getLabel() == 'activity' . $i);
             $this->out("current user : "******"' . $this->currentUser->getUri() . '"', true);
             $activityExecutions = $processExecutionService->getCurrentActivityExecutions($processInstance);
             $activityExecution = reset($activityExecutions);
             $this->checkAccessControl($activityExecution);
             //check ACL:
             switch ($i) {
                 case 1:
                     //INSTANCE_ACL_ROLE, $roleA:
                     $this->checkAclRole($users, $activityExecution, $processInstance);
                     $processVariableService->push($role_processVar_key, $roleB->getUri());
                     break;
                 case 2:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER, $roleB:
                     $this->checkAclRoleRestrictedUser($users, $activityExecution, $processInstance);
                     $processVariableService->push($user_processVar_key, $user2->getUri());
                     break;
                 case 3:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB
                     $this->assertTrue($this->changeUser($users[5]));
                     $this->assertTrue($activityExecutionService->checkAcl($activityExecution, $this->currentUser, $processInstance));
                     $this->assertNotNull($processExecutionService->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser));
                     $this->checkAclRoleRestrictedUserInherited($users, $activityExecution, $processInstance);
                     break;
                 case 4:
                     //INSTANCE_ACL_USER, $user2:
                     $this->checkAclUser($users, $activityExecution, $processInstance);
                     break;
                 case 5:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB:
                     //only user5 can access it normally:
                     $this->checkUser5($users, $activityExecution, $processInstance);
                     break;
                 case 6:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_DELIVERY, $roleA:
                     //only the user of $roleA that executed (the initial acivity belongs to user2:
                     $this->checkAclRoleRestrictedUserDelivery($users, $activityExecution, $processInstance);
                     break;
             }
             //init execution
             $activityExecution = $processExecutionService->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser);
             $this->assertNotNull($activityExecution);
             $activityExecStatus = $activityExecutionService->getStatus($activityExecution);
             $this->assertNotNull($activityExecStatus);
             $this->assertEquals($activityExecStatus->getUri(), INSTANCE_PROCESSSTATUS_RESUMED);
             //transition to next activity
             $transitionResult = $processExecutionService->performTransition($processInstance, $activityExecution);
             switch ($i) {
                 case 1:
                 case 3:
                 case 4:
                 case 5:
                     $this->assertFalse(count($transitionResult) > 0);
                     $this->assertTrue($processExecutionService->isPaused($processInstance));
                     break;
                 case 2:
                     $this->assertTrue(count($transitionResult) > 0);
                     $this->assertFalse($processExecutionService->isPaused($processInstance));
                     break;
                 case 6:
                     $this->assertFalse(count($transitionResult) > 0);
                     $this->assertTrue($processExecutionService->isFinished($processInstance));
                     break;
             }
             $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
             $this->out("process status: " . $processExecutionService->getStatus($processInstance)->getLabel());
             $i++;
         }
         $this->assertTrue($processExecutionService->isFinished($processInstance));
         $this->assertTrue($processExecutionService->resume($processInstance));
         $this->out("<strong>Backward transitions:</strong>", true);
         $j = 0;
         while ($j < $iterationNumber) {
             $activitieExecs = $processExecutionService->getCurrentActivityExecutions($processInstance);
             $this->assertEquals(count($activitieExecs), 1);
             $activityExecution = reset($activitieExecs);
             $activity = $activityExecutionService->getExecutionOf($activityExecution);
             $this->out("<strong>" . $activity->getLabel() . "</strong>", true);
             $index = $iterationNumber - $j;
             $this->assertEquals($activity->getLabel(), "activity{$index}");
             $this->out("current user : "******"' . $this->currentUser->getUri() . '"', true);
             $this->checkAccessControl($activityExecution);
             //check ACL:
             switch ($index) {
                 case 1:
                     //INSTANCE_ACL_ROLE, $roleA:
                     $this->checkAclRole($users, $activityExecution, $processInstance);
                     break;
                 case 2:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER, $roleB:
                     $this->checkAclRoleRestrictedUser($users, $activityExecution, $processInstance);
                     break;
                 case 3:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB
                     $this->checkAclRoleRestrictedUserInherited($users, $activityExecution, $processInstance);
                     break;
                 case 4:
                     //INSTANCE_ACL_USER, $user2:
                     $this->checkAclUser($users, $activityExecution, $processInstance);
                     break;
                 case 5:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB:
                     //only user5 can access it normally:
                     $this->checkUser5($users, $activityExecution, $processInstance);
                     break;
                 case 6:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_DELIVERY, $roleA:
                     //only the user of $roleA that executed (the initial acivity belongs to user2:
                     $this->checkAclRoleRestrictedUserDelivery($users, $activityExecution, $processInstance);
                     break;
             }
             //init execution
             $activityExecution = $processExecutionService->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser);
             $this->assertNotNull($activityExecution);
             $activityExecStatus = $activityExecutionService->getStatus($activityExecution);
             $this->assertNotNull($activityExecStatus);
             $this->assertEquals($activityExecStatus->getUri(), INSTANCE_PROCESSSTATUS_RESUMED);
             //transition to next activity
             $transitionResult = $processExecutionService->performBackwardTransition($processInstance, $activityExecution);
             $processStatus = $processExecutionService->getStatus($processInstance);
             $this->assertNotNull($processStatus);
             $this->assertEquals($processStatus->getUri(), INSTANCE_PROCESSSTATUS_RESUMED);
             if ($j < $iterationNumber - 1) {
                 $this->assertTrue(count($transitionResult) > 0);
             } else {
                 $this->assertFalse($transitionResult);
             }
             $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
             $this->out("process status: " . $processExecutionService->getStatus($processInstance)->getLabel());
             $j++;
         }
         $this->out("<strong>Forward transitions again:</strong>", true);
         $i = 1;
         while ($i <= $iterationNumber) {
             if ($i < $iterationNumber) {
                 //try deleting a process that is not finished
                 $this->assertFalse($processExecutionService->deleteProcessExecution($processInstance, true));
             }
             $activitieExecs = $processExecutionService->getCurrentActivityExecutions($processInstance);
             $this->assertEquals(count($activitieExecs), 1);
             $activityExecution = reset($activitieExecs);
             $activity = $activityExecutionService->getExecutionOf($activityExecution);
             $this->out("<strong>" . $activity->getLabel() . "</strong>", true);
             $this->assertTrue($activity->getLabel() == 'activity' . $i);
             $this->checkAccessControl($activityExecution);
             //check ACL:
             switch ($i) {
                 case 1:
                     //INSTANCE_ACL_ROLE, $roleA:
                     $this->checkAclRole($users, $activityExecution, $processInstance);
                     //TODO:to be modified after "back"
                     $processVariableService->push($role_processVar_key, $roleB->getUri());
                     break;
                 case 2:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER, $roleB:
                     $this->checkAclRoleRestrictedUser($users, $activityExecution, $processInstance);
                     //TODO:to be modified after "back"
                     $processVariableService->push($user_processVar_key, $user2->getUri());
                     break;
                 case 3:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB
                     $this->checkAclRoleRestrictedUserInherited($users, $activityExecution, $processInstance);
                     break;
                 case 4:
                     //INSTANCE_ACL_USER, $user2:
                     $this->checkAclUser($users, $activityExecution, $processInstance);
                     break;
                 case 5:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_INHERITED, $roleB:
                     //only user5 can access it normally:
                     $this->checkUser5($users, $activityExecution, $processInstance);
                     break;
                 case 6:
                     //INSTANCE_ACL_ROLE_RESTRICTED_USER_DELIVERY, $roleA:
                     //only the user of $roleA that executed (the initial acivity belongs to user2:
                     $this->checkAclRoleRestrictedUserDelivery($users, $activityExecution, $processInstance);
                     break;
             }
             //init execution
             $activityExecution = $processExecutionService->initCurrentActivityExecution($processInstance, $activityExecution, $this->currentUser);
             $this->assertNotNull($activityExecution);
             //transition to next activity
             $transitionResult = $processExecutionService->performTransition($processInstance, $activityExecution);
             switch ($i) {
                 case 1:
                 case 3:
                 case 4:
                 case 5:
                     $this->assertFalse(count($transitionResult) > 0);
                     $this->assertTrue($processExecutionService->isPaused($processInstance));
                     break;
                 case 2:
                     $this->assertTrue(count($transitionResult) > 0);
                     $this->assertFalse($processExecutionService->isPaused($processInstance));
                     break;
                 case 6:
                     $this->assertFalse(count($transitionResult) > 0);
                     $this->assertTrue($processExecutionService->isFinished($processInstance));
                     break;
             }
             $this->out("activity status: " . $activityExecutionService->getStatus($activityExecution)->getLabel());
             $this->out("process status: " . $processExecutionService->getStatus($processInstance)->getLabel());
             $i++;
         }
         $this->assertTrue($processExecutionService->isFinished($processInstance));
         //delete processdef:
         $this->assertTrue($authoringService->deleteProcess($processDefinition));
         //delete process execution:
         $this->assertTrue($processInstance->exists());
         $this->assertTrue($processExecutionService->deleteProcessExecution($processInstance));
         $this->assertFalse($processInstance->exists());
         if (!is_null($this->currentUser)) {
             $this->userService->logout();
             $this->userService->removeUser($this->currentUser);
         }
         $roleA->delete();
         $roleB->delete();
         $roleC->delete();
         $user1->delete();
         $user2->delete();
         $user3->delete();
         $user4->delete();
         $user5->delete();
         $user6->delete();
         $user_processVar->delete();
         $role_processVar->delete();
     } catch (common_Exception $ce) {
         $this->fail($ce);
     }
 }
 /**
  * Short description of method getAclMode
  *
  * @access public
  * @author Somsack Sipasseuth, <*****@*****.**>
  * @param  Resource activityExecution
  * @return core_kernel_classes_Resource
  */
 public function getAclMode(core_kernel_classes_Resource $activityExecution)
 {
     $returnValue = null;
     $aclMode = $activityExecution->getOnePropertyValue($this->ACLModeProperty);
     if ($aclMode instanceof core_kernel_classes_Resource) {
         $activityService = wfEngine_models_classes_ActivityService::singleton();
         if (array_key_exists($aclMode->getUri(), $activityService->getAclModes())) {
             $returnValue = $aclMode;
         }
     }
     return $returnValue;
 }
 /**
  * Short description of method __construct
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource process
  * @return mixed
  */
 public function __construct(core_kernel_classes_Resource $process)
 {
     $this->process = $process;
     $this->activityService = wfEngine_models_classes_ActivityService::singleton();
     $this->connectorService = wfEngine_models_classes_ConnectorService::singleton();
     $this->authoringService = wfAuthoring_models_classes_ProcessService::singleton();
     parent::__construct();
 }
 public function testCreateCBAProcess()
 {
     if (!$this->createProcess) {
         return;
     }
     $authoringService = wfAuthoring_models_classes_ProcessService::singleton();
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $connectorService = wfAuthoring_models_classes_ConnectorService::singleton();
     $processVariableService = wfEngine_models_classes_VariableService::singleton();
     $cardinalityService = wfEngine_models_classes_ActivityCardinalityService::singleton();
     //create some process variables:
     $varCodes = array('unitUri', 'countryCode', 'languageCode', 'npm', 'translatorsCount', 'translator', 'reconciler', 'verifier', 'translatorSelected', 'translationFinished', 'TDreview', 'layoutCheck', 'finalCheck', 'opticalCheck', 'TDsignOff', 'countrySignOff', 'xliff', 'vff', 'xliff_working', 'vff_working');
     //"workingFiles" holds the working versions of the xliff and vff files, plus their revision number, in an serialized array()
     //during translation: workingFiles = array('user'=>#007, 'xliff' => array('uri' => #123456, 'revision'=>3), 'vff'=> array('uri' => #456789, 'revision'=>5))
     $this->populateVariables($varCodes);
     $aclUser = new core_kernel_classes_Resource(INSTANCE_ACL_USER);
     $aclRole = new core_kernel_classes_Resource(INSTANCE_ACL_ROLE);
     $processDefinition = $authoringService->createProcess($this->processLabel['CBA'], 'For Unit test');
     $this->assertIsA($processDefinition, 'core_kernel_classes_Resource');
     //set process initialization rights:
     $this->assertTrue($authoringService->setAcl($processDefinition, $aclRole, $this->roles['consortium']));
     //define activities and connectors
     //Select translators:
     $activitySelectTranslators = $authoringService->createActivity($processDefinition, 'Select Translator');
     $this->assertNotNull($activitySelectTranslators);
     $authoringService->setFirstActivity($processDefinition, $activitySelectTranslators);
     $activityService->setAcl($activitySelectTranslators, $aclUser, $this->vars['npm']);
     $activityService->setControls($activitySelectTranslators, array(INSTANCE_CONTROL_FORWARD));
     $connectorSelectTranslators = $authoringService->createConnector($activitySelectTranslators);
     $this->assertNotNull($connectorSelectTranslators);
     //translate:
     $activityTranslate = $authoringService->createActivity($processDefinition, 'Translate');
     $this->assertNotNull($activityTranslate);
     $activityService->setAcl($activityTranslate, $aclUser, $this->vars['translator']);
     $activityService->setControls($activityTranslate, array(INSTANCE_CONTROL_FORWARD));
     $result = $authoringService->setParallelActivities($connectorSelectTranslators, array($activityTranslate->getUri() => $this->vars['translatorsCount']));
     $this->assertTrue($result);
     $this->assertTrue($connectorService->setSplitVariables($connectorSelectTranslators, array($activityTranslate->getUri() => $this->vars['translator'])));
     $nextActivities = $connectorService->getNextActivities($connectorSelectTranslators);
     $this->assertEqual(count($nextActivities), 1);
     $cardinality = reset($nextActivities);
     $this->assertTrue($cardinalityService->isCardinality($cardinality));
     $this->assertEqual($cardinalityService->getDestination($cardinality)->getUri(), $activityTranslate->getUri());
     $this->assertEqual($cardinalityService->getCardinality($cardinality)->getUri(), $this->vars['translatorsCount']->getUri());
     $connectorTranslate = $authoringService->createConnector($activityTranslate);
     $this->assertNotNull($connectorTranslate);
     //reconciliation:
     $activityReconciliation = $authoringService->createJoinActivity($connectorTranslate, null, 'Reconciliation', $activityTranslate);
     $prevActivities = $connectorService->getPreviousActivities($connectorTranslate);
     $this->assertEqual(count($prevActivities), 1);
     $cardinality = reset($prevActivities);
     $this->assertTrue($cardinalityService->isCardinality($cardinality));
     $this->assertEqual($cardinalityService->getSource($cardinality)->getUri(), $activityTranslate->getUri());
     $this->assertEqual($cardinalityService->getCardinality($cardinality)->getUri(), $this->vars['translatorsCount']->getUri());
     $this->assertNotNull($activityReconciliation);
     $activityService->setAcl($activityReconciliation, $aclUser, $this->vars['reconciler']);
     $activityService->setControls($activityReconciliation, array(INSTANCE_CONTROL_FORWARD));
     $connectorReconciliation = $authoringService->createConnector($activityReconciliation);
     $this->assertNotNull($connectorReconciliation);
     //verify translations
     $activityVerifyTranslations = $authoringService->createSequenceActivity($connectorReconciliation, null, 'Verify Translations');
     $this->assertNotNull($activityVerifyTranslations);
     $activityService->setAcl($activityVerifyTranslations, $aclUser, $this->vars['verifier']);
     $activityService->setControls($activityVerifyTranslations, array(INSTANCE_CONTROL_FORWARD));
     $connectorVerifyTranslations = $authoringService->createConnector($activityVerifyTranslations);
     $this->assertNotNull($connectorVerifyTranslations);
     //correct verification
     $activityCorrectVerification = $authoringService->createSequenceActivity($connectorVerifyTranslations, null, 'Correct Verification Issues');
     $this->assertNotNull($activityCorrectVerification);
     $activityService->setAcl($activityCorrectVerification, $aclUser, $this->vars['reconciler']);
     $activityService->setControls($activityCorrectVerification, array(INSTANCE_CONTROL_FORWARD));
     $connectorCorrectVerification = $authoringService->createConnector($activityCorrectVerification);
     $this->assertNotNull($connectorCorrectVerification);
     //TD review :
     $activityTDreview = $authoringService->createSequenceActivity($connectorCorrectVerification, null, 'TD review');
     $this->assertNotNull($activityTDreview);
     $activityService->setAcl($activityTDreview, $aclRole, $this->roles['testDeveloper']);
     $activityService->setControls($activityTDreview, array(INSTANCE_CONTROL_FORWARD));
     $connectorTDreview = $authoringService->createConnector($activityTDreview);
     $this->assertNotNull($connectorTDreview);
     //if TD review not ok, return to correct verification issues:
     $transitionRule = $authoringService->createTransitionRule($connectorTDreview, '^TDreview == 1');
     $this->assertNotNull($transitionRule);
     $activityCorrectVerificationBis = $authoringService->createConditionalActivity($connectorTDreview, 'else', $activityCorrectVerification);
     //if ^TDreview != 1
     $this->assertEqual($activityCorrectVerification->getUri(), $activityCorrectVerificationBis->getUri());
     //correct layout :
     $activityCorrectLayout = $authoringService->createConditionalActivity($connectorTDreview, 'then', null, 'Correct Layout Issues');
     //if ^TDreview == 1
     $this->assertNotNull($activityCorrectLayout);
     $activityService->setAcl($activityCorrectLayout, $aclRole, $this->roles['developer']);
     $activityService->setControls($activityCorrectLayout, array(INSTANCE_CONTROL_FORWARD));
     $connectorCorrectLayout = $authoringService->createConnector($activityCorrectLayout);
     $this->assertNotNull($connectorCorrectLayout);
     //if correct layout needs verification :
     $transitionRule = $authoringService->createTransitionRule($connectorCorrectLayout, '^layoutCheck == 1');
     $this->assertNotNull($transitionRule);
     $activityVerification = $authoringService->createConditionalActivity($connectorCorrectLayout, 'else', null, 'Verification Followup');
     //if ^layoutCheck != 1
     $this->assertNotNull($activityVerification);
     $activityService->setAcl($activityVerification, $aclUser, $this->vars['verifier']);
     $activityService->setControls($activityVerification, array(INSTANCE_CONTROL_FORWARD));
     $connectorVerification = $authoringService->createConnector($activityVerification);
     $this->assertNotNull($connectorVerification);
     //final check :
     $activityFinalCheck = $authoringService->createConditionalActivity($connectorCorrectLayout, 'then', null, 'Final Check');
     //if ^layoutCheck == 1
     $this->assertNotNull($activityFinalCheck);
     $activityService->setAcl($activityFinalCheck, $aclRole, $this->roles['testDeveloper']);
     $activityService->setControls($activityFinalCheck, array(INSTANCE_CONTROL_FORWARD));
     $connectorFinalCheck = $authoringService->createConnector($activityFinalCheck);
     $this->assertNotNull($connectorFinalCheck);
     //if final check ok, go to scoring definition :
     $transitionRule = $authoringService->createTransitionRule($connectorFinalCheck, '^finalCheck == 1');
     $this->assertNotNull($transitionRule);
     $activityScoringDefinition = $authoringService->createConditionalActivity($connectorFinalCheck, 'then', null, 'Scoring Definition and Testing');
     //if ^finalCheck == 1
     $this->assertNotNull($activityScoringDefinition);
     $activityService->setAcl($activityScoringDefinition, $aclUser, $this->vars['reconciler']);
     $activityService->setControls($activityScoringDefinition, array(INSTANCE_CONTROL_FORWARD));
     $connectorScoringDefinition = $authoringService->createConnector($activityScoringDefinition);
     $this->assertNotNull($connectorScoringDefinition);
     //if not ok, return to correct layout :
     $activityCorrectLayoutBis = $authoringService->createConditionalActivity($connectorFinalCheck, 'else', $activityCorrectLayout);
     //if ^finalCheck != 1
     $this->assertEqual($activityCorrectLayout->getUri(), $activityCorrectLayoutBis->getUri());
     //verification :
     $transitionRule = $authoringService->createTransitionRule($connectorVerification, '^opticalCheck == 1');
     $this->assertNotNull($transitionRule);
     $activityFinalCheckBis = $authoringService->createConditionalActivity($connectorVerification, 'then', $activityFinalCheck);
     //if ^opticalCheck == 1
     $this->assertEqual($activityFinalCheckBis->getUri(), $activityFinalCheck->getUri());
     $activityCorrectLayoutBis = $authoringService->createConditionalActivity($connectorVerification, 'else', $activityCorrectLayout);
     //if ^opticalCheck != 1
     $this->assertEqual($activityCorrectLayoutBis->getUri(), $activityCorrectLayout->getUri());
     //scoring verification:
     $activityScoringVerification = $authoringService->createSequenceActivity($connectorScoringDefinition, null, 'Scoring Verification');
     $this->assertNotNull($activityScoringVerification);
     $activityService->setAcl($activityScoringVerification, $aclUser, $this->vars['verifier']);
     $activityService->setControls($activityScoringVerification, array(INSTANCE_CONTROL_FORWARD));
     $connectorScoringVerification = $authoringService->createConnector($activityScoringVerification);
     $this->assertNotNull($connectorScoringVerification);
     //final sign off :
     $activityTDSignOff = $authoringService->createSequenceActivity($connectorScoringVerification, null, 'Test Developer Sign Off');
     $this->assertNotNull($activityTDSignOff);
     $activityService->setAcl($activityTDSignOff, $aclRole, $this->roles['testDeveloper']);
     $activityService->setControls($activityTDSignOff, array(INSTANCE_CONTROL_FORWARD));
     $connectorTDSignOff = $authoringService->createConnector($activityTDSignOff);
     $this->assertNotNull($connectorTDSignOff);
     //link back to final check:
     $transitionRule = $authoringService->createTransitionRule($connectorTDSignOff, '^TDsignOff == 1');
     $this->assertNotNull($transitionRule);
     $authoringService->createConditionalActivity($connectorTDSignOff, 'else', $activityFinalCheck);
     //sign off :
     $activityCountrySignOff = $authoringService->createConditionalActivity($connectorTDSignOff, 'then', null, 'Country Sign Off');
     $activityService->setAcl($activityCountrySignOff, $aclUser, $this->vars['reconciler']);
     $activityService->setControls($activityCountrySignOff, array(INSTANCE_CONTROL_FORWARD));
     //complete the process:
     $connectorCountrySignOff = $authoringService->createConnector($activityCountrySignOff);
     $this->assertNotNull($connectorCountrySignOff);
     $transitionRule = $authoringService->createTransitionRule($connectorCountrySignOff, '^countrySignOff == 1');
     $this->assertNotNull($transitionRule);
     $activityFinal = $authoringService->createConditionalActivity($connectorCountrySignOff, 'then', null, 'Completed');
     $activityService->setAcl($activityFinal, $aclUser, $this->vars['reconciler']);
     $activityService->setControls($activityFinal, array(INSTANCE_CONTROL_FORWARD));
     $activityService->setHidden($activityFinal, true);
     $activityTDSignOffBis = $authoringService->createConditionalActivity($connectorCountrySignOff, 'else', $activityTDSignOff);
     $this->assertEqual($activityTDSignOff->getUri(), $activityTDSignOffBis->getUri());
     //end of process definition
     $this->processDefinition['CBA'] = $processDefinition;
 }
 /**
  * Helper for get test items
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param
  *            Resource activity
  * @return core_kernel_classes_Resource
  */
 protected function getItemByActivity(core_kernel_classes_Resource $activity)
 {
     $returnValue = null;
     $services = wfEngine_models_classes_ActivityService::singleton()->getInteractiveServices($activity);
     foreach ($services as $iService) {
         if (!$iService instanceof core_kernel_classes_Resource) {
             throw new common_exception_InconsistentData('Non resource service call found for activity ' . $activity->getUri());
         }
         $serviceDefinition = $iService->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CALLOFSERVICES_SERVICEDEFINITION));
         if ($serviceDefinition->getUri() == INSTANCE_ITEMCONTAINER_SERVICE) {
             $returnValue = $this->getItemByService($iService);
             break;
         }
     }
     return $returnValue;
 }
 public function setUp()
 {
     $this->service = wfEngine_models_classes_ActivityService::singleton();
 }
 public static function nextActivityElements(core_kernel_classes_Resource $connector, $type, $allowCreation = true, $includeConnectors = true, $optionsWidget = 'Combobox')
 {
     $returnValue = array();
     $authorizedOptionsWidget = array('Combobox', 'Checkbox');
     if (!in_array($optionsWidget, $authorizedOptionsWidget)) {
         throw new Exception('Wrong type of widget');
         return $returnValue;
     }
     $idPrefix = '';
     $nextActivity = null;
     $propTransitionRule = new core_kernel_classes_Property(PROPERTY_CONNECTORS_TRANSITIONRULE);
     $propNextActivities = new core_kernel_classes_Property(PROPERTY_STEP_NEXT);
     //find the next activity if available
     switch (strtolower($type)) {
         case 'next':
             $nextActivityCollection = $connector->getPropertyValuesCollection($propNextActivities);
             foreach ($nextActivityCollection->getIterator() as $activity) {
                 if ($activity instanceof core_kernel_classes_Resource) {
                     $nextActivity = $activity;
                     //we take the last one...(note: there should be only one though)
                 }
             }
             $idPrefix = 'next';
             break;
         case 'then':
             $transitionRuleCollection = $connector->getPropertyValuesCollection($propTransitionRule);
             foreach ($transitionRuleCollection->getIterator() as $transitionRule) {
                 if ($transitionRule instanceof core_kernel_classes_Resource) {
                     foreach ($transitionRule->getPropertyValuesCollection(new core_kernel_classes_Property(PROPERTY_TRANSITIONRULES_THEN))->getIterator() as $then) {
                         if ($then instanceof core_kernel_classes_Resource) {
                             $nextActivity = $then;
                         }
                     }
                 }
             }
             $idPrefix = 'then';
             break;
         case 'else':
             $transitionRuleCollection = $connector->getPropertyValuesCollection($propTransitionRule);
             foreach ($transitionRuleCollection->getIterator() as $transitionRule) {
                 if ($transitionRule instanceof core_kernel_classes_Resource) {
                     foreach ($transitionRule->getPropertyValuesCollection(new core_kernel_classes_Property(PROPERTY_TRANSITIONRULES_ELSE))->getIterator() as $else) {
                         if ($else instanceof core_kernel_classes_Resource) {
                             $nextActivity = $else;
                         }
                     }
                 }
             }
             $idPrefix = 'else';
             break;
         case 'parallel':
             $nextActivity = array();
             $nextActivityCollection = $connector->getPropertyValuesCollection($propNextActivities);
             foreach ($nextActivityCollection->getIterator() as $cardinality) {
                 if ($cardinality instanceof core_kernel_classes_Resource) {
                     $nextActivity[] = $cardinality;
                 }
             }
             $idPrefix = 'parallel';
             break;
         case 'join':
             // should only have one following activity
             $nextActivity = $connector->getOnePropertyValue($propNextActivities);
             $idPrefix = $type;
             break;
         default:
             throw new Exception("unknown type for the next activity");
     }
     $activityOptions = array();
     $connectorOptions = array();
     if ($allowCreation) {
         //create the activity label element (used only in case of new activity craetion)
         $elementActivityLabel = tao_helpers_form_FormFactory::getElement($idPrefix . "_activityLabel", 'Textbox');
         $elementActivityLabel->setDescription(__('Label'));
         //add the "creating" option
         $activityOptions["newActivity"] = __("create new activity");
         $connectorOptions["newConnector"] = __("create new connector");
     }
     //the activity associated to the connector:
     $referencedActivity = $connector->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_CONNECTORS_ACTIVITYREFERENCE));
     //mandatory property value, initiated at the connector creation
     if ($referencedActivity instanceof core_kernel_classes_Resource) {
         $processDefClass = new core_kernel_classes_Class(CLASS_PROCESS);
         $processes = $processDefClass->searchInstances(array(PROPERTY_PROCESS_ACTIVITIES => $referencedActivity->getUri()), array('like' => false));
         if (count($processes) > 0) {
             $process = array_shift($processes);
             if (!empty($process)) {
                 //get list of activities and connectors for the current process:
                 $connectorClass = new core_kernel_classes_Class(CLASS_CONNECTORS);
                 $processAuthoringService = wfAuthoring_models_classes_ProcessService::singleton();
                 $activities = $processAuthoringService->getActivitiesByProcess($process);
                 foreach ($activities as $activityTemp) {
                     //include activities options:
                     $encodedUri = tao_helpers_Uri::encode($activityTemp->getUri());
                     $activityOptions[$encodedUri] = $activityTemp->getLabel();
                     if (strtolower($type) == 'parallel') {
                         $elementHidden = tao_helpers_form_FormFactory::getElement("{$encodedUri}_num_hidden", 'Hidden');
                         $returnValue[$idPrefix . '_' . $activityTemp->getUri()] = $elementHidden;
                     }
                     //include connectors options:
                     if ($includeConnectors) {
                         $connectors = $connectorClass->searchInstances(array(PROPERTY_CONNECTORS_ACTIVITYREFERENCE => $activityTemp->getUri()), array('like' => false));
                         foreach ($connectors as $connectorTemp) {
                             if ($connector->getUri() != $connectorTemp->getUri()) {
                                 $connectorOptions[tao_helpers_Uri::encode($connectorTemp->getUri())] = $connectorTemp->getLabel();
                             }
                         }
                     }
                 }
             }
         }
     }
     //create the description element
     $elementDescription = tao_helpers_form_FormFactory::getElement($idPrefix, 'Free');
     $elementDescription->setValue(strtoupper($type) . ' :');
     //create the activity select element:
     $elementActivities = tao_helpers_form_FormFactory::getElement($idPrefix . "_activityUri", $optionsWidget);
     $elementActivities->setDescription(__('Activity'));
     $elementActivities->setOptions($activityOptions);
     $elementChoice = null;
     $elementConnectors = null;
     if ($includeConnectors) {
         //the default radio button to select between the 3 possibilities:
         $elementChoice = tao_helpers_form_FormFactory::getElement($idPrefix . "_activityOrConnector", 'Radiobox');
         $elementChoice->setDescription(__('Activity or Connector'));
         $options = array("activity" => __("Activity"), "connector" => __("Connector"));
         $elementChoice->setOptions($options);
         //create the connector select element:
         $elementConnectors = tao_helpers_form_FormFactory::getElement($idPrefix . "_connectorUri", $optionsWidget);
         $elementConnectors->setDescription(__('Connector'));
         $elementConnectors->setOptions($connectorOptions);
     }
     if (!empty($nextActivity)) {
         if (is_array($nextActivity) && $optionsWidget == 'Checkbox') {
             if (strtolower($type) == 'parallel') {
                 $cardinalityService = wfEngine_models_classes_ActivityCardinalityService::singleton();
                 foreach ($nextActivity as $cardinality) {
                     $activity = $cardinalityService->getDestination($cardinality);
                     $number = $cardinalityService->getCardinality($cardinality);
                     if (isset($returnValue[$idPrefix . '_' . $activity->getUri()])) {
                         $returnValue[$idPrefix . '_' . $activity->getUri()]->setValue($number instanceof core_kernel_classes_Resource ? tao_helpers_Uri::encode($number->getUri()) : intval($number));
                     }
                     $elementActivities->setValue($activity->getUri());
                     //no need for tao_helpers_Uri::encode
                 }
             } else {
                 foreach ($nextActivity as $activity) {
                     $elementActivities->setValue($activity->getUri());
                     //no need for tao_helpers_Uri::encode
                 }
             }
         } elseif ($nextActivity instanceof core_kernel_classes_Resource) {
             $aService = wfEngine_models_classes_ActivityService::singleton();
             if ($aService->isActivity($nextActivity)) {
                 if ($includeConnectors) {
                     $elementChoice->setValue("activity");
                 }
                 $elementActivities->setValue($nextActivity->getUri());
                 //no need for tao_helpers_Uri::encode
             }
             $conmectorService = wfEngine_models_classes_ConnectorService::singleton();
             if ($conmectorService->isConnector($nextActivity) && $includeConnectors) {
                 $elementChoice->setValue("connector");
                 $elementConnectors->setValue($nextActivity->getUri());
             }
         }
     }
     //put all elements in the return value:
     $returnValue[$idPrefix . '_description'] = $elementDescription;
     if ($includeConnectors) {
         $returnValue[$idPrefix . '_choice'] = $elementChoice;
     }
     $returnValue[$idPrefix . '_activities'] = $elementActivities;
     if ($allowCreation) {
         $returnValue[$idPrefix . '_label'] = $elementActivityLabel;
     }
     if ($includeConnectors) {
         $returnValue[$idPrefix . '_connectors'] = $elementConnectors;
     }
     return $returnValue;
 }
 /**
  * Short description of method activityNode
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource activity
  * @param  string nodeClass
  * @param  boolean goto
  * @param  array portInfo
  * @param  string labelSuffix
  * @return array
  */
 public function activityNode(core_kernel_classes_Resource $activity, $nodeClass = '', $goto = false, $portInfo = array(), $labelSuffix = '')
 {
     $returnValue = array();
     $class = '';
     $linkAttribute = 'id';
     $activityService = wfEngine_models_classes_ActivityService::singleton();
     $connectorService = wfEngine_models_classes_ConnectorService::singleton();
     if ($activityService->isActivity($activity)) {
         $class = 'node-activity';
     } elseif ($connectorService->isConnector($activity)) {
         $class = 'node-connector';
     } else {
         return $returnValue;
         //unknown type
     }
     if ($goto) {
         $class .= "-goto";
         $linkAttribute = "rel";
     }
     if (empty($portInfo)) {
         $portInfo = array('id' => 0, 'label' => 'next', 'multiplicity' => 1);
     } else {
         if (!isset($portInfo['id'])) {
             $portInfo['id'] = 0;
         }
         if (!isset($portInfo['id'])) {
             $portInfo['label'] = 'next';
         }
         if (!isset($portInfo['id'])) {
             $portInfo['multiplicity'] = 1;
         }
     }
     $returnValue = array('data' => $activity->getLabel() . ' ' . $labelSuffix, 'attributes' => array($linkAttribute => tao_helpers_Uri::encode($activity->getUri()), 'class' => $class), 'port' => $nodeClass, 'portData' => $portInfo);
     $returnValue = self::addNodePrefix($returnValue, $nodeClass);
     return (array) $returnValue;
 }
 /**
  * Short description of method getInitialSteps
  *
  * @access public
  * @author Joel Bout, <*****@*****.**>
  * @param  Resource process
  * @return array
  */
 public function getInitialSteps(core_kernel_classes_Resource $process)
 {
     $returnValue = array();
     foreach ($this->getActivitiesByProcess($process) as $activity) {
         if (wfEngine_models_classes_ActivityService::singleton()->isInitial($activity)) {
             $returnValue[] = $activity;
         }
     }
     return (array) $returnValue;
 }