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": "7c3ba713-7e24-4a67-9e1d-21a78cad0460",
    "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": {
        "2e91a89a-f903-45a7-8571-e694e5dfdd4d": {
            "id": "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "retry",
            "from": "send_email",
            "to": "send_email"
        },
        "1d5ce51a-2244-4a34-8ef6-554f32996b03": {
            "id": "1d5ce51a-2244-4a34-8ef6-554f32996b03",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "send_email",
            "to": "email_sent"
        },
        "5b2633d8-be6c-4797-b546-5879ebec47b4": {
            "id": "5b2633d8-be6c-4797-b546-5879ebec47b4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "send_email"
        }
    },
    "outTransitions": {
        "send_email": [
            "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
            "1d5ce51a-2244-4a34-8ef6-554f32996b03"
        ]
    },
    "inTransitions": {
        "send_email": [
            "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
            "5b2633d8-be6c-4797-b546-5879ebec47b4"
        ],
        "email_sent": [
            "1d5ce51a-2244-4a34-8ef6-554f32996b03"
        ]
    },
    "tokens": {
        "565848ac-9ca4-4a61-998b-d4ceedc8ef1d": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "565848ac-9ca4-4a61-998b-d4ceedc8ef1d",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dd69d18f-4f64-4d90-bc96-be39a2879da2",
                    "transitionId": "5b2633d8-be6c-4797-b546-5879ebec47b4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906512973167
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "14ca14b2-0e32-45a1-9804-d85b066a9f1f",
                    "transitionId": "5b2633d8-be6c-4797-b546-5879ebec47b4",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906512973175
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "034e6016-2c36-4ff4-8a15-0af7e832b4ef",
                    "transitionId": "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906512973180
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5bc68a49-60bc-41c0-87e3-cbcf69e2f26b",
                    "transitionId": "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906512973190
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3452619c-5499-4067-ab4c-f5f6d1b98ba4",
                    "transitionId": "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906512973195
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "05d734ef-8aff-400d-b478-9d819b640944",
                    "transitionId": "2e91a89a-f903-45a7-8571-e694e5dfdd4d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906512973202
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4203d248-6d80-4acf-a885-cc3ce3dc2ace",
                    "transitionId": "1d5ce51a-2244-4a34-8ef6-554f32996b03",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906512973207
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0dddfe44-3f1c-4fe9-84dd-b2710ba00d85",
                    "transitionId": "1d5ce51a-2244-4a34-8ef6-554f32996b03",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906512973213
                }
            ],
            "maxRetries": 3,
            "counter": 3
        }
    }
}