Skip to content

Commit

Permalink
Merge pull request #164 from ProcessMaker/feature/163
Browse files Browse the repository at this point in the history
Modify engine to work with multiple different BPMN documents at the same time. Resolves #163
  • Loading branch information
danloa authored Feb 4, 2020
2 parents 5804d26 + 138f864 commit 75c2cb4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 56 deletions.
32 changes: 31 additions & 1 deletion src/ProcessMaker/Nayra/Bpmn/BaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ trait BaseTrait
/**
* Factory used to build this element.
*
* @var StorageInterface $factory
* @var RepositoryInterface $factory
*/
private $repository;

/**
* BPMN document of this object.
*
* @var StorageInterface $ownerDocument
*/
private $ownerDocument;

/**
* BaseTrait constructor.
*
Expand Down Expand Up @@ -72,6 +79,29 @@ public function setRepository(RepositoryInterface $repository)
return $this;
}

/**
* Get the owner BPMN document of this object.
*
* @return \ProcessMaker\Nayra\Contracts\StorageInterface
*/
public function getOwnerDocument()
{
return $this->ownerDocument;
}

/**
* Set the owner BPMN document of this object.
*
* @param \ProcessMaker\Nayra\Contracts\Repositories\StorageInterface $ownerDocument
*
* @return $this
*/
public function setOwnerDocument(StorageInterface $ownerDocument)
{
$this->ownerDocument = $ownerDocument;
return $this;
}

/**
* Get properties.
*
Expand Down
15 changes: 14 additions & 1 deletion src/ProcessMaker/Nayra/Contracts/Bpmn/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProcessMaker\Nayra\Contracts\Bpmn;

use ProcessMaker\Nayra\Contracts\Repositories\StorageInterface;
use ProcessMaker\Nayra\Contracts\RepositoryInterface;

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ public function getProperty($name, $default = null);
public function addProperty($name, $value);

/**
* @return \ProcessMaker\Nayra\Contracts\Repositories\StorageInterface
* @return \ProcessMaker\Nayra\Contracts\RepositoryInterface
*/
public function getRepository();

Expand Down Expand Up @@ -98,4 +99,16 @@ public function getName();
* @return $this
*/
public function setName($name);

/**
* @return \ProcessMaker\Nayra\Contracts\Repositories\StorageInterface
*/
public function getOwnerDocument();

/**
* @param StorageInterface $ownerDocument
*
* @return $this
*/
public function setOwnerDocument(StorageInterface $ownerDocument);
}
18 changes: 2 additions & 16 deletions src/ProcessMaker/Nayra/Contracts/Engine/EngineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function createExecutionInstance(ProcessInterface $process, DataStoreInte
* Load an execution instance from the storage.
*
* @param string $id
* @param DataStoreInterface $data
* @param StorageInterface $storage
*
* @return ExecutionInstanceInterface
*/
public function loadExecutionInstance($id);
public function loadExecutionInstance($id, StorageInterface $storage);

/**
* Close all the execution instances.
Expand All @@ -115,13 +115,6 @@ public function getDataStore();
*/
public function setDataStore(DataStoreInterface $dataStore);

/**
* Set the repository storage of the engine.
*
* @param StorageInterface $repository
*/
public function setStorage(StorageInterface $repository);

/**
* Get the engine job manager for timer tasks and events.
*
Expand Down Expand Up @@ -171,11 +164,4 @@ public function loadBpmnDocument(BpmnDocumentInterface $document);
* @return EngineInterface
*/
public function loadCollaboration(CollaborationInterface $collaboration);

/**
* Get the repository storage of the engine.
*
* @return StorageInterface
*/
public function getStorage();
}
28 changes: 3 additions & 25 deletions src/ProcessMaker/Nayra/Engine/EngineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ trait EngineTrait
*/
private $dataStore;

private $storage;

protected $jobManager;

/**
Expand Down Expand Up @@ -149,11 +147,11 @@ public function createExecutionInstance(ProcessInterface $process, DataStoreInte
* Load an execution instance from the storage.
*
* @param string $id
* @param DataStoreInterface $data
* @param StorageInterface $storage
*
* @return ExecutionInstanceInterface|null
*/
public function loadExecutionInstance($id)
public function loadExecutionInstance($id, StorageInterface $storage)
{
// If exists return the already loaded instance by id
foreach($this->executionInstances as $executionInstance) {
Expand All @@ -163,7 +161,7 @@ public function loadExecutionInstance($id)
}
// Create and load an instance by id
$repository = $this->getRepository()->createExecutionInstanceRepository();
$executionInstance = $repository->loadExecutionInstanceByUid($id, $this->getStorage());
$executionInstance = $repository->loadExecutionInstanceByUid($id, $storage);
if (!$executionInstance) {
return;
}
Expand Down Expand Up @@ -279,26 +277,6 @@ private function registerCatchEvents(ProcessInterface $process)
}
}

/**
* Get the repository storage of the engine.
*
* @return StorageInterface
*/
public function getStorage()
{
return $this->storage;
}

/**
* Set the repository storage of the engine.
*
* @param StorageInterface $storage
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;
}

/**
* Get the engine job manager for timer tasks and events.
*
Expand Down
1 change: 1 addition & 0 deletions src/ProcessMaker/Nayra/Storage/BpmnElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function getBpmnElementInstance($owner = null)
}
} else {
$bpmnElement = $this->ownerDocument->getFactory()->create($classInterface);
$bpmnElement->setOwnerDocument($this->ownerDocument);
$id ? $this->ownerDocument->indexBpmnElement($id, $bpmnElement) : null;
$bpmnElement->setRepository($this->ownerDocument->getFactory());
if ($bpmnElement instanceof CallableElementInterface) {
Expand Down
20 changes: 7 additions & 13 deletions tests/Feature/Engine/LoadExecutionInstancesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@ public function testLoadExecutionInstanceWithOneToken()
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$this->engine->setRepository($this->repository);
$this->engine->setStorage($bpmnRepository);
$bpmnRepository->load(__DIR__ . '/files/LoadTokens.bpmn');

//Set test data to load the sequential process
$this->prepareSequentialProcess($bpmnRepository);

//Load the execution instance
$instance = $this->engine->loadExecutionInstance('executionInstanceId');
$instance = $this->engine->loadExecutionInstance('executionInstanceId', $bpmnRepository);

//Get References by id
$secondActivity = $bpmnRepository->getScriptTask('second');
Expand Down Expand Up @@ -166,14 +165,13 @@ public function testLoadExecutionInstanceWithMultipleTokens()
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$this->engine->setRepository($this->repository);
$this->engine->setStorage($bpmnRepository);
$bpmnRepository->load(__DIR__ . '/files/LoadTokens.bpmn');

//Set test data to load the sequential process
$this->prepareParallelProcess($bpmnRepository);

//Load the execution instance
$instance = $this->engine->loadExecutionInstance('otherExecutionInstanceId');
$instance = $this->engine->loadExecutionInstance('otherExecutionInstanceId', $bpmnRepository);

//Get References by id
$secondActivity = $bpmnRepository->getScriptTask('task2');
Expand Down Expand Up @@ -220,14 +218,13 @@ public function testLoadExecutionInstanceWithMultipleTokensStates()
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$this->engine->setRepository($this->repository);
$this->engine->setStorage($bpmnRepository);
$bpmnRepository->load(__DIR__ . '/files/LoadTokens.bpmn');

//Set test data to load the sequential process
$this->prepareParallelProcessWithActivityCompleted($bpmnRepository);

//Load the execution instance
$instance = $this->engine->loadExecutionInstance('otherExecutionInstanceId');
$instance = $this->engine->loadExecutionInstance('otherExecutionInstanceId', $bpmnRepository);

//Get References by id
$secondActivity = $bpmnRepository->getScriptTask('task2');
Expand Down Expand Up @@ -267,14 +264,13 @@ public function testLoadExecutionInstanceWithMultipleTokensFallingState()
$bpmnRepository = new BpmnDocument();
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$this->engine->setStorage($bpmnRepository);
$bpmnRepository->load(__DIR__ . '/files/LoadTokens.bpmn');

//Set test data to load the sequential process
$this->prepareParallelProcessWithException($bpmnRepository);

//Load the execution instance
$instance = $this->engine->loadExecutionInstance('otherExecutionInstanceId');
$instance = $this->engine->loadExecutionInstance('otherExecutionInstanceId', $bpmnRepository);

//Get References by id
$thirdActivity = $bpmnRepository->getActivity('task3');
Expand All @@ -294,11 +290,10 @@ public function testLoadNonExistingInstance()
$bpmnRepository = new BpmnDocument();
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$this->engine->setStorage($bpmnRepository);
$bpmnRepository->load(__DIR__ . '/files/LoadTokens.bpmn');

//Load the execution instance
$instance = $this->engine->loadExecutionInstance('nonExistingInstance');
$instance = $this->engine->loadExecutionInstance('nonExistingInstance', $bpmnRepository);

//Assertion: The returned value must be null
$this->assertNull($instance);
Expand All @@ -315,15 +310,14 @@ public function testLoadTheSameExistingInstanceTwice()
$bpmnRepository->setEngine($this->engine);
$bpmnRepository->setFactory($this->repository);
$this->engine->setRepository($this->repository);
$this->engine->setStorage($bpmnRepository);
$bpmnRepository->load(__DIR__ . '/files/LoadTokens.bpmn');

//Set test data to load the sequential process
$this->prepareSequentialProcess($bpmnRepository);

//Load the execution instance twice
$instance1 = $this->engine->loadExecutionInstance('executionInstanceId');
$instance2 = $this->engine->loadExecutionInstance('executionInstanceId');
$instance1 = $this->engine->loadExecutionInstance('executionInstanceId', $bpmnRepository);
$instance2 = $this->engine->loadExecutionInstance('executionInstanceId', $bpmnRepository);

//Assertion: Both variables point to the same instance
$this->assertEquals($instance1, $instance2);
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/Engine/LoadFromBPMNFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function testParallelGateway()
$activityB = $bpmnRepository->getScriptTask('ScriptTask_2');
$endActivity = $bpmnRepository->getScriptTask('end');

//Assertion: Check reference to BpmnDocument
$this->assertEquals($bpmnRepository, $start->getOwnerDocument());
$this->assertEquals($bpmnRepository, $startActivity->getOwnerDocument());
$this->assertEquals($bpmnRepository, $activityA->getOwnerDocument());
$this->assertEquals($bpmnRepository, $activityB->getOwnerDocument());
$this->assertEquals($bpmnRepository, $endActivity->getOwnerDocument());

//Start the process
$start->start($instance);
$this->engine->runToNextState();
Expand Down

0 comments on commit 75c2cb4

Please sign in to comment.