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": "ae747bd9-8e83-4e4e-a90d-011c8aae031e",
    "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": {
        "62f9be2a-0a55-49a5-b02a-3267723b6fa1": {
            "id": "62f9be2a-0a55-49a5-b02a-3267723b6fa1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "6f3f33db-e999-4a8a-b512-7bf8810fa17f": {
            "id": "6f3f33db-e999-4a8a-b512-7bf8810fa17f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "b6f443c9-3782-41c6-bc80-51f31ecbe207": {
            "id": "b6f443c9-3782-41c6-bc80-51f31ecbe207",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "62f9be2a-0a55-49a5-b02a-3267723b6fa1",
            "6f3f33db-e999-4a8a-b512-7bf8810fa17f"
        ]
    },
    "inTransitions": {
        "send_email": [
            "62f9be2a-0a55-49a5-b02a-3267723b6fa1"
        ],
        "send_sms": [
            "6f3f33db-e999-4a8a-b512-7bf8810fa17f"
        ],
        "purchase_order": [
            "b6f443c9-3782-41c6-bc80-51f31ecbe207"
        ]
    },
    "tokens": {
        "0fa40219-6364-4a93-9c2b-f4aff1f34921": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0fa40219-6364-4a93-9c2b-f4aff1f34921",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f6297cd3-9c8a-4147-8b8e-acd9e94b9939",
                    "transitionId": "b6f443c9-3782-41c6-bc80-51f31ecbe207",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070805685068
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2bfb9577-433b-4d16-804e-6c8fee73b6b9",
                    "transitionId": "b6f443c9-3782-41c6-bc80-51f31ecbe207",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070805685073
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4dee8637-6ade-4bea-ac14-f6018748e674",
                    "transitionId": "62f9be2a-0a55-49a5-b02a-3267723b6fa1",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070805685076
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2f7fd63f-1409-4ac3-a8b8-04eaa4a4fe1b",
                    "transitionId": "62f9be2a-0a55-49a5-b02a-3267723b6fa1",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070805685079
                }
            ]
        },
        "7d2f94cf-4e29-4fd5-bfbb-6d48fbc79b69": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7d2f94cf-4e29-4fd5-bfbb-6d48fbc79b69",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a6fe645e-321d-404f-9c10-e6c1fbb927c1",
                    "transitionId": "6f3f33db-e999-4a8a-b512-7bf8810fa17f",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070805685083
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b9786775-8554-42fe-8f2c-1f44539e4cab",
                    "transitionId": "6f3f33db-e999-4a8a-b512-7bf8810fa17f",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070805685087
                }
            ]
        }
    }
}