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": "2e288ae0-4a7e-4358-bc5e-cdaa2369c449",
    "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": {
        "94229bec-0ee5-4c44-a544-6d42ab1c6b61": {
            "id": "94229bec-0ee5-4c44-a544-6d42ab1c6b61",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "ab23c6f9-6dc2-40d9-8a05-28df198648cd": {
            "id": "ab23c6f9-6dc2-40d9-8a05-28df198648cd",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "e4592237-56a7-436a-9aae-136189c5d0f0": {
            "id": "e4592237-56a7-436a-9aae-136189c5d0f0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "94229bec-0ee5-4c44-a544-6d42ab1c6b61",
            "ab23c6f9-6dc2-40d9-8a05-28df198648cd"
        ]
    },
    "inTransitions": {
        "send_email": [
            "94229bec-0ee5-4c44-a544-6d42ab1c6b61"
        ],
        "send_sms": [
            "ab23c6f9-6dc2-40d9-8a05-28df198648cd"
        ],
        "purchase_order": [
            "e4592237-56a7-436a-9aae-136189c5d0f0"
        ]
    },
    "tokens": {
        "65215f71-ff70-41cf-98fa-79ca52585aa4": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "65215f71-ff70-41cf-98fa-79ca52585aa4",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "96703e3c-7621-482b-8d18-d64117d39aef",
                    "transitionId": "e4592237-56a7-436a-9aae-136189c5d0f0",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421477174164
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a35a8891-8707-4143-9a7e-9ff534f610f4",
                    "transitionId": "e4592237-56a7-436a-9aae-136189c5d0f0",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421477174173
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ce67a665-ae4e-4794-a904-309eb0561efb",
                    "transitionId": "94229bec-0ee5-4c44-a544-6d42ab1c6b61",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421477174178
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6fe4eb44-024c-4651-94c6-8c026393445f",
                    "transitionId": "94229bec-0ee5-4c44-a544-6d42ab1c6b61",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421477174185
                }
            ]
        },
        "38024d8d-35c8-4b65-86f8-54af3dd4712f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "38024d8d-35c8-4b65-86f8-54af3dd4712f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5d0e9b23-b62c-402c-abdc-c4897d28b4f4",
                    "transitionId": "ab23c6f9-6dc2-40d9-8a05-28df198648cd",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421477174195
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "153c0130-332b-41dd-8a79-57028f9f2f74",
                    "transitionId": "ab23c6f9-6dc2-40d9-8a05-28df198648cd",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421477174203
                }
            ]
        }
    }
}