Condition

The example shows how to go only part of out transitions.

For this, out transitions should have names and a condition behavior returns one of them.

Setup


mkdir ~/pvm-condition
cd ~/pvm-condition
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev

# create such file with the code below
php condition.php

Result

condition.php

Total price (free if >= 50): 79
free_shipping

condition.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\ProcessBuilder;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;

require_once 
__DIR__.'/vendor/autoload.php';

$process = (new ProcessBuilder())
    ->
createNode('shipping_decision''shipping_decision')->end()
    ->
createNode('free_shipping''print_label')->end()
    ->
createNode('ups_shipping''print_label')->end()

    ->
createTransition('shipping_decision''free_shipping''free')->end()
    ->
createTransition('shipping_decision''ups_shipping''ups')->end()
    ->
createStartTransition('shipping_decision')->end()

    ->
getProcess()
;

$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    
'print_label' => function(Token $token) {
        echo 
$token->getTo()->getId().PHP_EOL;
    },
    
'shipping_decision' => function(Token $token) {
        echo 
'Total price (free if >= 50): '$token->getValue('total_price').PHP_EOL;

        return 
$token->getValue('total_price') > 50 'free' 'ups';
    }
]));

$token $engine->createTokenFor($process->getStartTransition());
$token->setValue('total_price'rand(10100));

$engine->proceed($token);
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "efc15278-82bd-4fff-896e-7a5f43aee02d",
    "nodes": {
        "shipping_decision": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "shipping_decision",
            "behavior": "shipping_decision"
        },
        "free_shipping": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "free_shipping",
            "behavior": "print_label"
        },
        "ups_shipping": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "ups_shipping",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "7635f404-249e-4f26-b294-eccb0b8fbb14": {
            "id": "7635f404-249e-4f26-b294-eccb0b8fbb14",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "free",
            "from": "shipping_decision",
            "to": "free_shipping"
        },
        "47de6026-2c8c-4629-ac2c-18a1dab30364": {
            "id": "47de6026-2c8c-4629-ac2c-18a1dab30364",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "name": "ups",
            "from": "shipping_decision",
            "to": "ups_shipping"
        },
        "5e4ea9bd-53e9-4aeb-826c-cc6aaf280054": {
            "id": "5e4ea9bd-53e9-4aeb-826c-cc6aaf280054",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "shipping_decision"
        }
    },
    "outTransitions": {
        "shipping_decision": [
            "7635f404-249e-4f26-b294-eccb0b8fbb14",
            "47de6026-2c8c-4629-ac2c-18a1dab30364"
        ]
    },
    "inTransitions": {
        "free_shipping": [
            "7635f404-249e-4f26-b294-eccb0b8fbb14"
        ],
        "ups_shipping": [
            "47de6026-2c8c-4629-ac2c-18a1dab30364"
        ],
        "shipping_decision": [
            "5e4ea9bd-53e9-4aeb-826c-cc6aaf280054"
        ]
    },
    "tokens": {
        "475f6389-ea07-499f-a280-23e4c97d5700": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "475f6389-ea07-499f-a280-23e4c97d5700",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "45d0d087-cc0e-420f-ab66-33d449600534",
                    "transitionId": "5e4ea9bd-53e9-4aeb-826c-cc6aaf280054",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968811386611
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "e5a5a0e8-3c59-464f-8140-2fb3998caea4",
                    "transitionId": "5e4ea9bd-53e9-4aeb-826c-cc6aaf280054",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968811386615
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "73a30f44-5732-4b28-a33c-6d02b26f576e",
                    "transitionId": "7635f404-249e-4f26-b294-eccb0b8fbb14",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968811386618
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "7e789192-b25e-4fd1-a56b-c610d7d670e4",
                    "transitionId": "7635f404-249e-4f26-b294-eccb0b8fbb14",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968811386621
                }
            ],
            "total_price": 79
        }
    }
}