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: 94
item price: 59
total price: 153
shipping price: 15.3

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": "7bd1f3c5-fa3e-431a-a329-39e0207cb06c",
    "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": {
        "afd2156c-3268-436d-a3eb-bbd796c33703": {
            "id": "afd2156c-3268-436d-a3eb-bbd796c33703",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "d9b2244c-69ea-499a-8273-8c7dfeba7e44": {
            "id": "d9b2244c-69ea-499a-8273-8c7dfeba7e44",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "afd2156c-3268-436d-a3eb-bbd796c33703"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "afd2156c-3268-436d-a3eb-bbd796c33703"
        ],
        "calculate_total_price": [
            "d9b2244c-69ea-499a-8273-8c7dfeba7e44"
        ]
    },
    "tokens": {
        "a69b008b-94c3-4389-9679-0598f7f855aa": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a69b008b-94c3-4389-9679-0598f7f855aa",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9f8519cc-2676-441c-9d29-ad03ecd683d1",
                    "transitionId": "d9b2244c-69ea-499a-8273-8c7dfeba7e44",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479165895143
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2585656c-6dde-4a14-98e1-33e38d35f3fd",
                    "transitionId": "d9b2244c-69ea-499a-8273-8c7dfeba7e44",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479165895149
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "761d5ff1-7800-4490-8ac3-9f91792a0581",
                    "transitionId": "afd2156c-3268-436d-a3eb-bbd796c33703",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479165895151
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "94b51479-d2df-4990-9abe-4e54f959b65d",
                    "transitionId": "afd2156c-3268-436d-a3eb-bbd796c33703",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479165895156
                }
            ],
            "items": [
                {
                    "price": 94
                },
                {
                    "price": 59
                }
            ],
            "total_price": 153,
            "shipping_price": 15.3
        }
    }
}