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": "56bbaed6-673b-4867-bb8f-6a77df1a3eef",
    "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": {
        "adb91d50-a30d-4c76-8cc0-7c1f0b7f612b": {
            "id": "adb91d50-a30d-4c76-8cc0-7c1f0b7f612b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "10f48cd1-4785-4ff5-a3d7-8e016e0bc92b": {
            "id": "10f48cd1-4785-4ff5-a3d7-8e016e0bc92b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "ee372882-366d-45d7-bd89-6157097ee4ff": {
            "id": "ee372882-366d-45d7-bd89-6157097ee4ff",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "adb91d50-a30d-4c76-8cc0-7c1f0b7f612b",
            "10f48cd1-4785-4ff5-a3d7-8e016e0bc92b"
        ]
    },
    "inTransitions": {
        "send_email": [
            "adb91d50-a30d-4c76-8cc0-7c1f0b7f612b"
        ],
        "send_sms": [
            "10f48cd1-4785-4ff5-a3d7-8e016e0bc92b"
        ],
        "purchase_order": [
            "ee372882-366d-45d7-bd89-6157097ee4ff"
        ]
    },
    "tokens": {
        "0a044880-ec3d-48b6-9ff6-c7c00c8e5827": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0a044880-ec3d-48b6-9ff6-c7c00c8e5827",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fd827441-d038-44a9-a6bf-2e5592b14434",
                    "transitionId": "ee372882-366d-45d7-bd89-6157097ee4ff",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691096188972
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e1954d4e-f38b-454d-bfb7-86766eef1d83",
                    "transitionId": "ee372882-366d-45d7-bd89-6157097ee4ff",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691096188976
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c555bce3-8d7a-4a6a-8090-f600e7cbb549",
                    "transitionId": "adb91d50-a30d-4c76-8cc0-7c1f0b7f612b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691096188979
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4c342d80-d247-45a7-a94f-3b668904c40a",
                    "transitionId": "adb91d50-a30d-4c76-8cc0-7c1f0b7f612b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691096188983
                }
            ]
        },
        "5fe82aa0-fd90-465a-b152-385df433078b": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "5fe82aa0-fd90-465a-b152-385df433078b",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fa8dccf9-312c-4e6f-ae0f-1040ca1cbf4e",
                    "transitionId": "10f48cd1-4785-4ff5-a3d7-8e016e0bc92b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691096188987
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f65f6de9-b539-4fa6-855e-150ed1b8fa41",
                    "transitionId": "10f48cd1-4785-4ff5-a3d7-8e016e0bc92b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691096188993
                }
            ]
        }
    }
}