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": "7b2d6a2b-dd65-4273-863e-dae494f5f48e",
    "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": {
        "fa65251d-cfb6-45d7-9c9b-1bcbd95a5e12": {
            "id": "fa65251d-cfb6-45d7-9c9b-1bcbd95a5e12",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "1ed886f2-66c7-4e7f-bb1c-f76c8c434612": {
            "id": "1ed886f2-66c7-4e7f-bb1c-f76c8c434612",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "9300925f-23a6-4e8e-95ac-0d19ef39fe54": {
            "id": "9300925f-23a6-4e8e-95ac-0d19ef39fe54",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "fa65251d-cfb6-45d7-9c9b-1bcbd95a5e12",
            "1ed886f2-66c7-4e7f-bb1c-f76c8c434612"
        ]
    },
    "inTransitions": {
        "send_email": [
            "fa65251d-cfb6-45d7-9c9b-1bcbd95a5e12"
        ],
        "send_sms": [
            "1ed886f2-66c7-4e7f-bb1c-f76c8c434612"
        ],
        "purchase_order": [
            "9300925f-23a6-4e8e-95ac-0d19ef39fe54"
        ]
    },
    "tokens": {
        "aba7d781-3a14-47bb-ada7-390db2f2f6bd": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "aba7d781-3a14-47bb-ada7-390db2f2f6bd",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "11343522-ce0f-463c-a6c7-fe8827516bc7",
                    "transitionId": "9300925f-23a6-4e8e-95ac-0d19ef39fe54",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508382147013
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ad242272-65d2-480b-83df-9322408c1163",
                    "transitionId": "9300925f-23a6-4e8e-95ac-0d19ef39fe54",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508382147019
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "340166b8-f5a6-4cdc-8136-86682e72f5d2",
                    "transitionId": "fa65251d-cfb6-45d7-9c9b-1bcbd95a5e12",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508382147022
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2f3a4c38-5aec-4bd4-87ed-49c4b7b0b34e",
                    "transitionId": "fa65251d-cfb6-45d7-9c9b-1bcbd95a5e12",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508382147029
                }
            ]
        },
        "cf1775b5-5b06-4cea-9177-ab8b4286c783": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "cf1775b5-5b06-4cea-9177-ab8b4286c783",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b6919074-a4b3-4f40-b5ad-0f24eea77a89",
                    "transitionId": "1ed886f2-66c7-4e7f-bb1c-f76c8c434612",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508382147035
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e71cb12b-34fb-4099-a686-58f491e3a993",
                    "transitionId": "1ed886f2-66c7-4e7f-bb1c-f76c8c434612",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508382147041
                }
            ]
        }
    }
}