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: 90
item price: 17
total price: 107
shipping price: 10.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": "ad33cb37-ea98-4997-89ff-4a88099151a5",
    "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": {
        "3e91b9f7-fb88-4dbc-a5ce-6f70ca855c0c": {
            "id": "3e91b9f7-fb88-4dbc-a5ce-6f70ca855c0c",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "cb6d8753-6707-4906-9503-e0531ab7448e": {
            "id": "cb6d8753-6707-4906-9503-e0531ab7448e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "3e91b9f7-fb88-4dbc-a5ce-6f70ca855c0c"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "3e91b9f7-fb88-4dbc-a5ce-6f70ca855c0c"
        ],
        "calculate_total_price": [
            "cb6d8753-6707-4906-9503-e0531ab7448e"
        ]
    },
    "tokens": {
        "0a9222f8-4e05-4b92-a383-b7723d36a05e": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0a9222f8-4e05-4b92-a383-b7723d36a05e",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "382d6bc8-bf8a-4d1a-ae0d-518656861119",
                    "transitionId": "cb6d8753-6707-4906-9503-e0531ab7448e",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070812783789
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "cebfbfca-0c71-4afb-95d4-111eb1354257",
                    "transitionId": "cb6d8753-6707-4906-9503-e0531ab7448e",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070812783793
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ad5b717b-6328-4551-b6f4-f3ab70d78cd0",
                    "transitionId": "3e91b9f7-fb88-4dbc-a5ce-6f70ca855c0c",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070812783796
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "004866e7-8989-4b75-b60f-0a89be81d4a5",
                    "transitionId": "3e91b9f7-fb88-4dbc-a5ce-6f70ca855c0c",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070812783800
                }
            ],
            "items": [
                {
                    "price": 90
                },
                {
                    "price": 17
                }
            ],
            "total_price": 107,
            "shipping_price": 10.700000000000001
        }
    }
}