Metainformationen zur Seite
  •  
Übersetzungen dieser Seite:
  • de

Back to Workflows Overview | Parent Page

Example: Create a Graph PNG from the Process

This example is based on the sequence.php example (Link).

When the process was processed, you should have a PNG file sequence-graph.png next to the sequence.php file.

sequence.php
<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Token;
use Formapro\Pvm\ProcessBuilder;
use Formapro\Pvm\Visual\BuildDigraphScript;
use Formapro\Pvm\Visual\VisualizeFlow;
use function Formapro\Values\get_values;
 
require_once __DIR__.'/vendor/autoload.php';
 
// Create the process
$process = (new ProcessBuilder())
    ->createNode('calculate_total_price', 'calculate_total_price')->end()
    ->createNode('calculate_shipping_price', 'calculate_shipping_price')->end()
 
    ->createTransition('calculate_total_price', 'calculate_shipping_price')->end()
    ->createStartTransition('calculate_total_price')->end()
 
    ->getProcess()
;
 
// Create graph
$graph = (new VisualizeFlow())->createGraph($process);
 
// Create the process engine with callbacks
$engine = new ProcessEngine(new DefaultBehaviorRegistry([
    'calculate_total_price' => function(Token $token) {
        $totalPrice = 0;
        foreach ($token->getValue('items') as $item) {
            echo 'item price: '.$item['price'].PHP_EOL;
 
            $totalPrice += $item['price'];
        }
 
        $token->setValue('total_price', $totalPrice);
 
        echo 'total price: '.$totalPrice.PHP_EOL;
    },
    'calculate_shipping_price' => function(Token $token) {
        $totalPrice = $token->getValue('total_price');
        $token->setValue('shipping_price', $totalPrice * 0.1);
 
        echo 'shipping price: '.$token->getValue('shipping_price').PHP_EOL;
    },
]));
 
 
$token = $engine->createTokenFor($process->getStartTransition());
$token->setValue('items.0.price', rand(10, 100));
$token->setValue('items.1.price', rand(10, 100));
 
$engine->proceed($token);
echo "Proceeded.\n";
 
// Get the raw tokens
$rawTokens = [];
if ($engine instanceof ProcessEngine) {
    $tokens = iterator_to_array($engine->getProcessTokens($process));
    foreach ($tokens as $token) {
        $rawTokens[] = get_values($token);
    }
    // Needed? apply tokens
    (new VisualizeFlow())->applyTokens($graph, $process, $tokens);
}
echo "Tokens applied to Viz Flow\n";
 
// Save digraph into temp file
$tmpFile = tempnam(sys_get_temp_dir(), 'dot-sequence-');
$digraph = (new BuildDigraphScript())->build($graph);
try {
    file_put_contents($tmpFile, $digraph);
    echo 'Graph Saved to file: '.$tmpFile.PHP_EOL;
    // Call dot to create PNG from graph
    exec('dot -Tpng ' . $tmpFile . ' > ' . __DIR__ . '/sequence-graph.png');
} finally {
    unlink($tmpFile);
}