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": "599559d9-feb2-407a-8b50-e074c524f3f9",
    "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": {
        "c8261a9b-ecbf-47ee-b6fe-0b831cbf340f": {
            "id": "c8261a9b-ecbf-47ee-b6fe-0b831cbf340f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "bf0c0ae5-3431-44e4-98d8-cd769e0c740c": {
            "id": "bf0c0ae5-3431-44e4-98d8-cd769e0c740c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "7d7971ec-9f93-4010-b945-6ef58e1b6dfa": {
            "id": "7d7971ec-9f93-4010-b945-6ef58e1b6dfa",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "c8261a9b-ecbf-47ee-b6fe-0b831cbf340f",
            "bf0c0ae5-3431-44e4-98d8-cd769e0c740c"
        ]
    },
    "inTransitions": {
        "send_email": [
            "c8261a9b-ecbf-47ee-b6fe-0b831cbf340f"
        ],
        "send_sms": [
            "bf0c0ae5-3431-44e4-98d8-cd769e0c740c"
        ],
        "purchase_order": [
            "7d7971ec-9f93-4010-b945-6ef58e1b6dfa"
        ]
    },
    "tokens": {
        "89c0fbea-1194-4f8a-a86d-ddd5f7f0aa31": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "89c0fbea-1194-4f8a-a86d-ddd5f7f0aa31",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7d7a2c27-6387-4239-80ec-58116e49a2c3",
                    "transitionId": "7d7971ec-9f93-4010-b945-6ef58e1b6dfa",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479159579692
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "af865be3-b863-43f1-b055-4a93134c91fe",
                    "transitionId": "7d7971ec-9f93-4010-b945-6ef58e1b6dfa",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479159579696
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5d67fd76-e167-425d-b3eb-67e82133bf7d",
                    "transitionId": "c8261a9b-ecbf-47ee-b6fe-0b831cbf340f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479159579699
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8d9a8e84-b208-4bcb-9b37-5e4b9e632a30",
                    "transitionId": "c8261a9b-ecbf-47ee-b6fe-0b831cbf340f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479159579705
                }
            ]
        },
        "fa4e1fd6-f0c3-4479-af5e-459d74254713": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "fa4e1fd6-f0c3-4479-af5e-459d74254713",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1341185c-276c-45da-984f-4e2355c3875b",
                    "transitionId": "bf0c0ae5-3431-44e4-98d8-cd769e0c740c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479159579709
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d28aaaeb-1dc8-4d32-b041-bab5d30f5cfc",
                    "transitionId": "bf0c0ae5-3431-44e4-98d8-cd769e0c740c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479159579712
                }
            ]
        }
    }
}