From 88c15575371ddb6cfd17b52d3b8626e1fc8efc5b Mon Sep 17 00:00:00 2001 From: David Callizaya Date: Fri, 28 Jun 2024 16:51:14 -0400 Subject: [PATCH] Implement demo mode for Parallel gateways --- .../Bpmn/ForceGatewayTransitionTrait.php | 30 +++++++++++++++ .../Nayra/Bpmn/ParallelGatewayTrait.php | 2 +- .../Nayra/Bpmn/ParallelGatewayTransition.php | 17 +++------ .../Nayra/Bpmn/ParallelOutputTransition.php | 38 +++++++++++++++++++ 4 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 src/ProcessMaker/Nayra/Bpmn/ParallelOutputTransition.php diff --git a/src/ProcessMaker/Nayra/Bpmn/ForceGatewayTransitionTrait.php b/src/ProcessMaker/Nayra/Bpmn/ForceGatewayTransitionTrait.php index aceb582a..9403c7b6 100644 --- a/src/ProcessMaker/Nayra/Bpmn/ForceGatewayTransitionTrait.php +++ b/src/ProcessMaker/Nayra/Bpmn/ForceGatewayTransitionTrait.php @@ -2,6 +2,7 @@ namespace ProcessMaker\Nayra\Bpmn; +use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface; use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface; /** @@ -44,4 +45,33 @@ function shouldDebugSkipThisTransition(ExecutionInstanceInterface $executionInst $targetEntrypoint = $connection->target()->getOwner(); return $demoMode && $engine->getSelectedDemoFlow($gateway)->getTarget() !== $targetEntrypoint; } + + /** + * If the condition is not met. + * + * @param \ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface $executionInstance + * + * @return bool + */ + protected function conditionIsFalse() + { + $executionInstance = func_get_arg(0); + $this->collect($executionInstance); + + return true; + } + + /** + * Consume the input tokens. + * + * @param \ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface $executionInstance + */ + private function collect(ExecutionInstanceInterface $executionInstance) + { + return $this->incoming()->sum(function (Connection $flow) use ($executionInstance) { + return $flow->origin()->getTokens($executionInstance)->sum(function (TokenInterface $token) { + return $token->getOwner()->consumeToken($token) ? 1 : 0; + }); + }); + } } diff --git a/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTrait.php b/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTrait.php index 8b0bb31d..3fcdbe78 100644 --- a/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTrait.php +++ b/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTrait.php @@ -74,7 +74,7 @@ public function getInputPlace(FlowInterface $targetFlow = null) protected function buildConnectionTo(FlowInterface $targetFlow) { $outgoingPlace = new State($this, GatewayInterface::TOKEN_STATE_OUTGOING); - $outgoingTransition = new Transition($this); + $outgoingTransition = new ParallelOutputTransition($this); $outgoingTransition->attachEvent( TransitionInterface::EVENT_AFTER_CONSUME, function (TransitionInterface $transition, Collection $consumedTokens) { diff --git a/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTransition.php b/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTransition.php index 1423138d..d11c6c17 100644 --- a/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTransition.php +++ b/src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTransition.php @@ -12,16 +12,7 @@ class ParallelGatewayTransition implements TransitionInterface { use TransitionTrait; - - /** - * Initialize the tokens consumed property, the Parallel Gateway consumes - * exactly one token from each incoming Sequence Flow. - */ - protected function initParallelGatewayTransition() - { - $this->setTokensConsumedPerTransition(-1); - $this->setTokensConsumedPerIncoming(1); - } + use PauseOnGatewayTransitionTrait; /** * Always true because the conditions are not defined in the gateway, but for each @@ -33,7 +24,7 @@ protected function initParallelGatewayTransition() */ public function assertCondition(TokenInterface $token = null, ExecutionInstanceInterface $executionInstance = null) { - return true; + return !$this->shouldPauseGatewayTransition($executionInstance); } /** @@ -46,6 +37,10 @@ public function assertCondition(TokenInterface $token = null, ExecutionInstanceI */ protected function hasAllRequiredTokens(ExecutionInstanceInterface $executionInstance) { + if ($this->doesDemoHasAllRequiredTokens($executionInstance)) { + return true; + } + $incomingWithToken = $this->incoming()->find(function (Connection $flow) use ($executionInstance) { return $flow->originState()->getTokens($executionInstance)->count() > 0; }); diff --git a/src/ProcessMaker/Nayra/Bpmn/ParallelOutputTransition.php b/src/ProcessMaker/Nayra/Bpmn/ParallelOutputTransition.php new file mode 100644 index 00000000..e9a9dbfd --- /dev/null +++ b/src/ProcessMaker/Nayra/Bpmn/ParallelOutputTransition.php @@ -0,0 +1,38 @@ +shouldDebugTriggerThisTransition($executionInstance)) { + return true; + } + // If debug mode is enabled, the transition is not triggered if it is not selected + if ($executionInstance && $this->shouldDebugSkipThisTransition($executionInstance)) { + return false; + } + + // By default the transition is triggered + return true; + } +}