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": "e7f2f5d1-3af4-4116-979d-ee29475b391f",
    "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": {
        "aeeef317-51ad-40b1-9991-38fb18788aee": {
            "id": "aeeef317-51ad-40b1-9991-38fb18788aee",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "133eccbe-d3c2-4a5a-8e56-093b6f964e8d": {
            "id": "133eccbe-d3c2-4a5a-8e56-093b6f964e8d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "5ae432ba-420f-4023-89ce-cff712e38197": {
            "id": "5ae432ba-420f-4023-89ce-cff712e38197",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "aeeef317-51ad-40b1-9991-38fb18788aee",
            "133eccbe-d3c2-4a5a-8e56-093b6f964e8d"
        ]
    },
    "inTransitions": {
        "send_email": [
            "aeeef317-51ad-40b1-9991-38fb18788aee"
        ],
        "send_sms": [
            "133eccbe-d3c2-4a5a-8e56-093b6f964e8d"
        ],
        "purchase_order": [
            "5ae432ba-420f-4023-89ce-cff712e38197"
        ]
    },
    "tokens": {
        "fa160c15-04c5-4c3e-9402-dbbc443392f1": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "fa160c15-04c5-4c3e-9402-dbbc443392f1",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0b591941-f504-4d31-9be8-dd82c85a6a42",
                    "transitionId": "5ae432ba-420f-4023-89ce-cff712e38197",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606680275169
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6fba280b-d1c0-4dff-bc7c-25c413547c83",
                    "transitionId": "5ae432ba-420f-4023-89ce-cff712e38197",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606680275174
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "003e900a-c15d-402a-aa7c-83247a7c81ad",
                    "transitionId": "aeeef317-51ad-40b1-9991-38fb18788aee",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606680275177
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "21057800-f7eb-445a-a2b2-cdda31211ec2",
                    "transitionId": "aeeef317-51ad-40b1-9991-38fb18788aee",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606680275182
                }
            ]
        },
        "00323fc9-13c7-4431-9edf-c38e08e20c7c": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "00323fc9-13c7-4431-9edf-c38e08e20c7c",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3f76328c-6f3d-4bc2-92b1-34046c66e9d9",
                    "transitionId": "133eccbe-d3c2-4a5a-8e56-093b6f964e8d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606680275190
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b6d49407-80e2-4f7c-9fad-08bd1194451d",
                    "transitionId": "133eccbe-d3c2-4a5a-8e56-093b6f964e8d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606680275195
                }
            ]
        }
    }
}