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": "8e8eac0b-63aa-4c4d-a556-a3e314fba855",
    "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": {
        "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b": {
            "id": "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "c8f52766-acae-4e85-884a-2776db16727c": {
            "id": "c8f52766-acae-4e85-884a-2776db16727c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "fca9d78a-8ce9-4a3f-bcea-a7026d1fd9b4": {
            "id": "fca9d78a-8ce9-4a3f-bcea-a7026d1fd9b4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
            "c8f52766-acae-4e85-884a-2776db16727c"
        ]
    },
    "inTransitions": {
        "send_email": [
            "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
            "fca9d78a-8ce9-4a3f-bcea-a7026d1fd9b4"
        ],
        "email_sent": [
            "c8f52766-acae-4e85-884a-2776db16727c"
        ]
    },
    "tokens": {
        "57c0fea6-4e7c-49ab-a4ae-988bc8a9ded8": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "57c0fea6-4e7c-49ab-a4ae-988bc8a9ded8",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a81afc36-07af-4bb6-9fcf-1950eaa640d0",
                    "transitionId": "fca9d78a-8ce9-4a3f-bcea-a7026d1fd9b4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757103823473
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7ad5ab22-974d-42ad-8f0a-393ff0853791",
                    "transitionId": "fca9d78a-8ce9-4a3f-bcea-a7026d1fd9b4",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757103823477
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ad2c9488-b6b7-49b6-841d-c03af87649ae",
                    "transitionId": "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757103823480
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8a2e46fa-fe1b-4d66-a5dd-d23f8a37a609",
                    "transitionId": "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757103823484
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dd7f6211-30dd-4045-b8f8-1901c0f71ebd",
                    "transitionId": "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757103823486
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "02726e0a-692b-4925-9162-e2d15b4f8cd6",
                    "transitionId": "3c2ee298-efa2-4edd-a9e3-2d466e2e8a8b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757103823490
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1dd3e61a-4d41-4834-b0e2-d01b33f7a21e",
                    "transitionId": "c8f52766-acae-4e85-884a-2776db16727c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757103823493
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "81cc8026-f854-41e2-9c67-bce0c3239023",
                    "transitionId": "c8f52766-acae-4e85-884a-2776db16727c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757103823496
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}