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": "b3cb0d3c-1ae9-4150-8b8e-abf28677b902",
    "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": {
        "22800688-83b3-4db0-a6db-b4551916bb53": {
            "id": "22800688-83b3-4db0-a6db-b4551916bb53",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "13a844f5-dad4-47c6-9fcc-7e4927eac103": {
            "id": "13a844f5-dad4-47c6-9fcc-7e4927eac103",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "6480b313-218d-4208-a4fc-6b1291835a49": {
            "id": "6480b313-218d-4208-a4fc-6b1291835a49",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "22800688-83b3-4db0-a6db-b4551916bb53",
            "13a844f5-dad4-47c6-9fcc-7e4927eac103"
        ]
    },
    "inTransitions": {
        "send_email": [
            "22800688-83b3-4db0-a6db-b4551916bb53",
            "6480b313-218d-4208-a4fc-6b1291835a49"
        ],
        "email_sent": [
            "13a844f5-dad4-47c6-9fcc-7e4927eac103"
        ]
    },
    "tokens": {
        "cee65290-a70c-476d-93e9-92ffdf2cd1d3": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "cee65290-a70c-476d-93e9-92ffdf2cd1d3",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "151fef79-4692-440f-a169-9c36bbc72ea3",
                    "transitionId": "6480b313-218d-4208-a4fc-6b1291835a49",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508389442047
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ecf085ab-9a39-4fee-8d33-4880ebf912d3",
                    "transitionId": "6480b313-218d-4208-a4fc-6b1291835a49",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508389442053
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "bd666da7-0826-44f5-89c8-fdb89bb1a4de",
                    "transitionId": "22800688-83b3-4db0-a6db-b4551916bb53",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508389442057
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f0bbd4ba-cb2f-42e5-9a16-e16148a0769f",
                    "transitionId": "22800688-83b3-4db0-a6db-b4551916bb53",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508389442063
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e7f467e9-8627-4c37-9f3c-3929ff94d306",
                    "transitionId": "22800688-83b3-4db0-a6db-b4551916bb53",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508389442067
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "afc185c9-2d1e-4b21-9285-c29a783407ac",
                    "transitionId": "22800688-83b3-4db0-a6db-b4551916bb53",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508389442072
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "86d4a022-7c6f-41da-b39c-2d693423acc6",
                    "transitionId": "22800688-83b3-4db0-a6db-b4551916bb53",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508389442075
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cf73d546-67ec-4836-a4c1-2f591f1b6579",
                    "transitionId": "22800688-83b3-4db0-a6db-b4551916bb53",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508389442080
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fd6cad15-a09a-428d-9946-ffca9f6378f8",
                    "transitionId": "13a844f5-dad4-47c6-9fcc-7e4927eac103",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508389442083
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4f756d24-ce51-4b6a-b12a-4f054460d696",
                    "transitionId": "13a844f5-dad4-47c6-9fcc-7e4927eac103",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508389442089
                }
            ],
            "maxRetries": 4,
            "counter": 4
        }
    }
}