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: 100
item price: 92
total price: 192
shipping price: 19.2

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": "41fbec8b-e2a3-4fa3-a6d1-aa05989eb16e",
    "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": {
        "a72157a3-ae0b-4142-b22b-c89c02f8fd65": {
            "id": "a72157a3-ae0b-4142-b22b-c89c02f8fd65",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "e5c4aca7-fa9e-4ec7-85c5-2900a01a9c04": {
            "id": "e5c4aca7-fa9e-4ec7-85c5-2900a01a9c04",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "a72157a3-ae0b-4142-b22b-c89c02f8fd65"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "a72157a3-ae0b-4142-b22b-c89c02f8fd65"
        ],
        "calculate_total_price": [
            "e5c4aca7-fa9e-4ec7-85c5-2900a01a9c04"
        ]
    },
    "tokens": {
        "73247285-c562-43db-9dc2-92b08557e721": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "73247285-c562-43db-9dc2-92b08557e721",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "a3e76a77-ae2e-4aeb-ae9e-1616dc7df43f",
                    "transitionId": "e5c4aca7-fa9e-4ec7-85c5-2900a01a9c04",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968783953154
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "12fd71ad-edbf-40b6-b95a-bf1a2e3db66a",
                    "transitionId": "e5c4aca7-fa9e-4ec7-85c5-2900a01a9c04",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968783953162
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "760d3f3d-0092-41f5-91fd-23e7a99fb249",
                    "transitionId": "a72157a3-ae0b-4142-b22b-c89c02f8fd65",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968783953167
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d892a62c-9d0a-4eba-8a7e-8bab4a65017d",
                    "transitionId": "a72157a3-ae0b-4142-b22b-c89c02f8fd65",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968783953173
                }
            ],
            "items": [
                {
                    "price": 100
                },
                {
                    "price": 92
                }
            ],
            "total_price": 192,
            "shipping_price": 19.200000000000003
        }
    }
}