Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ASKozienko committed Jan 4, 2017
0 parents commit eb1d64d
Show file tree
Hide file tree
Showing 19 changed files with 739 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
.idea/
28 changes: 28 additions & 0 deletions composer.json
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"
}
}
}
87 changes: 87 additions & 0 deletions examples/fork-join.php
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));

7 changes: 7 additions & 0 deletions src/AsyncTransition.php
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);
}
13 changes: 13 additions & 0 deletions src/Behavior.php
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);
}
12 changes: 12 additions & 0 deletions src/BehaviorRegistry.php
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;
}
26 changes: 26 additions & 0 deletions src/CallbackBehavior.php
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);
}
}
31 changes: 31 additions & 0 deletions src/DefaultBehaviorRegistry.php
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];
}
}
11 changes: 11 additions & 0 deletions src/EchoBehavior.php
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;
}
}
7 changes: 7 additions & 0 deletions src/Exception/InterruptExecutionException.php
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
{

}
6 changes: 6 additions & 0 deletions src/Exception/PvmException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Formapro\Pvm\Exception;

interface PvmException
{
}
11 changes: 11 additions & 0 deletions src/MongoProcessStorage.php
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.
}
}
51 changes: 51 additions & 0 deletions src/Node.php
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);
}
}
Loading

0 comments on commit eb1d64d

Please sign in to comment.