Cycle

The example shows how some nodes could be executed several times in a cycle.
For example you want to send an email but something went wrong and the server is not available and you want to retry the operation.

Setup


mkdir ~/pvm-cycle
cd ~/pvm-cycle
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php cycle.php

Result

cycle.php

failed to send email
failed to send email
email is sent

cycle.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('send_email''send_email')->end()
    ->
createNode('email_sent''email_sent')->end()

    ->
createTransition('send_email''send_email''retry')->end()
    ->
createTransition('send_email''email_sent')->end()
    ->
createStartTransition('send_email')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'email_sent' => function(Token $token) {
        echo 
'email is sent'.PHP_EOL;
    },
    
'send_email' => function(Token $token) {
        
$counter $token->getValue('counter'0);
        
$counter++;

        
$token->setValue('counter'$counter);

        try {
            if (
$counter $token->getValue('maxRetries')) {
                throw new \
LogicException('Mail server is down');
            }
        } catch (\
Throwable $e) {
            echo 
'failed to send email'.PHP_EOL;
            return 
'retry';
        }
    },
]));

$token $engine->createTokenFor($process->getStartTransition());
$token->setValue('maxRetries'rand(24));

$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "c14f665e-ac01-4200-855d-e4c4a396e0f5",
    "nodes": {
        "send_email": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "send_email",
            "behavior": "send_email"
        },
        "email_sent": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "email_sent",
            "behavior": "email_sent"
        }
    },
    "transitions": {
        "61f9ae04-e9c1-4c32-9602-6e334d53c341": {
            "id": "61f9ae04-e9c1-4c32-9602-6e334d53c341",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "d6a61ade-5b12-4cce-a56c-62356cfaceb2": {
            "id": "d6a61ade-5b12-4cce-a56c-62356cfaceb2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "22882f62-d648-410d-a551-559a73fe81da": {
            "id": "22882f62-d648-410d-a551-559a73fe81da",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "61f9ae04-e9c1-4c32-9602-6e334d53c341",
            "d6a61ade-5b12-4cce-a56c-62356cfaceb2"
        ]
    },
    "inTransitions": {
        "send_email": [
            "61f9ae04-e9c1-4c32-9602-6e334d53c341",
            "22882f62-d648-410d-a551-559a73fe81da"
        ],
        "email_sent": [
            "d6a61ade-5b12-4cce-a56c-62356cfaceb2"
        ]
    },
    "tokens": {
        "ee1d0c93-3ae6-403d-a696-c0b1a669cc16": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "ee1d0c93-3ae6-403d-a696-c0b1a669cc16",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "23ed0c1d-6824-4ac9-8a5c-22f5dc2304f1",
                    "transitionId": "22882f62-d648-410d-a551-559a73fe81da",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421483230209
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5b5a4c1e-9123-419c-a3a1-8b37c607685f",
                    "transitionId": "22882f62-d648-410d-a551-559a73fe81da",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421483230213
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "feca1221-5e89-4897-99b8-2dd82e1b832a",
                    "transitionId": "61f9ae04-e9c1-4c32-9602-6e334d53c341",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421483230216
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4d6f689c-7ac6-4759-81f2-8610e9ab2d1e",
                    "transitionId": "61f9ae04-e9c1-4c32-9602-6e334d53c341",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421483230220
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b8313ab9-8d34-4980-9ea5-94c31df55703",
                    "transitionId": "61f9ae04-e9c1-4c32-9602-6e334d53c341",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421483230222
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f6b9bf3a-62d5-4396-bd75-e31317ee6ba9",
                    "transitionId": "61f9ae04-e9c1-4c32-9602-6e334d53c341",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421483230226
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a5ed08cb-93cb-4a20-b3fc-00ef5ce66c95",
                    "transitionId": "d6a61ade-5b12-4cce-a56c-62356cfaceb2",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421483230229
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5a863593-4b52-4572-a1bd-b9c8ef361e48",
                    "transitionId": "d6a61ade-5b12-4cce-a56c-62356cfaceb2",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421483230233
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}