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
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": "60494887-cc39-43e5-ba47-4c39047a0d76",
    "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": {
        "058e9c00-0e88-491e-bad3-08f922f96cf2": {
            "id": "058e9c00-0e88-491e-bad3-08f922f96cf2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "3bae8563-2b0f-4f27-b686-4a20616fb253": {
            "id": "3bae8563-2b0f-4f27-b686-4a20616fb253",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "7ee26284-0952-44a2-a0d8-7e3c1c5524d1": {
            "id": "7ee26284-0952-44a2-a0d8-7e3c1c5524d1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "058e9c00-0e88-491e-bad3-08f922f96cf2",
            "3bae8563-2b0f-4f27-b686-4a20616fb253"
        ]
    },
    "inTransitions": {
        "send_email": [
            "058e9c00-0e88-491e-bad3-08f922f96cf2",
            "7ee26284-0952-44a2-a0d8-7e3c1c5524d1"
        ],
        "email_sent": [
            "3bae8563-2b0f-4f27-b686-4a20616fb253"
        ]
    },
    "tokens": {
        "6cdae9d7-de4a-4968-99b0-8c54e043ae67": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "6cdae9d7-de4a-4968-99b0-8c54e043ae67",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "de76e71b-da1c-46be-8715-d5da051e584e",
                    "transitionId": "7ee26284-0952-44a2-a0d8-7e3c1c5524d1",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635972293
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3e04e0df-1720-4826-894e-115d7db71992",
                    "transitionId": "7ee26284-0952-44a2-a0d8-7e3c1c5524d1",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635972298
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "94d3deb2-b767-4406-aa7d-4930cb9ebb80",
                    "transitionId": "058e9c00-0e88-491e-bad3-08f922f96cf2",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635972301
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "68eb7c1a-c919-499f-a107-182f22ea6b6a",
                    "transitionId": "058e9c00-0e88-491e-bad3-08f922f96cf2",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635972306
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9ec881bd-ce45-45e8-aedd-8332c333f1fb",
                    "transitionId": "058e9c00-0e88-491e-bad3-08f922f96cf2",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635972310
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5b193997-2d95-41ed-8f97-7b6962b1b6e5",
                    "transitionId": "058e9c00-0e88-491e-bad3-08f922f96cf2",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635972315
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "381b24f3-2a58-4cad-9c3e-67acae446e9f",
                    "transitionId": "058e9c00-0e88-491e-bad3-08f922f96cf2",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635972318
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b36fc349-7989-4e07-bdff-4f1e2a6fec23",
                    "transitionId": "058e9c00-0e88-491e-bad3-08f922f96cf2",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635972322
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1a1dbd12-d10b-41c4-bd55-a153b9404eb4",
                    "transitionId": "3bae8563-2b0f-4f27-b686-4a20616fb253",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635972325
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "75ba0613-4cfa-4d58-8ab6-d153c5320a3e",
                    "transitionId": "3bae8563-2b0f-4f27-b686-4a20616fb253",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635972328
                }
            ],
            "maxRetries": 4,
            "counter": 4
        }
    }
}