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-Alxu7K
Read from file: /tmp/pvm-demo-Alxu7K
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": "12e6dbaa-ffa4-4ec8-9bc8-55d72735d8cc",
    "nodes": {
        "a_task": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "a_task",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "665d2f86-ec09-437e-87b3-cdff84367a76": {
            "id": "665d2f86-ec09-437e-87b3-cdff84367a76",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "a_task"
        }
    },
    "inTransitions": {
        "a_task": [
            "665d2f86-ec09-437e-87b3-cdff84367a76"
        ]
    },
    "tokens": {
        "74d35998-dddc-4734-a91e-b2c4ef49ce77": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "74d35998-dddc-4734-a91e-b2c4ef49ce77",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6173ce2e-f19c-410e-bc4c-29b2b079436a",
                    "transitionId": "665d2f86-ec09-437e-87b3-cdff84367a76",
                    "weight": 1,
                    "state": "opened",
                    "time": 15450742329920
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "99915357-0955-449d-acd5-066aa43dce19",
                    "transitionId": "665d2f86-ec09-437e-87b3-cdff84367a76",
                    "weight": 1,
                    "state": "passed",
                    "time": 15450742329925
                }
            ]
        }
    }
}