-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eb1d64d
Showing
19 changed files
with
739 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "formapro/pvm", | ||
"license": "MIT", | ||
"type": "lib", | ||
"description": "FormaPro PVM", | ||
"autoload": { | ||
"psr-4": { "Formapro\\Pvm\\": "src/" } | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { "Tests\\": "tests/" } | ||
}, | ||
"require": { | ||
"php": ">=7.0.0", | ||
"makasim/values": "@dev", | ||
"makasim/yadm": "@dev", | ||
"mongodb/mongodb": "^1" | ||
}, | ||
"config": { | ||
"platform": { | ||
"php": "7.0.0" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "0.1.x-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
use Formapro\Pvm\CallbackBehavior; | ||
use Formapro\Pvm\DefaultBehaviorRegistry; | ||
use Formapro\Pvm\EchoBehavior; | ||
use Formapro\Pvm\Exception\InterruptExecutionException; | ||
use Formapro\Pvm\MongoProcessStorage; | ||
use Formapro\Pvm\ProcessEngine; | ||
use Formapro\Pvm\Token; | ||
use Formapro\Pvm\Node; | ||
use Formapro\Pvm\Process; | ||
use Formapro\Pvm\Transition; | ||
|
||
include_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
$process = new Process(); | ||
|
||
$fork = new Node(); | ||
$fork->setId('1'); | ||
$fork->setBehavior('fork'); | ||
|
||
$task1 = new Node(); | ||
$task1->setId('2'); | ||
$task1->setBehavior('echo'); | ||
$task1->setOption('text', 'task 1'); | ||
|
||
$task2 = new Node(); | ||
$task2->setId('3'); | ||
$task2->setBehavior('echo'); | ||
$task2->setOption('text', 'task 2'); | ||
|
||
$task3 = new Node(); | ||
$task3->setId('4'); | ||
$task3->setBehavior('echo'); | ||
$task3->setOption('text', 'task 3'); | ||
|
||
$join = new Node(); | ||
$join->setId('5'); | ||
$join->setBehavior('join'); | ||
|
||
$task4 = new Node(); | ||
$task4->setId('6'); | ||
$task4->setBehavior('echo'); | ||
$task4->setOption('text', 'task 4'); | ||
|
||
$process->addNode($fork); | ||
$process->addNode($task1); | ||
$process->addNode($task2); | ||
$process->addNode($task3); | ||
$process->addNode($task4); | ||
$process->addNode($join); | ||
$process->addTransition($start = new Transition(null, $fork)); | ||
$process->addTransition(new Transition($fork, $task1)); | ||
$process->addTransition(new Transition($fork, $task2)); | ||
$process->addTransition(new Transition($fork, $task3)); | ||
$process->addTransition(new Transition($task1, $join)); | ||
$process->addTransition(new Transition($task2, $join)); | ||
$process->addTransition(new Transition($task3, $join)); | ||
$process->addTransition(new Transition($join, $task4)); | ||
|
||
$behaviorRegistry = new DefaultBehaviorRegistry(); | ||
$behaviorRegistry->register('echo', new EchoBehavior()); | ||
$behaviorRegistry->register('fork', new CallbackBehavior(function (Token $token) { | ||
$transitions = $token->getProcess()->getOutTransitionsForNode($token->getTransition()->getTo()); | ||
|
||
$transitions[0]->setWeight(1); | ||
$transitions[1]->setWeight(1); | ||
$transitions[2]->setWeight(1); | ||
|
||
return $transitions; | ||
})); | ||
$behaviorRegistry->register('join', new CallbackBehavior(function (Token $token) { | ||
static $weight = 0; | ||
$weight += $token->getTransition()->getWeight(); | ||
|
||
if ($weight === 3) { | ||
return; | ||
} | ||
|
||
throw new InterruptExecutionException(); | ||
})); | ||
|
||
$processStorage = new MongoProcessStorage(); | ||
|
||
$engine = new ProcessEngine($behaviorRegistry, $processStorage); | ||
$engine->proceed($process->createToken($start)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
interface AsyncTransition | ||
{ | ||
public function transition(array $tokens); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
|
||
interface Behavior | ||
{ | ||
/** | ||
* @param Token $token | ||
* | ||
* @return Transition[] | ||
*/ | ||
public function execute(Token $token); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
interface BehaviorRegistry | ||
{ | ||
/** | ||
* @param string $name | ||
* | ||
* @return Behavior | ||
*/ | ||
public function get(string $name) :Behavior; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
class CallbackBehavior implements Behavior | ||
{ | ||
/** | ||
* @var \Closure | ||
*/ | ||
private $callback; | ||
|
||
/** | ||
* @param \Closure $callback | ||
*/ | ||
public function __construct(\Closure $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute(Token $token) | ||
{ | ||
return call_user_func($this->callback, $token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
class DefaultBehaviorRegistry implements BehaviorRegistry | ||
{ | ||
/** | ||
* @var Behavior[] | ||
*/ | ||
private $behaviors; | ||
|
||
/** | ||
* @param string $name | ||
* @param Behavior $behavior | ||
*/ | ||
public function register(string $name, Behavior $behavior) | ||
{ | ||
$this->behaviors[$name] = $behavior; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $name) :Behavior | ||
{ | ||
if (false == isset($this->behaviors[$name])) { | ||
throw new \LogicException(sprintf('Behavior is not registered with name: "%s"', $name)); | ||
} | ||
|
||
return $this->behaviors[$name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
|
||
class EchoBehavior implements Behavior | ||
{ | ||
public function execute(Token $token) | ||
{ | ||
echo $token->getTransition()->getTo()->getOption('text') . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
namespace Formapro\Pvm\Exception; | ||
|
||
class InterruptExecutionException extends \RuntimeException implements PvmException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
namespace Formapro\Pvm\Exception; | ||
|
||
interface PvmException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
|
||
class MongoProcessStorage implements ProcessStorage | ||
{ | ||
public function persist(Process $process) | ||
{ | ||
// TODO: Implement persist() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace Formapro\Pvm; | ||
|
||
use Makasim\Values\ValuesTrait; | ||
|
||
class Node | ||
{ | ||
use ValuesTrait; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->getSelfValue('id'); | ||
} | ||
|
||
/** | ||
* @param mixed $id | ||
*/ | ||
public function setId($id) | ||
{ | ||
$this->setSelfValue('id', $id); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getBehavior() | ||
{ | ||
return $this->getSelfValue('behavior'); | ||
} | ||
|
||
/** | ||
* @param string $behavior | ||
*/ | ||
public function setBehavior(string $behavior) | ||
{ | ||
$this->setSelfValue('behavior', $behavior); | ||
} | ||
|
||
public function setOption($key, $value) | ||
{ | ||
$this->setValue('option', $key, $value); | ||
} | ||
|
||
public function getOption($key) | ||
{ | ||
return $this->getValue('option', $key); | ||
} | ||
} |
Oops, something went wrong.