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": "aec7855e-5c55-4947-a544-370441244074",
    "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": {
        "69a599ff-ed04-4a0a-80fd-b7e565813142": {
            "id": "69a599ff-ed04-4a0a-80fd-b7e565813142",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "36dbe2c2-0629-42f3-be3b-cb2be52f9d04": {
            "id": "36dbe2c2-0629-42f3-be3b-cb2be52f9d04",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "93d01dd8-1ef0-4e8b-8b5f-55a688ec692b": {
            "id": "93d01dd8-1ef0-4e8b-8b5f-55a688ec692b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "69a599ff-ed04-4a0a-80fd-b7e565813142",
            "36dbe2c2-0629-42f3-be3b-cb2be52f9d04"
        ]
    },
    "inTransitions": {
        "send_email": [
            "69a599ff-ed04-4a0a-80fd-b7e565813142",
            "93d01dd8-1ef0-4e8b-8b5f-55a688ec692b"
        ],
        "email_sent": [
            "36dbe2c2-0629-42f3-be3b-cb2be52f9d04"
        ]
    },
    "tokens": {
        "d1e7f235-4037-4641-aadf-0c6bdb118eaf": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "d1e7f235-4037-4641-aadf-0c6bdb118eaf",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2ab66fff-89c3-4541-a5bf-66738758f3a2",
                    "transitionId": "93d01dd8-1ef0-4e8b-8b5f-55a688ec692b",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070813352720
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dd0b0ea0-3616-429f-987c-4fd03ce0f99d",
                    "transitionId": "93d01dd8-1ef0-4e8b-8b5f-55a688ec692b",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070813352724
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "60e88579-ea7c-4d9c-9e35-dbede142b622",
                    "transitionId": "69a599ff-ed04-4a0a-80fd-b7e565813142",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070813352727
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c5f72c16-130d-4ab1-9824-6756fb36d11d",
                    "transitionId": "69a599ff-ed04-4a0a-80fd-b7e565813142",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070813352731
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fd462d25-39b3-4767-adb1-6382bef095b0",
                    "transitionId": "69a599ff-ed04-4a0a-80fd-b7e565813142",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070813352734
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8711c6c7-5f21-4d2b-a951-8c2349f459d2",
                    "transitionId": "69a599ff-ed04-4a0a-80fd-b7e565813142",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070813352738
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c2a91e28-9338-4ab6-9da5-8571b5a84a73",
                    "transitionId": "36dbe2c2-0629-42f3-be3b-cb2be52f9d04",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070813352742
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "02f4f759-c2b9-42e2-a54d-9b03da948ef8",
                    "transitionId": "36dbe2c2-0629-42f3-be3b-cb2be52f9d04",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070813352746
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}