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: 89
item price: 24
total price: 113
shipping price: 11.3

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": "f3223c07-018a-48d9-a48d-1bf92050e8d2",
    "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": {
        "d48d622e-cbfe-4199-8a1e-65662e43b518": {
            "id": "d48d622e-cbfe-4199-8a1e-65662e43b518",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "3d55dc78-9086-47a7-96fa-3b31d4dec82a": {
            "id": "3d55dc78-9086-47a7-96fa-3b31d4dec82a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "d48d622e-cbfe-4199-8a1e-65662e43b518"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "d48d622e-cbfe-4199-8a1e-65662e43b518"
        ],
        "calculate_total_price": [
            "3d55dc78-9086-47a7-96fa-3b31d4dec82a"
        ]
    },
    "tokens": {
        "3d8582f2-4ec9-4748-91b7-184b2681e3ef": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "3d8582f2-4ec9-4748-91b7-184b2681e3ef",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9284fe67-cc5e-49e2-94c9-e7d5356d3063",
                    "transitionId": "3d55dc78-9086-47a7-96fa-3b31d4dec82a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906512321438
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "83881ec9-fc2f-4637-9539-7f57a5c689c1",
                    "transitionId": "3d55dc78-9086-47a7-96fa-3b31d4dec82a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906512321442
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "db860ff9-87c8-4e80-84b8-bdbf2812ae0c",
                    "transitionId": "d48d622e-cbfe-4199-8a1e-65662e43b518",
                    "weight": 1,
                    "state": "opened",
                    "time": 15906512321444
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fb6aceb3-bcfa-46c9-88fe-f6d9061a7021",
                    "transitionId": "d48d622e-cbfe-4199-8a1e-65662e43b518",
                    "weight": 1,
                    "state": "passed",
                    "time": 15906512321448
                }
            ],
            "items": [
                {
                    "price": 89
                },
                {
                    "price": 24
                }
            ],
            "total_price": 113,
            "shipping_price": 11.3
        }
    }
}