Skip to content

Commit

Permalink
Implement demo mode for Parallel gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
caleeli committed Jun 28, 2024
1 parent 7a7a5bc commit 88c1557
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
30 changes: 30 additions & 0 deletions src/ProcessMaker/Nayra/Bpmn/ForceGatewayTransitionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProcessMaker\Nayra\Bpmn;

use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;
use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;

/**
Expand Down Expand Up @@ -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;
});
});
}
}
2 changes: 1 addition & 1 deletion src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 6 additions & 11 deletions src/ProcessMaker/Nayra/Bpmn/ParallelGatewayTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +24,7 @@ protected function initParallelGatewayTransition()
*/
public function assertCondition(TokenInterface $token = null, ExecutionInstanceInterface $executionInstance = null)
{
return true;
return !$this->shouldPauseGatewayTransition($executionInstance);
}

/**
Expand All @@ -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;
});
Expand Down
38 changes: 38 additions & 0 deletions src/ProcessMaker/Nayra/Bpmn/ParallelOutputTransition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ProcessMaker\Nayra\Bpmn;

use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\TransitionInterface;
use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;

/**
* Transition rule that always pass the token.
*/
class ParallelOutputTransition extends Transition implements TransitionInterface
{
use ForceGatewayTransitionTrait;

/**
* Condition required to transit the element.
*
* @param \ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface|null $token
* @param \ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface|null $executionInstance
*
* @return bool
*/
public function assertCondition(TokenInterface $token = null, ExecutionInstanceInterface $executionInstance = null)
{
// If debug mode is enabled, the transition is triggered only if it is selected
if ($executionInstance && $this->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;
}
}

0 comments on commit 88c1557

Please sign in to comment.