Store to File

The process could be easily serialized and unserialized because the process state is an array.
In the example we show how to create a process than store it to a file as json string. Read the file and execute the process.

Setup


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

# create such file with the code below
php store-to-file.php

Result

store-to-file.php

Saved to file: /tmp/pvm-demo-3uHaru
Read from file: /tmp/pvm-demo-3uHaru
a_task
Found the process on the filesystem

store-to-file.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\FileDAL;
use 
Formapro\Pvm\Process;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;
use 
Formapro\Pvm\ProcessBuilder;
use function 
Makasim\Values\get_values;

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

$process = (new ProcessBuilder())
    ->
createNode('a_task''print_label')->end()
    ->
createStartTransition('a_task')->end()

    ->
getProcess()
;

// store to file manually
$tmpFile tempnam(sys_get_temp_dir(), 'pvm-demo-');
try {
    
file_put_contents($tmpFilejson_encode(get_values($process)));
    echo 
'Saved to file: '.$tmpFile.PHP_EOL;

    unset(
$process);

    
$process Process::create(json_decode(file_get_contents($tmpFile), true));

    echo 
'Read from file: '.$tmpFile.PHP_EOL;
} finally {
    
unlink($tmpFile);
}

// use fileDal to store process state within the engine.
$registry = new DefaultBehaviorRegistry([
    
'print_label' => function(Token $token) {
        echo 
$token->getTo()->getId().PHP_EOL;
    },
]);

$fileDal = new FileDAL(__DIR__.'/store');

$engine = new ProcessEngine($registry$fileDal);

$token $engine->createTokenFor($process->getStartTransition());
$engine->proceed($token);

if (
file_exists(__DIR__.'/store/'.$process->getId().'.json')) {
    echo 
'Found the process on the filesystem'.PHP_EOL;
} else {
    echo 
'the process was not found on the filesystem'.PHP_EOL;
}
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "6513987d-b547-475d-9c88-d54fb734679c",
    "nodes": {
        "a_task": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "a_task",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "ee6b0ea8-09e2-4b8a-a85d-e9140f3e772d": {
            "id": "ee6b0ea8-09e2-4b8a-a85d-e9140f3e772d",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "a_task"
        }
    },
    "inTransitions": {
        "a_task": [
            "ee6b0ea8-09e2-4b8a-a85d-e9140f3e772d"
        ]
    },
    "tokens": {
        "a09ef273-af41-4b95-ac76-2907148be8ea": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a09ef273-af41-4b95-ac76-2907148be8ea",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "41607d12-f5fc-4520-89ab-2a9e7c3a05aa",
                    "transitionId": "ee6b0ea8-09e2-4b8a-a85d-e9140f3e772d",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968773104876
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0921865f-1a5c-46af-98d6-a58dd8edc60a",
                    "transitionId": "ee6b0ea8-09e2-4b8a-a85d-e9140f3e772d",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968773104880
                }
            ]
        }
    }
}