Fork

The example shows how two tasks could be executed in parallel.

Internally, by default the engine still executes tasks one by one because of the nature of PHP. The tasks could be done in parallel for real if an async transition implementation is provided. A solution could use message queue (MQ) or process forking in background.

Setup


mkdir ~/pvm-fork
cd ~/pvm-fork
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php fork.php

Result

fork.php

purchase_order
send_email
send_sms

fork.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;
use 
Formapro\Pvm\ProcessBuilder;

require_once 
__DIR__.'/vendor/autoload.php';

$process = (new ProcessBuilder())
    ->
createNode('purchase_order''print_label')->end()
    ->
createNode('send_email''print_label')->end()
    ->
createNode('send_sms''print_label')->end()

    ->
createTransition('purchase_order''send_email')->end()
    ->
createTransition('purchase_order''send_sms')->end()
    ->
createStartTransition('purchase_order')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'print_label' => function(Token $token) {
        echo 
$token->getTo()->getId().PHP_EOL;
    },
]));

$token $engine->createTokenFor($process->getStartTransition());
$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "02838dcd-9dad-42f2-af7a-98d482795318",
    "nodes": {
        "purchase_order": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "purchase_order",
            "behavior": "print_label"
        },
        "send_email": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "send_email",
            "behavior": "print_label"
        },
        "send_sms": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "send_sms",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "1194545e-6f06-4da3-8a80-cb00296d0680": {
            "id": "1194545e-6f06-4da3-8a80-cb00296d0680",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "d757f593-2df4-4fdd-9c40-f95ed74d157c": {
            "id": "d757f593-2df4-4fdd-9c40-f95ed74d157c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "de1b9e30-9d75-4f22-9d25-a431d8f214ef": {
            "id": "de1b9e30-9d75-4f22-9d25-a431d8f214ef",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "1194545e-6f06-4da3-8a80-cb00296d0680",
            "d757f593-2df4-4fdd-9c40-f95ed74d157c"
        ]
    },
    "inTransitions": {
        "send_email": [
            "1194545e-6f06-4da3-8a80-cb00296d0680"
        ],
        "send_sms": [
            "d757f593-2df4-4fdd-9c40-f95ed74d157c"
        ],
        "purchase_order": [
            "de1b9e30-9d75-4f22-9d25-a431d8f214ef"
        ]
    },
    "tokens": {
        "7b6d8fc9-5c89-4741-a039-92a7591b1179": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7b6d8fc9-5c89-4741-a039-92a7591b1179",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c2752ca4-fe62-418e-a75b-d9f7c98c63c2",
                    "transitionId": "de1b9e30-9d75-4f22-9d25-a431d8f214ef",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906504915063
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "750c38eb-1ab2-424a-ac74-d34b4f535821",
                    "transitionId": "de1b9e30-9d75-4f22-9d25-a431d8f214ef",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906504915072
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b81a6ab7-01b4-479a-90b2-27551d9e0faf",
                    "transitionId": "1194545e-6f06-4da3-8a80-cb00296d0680",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906504915077
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "10839d11-79dc-4aa1-8df8-f761ee87ac6e",
                    "transitionId": "1194545e-6f06-4da3-8a80-cb00296d0680",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906504915085
                }
            ]
        },
        "cc699cd1-8b95-4ac3-82de-7e88901bc4ac": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "cc699cd1-8b95-4ac3-82de-7e88901bc4ac",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ba6d2e44-5443-4a25-a0cd-1644eca2dfb6",
                    "transitionId": "d757f593-2df4-4fdd-9c40-f95ed74d157c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906504915092
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f18c93ea-7047-48e2-9ce8-d15347976545",
                    "transitionId": "d757f593-2df4-4fdd-9c40-f95ed74d157c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906504915100
                }
            ]
        }
    }
}