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": "0dc17742-47dd-450a-be5b-f6b3ef42e886",
    "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": {
        "420648ca-4d25-4e93-af5c-616ecf932ce2": {
            "id": "420648ca-4d25-4e93-af5c-616ecf932ce2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "7a96a138-37ac-4f39-82e1-670bdd55e129": {
            "id": "7a96a138-37ac-4f39-82e1-670bdd55e129",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "e94ea915-c23a-4544-9e5b-5a198bb53c00": {
            "id": "e94ea915-c23a-4544-9e5b-5a198bb53c00",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "420648ca-4d25-4e93-af5c-616ecf932ce2",
            "7a96a138-37ac-4f39-82e1-670bdd55e129"
        ]
    },
    "inTransitions": {
        "send_email": [
            "420648ca-4d25-4e93-af5c-616ecf932ce2"
        ],
        "send_sms": [
            "7a96a138-37ac-4f39-82e1-670bdd55e129"
        ],
        "purchase_order": [
            "e94ea915-c23a-4544-9e5b-5a198bb53c00"
        ]
    },
    "tokens": {
        "8aa25b91-8ec1-458f-9e34-44031241a57f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "8aa25b91-8ec1-458f-9e34-44031241a57f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f2c38839-841e-438e-a17c-5dd1d4f488fc",
                    "transitionId": "e94ea915-c23a-4544-9e5b-5a198bb53c00",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823424974871
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "76cf2268-8e28-48f3-b183-1ecb28b46092",
                    "transitionId": "e94ea915-c23a-4544-9e5b-5a198bb53c00",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823424974876
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "267537ce-ba42-4c4b-a4cf-33b6a5610ba2",
                    "transitionId": "420648ca-4d25-4e93-af5c-616ecf932ce2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823424974879
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c1723ed1-176f-4b60-91d7-155575d8e256",
                    "transitionId": "420648ca-4d25-4e93-af5c-616ecf932ce2",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823424974883
                }
            ]
        },
        "8c181099-7a54-4f3c-bf19-117d3afbd313": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "8c181099-7a54-4f3c-bf19-117d3afbd313",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a816fc64-93ac-40be-989f-49d7e4ad80bc",
                    "transitionId": "7a96a138-37ac-4f39-82e1-670bdd55e129",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823424974887
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5694ebb5-ca6a-4f80-a437-bce71f736ea4",
                    "transitionId": "7a96a138-37ac-4f39-82e1-670bdd55e129",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823424974892
                }
            ]
        }
    }
}