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: 71
item price: 29
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": "eedc3e95-6923-46e6-81ba-8e417fc7e372",
    "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": {
        "daecabee-d939-47f3-b588-51262da5f3b0": {
            "id": "daecabee-d939-47f3-b588-51262da5f3b0",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "calculate_total_price",
            "to": "calculate_shipping_price"
        },
        "ba7fc9c2-e69e-489b-9581-8bbc2a65042a": {
            "id": "ba7fc9c2-e69e-489b-9581-8bbc2a65042a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "calculate_total_price"
        }
    },
    "outTransitions": {
        "calculate_total_price": [
            "daecabee-d939-47f3-b588-51262da5f3b0"
        ]
    },
    "inTransitions": {
        "calculate_shipping_price": [
            "daecabee-d939-47f3-b588-51262da5f3b0"
        ],
        "calculate_total_price": [
            "ba7fc9c2-e69e-489b-9581-8bbc2a65042a"
        ]
    },
    "tokens": {
        "feaeb3b3-e6b4-4129-b7f1-d22488f2852f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "feaeb3b3-e6b4-4129-b7f1-d22488f2852f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "74b3fe9d-5e1f-40d5-aee3-22890aaf903e",
                    "transitionId": "ba7fc9c2-e69e-489b-9581-8bbc2a65042a",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559356768139
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "18ffff93-ed24-458f-9f10-ee06bed3f51c",
                    "transitionId": "ba7fc9c2-e69e-489b-9581-8bbc2a65042a",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559356768148
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "60f0c518-327b-474c-94c8-bd131f32e49f",
                    "transitionId": "daecabee-d939-47f3-b588-51262da5f3b0",
                    "weight": 1,
                    "state": "opened",
                    "time": 15559356768152
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3b94073f-be1a-401a-afc8-dd4cf6b4ccf1",
                    "transitionId": "daecabee-d939-47f3-b588-51262da5f3b0",
                    "weight": 1,
                    "state": "passed",
                    "time": 15559356768156
                }
            ],
            "items": [
                {
                    "price": 71
                },
                {
                    "price": 29
                }
            ],
            "total_price": 100,
            "shipping_price": 10
        }
    }
}