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": "05749845-6b48-492e-b110-f3e58483e7c9",
    "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": {
        "9c5d8e3c-2310-4190-ba9a-898580a54155": {
            "id": "9c5d8e3c-2310-4190-ba9a-898580a54155",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "c000f0d9-475f-4ad9-a9b8-b0977cc4b4ee": {
            "id": "c000f0d9-475f-4ad9-a9b8-b0977cc4b4ee",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "dd11363c-fb03-4f01-a69b-19e57e83438f": {
            "id": "dd11363c-fb03-4f01-a69b-19e57e83438f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "9c5d8e3c-2310-4190-ba9a-898580a54155",
            "c000f0d9-475f-4ad9-a9b8-b0977cc4b4ee"
        ]
    },
    "inTransitions": {
        "send_email": [
            "9c5d8e3c-2310-4190-ba9a-898580a54155",
            "dd11363c-fb03-4f01-a69b-19e57e83438f"
        ],
        "email_sent": [
            "c000f0d9-475f-4ad9-a9b8-b0977cc4b4ee"
        ]
    },
    "tokens": {
        "846def2f-9458-4e58-90c7-d20fa2773964": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "846def2f-9458-4e58-90c7-d20fa2773964",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "73af28fe-66af-4bd8-b808-c77f3614fc6f",
                    "transitionId": "dd11363c-fb03-4f01-a69b-19e57e83438f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606685018049
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cccf5fca-6f33-4109-9b0e-2d006cb344f5",
                    "transitionId": "dd11363c-fb03-4f01-a69b-19e57e83438f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606685018054
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ae44ba62-e451-444a-9e0b-acb9f3c36fc2",
                    "transitionId": "9c5d8e3c-2310-4190-ba9a-898580a54155",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606685018057
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b54c8187-fa1e-4fa9-93f8-b0c621b69d13",
                    "transitionId": "9c5d8e3c-2310-4190-ba9a-898580a54155",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606685018060
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "36bd6920-8fc7-48d2-b593-9a3c9311d983",
                    "transitionId": "9c5d8e3c-2310-4190-ba9a-898580a54155",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606685018063
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "464ea29d-6a48-4f5e-ac06-d7d3a74c1a53",
                    "transitionId": "9c5d8e3c-2310-4190-ba9a-898580a54155",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606685018068
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "048486e0-4f16-4f09-a97f-a2a6ddc0f292",
                    "transitionId": "9c5d8e3c-2310-4190-ba9a-898580a54155",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606685018070
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b6a10ec2-b88b-4d0e-8844-7f6c8ee14bb4",
                    "transitionId": "9c5d8e3c-2310-4190-ba9a-898580a54155",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606685018074
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9b6c132c-8ccd-4278-9baf-f3f3b2e52924",
                    "transitionId": "c000f0d9-475f-4ad9-a9b8-b0977cc4b4ee",
                    "weight": 1,
                    "state": "opened",
                    "time": 15606685018077
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7b6ec289-383e-47f6-ab25-668154452043",
                    "transitionId": "c000f0d9-475f-4ad9-a9b8-b0977cc4b4ee",
                    "weight": 1,
                    "state": "passed",
                    "time": 15606685018080
                }
            ],
            "maxRetries": 4,
            "counter": 4
        }
    }
}