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: 15
item price: 85
total price: 100
shipping price: 10

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": "ed836c2a-d8be-4043-b0ba-f8df3ce3b493",
    "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": {
        "0359aa8a-4463-4463-9a3c-9c39beeb183a": {
            "id": "0359aa8a-4463-4463-9a3c-9c39beeb183a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "8f52e93d-b211-4b62-9b8a-16afb653c378": {
            "id": "8f52e93d-b211-4b62-9b8a-16afb653c378",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "0359aa8a-4463-4463-9a3c-9c39beeb183a"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "0359aa8a-4463-4463-9a3c-9c39beeb183a"
        ],
        "calculate_total_price": [
            "8f52e93d-b211-4b62-9b8a-16afb653c378"
        ]
    },
    "tokens": {
        "8bd754a4-ded3-41ee-9fa5-dfd5791fefc2": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "8bd754a4-ded3-41ee-9fa5-dfd5791fefc2",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0c5f46f7-27af-4f0f-aa02-d3779cf1c17c",
                    "transitionId": "8f52e93d-b211-4b62-9b8a-16afb653c378",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823431862123
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6a1b998e-bd9a-4277-96f3-4bd8afca8d8a",
                    "transitionId": "8f52e93d-b211-4b62-9b8a-16afb653c378",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823431862128
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "13a6cb50-3fb4-4a64-a3fd-c12402096e9a",
                    "transitionId": "0359aa8a-4463-4463-9a3c-9c39beeb183a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15823431862131
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9123521f-44c3-408b-840c-c9f269068d8d",
                    "transitionId": "0359aa8a-4463-4463-9a3c-9c39beeb183a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15823431862135
                }
            ],
            "items": [
                {
                    "price": 15
                },
                {
                    "price": 85
                }
            ],
            "total_price": 100,
            "shipping_price": 10
        }
    }
}