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: 34
item price: 45
total price: 79
shipping price: 7.9

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": "bf472955-533c-4267-93e7-fa6b992d67f7",
    "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": {
        "dceb95b9-470d-4d24-9749-820e8ba431ad": {
            "id": "dceb95b9-470d-4d24-9749-820e8ba431ad",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "a005a1b4-f848-46fc-a917-3563619cb180": {
            "id": "a005a1b4-f848-46fc-a917-3563619cb180",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "dceb95b9-470d-4d24-9749-820e8ba431ad"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "dceb95b9-470d-4d24-9749-820e8ba431ad"
        ],
        "calculate_total_price": [
            "a005a1b4-f848-46fc-a917-3563619cb180"
        ]
    },
    "tokens": {
        "258bb12f-0036-4e19-9e90-eda75ffca73c": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "258bb12f-0036-4e19-9e90-eda75ffca73c",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3e3b048c-b5e6-4b62-a3d6-201c45c6d787",
                    "transitionId": "a005a1b4-f848-46fc-a917-3563619cb180",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450752961472
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8cc82413-7643-4059-8795-9eb80931161d",
                    "transitionId": "a005a1b4-f848-46fc-a917-3563619cb180",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450752961476
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "26989026-f4d6-444f-8057-4f84d4b7a768",
                    "transitionId": "dceb95b9-470d-4d24-9749-820e8ba431ad",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450752961479
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0a446f14-0e83-4b34-a664-bd34ee140863",
                    "transitionId": "dceb95b9-470d-4d24-9749-820e8ba431ad",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450752961483
                }
            ],
            "items": [
                {
                    "price": 34
                },
                {
                    "price": 45
                }
            ],
            "total_price": 79,
            "shipping_price": 7.9
        }
    }
}