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: 73
item price: 57
total price: 130
shipping price: 13

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": "e71c32cb-1cb3-417d-a86d-5218206835ba",
    "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": {
        "5077449f-0205-4fa0-8c30-6834bd4ffbed": {
            "id": "5077449f-0205-4fa0-8c30-6834bd4ffbed",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "30b94ec3-086f-4589-86cf-19e8c779658b": {
            "id": "30b94ec3-086f-4589-86cf-19e8c779658b",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "5077449f-0205-4fa0-8c30-6834bd4ffbed"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "5077449f-0205-4fa0-8c30-6834bd4ffbed"
        ],
        "calculate_total_price": [
            "30b94ec3-086f-4589-86cf-19e8c779658b"
        ]
    },
    "tokens": {
        "64c6da72-0c64-4fbb-b027-a45b9b3d0648": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "64c6da72-0c64-4fbb-b027-a45b9b3d0648",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "514d9bdb-40ba-47bf-bae3-b6e0bdcbb205",
                    "transitionId": "30b94ec3-086f-4589-86cf-19e8c779658b",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714974661204
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9079283a-8855-49dd-9d58-eda809f38a7f",
                    "transitionId": "30b94ec3-086f-4589-86cf-19e8c779658b",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714974661208
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4f5658f6-1dda-4eda-95c0-5309520ede88",
                    "transitionId": "5077449f-0205-4fa0-8c30-6834bd4ffbed",
                    "weight": 1,
                    "state": "opened",
                    "time": 15714974661210
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a6c2a1b5-5ff0-47f2-b1a7-1d9e5890cd1a",
                    "transitionId": "5077449f-0205-4fa0-8c30-6834bd4ffbed",
                    "weight": 1,
                    "state": "passed",
                    "time": 15714974661214
                }
            ],
            "items": [
                {
                    "price": 73
                },
                {
                    "price": 57
                }
            ],
            "total_price": 130,
            "shipping_price": 13
        }
    }
}