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": "d86328e0-ea0e-4372-8110-4316cb2399d6",
    "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": {
        "9b392ccc-65bd-45b3-93fd-d3f2118cdb89": {
            "id": "9b392ccc-65bd-45b3-93fd-d3f2118cdb89",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_email"
        },
        "bbac5786-398c-4693-93b1-38002405aa6f": {
            "id": "bbac5786-398c-4693-93b1-38002405aa6f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "purchase_order",
            "to": "send_sms"
        },
        "89643e0a-f6c2-43c5-aa4e-f7e4cbf7bead": {
            "id": "89643e0a-f6c2-43c5-aa4e-f7e4cbf7bead",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "purchase_order"
        }
    },
    "outTransitions": {
        "purchase_order": [
            "9b392ccc-65bd-45b3-93fd-d3f2118cdb89",
            "bbac5786-398c-4693-93b1-38002405aa6f"
        ]
    },
    "inTransitions": {
        "send_email": [
            "9b392ccc-65bd-45b3-93fd-d3f2118cdb89"
        ],
        "send_sms": [
            "bbac5786-398c-4693-93b1-38002405aa6f"
        ],
        "purchase_order": [
            "89643e0a-f6c2-43c5-aa4e-f7e4cbf7bead"
        ]
    },
    "tokens": {
        "773171a9-4889-408a-9fb9-908e3b3f7307": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "773171a9-4889-408a-9fb9-908e3b3f7307",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c32d162d-c1f0-45f0-80a6-df7e5fdfb86d",
                    "transitionId": "89643e0a-f6c2-43c5-aa4e-f7e4cbf7bead",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968776053677
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d88bd0ae-bf00-4d81-8556-a516f5421aab",
                    "transitionId": "89643e0a-f6c2-43c5-aa4e-f7e4cbf7bead",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968776053681
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "37a245f0-5cd7-47c4-9cc9-f8098298e43f",
                    "transitionId": "9b392ccc-65bd-45b3-93fd-d3f2118cdb89",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968776053684
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "99ac40e9-caab-4fae-a058-28f69b818121",
                    "transitionId": "9b392ccc-65bd-45b3-93fd-d3f2118cdb89",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968776053689
                }
            ]
        },
        "0020cc13-c61a-49f4-8391-b217aa978615": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0020cc13-c61a-49f4-8391-b217aa978615",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "928af47e-f28a-4c3a-bb2e-24a8de3dc8bf",
                    "transitionId": "bbac5786-398c-4693-93b1-38002405aa6f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968776053695
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c599a8dc-5c90-402c-b7a6-81ceac5ef802",
                    "transitionId": "bbac5786-398c-4693-93b1-38002405aa6f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968776053704
                }
            ]
        }
    }
}