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": "9b5ff632-b6bb-4f66-892e-7d4a409b3d85",
    "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": {
        "c53fed89-e0e6-42c3-92ca-d97f92e13a0c": {
            "id": "c53fed89-e0e6-42c3-92ca-d97f92e13a0c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "17741eb2-db3d-4038-85fa-24bc8fd6a176": {
            "id": "17741eb2-db3d-4038-85fa-24bc8fd6a176",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "5b5ae9c8-568d-43b9-922a-0627d3fb955e": {
            "id": "5b5ae9c8-568d-43b9-922a-0627d3fb955e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "c53fed89-e0e6-42c3-92ca-d97f92e13a0c",
            "17741eb2-db3d-4038-85fa-24bc8fd6a176"
        ]
    },
    "inTransitions": {
        "send_email": [
            "c53fed89-e0e6-42c3-92ca-d97f92e13a0c"
        ],
        "send_sms": [
            "17741eb2-db3d-4038-85fa-24bc8fd6a176"
        ],
        "purchase_order": [
            "5b5ae9c8-568d-43b9-922a-0627d3fb955e"
        ]
    },
    "tokens": {
        "274eee71-09ab-4cb1-bf2f-afe1c0393a9a": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "274eee71-09ab-4cb1-bf2f-afe1c0393a9a",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "42d53591-0bbe-49d4-814b-cc7406adf9cd",
                    "transitionId": "5b5ae9c8-568d-43b9-922a-0627d3fb955e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757096293446
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4cfa6f1c-4893-45bd-818e-6f0b6c25ea7b",
                    "transitionId": "5b5ae9c8-568d-43b9-922a-0627d3fb955e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757096293451
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "46f57e42-0b38-4b43-957d-6ea2e77f901c",
                    "transitionId": "c53fed89-e0e6-42c3-92ca-d97f92e13a0c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757096293453
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "478c4604-a724-4461-9071-a423883eb862",
                    "transitionId": "c53fed89-e0e6-42c3-92ca-d97f92e13a0c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757096293457
                }
            ]
        },
        "3b568227-0fa4-49b6-8858-aafad8b520d5": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "3b568227-0fa4-49b6-8858-aafad8b520d5",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ce4435a4-a844-43d6-a1a4-89aa48c4f30c",
                    "transitionId": "17741eb2-db3d-4038-85fa-24bc8fd6a176",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757096293461
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3f8d0d60-f6d3-4e50-982c-6981ee6c480c",
                    "transitionId": "17741eb2-db3d-4038-85fa-24bc8fd6a176",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757096293466
                }
            ]
        }
    }
}