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
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": "cb14b8bf-2ac6-4ad9-a52b-189f953b7ccf",
    "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": {
        "4d533854-0c53-4251-a5dd-fa1c743fa88e": {
            "id": "4d533854-0c53-4251-a5dd-fa1c743fa88e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "4c97041d-7c8b-407f-9ef5-3e315492401d": {
            "id": "4c97041d-7c8b-407f-9ef5-3e315492401d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "f367e67d-acc0-4f59-a64b-a5bc467c1268": {
            "id": "f367e67d-acc0-4f59-a64b-a5bc467c1268",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "4d533854-0c53-4251-a5dd-fa1c743fa88e",
            "4c97041d-7c8b-407f-9ef5-3e315492401d"
        ]
    },
    "inTransitions": {
        "send_email": [
            "4d533854-0c53-4251-a5dd-fa1c743fa88e",
            "f367e67d-acc0-4f59-a64b-a5bc467c1268"
        ],
        "email_sent": [
            "4c97041d-7c8b-407f-9ef5-3e315492401d"
        ]
    },
    "tokens": {
        "e6eea177-72e1-4c4e-ac06-eaca2bf503b5": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "e6eea177-72e1-4c4e-ac06-eaca2bf503b5",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d1b6e7c2-cac1-4c88-9dd7-7ca9887c9f19",
                    "transitionId": "f367e67d-acc0-4f59-a64b-a5bc467c1268",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479166328466
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "11b30ce9-2082-40d2-af98-e3215ece3d0a",
                    "transitionId": "f367e67d-acc0-4f59-a64b-a5bc467c1268",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479166328473
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "088baf9d-b824-499d-a6a8-012ad4a57f3f",
                    "transitionId": "4d533854-0c53-4251-a5dd-fa1c743fa88e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479166328476
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9946f2d8-03b3-45dc-949c-b523fcd1c662",
                    "transitionId": "4d533854-0c53-4251-a5dd-fa1c743fa88e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479166328481
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "18c0b6b5-0f1f-4d8f-8d53-c9be3791b055",
                    "transitionId": "4c97041d-7c8b-407f-9ef5-3e315492401d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479166328484
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "01289248-907d-4bff-ac62-293e9697e476",
                    "transitionId": "4c97041d-7c8b-407f-9ef5-3e315492401d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479166328488
                }
            ],
            "maxRetries": 2,
            "counter": 2
        }
    }
}