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": "6a22ee67-55ab-40fe-9d5b-2a76161fc0d2",
    "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": {
        "8d41fe68-b113-47f5-a6e9-591cadd6623c": {
            "id": "8d41fe68-b113-47f5-a6e9-591cadd6623c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "145ce0ad-904a-45a3-8f84-097352430ab2": {
            "id": "145ce0ad-904a-45a3-8f84-097352430ab2",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "798909a9-148a-482c-ab62-6590da0f26ea": {
            "id": "798909a9-148a-482c-ab62-6590da0f26ea",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "8d41fe68-b113-47f5-a6e9-591cadd6623c",
            "145ce0ad-904a-45a3-8f84-097352430ab2"
        ]
    },
    "inTransitions": {
        "send_email": [
            "8d41fe68-b113-47f5-a6e9-591cadd6623c",
            "798909a9-148a-482c-ab62-6590da0f26ea"
        ],
        "email_sent": [
            "145ce0ad-904a-45a3-8f84-097352430ab2"
        ]
    },
    "tokens": {
        "da7d60e7-e129-4e45-9741-574001120b6d": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "da7d60e7-e129-4e45-9741-574001120b6d",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dd74e9e7-b998-4636-9d80-dd0306d750bb",
                    "transitionId": "798909a9-148a-482c-ab62-6590da0f26ea",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107335324051
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f129896a-0c19-480f-ba2a-a9f2ba6ac2cc",
                    "transitionId": "798909a9-148a-482c-ab62-6590da0f26ea",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107335324055
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3423d150-8634-4f33-beb5-56eedbfc471e",
                    "transitionId": "8d41fe68-b113-47f5-a6e9-591cadd6623c",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107335324058
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "740d15a7-0b27-4e1a-9f32-70c921acb481",
                    "transitionId": "8d41fe68-b113-47f5-a6e9-591cadd6623c",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107335324061
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "907be96e-7ac0-4746-ae34-21b695c9439c",
                    "transitionId": "8d41fe68-b113-47f5-a6e9-591cadd6623c",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107335324065
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5c023927-1251-4fd1-96ef-7979ebb76a91",
                    "transitionId": "8d41fe68-b113-47f5-a6e9-591cadd6623c",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107335324069
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d2cfc18a-c35c-4416-9e62-ad6c2d9a7bb0",
                    "transitionId": "145ce0ad-904a-45a3-8f84-097352430ab2",
                    "weight": 1,
                    "state": "opened",
                    "time": 16107335324072
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "989f58d3-26f4-41cd-a9b8-29967e6cacfc",
                    "transitionId": "145ce0ad-904a-45a3-8f84-097352430ab2",
                    "weight": 1,
                    "state": "passed",
                    "time": 16107335324075
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}