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": "7692866a-a0dc-4e1a-8b36-59d3765d44e9",
    "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": {
        "8006455c-b7a9-4e04-8b0c-42b3c46cd8be": {
            "id": "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "d0ab8697-f9ef-4540-b6de-8f48edde3c63": {
            "id": "d0ab8697-f9ef-4540-b6de-8f48edde3c63",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "d559a398-d595-4581-8232-404e8bb84195": {
            "id": "d559a398-d595-4581-8232-404e8bb84195",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
            "d0ab8697-f9ef-4540-b6de-8f48edde3c63"
        ]
    },
    "inTransitions": {
        "send_email": [
            "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
            "d559a398-d595-4581-8232-404e8bb84195"
        ],
        "email_sent": [
            "d0ab8697-f9ef-4540-b6de-8f48edde3c63"
        ]
    },
    "tokens": {
        "52ad04f8-aa31-4e67-ab22-bbf3ee121cce": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "52ad04f8-aa31-4e67-ab22-bbf3ee121cce",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c2ea90be-bd09-4dc8-873b-6987fdb60f2a",
                    "transitionId": "d559a398-d595-4581-8232-404e8bb84195",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450753487825
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "53b27f46-1e48-4be6-8568-e4e4c7fae324",
                    "transitionId": "d559a398-d595-4581-8232-404e8bb84195",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450753487830
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b5eb45d8-9420-4624-985d-add437c58b00",
                    "transitionId": "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450753487833
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1f1caa68-c802-4e5a-85d3-c4d429e9ce7e",
                    "transitionId": "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450753487838
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "732a61be-4694-4cb5-b75f-8c94227e81a5",
                    "transitionId": "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450753487841
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c82f51ca-6ad1-4140-82b7-2c7775705dbc",
                    "transitionId": "8006455c-b7a9-4e04-8b0c-42b3c46cd8be",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450753487846
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ed6a3980-4e7a-40cd-a1ab-5e3b2667eda2",
                    "transitionId": "d0ab8697-f9ef-4540-b6de-8f48edde3c63",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450753487849
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7fe68f47-f81f-4050-8cd5-bfbc53a12239",
                    "transitionId": "d0ab8697-f9ef-4540-b6de-8f48edde3c63",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450753487853
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}