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": "d71228b2-dc88-44f9-9305-6abc2305fba4",
    "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": {
        "c67c4cce-150d-42f5-b1b6-609d9780e61d": {
            "id": "c67c4cce-150d-42f5-b1b6-609d9780e61d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "43e2ea14-6a13-411c-8db0-aa341f815a41": {
            "id": "43e2ea14-6a13-411c-8db0-aa341f815a41",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "057fed26-ade5-4e63-8b40-e4bfc2bbbf85": {
            "id": "057fed26-ade5-4e63-8b40-e4bfc2bbbf85",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "c67c4cce-150d-42f5-b1b6-609d9780e61d",
            "43e2ea14-6a13-411c-8db0-aa341f815a41"
        ]
    },
    "inTransitions": {
        "send_email": [
            "c67c4cce-150d-42f5-b1b6-609d9780e61d"
        ],
        "send_sms": [
            "43e2ea14-6a13-411c-8db0-aa341f815a41"
        ],
        "purchase_order": [
            "057fed26-ade5-4e63-8b40-e4bfc2bbbf85"
        ]
    },
    "tokens": {
        "8718daf8-d9f0-49af-bdf9-f5b81592ac65": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "8718daf8-d9f0-49af-bdf9-f5b81592ac65",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bb28d7ae-9d5b-4928-b480-7624cd261b89",
                    "transitionId": "057fed26-ade5-4e63-8b40-e4bfc2bbbf85",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107327439731
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8cd3a55e-2385-406f-98ca-b3ec7fab17d2",
                    "transitionId": "057fed26-ade5-4e63-8b40-e4bfc2bbbf85",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107327439736
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7d993c8c-1dc3-42a3-a5cf-4dc633c0bc08",
                    "transitionId": "c67c4cce-150d-42f5-b1b6-609d9780e61d",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107327439739
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b4f65713-bbce-4b43-8381-417e48f58de7",
                    "transitionId": "c67c4cce-150d-42f5-b1b6-609d9780e61d",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107327439743
                }
            ]
        },
        "9e8a2134-fd8e-4831-8a26-b89fdd03e3b4": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "9e8a2134-fd8e-4831-8a26-b89fdd03e3b4",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a55e299a-ce56-473a-88c3-5711fb90141c",
                    "transitionId": "43e2ea14-6a13-411c-8db0-aa341f815a41",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107327439748
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "36ce9d17-6d77-4431-a5c4-6d0030d2d634",
                    "transitionId": "43e2ea14-6a13-411c-8db0-aa341f815a41",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107327439752
                }
            ]
        }
    }
}