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: 71
item price: 76
total price: 147
shipping price: 14.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": "48c70403-e55f-4103-9f76-a6e97a52a9c0",
    "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": {
        "2f3059f6-bbbb-4e38-9c61-8af6adff73a3": {
            "id": "2f3059f6-bbbb-4e38-9c61-8af6adff73a3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "a6d36ac9-eb23-48e6-b0b4-67ef83b4112f": {
            "id": "a6d36ac9-eb23-48e6-b0b4-67ef83b4112f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "2f3059f6-bbbb-4e38-9c61-8af6adff73a3"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "2f3059f6-bbbb-4e38-9c61-8af6adff73a3"
        ],
        "calculate_total_price": [
            "a6d36ac9-eb23-48e6-b0b4-67ef83b4112f"
        ]
    },
    "tokens": {
        "a658fde8-b0e6-4327-81a4-fa403cfc7f14": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a658fde8-b0e6-4327-81a4-fa403cfc7f14",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5879066a-1426-42a9-b3a2-4b561ba4bc1b",
                    "transitionId": "a6d36ac9-eb23-48e6-b0b4-67ef83b4112f",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635458158
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fad16159-48f5-43c5-a462-b0d005e1ea7c",
                    "transitionId": "a6d36ac9-eb23-48e6-b0b4-67ef83b4112f",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635458162
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "b8d52a33-4cc6-42a5-b79b-c48440740973",
                    "transitionId": "2f3059f6-bbbb-4e38-9c61-8af6adff73a3",
                    "weight": 1,
                    "state": "opened",
                    "time": 16031635458165
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e9fe5df6-eeae-40d7-adb8-e2eeee1c1644",
                    "transitionId": "2f3059f6-bbbb-4e38-9c61-8af6adff73a3",
                    "weight": 1,
                    "state": "passed",
                    "time": 16031635458168
                }
            ],
            "items": [
                {
                    "price": 71
                },
                {
                    "price": 76
                }
            ],
            "total_price": 147,
            "shipping_price": 14.700000000000001
        }
    }
}