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: 74
item price: 83
total price: 157
shipping price: 15.7

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": "8bd3cfca-3aaf-4959-b993-d3d1259a02ce",
    "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": {
        "54123514-1876-4637-ac7a-5c8796c72554": {
            "id": "54123514-1876-4637-ac7a-5c8796c72554",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "0faee96f-5b94-452f-aef9-979ecf208daa": {
            "id": "0faee96f-5b94-452f-aef9-979ecf208daa",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "54123514-1876-4637-ac7a-5c8796c72554"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "54123514-1876-4637-ac7a-5c8796c72554"
        ],
        "calculate_total_price": [
            "0faee96f-5b94-452f-aef9-979ecf208daa"
        ]
    },
    "tokens": {
        "938c76f4-793e-42ac-8363-e49762eb075f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "938c76f4-793e-42ac-8363-e49762eb075f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4d40f1dc-7669-426a-bd03-b6eb42e8ec19",
                    "transitionId": "0faee96f-5b94-452f-aef9-979ecf208daa",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664812641270
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4f5746df-2442-4b50-a7ef-1d5bc27c0fca",
                    "transitionId": "0faee96f-5b94-452f-aef9-979ecf208daa",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664812641276
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2fc8e2a1-128a-4d6f-98c9-c12823873d0e",
                    "transitionId": "54123514-1876-4637-ac7a-5c8796c72554",
                    "weight": 1,
                    "state": "opened",
                    "time": 15664812641281
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "58c900a3-d1fd-4ced-8acc-51ef89a4140a",
                    "transitionId": "54123514-1876-4637-ac7a-5c8796c72554",
                    "weight": 1,
                    "state": "passed",
                    "time": 15664812641290
                }
            ],
            "items": [
                {
                    "price": 74
                },
                {
                    "price": 83
                }
            ],
            "total_price": 157,
            "shipping_price": 15.700000000000001
        }
    }
}