venture icon indicating copy to clipboard operation
venture copied to clipboard

Generating a Graphviz dot file from `WorkflowDefinition`

Open tatupesonen opened this issue 1 year ago • 0 comments

Hi!

I've built some functionality outside of the library that allows you to generate a Graphviz dot file directly from the WorkflowDefinition. Is this something you'd be interested in upstreaming to your package?

Currently I've manually made the $id, $dependents and $instance fields of Node of the library public because it's needed for my custom extension to work, but if it's integrated into the library the API would largely stay the same.

Here's the source code I currently use for it:

    public function handle()
    {
        $workflow = new ExampleWorkflow();
        $graph = $workflow->definition()->graph();
        echo 'digraph G {' . PHP_EOL;
        echo '    rankdir="LR";' . PHP_EOL;
        echo '    graph [pad="0.5", nodesep="1", ranksep="2"]' . PHP_EOL;
        array_walk($graph->graph, function ($n) use (&$buf) {
            $this->getDot($n, $buf);
        });
    }

    public function getDot(Node $node, &$dot, &$visited = [])
    {
        $visited[$node->id] = true;
        if ($node->dependents) {
            foreach ($node->dependents as $dep) {
                echo '    "' . $node->id . '" -> { "' . $dep->id . '" }' . PHP_EOL;
                if (!$visited[$node->id]) {
                    $this->getDot($dep, $dot, $visited);
                }
            }
        }
    }

tatupesonen avatar Aug 07 '23 09:08 tatupesonen