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: 78
item price: 20
total price: 98
shipping price: 9.8

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": "e0cfacec-e05b-4b2e-a9c2-ee7ec6c129b2",
    "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": {
        "b3cb3cda-d441-4358-867a-36a777f154fd": {
            "id": "b3cb3cda-d441-4358-867a-36a777f154fd",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "3a1d1899-4e7a-4694-a13a-d55d0eb7a11c": {
            "id": "3a1d1899-4e7a-4694-a13a-d55d0eb7a11c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "b3cb3cda-d441-4358-867a-36a777f154fd"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "b3cb3cda-d441-4358-867a-36a777f154fd"
        ],
        "calculate_total_price": [
            "3a1d1899-4e7a-4694-a13a-d55d0eb7a11c"
        ]
    },
    "tokens": {
        "7f40d46c-e5a5-40f0-9ba7-f6a54eba0fe9": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "7f40d46c-e5a5-40f0-9ba7-f6a54eba0fe9",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "fc6d6aab-679a-4559-b548-4572d6d983fd",
                    "transitionId": "3a1d1899-4e7a-4694-a13a-d55d0eb7a11c",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421482808600
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1e780a6b-6905-46b6-adc0-eb593275945c",
                    "transitionId": "3a1d1899-4e7a-4694-a13a-d55d0eb7a11c",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421482808604
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "5de74d6f-658d-43ce-b336-cd9961689e69",
                    "transitionId": "b3cb3cda-d441-4358-867a-36a777f154fd",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421482808607
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "017f3a29-944c-4081-898e-59804d5e76a6",
                    "transitionId": "b3cb3cda-d441-4358-867a-36a777f154fd",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421482808611
                }
            ],
            "items": [
                {
                    "price": 78
                },
                {
                    "price": 20
                }
            ],
            "total_price": 98,
            "shipping_price": 9.8
        }
    }
}