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-pdkWIH
Read from file: /tmp/pvm-demo-pdkWIH
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": "e9d9765b-0304-472f-9b4b-acea5076de1b",
    "nodes": {
        "a_task": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "a_task",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "6870fd1a-a3ab-4b9d-86fa-d8fb06b10b02": {
            "id": "6870fd1a-a3ab-4b9d-86fa-d8fb06b10b02",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "a_task"
        }
    },
    "inTransitions": {
        "a_task": [
            "6870fd1a-a3ab-4b9d-86fa-d8fb06b10b02"
        ]
    },
    "tokens": {
        "6bfabc6b-82b3-456c-ad45-50fc797b470b": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "6bfabc6b-82b3-456c-ad45-50fc797b470b",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0c6a287e-a972-4893-8267-282680b154c1",
                    "transitionId": "6870fd1a-a3ab-4b9d-86fa-d8fb06b10b02",
                    "weight": 1,
                    "state": "opened",
                    "time": 15588049928275
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "d514805b-33a9-4f95-bc3a-c125c6d62028",
                    "transitionId": "6870fd1a-a3ab-4b9d-86fa-d8fb06b10b02",
                    "weight": 1,
                    "state": "passed",
                    "time": 15588049928291
                }
            ]
        }
    }
}