Sequence

The example shows how two tasks could be executed one by one.

Setup


mkdir ~/pvm-sequence
cd ~/pvm-sequence
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php sequence.php

Result

sequence.php

item price: 57
item price: 17
total price: 74
shipping price: 7.4

sequence.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('calculate_total_price''calculate_total_price')->end()
    ->
createNode('calculate_shipping_price''calculate_shipping_price')->end()

    ->
createTransition('calculate_total_price''calculate_shipping_price')->end()
    ->
createStartTransition('calculate_total_price')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'calculate_total_price' => function(Token $token) {
        
$totalPrice 0;
        foreach (
$token->getValue('items') as $item) {
            echo 
'item price: '.$item['price'].PHP_EOL;

            
$totalPrice += $item['price'];
        }

        
$token->setValue('total_price'$totalPrice);

        echo 
'total price: '.$totalPrice.PHP_EOL;
    },
    
'calculate_shipping_price' => function(Token $token) {
        
$totalPrice $token->getValue('total_price');
        
$token->setValue('shipping_price'$totalPrice 0.1);

        echo 
'shipping price: '.$token->getValue('shipping_price').PHP_EOL;
    },
]));

$token $engine->createTokenFor($process->getStartTransition());
$token->setValue('items.0.price'rand(10100));
$token->setValue('items.1.price'rand(10100));

$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "c9ceebcd-ec28-4744-a530-d4bfc688df58",
    "nodes": {
        "calculate_total_price": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "calculate_total_price",
            "behavior": "calculate_total_price"
        },
        "calculate_shipping_price": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "calculate_shipping_price",
            "behavior": "calculate_shipping_price"
        }
    },
    "transitions": {
        "f4f46e51-80cc-4b4e-835b-33c4ff40a177": {
            "id": "f4f46e51-80cc-4b4e-835b-33c4ff40a177",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "6cd8e500-e4bf-47b8-b3bf-139fde93e19e": {
            "id": "6cd8e500-e4bf-47b8-b3bf-139fde93e19e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "f4f46e51-80cc-4b4e-835b-33c4ff40a177"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "f4f46e51-80cc-4b4e-835b-33c4ff40a177"
        ],
        "calculate_total_price": [
            "6cd8e500-e4bf-47b8-b3bf-139fde93e19e"
        ]
    },
    "tokens": {
        "4174b928-e467-4a3f-9c10-bcca20c75e99": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "4174b928-e467-4a3f-9c10-bcca20c75e99",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f97054b7-41c1-4fd3-ae71-cee597f0df73",
                    "transitionId": "6cd8e500-e4bf-47b8-b3bf-139fde93e19e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691102974426
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d4c7de32-88ba-4f2d-b4fb-4e7d938b97b8",
                    "transitionId": "6cd8e500-e4bf-47b8-b3bf-139fde93e19e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691102974431
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ccae0335-7870-4183-91c2-9828a0eef646",
                    "transitionId": "f4f46e51-80cc-4b4e-835b-33c4ff40a177",
                    "weight": 1,
                    "state": "opened",
                    "time": 15691102974434
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a203718d-0a58-422a-b44f-305166dba8b6",
                    "transitionId": "f4f46e51-80cc-4b4e-835b-33c4ff40a177",
                    "weight": 1,
                    "state": "passed",
                    "time": 15691102974438
                }
            ],
            "items": [
                {
                    "price": 57
                },
                {
                    "price": 17
                }
            ],
            "total_price": 74,
            "shipping_price": 7.4
        }
    }
}