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": "8af6d1da-b7c0-482e-b567-cd7db708bd41",
    "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": {
        "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8": {
            "id": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "e8913f9f-2fa0-4c0e-a14c-fbb5e9c60117": {
            "id": "e8913f9f-2fa0-4c0e-a14c-fbb5e9c60117",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "05252b47-a235-4519-80a3-8dd5ec92d335": {
            "id": "05252b47-a235-4519-80a3-8dd5ec92d335",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
            "e8913f9f-2fa0-4c0e-a14c-fbb5e9c60117"
        ]
    },
    "inTransitions": {
        "send_email": [
            "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
            "05252b47-a235-4519-80a3-8dd5ec92d335"
        ],
        "email_sent": [
            "e8913f9f-2fa0-4c0e-a14c-fbb5e9c60117"
        ]
    },
    "tokens": {
        "4f6329f7-82d1-45b2-b465-a347987c36d3": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "4f6329f7-82d1-45b2-b465-a347987c36d3",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4f678ae1-3d73-4468-b9da-09847d0f565c",
                    "transitionId": "05252b47-a235-4519-80a3-8dd5ec92d335",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857737719667
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "faba04ba-e308-4c52-b885-0f168df6c952",
                    "transitionId": "05252b47-a235-4519-80a3-8dd5ec92d335",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857737719675
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d305a2cd-3573-464e-9b57-2ef1dce68909",
                    "transitionId": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857737719680
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d14974ec-3b2a-4316-88d0-558482765b2b",
                    "transitionId": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857737719688
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0e1ebd3e-3657-4541-9783-759a9375bbdb",
                    "transitionId": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857737719693
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d25453d4-55db-4ea1-bf2b-3720f9c113dc",
                    "transitionId": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857737719700
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "18b2d811-fb61-47a9-9ae9-fbdb0a8d511c",
                    "transitionId": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857737719705
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5ff67052-c264-420f-af5b-658556de4fbc",
                    "transitionId": "37ff0e80-e9db-49c0-8cb1-4e85a2cb8ab8",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857737719711
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6d71dcb3-5637-4866-b8d8-d2ae45e2fb0b",
                    "transitionId": "e8913f9f-2fa0-4c0e-a14c-fbb5e9c60117",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857737719716
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5d0e6a72-b4af-4f50-9518-e025310320dd",
                    "transitionId": "e8913f9f-2fa0-4c0e-a14c-fbb5e9c60117",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857737719721
                }
            ],
            "maxRetries": 4,
            "counter": 4
        }
    }
}