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: 92
item price: 39
total price: 131
shipping price: 13.1

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": "a2055f7c-4874-471c-a858-0a41230f1ff7",
    "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": {
        "7a39437b-7e72-4ea6-9ee0-1a705e258098": {
            "id": "7a39437b-7e72-4ea6-9ee0-1a705e258098",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "ce3c5394-7ae8-41c6-b646-2f6442c52026": {
            "id": "ce3c5394-7ae8-41c6-b646-2f6442c52026",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "7a39437b-7e72-4ea6-9ee0-1a705e258098"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "7a39437b-7e72-4ea6-9ee0-1a705e258098"
        ],
        "calculate_total_price": [
            "ce3c5394-7ae8-41c6-b646-2f6442c52026"
        ]
    },
    "tokens": {
        "17251bb9-5218-426b-8ea5-f3246ef3d239": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "17251bb9-5218-426b-8ea5-f3246ef3d239",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e3003a30-9566-40f0-9410-b83fd20617c0",
                    "transitionId": "ce3c5394-7ae8-41c6-b646-2f6442c52026",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857736977750
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e5e8bb46-3411-4ed3-a6ab-9c9d0c6eda6e",
                    "transitionId": "ce3c5394-7ae8-41c6-b646-2f6442c52026",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857736977757
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8302ee67-c56b-4b1b-96e4-b6c075261f9a",
                    "transitionId": "7a39437b-7e72-4ea6-9ee0-1a705e258098",
                    "weight": 1,
                    "state": "opened",
                    "time": 15857736977762
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dd609218-27c4-48d1-af58-bee1e511ae85",
                    "transitionId": "7a39437b-7e72-4ea6-9ee0-1a705e258098",
                    "weight": 1,
                    "state": "passed",
                    "time": 15857736977769
                }
            ],
            "items": [
                {
                    "price": 92
                },
                {
                    "price": 39
                }
            ],
            "total_price": 131,
            "shipping_price": 13.100000000000001
        }
    }
}