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": "6a64ebf3-a704-4f1a-88b0-ca52f2cc1ec6",
    "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": {
        "0864587b-79cc-445d-b18b-3d02d4359a97": {
            "id": "0864587b-79cc-445d-b18b-3d02d4359a97",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "53d690d6-8468-4f99-b265-f412c9eb1486": {
            "id": "53d690d6-8468-4f99-b265-f412c9eb1486",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "ca83f5bd-b544-4c54-af96-14833025f671": {
            "id": "ca83f5bd-b544-4c54-af96-14833025f671",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "0864587b-79cc-445d-b18b-3d02d4359a97",
            "53d690d6-8468-4f99-b265-f412c9eb1486"
        ]
    },
    "inTransitions": {
        "send_email": [
            "0864587b-79cc-445d-b18b-3d02d4359a97"
        ],
        "send_sms": [
            "53d690d6-8468-4f99-b265-f412c9eb1486"
        ],
        "purchase_order": [
            "ca83f5bd-b544-4c54-af96-14833025f671"
        ]
    },
    "tokens": {
        "b0434a17-55a6-4e70-9a09-b2805502da2c": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "b0434a17-55a6-4e70-9a09-b2805502da2c",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2da7f859-2efe-4f94-9597-fecb90bf6199",
                    "transitionId": "ca83f5bd-b544-4c54-af96-14833025f671",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714967916544
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1cf2e032-0a4e-4947-bec0-ac8938a596ff",
                    "transitionId": "ca83f5bd-b544-4c54-af96-14833025f671",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714967916550
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a8eb7b6e-d5f2-42dc-b25c-e199311a3106",
                    "transitionId": "0864587b-79cc-445d-b18b-3d02d4359a97",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714967916555
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "42ddf112-53a0-495a-af06-e17107e313f9",
                    "transitionId": "0864587b-79cc-445d-b18b-3d02d4359a97",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714967916561
                }
            ]
        },
        "7695b105-bde7-4af2-929f-2bde9190de86": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7695b105-bde7-4af2-929f-2bde9190de86",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1dbb4d2c-756c-4afe-ae08-e4568a7168e8",
                    "transitionId": "53d690d6-8468-4f99-b265-f412c9eb1486",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714967916567
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "df8bd9a4-9abd-4356-ac25-615b77766a2b",
                    "transitionId": "53d690d6-8468-4f99-b265-f412c9eb1486",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714967916572
                }
            ]
        }
    }
}