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-y3VSgE
Read from file: /tmp/pvm-demo-y3VSgE
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": "0de6affc-02e0-4b72-9360-e2c317c660e8",
    "nodes": {
        "a_task": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "a_task",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "f67fe0be-fc4c-4e12-8765-5e7e9a7f01d5": {
            "id": "f67fe0be-fc4c-4e12-8765-5e7e9a7f01d5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "a_task"
        }
    },
    "inTransitions": {
        "a_task": [
            "f67fe0be-fc4c-4e12-8765-5e7e9a7f01d5"
        ]
    },
    "tokens": {
        "74b552f0-56d3-4aab-9776-38048800cea2": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "74b552f0-56d3-4aab-9776-38048800cea2",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6e6e2fb6-4865-4201-bd46-b3ce026ebef3",
                    "transitionId": "f67fe0be-fc4c-4e12-8765-5e7e9a7f01d5",
                    "weight": 1,
                    "state": "opened",
                    "time": 16070802220911
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d140c9bc-b8ed-44c8-ab76-106d28f9e17e",
                    "transitionId": "f67fe0be-fc4c-4e12-8765-5e7e9a7f01d5",
                    "weight": 1,
                    "state": "passed",
                    "time": 16070802220915
                }
            ]
        }
    }
}