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": "cf566a51-81d6-4bd8-ba1e-b83201ee700b",
    "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": {
        "f42a0656-5240-48f4-8e16-d919eb2f8aa6": {
            "id": "f42a0656-5240-48f4-8e16-d919eb2f8aa6",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "b2739eb9-e188-43bb-8192-cdadda114924": {
            "id": "b2739eb9-e188-43bb-8192-cdadda114924",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "41c53693-cbbd-4334-ba0b-ea3e0dce9a95": {
            "id": "41c53693-cbbd-4334-ba0b-ea3e0dce9a95",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "f42a0656-5240-48f4-8e16-d919eb2f8aa6",
            "b2739eb9-e188-43bb-8192-cdadda114924"
        ]
    },
    "inTransitions": {
        "send_email": [
            "f42a0656-5240-48f4-8e16-d919eb2f8aa6"
        ],
        "send_sms": [
            "b2739eb9-e188-43bb-8192-cdadda114924"
        ],
        "purchase_order": [
            "41c53693-cbbd-4334-ba0b-ea3e0dce9a95"
        ]
    },
    "tokens": {
        "9e88a517-2069-4124-a62a-ed330fbb7004": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "9e88a517-2069-4124-a62a-ed330fbb7004",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9f870798-1f6a-41f9-96e0-b0b29c73fd22",
                    "transitionId": "41c53693-cbbd-4334-ba0b-ea3e0dce9a95",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559353398143
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "eac11086-f10e-44b1-a66e-93ec74c52dee",
                    "transitionId": "41c53693-cbbd-4334-ba0b-ea3e0dce9a95",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559353398152
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0dd9840c-4edd-413a-9396-811094a67ba6",
                    "transitionId": "f42a0656-5240-48f4-8e16-d919eb2f8aa6",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559353398157
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "738d00b6-af21-48d9-b5d5-cd11c1234b91",
                    "transitionId": "f42a0656-5240-48f4-8e16-d919eb2f8aa6",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559353398165
                }
            ]
        },
        "75cd1237-3512-4362-a87a-18e4cd1bf7d0": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "75cd1237-3512-4362-a87a-18e4cd1bf7d0",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5b5a767b-19bf-4ff4-8a29-6bf134a390d2",
                    "transitionId": "b2739eb9-e188-43bb-8192-cdadda114924",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559353398172
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f42dbafa-e1ae-40a1-b34f-b377cf9606a9",
                    "transitionId": "b2739eb9-e188-43bb-8192-cdadda114924",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559353398180
                }
            ]
        }
    }
}