Skip to content

Commit

Permalink
Implement Demo behavior for Exclusive Gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
caleeli committed Jun 26, 2024
1 parent 7781f2c commit 3c7d7aa
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/ProcessMaker/Nayra/Bpmn/ConditionedExclusiveTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class ConditionedExclusiveTransition implements TransitionInterface, ConditionedTransitionInterface
{
use TransitionTrait;
use ForceGatewayTransitionTrait;

/**
* @var callable
Expand All @@ -33,6 +34,15 @@ class ConditionedExclusiveTransition implements TransitionInterface, Conditioned
public function assertCondition(TokenInterface $token = null, ExecutionInstanceInterface $executionInstance = null)
{
try {
// 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;
}

$result = false;
$myIndex = $this->owner->getConditionedTransitions()->indexOf($this);
$condition = $this->condition;
Expand Down Expand Up @@ -60,7 +70,7 @@ public function assertCondition(TokenInterface $token = null, ExecutionInstanceI

return $result;
} catch (Throwable $exception) {
throw new RuntimeException($exception->getMessage(), $exception->getCode(), $exception, $this->owner);
throw new RuntimeException($exception->getMessage(), $exception->getCode(), null, $this->owner);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/ProcessMaker/Nayra/Bpmn/DefaultTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class DefaultTransition implements TransitionInterface
{
use TransitionTrait;
use ForceGatewayTransitionTrait;

/**
* Returns true if the condition of the transition is met with the DataStore of the passed execution instance
Expand All @@ -24,6 +25,15 @@ class DefaultTransition implements TransitionInterface
*/
public function assertCondition(TokenInterface $token = null, ExecutionInstanceInterface $executionInstance = null)
{
// If debug mode is enabled, the transition is triggered only if it is selected
if ($this->shouldDebugTriggerThisTransition($executionInstance)) {
return true;
}
// If debug mode is enabled, the transition is not triggered if it is not selected
if ($this->shouldDebugSkipThisTransition($executionInstance)) {
return false;
}

$executeDefaultTransition = true;
foreach ($this->owner->getConditionedTransitions() as $transition) {
if ($transition->assertCondition($token, $executionInstance)) {
Expand Down
4 changes: 3 additions & 1 deletion src/ProcessMaker/Nayra/Bpmn/ExclusiveGatewayTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class ExclusiveGatewayTransition implements TransitionInterface
{
use TransitionTrait;
use PauseOnGatewayTransitionTrait;

/**
* Initialize the tokens consumed property, the Exclusive Gateway consumes
Expand All @@ -34,7 +35,8 @@ protected function initExclusiveGatewayTransition()
*/
public function assertCondition(TokenInterface $token = null, ExecutionInstanceInterface $executionInstance = null)
{
return true;
// Execution is paused if Engine is in demo mode and the gateway choose is not selected
return !$this->shouldPauseGatewayTransition($executionInstance);
}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/ProcessMaker/Nayra/Bpmn/ForceGatewayTransitionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace ProcessMaker\Nayra\Bpmn;

use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;

/**
* Process base implementation.
*/
trait ForceGatewayTransitionTrait
{
use TransitionTrait;

/**
* Check if the transition should be triggered in debug mode.
*
* @param ExecutionInstanceInterface $executionInstance
*
* @return bool
*/
function shouldDebugTriggerThisTransition(ExecutionInstanceInterface $executionInstance)
{
$engine = $executionInstance->getEngine();
$demoMode = $engine->isDemoMode();
$gateway = $this->getOwner();
$connection = $this->outgoing()->item(0);
$targetEntrypoint = $connection->target()->getOwner();
return $demoMode && $engine->getSelectedDemoFlow($gateway)->getTarget() === $targetEntrypoint;
}

/**
* Check if the transition should be skipped in debug mode.
*
* @param ExecutionInstanceInterface $executionInstance
*
* @return bool
*/
function shouldDebugSkipThisTransition(ExecutionInstanceInterface $executionInstance)
{
$engine = $executionInstance->getEngine();
$demoMode = $engine->isDemoMode();
$gateway = $this->getOwner();
$connection = $this->outgoing()->item(0);
$targetEntrypoint = $connection->target()->getOwner();
return $demoMode && $engine->getSelectedDemoFlow($gateway)->getTarget() !== $targetEntrypoint;
}
}
21 changes: 21 additions & 0 deletions src/ProcessMaker/Nayra/Bpmn/PauseOnGatewayTransitionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace ProcessMaker\Nayra\Bpmn;

use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;

/**
* Process base implementation.
*/
trait PauseOnGatewayTransitionTrait
{
use TransitionTrait;

function shouldPauseGatewayTransition(ExecutionInstanceInterface $executionInstance)
{
$engine = $executionInstance->getEngine();
$demoMode = $engine->isDemoMode();
$gateway = $this->getOwner();
return $demoMode && !$engine->getSelectedDemoFlow($gateway);
}
}
70 changes: 70 additions & 0 deletions src/ProcessMaker/Nayra/Engine/DemoModeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace ProcessMaker\Nayra\Engine;

use ProcessMaker\Nayra\Contracts\Bpmn\FlowInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\GatewayInterface;

/**
* Implements the engine behavior for debugging.
*/
trait DemoModeTrait
{
/**
* Is debug mode enabled
*
* @var bool
*/
private $demoMode = false;

/**
* Flow selected by the user in demo mode.
* @var FlowInterface[]
*/
private $selectedFlows = [];

/**
* Returns true if the engine is in demo mode.
*
* @return bool
*/
public function isDemoMode()
{
return $this->demoMode;
}

/**
* Set if the engine is in demo mode.
*
* @param bool $value
*/
public function setDemoMode(bool $value)
{
$this->demoMode = $value;
}

/**
* Retrieves the selected flow by the user in demo mode.
*
* @param GatewayInterface $gateway
*
* @return FlowInterface|null
*/
public function getSelectedDemoFlow(GatewayInterface $gateway)
{
return $this->selectedFlow[$gateway->getId()] ?? null;
}

/**
* Set the selected flow by the user in demo mode.
*
* @param GatewayInterface $gateway
* @param bool $value
*/
public function setSelectedDemoFlow(
GatewayInterface $gateway,
FlowInterface $selectedFlow = null
) {
$this->selectedFlow[$gateway->getId()] = $selectedFlow;
}
}
2 changes: 2 additions & 0 deletions src/ProcessMaker/Nayra/Engine/EngineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
trait EngineTrait
{
use DemoModeTrait;

/**
* Instances of process.
*
Expand Down
10 changes: 10 additions & 0 deletions src/ProcessMaker/Nayra/Engine/ExecutionInstanceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,14 @@ public function getTokens()
{
return $this->tokens;
}

/**
* Get the engine.
*
* @return EngineInterface
*/
public function getEngine()
{
return $this->getProcess()->getEngine();
}
}

0 comments on commit 3c7d7aa

Please sign in to comment.