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": "181d2a8d-e8b0-4924-b7f7-3213783588fd",
    "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": {
        "b5e7436d-c991-415e-8c00-5facabfcd906": {
            "id": "b5e7436d-c991-415e-8c00-5facabfcd906",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "3c8462d2-4f11-4565-bdf9-99a4a2dce18f": {
            "id": "3c8462d2-4f11-4565-bdf9-99a4a2dce18f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "9a5d43bf-b92f-4103-b5c6-1f8b01b11797": {
            "id": "9a5d43bf-b92f-4103-b5c6-1f8b01b11797",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "b5e7436d-c991-415e-8c00-5facabfcd906",
            "3c8462d2-4f11-4565-bdf9-99a4a2dce18f"
        ]
    },
    "inTransitions": {
        "send_email": [
            "b5e7436d-c991-415e-8c00-5facabfcd906"
        ],
        "send_sms": [
            "3c8462d2-4f11-4565-bdf9-99a4a2dce18f"
        ],
        "purchase_order": [
            "9a5d43bf-b92f-4103-b5c6-1f8b01b11797"
        ]
    },
    "tokens": {
        "0b1d9b86-84ff-4bef-9aa7-d3172dbda7f9": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0b1d9b86-84ff-4bef-9aa7-d3172dbda7f9",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e556ff7b-3750-4a20-bdf8-11dda1ab2860",
                    "transitionId": "9a5d43bf-b92f-4103-b5c6-1f8b01b11797",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450745873560
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "951cbe41-c8d1-4922-ad90-2a76f8cf860f",
                    "transitionId": "9a5d43bf-b92f-4103-b5c6-1f8b01b11797",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450745873564
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c02ce484-8e51-4b08-b0e4-b9e2f4bf43c7",
                    "transitionId": "b5e7436d-c991-415e-8c00-5facabfcd906",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450745873567
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ba590a4e-a994-4e1a-a0ab-7abf1bb1cbbf",
                    "transitionId": "b5e7436d-c991-415e-8c00-5facabfcd906",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450745873571
                }
            ]
        },
        "7f3e1ac5-09cf-4220-8d5b-dc2a0069344d": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7f3e1ac5-09cf-4220-8d5b-dc2a0069344d",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "80b9be42-79e5-4c50-9e07-da125e2bde62",
                    "transitionId": "3c8462d2-4f11-4565-bdf9-99a4a2dce18f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450745873574
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "df293a7b-62b3-404d-af35-133f033b8e1d",
                    "transitionId": "3c8462d2-4f11-4565-bdf9-99a4a2dce18f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450745873578
                }
            ]
        }
    }
}