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": "f008df33-456f-4bcf-b260-7dee68a9af4d",
    "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": {
        "e4b9d668-1d5e-4757-b54a-37dce8a251b9": {
            "id": "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "2344a605-9e04-41a4-af12-7cc5036e0219": {
            "id": "2344a605-9e04-41a4-af12-7cc5036e0219",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "706c4bad-64a2-4f4f-9152-55c5e695c18c": {
            "id": "706c4bad-64a2-4f4f-9152-55c5e695c18c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
            "2344a605-9e04-41a4-af12-7cc5036e0219"
        ]
    },
    "inTransitions": {
        "send_email": [
            "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
            "706c4bad-64a2-4f4f-9152-55c5e695c18c"
        ],
        "email_sent": [
            "2344a605-9e04-41a4-af12-7cc5036e0219"
        ]
    },
    "tokens": {
        "a8d7f315-90a3-4d5f-94f8-c4c70f3ddf77": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a8d7f315-90a3-4d5f-94f8-c4c70f3ddf77",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1e8e50d3-44fa-4263-b1fa-26b87e03949d",
                    "transitionId": "706c4bad-64a2-4f4f-9152-55c5e695c18c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714975125400
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "83d18a19-6530-440f-823f-f29caa3d15a2",
                    "transitionId": "706c4bad-64a2-4f4f-9152-55c5e695c18c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714975125405
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9a18680a-0c3d-4ba9-996b-f24aeb0986a0",
                    "transitionId": "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714975125408
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e2579a1b-7d7e-40a6-8fc7-bed52828e666",
                    "transitionId": "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714975125411
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5e400519-577e-483a-9f89-f1d86644c965",
                    "transitionId": "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714975125414
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "750f67b2-62d5-4d36-95ce-4e8a48210ba9",
                    "transitionId": "e4b9d668-1d5e-4757-b54a-37dce8a251b9",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714975125418
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1ce729ed-1b3f-474c-b864-a9f8689f43cb",
                    "transitionId": "2344a605-9e04-41a4-af12-7cc5036e0219",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714975125421
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e0a360de-d755-4d7e-8168-386673326bc1",
                    "transitionId": "2344a605-9e04-41a4-af12-7cc5036e0219",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714975125425
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}