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": "6b8ebf40-0604-4119-baf0-15f0983ad2c3",
    "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": {
        "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46": {
            "id": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "10a0e2db-beb4-4aec-a9fb-3ea68a0ce992": {
            "id": "10a0e2db-beb4-4aec-a9fb-3ea68a0ce992",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "a2b77227-4ea6-4099-8424-700078c526c7": {
            "id": "a2b77227-4ea6-4099-8424-700078c526c7",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
            "10a0e2db-beb4-4aec-a9fb-3ea68a0ce992"
        ]
    },
    "inTransitions": {
        "send_email": [
            "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
            "a2b77227-4ea6-4099-8424-700078c526c7"
        ],
        "email_sent": [
            "10a0e2db-beb4-4aec-a9fb-3ea68a0ce992"
        ]
    },
    "tokens": {
        "b7b260de-cf52-4ee9-ae52-bb6336c872d7": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "b7b260de-cf52-4ee9-ae52-bb6336c872d7",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7773629b-9ef0-481f-bfb6-0d4ce431b980",
                    "transitionId": "a2b77227-4ea6-4099-8424-700078c526c7",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968784358511
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c5747212-31da-403a-b260-871db27a20d9",
                    "transitionId": "a2b77227-4ea6-4099-8424-700078c526c7",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968784358516
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "50e413cc-cfc2-44c0-b35c-52ddf30a961c",
                    "transitionId": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968784358519
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4a52a971-0433-44a4-a65d-348d9ce60c18",
                    "transitionId": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968784358525
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9d0f61b7-ad05-4fac-9077-bbed55d3c098",
                    "transitionId": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968784358573
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "951a18b6-baf5-4400-816b-e2df7f5af1ae",
                    "transitionId": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968784358577
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "97088fe7-ab17-47e3-a819-c5c568dfbdb8",
                    "transitionId": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968784358579
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d2f57283-7bed-409a-a3ca-76f7bfbba61e",
                    "transitionId": "079fb0e0-cc7d-45eb-b82d-ca89a3bd9f46",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968784358583
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "545cabe6-b1cf-44d3-81fc-d0b16f67c262",
                    "transitionId": "10a0e2db-beb4-4aec-a9fb-3ea68a0ce992",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968784358594
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cdecc00f-0c42-4b3e-895e-c4f764667428",
                    "transitionId": "10a0e2db-beb4-4aec-a9fb-3ea68a0ce992",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968784358597
                }
            ],
            "maxRetries": 4,
            "counter": 4
        }
    }
}