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": "9737f19a-738c-46ed-aff9-ba90eb33cfd8",
    "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": {
        "10525a3e-f218-4307-a02d-5969cf688552": {
            "id": "10525a3e-f218-4307-a02d-5969cf688552",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "2b1b707d-180c-4984-b8df-4503d9fc73c9": {
            "id": "2b1b707d-180c-4984-b8df-4503d9fc73c9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "8c3a6aca-ca2d-41b8-b405-8df5a255517e": {
            "id": "8c3a6aca-ca2d-41b8-b405-8df5a255517e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "10525a3e-f218-4307-a02d-5969cf688552",
            "2b1b707d-180c-4984-b8df-4503d9fc73c9"
        ]
    },
    "inTransitions": {
        "send_email": [
            "10525a3e-f218-4307-a02d-5969cf688552"
        ],
        "send_sms": [
            "2b1b707d-180c-4984-b8df-4503d9fc73c9"
        ],
        "purchase_order": [
            "8c3a6aca-ca2d-41b8-b405-8df5a255517e"
        ]
    },
    "tokens": {
        "7c211e8d-a4ec-4125-ba84-35cee29c8325": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7c211e8d-a4ec-4125-ba84-35cee29c8325",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cdde5b01-79af-4103-a51f-787429352362",
                    "transitionId": "8c3a6aca-ca2d-41b8-b405-8df5a255517e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857730165080
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f20aa271-9f83-4f0c-b800-3f7e0f4a3f42",
                    "transitionId": "8c3a6aca-ca2d-41b8-b405-8df5a255517e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857730165086
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "da154529-c891-4f14-988b-fd8c9fa665da",
                    "transitionId": "10525a3e-f218-4307-a02d-5969cf688552",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857730165092
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "75ed73e8-6805-4edb-a709-c191bbd08271",
                    "transitionId": "10525a3e-f218-4307-a02d-5969cf688552",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857730165098
                }
            ]
        },
        "1ea95e7d-fdac-4987-afc5-07b6fb2d19e5": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "1ea95e7d-fdac-4987-afc5-07b6fb2d19e5",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "102aea53-ce6b-42a8-bbcd-3ce96c04afe3",
                    "transitionId": "2b1b707d-180c-4984-b8df-4503d9fc73c9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857730165105
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "da66a681-18ba-4dd3-b71c-01b8cf5d1394",
                    "transitionId": "2b1b707d-180c-4984-b8df-4503d9fc73c9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857730165112
                }
            ]
        }
    }
}