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": "71c3233f-4270-43d0-86a7-32c195966f0e",
    "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": {
        "cbe0d8de-bcfd-4323-a94b-49e536023eff": {
            "id": "cbe0d8de-bcfd-4323-a94b-49e536023eff",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "347eb0b4-751f-4aea-9ba0-0d6b29fd0fc4": {
            "id": "347eb0b4-751f-4aea-9ba0-0d6b29fd0fc4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "b700f606-3ff7-4358-9628-9d8cae3b7588": {
            "id": "b700f606-3ff7-4358-9628-9d8cae3b7588",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "cbe0d8de-bcfd-4323-a94b-49e536023eff",
            "347eb0b4-751f-4aea-9ba0-0d6b29fd0fc4"
        ]
    },
    "inTransitions": {
        "send_email": [
            "cbe0d8de-bcfd-4323-a94b-49e536023eff"
        ],
        "send_sms": [
            "347eb0b4-751f-4aea-9ba0-0d6b29fd0fc4"
        ],
        "purchase_order": [
            "b700f606-3ff7-4358-9628-9d8cae3b7588"
        ]
    },
    "tokens": {
        "a3987320-a341-4750-ac3f-99c7c56d6b56": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a3987320-a341-4750-ac3f-99c7c56d6b56",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c7373372-192d-4e01-9602-9cd5b3cab615",
                    "transitionId": "b700f606-3ff7-4358-9628-9d8cae3b7588",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664807644120
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "57581db3-5996-4642-9d5b-27c831246c4a",
                    "transitionId": "b700f606-3ff7-4358-9628-9d8cae3b7588",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664807644124
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4249bb24-22ed-44da-8595-9ebc33014025",
                    "transitionId": "cbe0d8de-bcfd-4323-a94b-49e536023eff",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664807644127
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c370e2e8-7760-41fc-944b-0c8d19b0e2c8",
                    "transitionId": "cbe0d8de-bcfd-4323-a94b-49e536023eff",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664807644131
                }
            ]
        },
        "b5bea919-a439-4c31-9370-e5ea9ee2e626": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "b5bea919-a439-4c31-9370-e5ea9ee2e626",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "912c2a9d-aeec-450d-bf1f-b85027088ea8",
                    "transitionId": "347eb0b4-751f-4aea-9ba0-0d6b29fd0fc4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664807644135
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1754bf7a-9af2-4abb-aeba-a196c21582b6",
                    "transitionId": "347eb0b4-751f-4aea-9ba0-0d6b29fd0fc4",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664807644143
                }
            ]
        }
    }
}