new: [workflow:mermaid] New tool to convert graph into mermaid notation

pull/8530/head
Sami Mokaddem 2022-07-13 10:42:43 +02:00
parent a048098be5
commit ebb31aadaf
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
5 changed files with 111 additions and 19 deletions

View File

@ -87,6 +87,17 @@ class WorkflowsController extends AppController
public function view($id)
{
$filters = $this->IndexFilter->harvestParameters(['format']);
if (!empty($filters['format'])) {
if ($filters['format'] == 'dot') {
$dot = $this->Workflow->getDotNotation($id);
return $this->RestResponse->viewData($dot, $this->response->type());
} else if ($filters['format'] == 'mermaid') {
$mermaid = $this->Workflow->getMermaid($id);
debug($mermaid);
return $this->RestResponse->viewData($mermaid, $this->response->type());
}
}
$this->CRUD->view($id, [
]);
if ($this->IndexFilter->isRest()) {
@ -96,12 +107,6 @@ class WorkflowsController extends AppController
$this->set('menuData', array('menuList' => 'workflows', 'menuItem' => 'view'));
}
public function getDotNotation($id)
{
$dot = $this->Workflow->getDotNotation($id);
return $this->RestResponse->viewData($dot, $this->response->type());
}
public function editor($trigger_id)
{
$modules = $this->Workflow->getModulesByType();

View File

@ -25,11 +25,11 @@ class GraphvizDOTTool
*/
public static function dot(array $graph_data)
{
$parsedGraph = GraphvizDOTTool::__parseGraph($graph_data);
$str = GraphvizDOTTool::__header();
$str .= GraphvizDOTTool::__nodes($parsedGraph['nodes']);
$str .= GraphvizDOTTool::__edges($parsedGraph['edges']);
$str .= GraphvizDOTTool::__footer();
$parsedGraph = self::__parseGraph($graph_data);
$str = self::__header();
$str .= self::__nodes($parsedGraph['nodes']);
$str .= self::__edges($parsedGraph['edges']);
$str .= self::__footer();
return $str;
}
@ -58,7 +58,7 @@ class GraphvizDOTTool
{
$str = ' {' . PHP_EOL;
foreach ($nodes as $node) {
$str .= ' ' . GraphvizDOTTool::__node($node);
$str .= ' ' . self::__node($node);
}
$str .= ' }' . PHP_EOL;
return $str;
@ -66,9 +66,9 @@ class GraphvizDOTTool
private static function __node(array $node)
{
$node_attributes = GraphvizDOTTool::NODE_STYLE[$node['data']['module_type']];
$node_attributes = self::NODE_STYLE[$node['data']['module_type']];
$node_attributes['label'] = $node['data']['name'];
$node_attributes_text = GraphvizDOTTool::__arrayToAttributes($node_attributes);
$node_attributes_text = self::__arrayToAttributes($node_attributes);
return sprintf('%s [%s]' . PHP_EOL, $node['id'], $node_attributes_text);
}
@ -77,7 +77,7 @@ class GraphvizDOTTool
$str = '';
foreach ($edges as $source_id => $target_ids) {
foreach ($target_ids as $target_id) {
$str .= ' ' . GraphvizDOTTool::__edge($source_id, $target_id);
$str .= ' ' . self::__edge($source_id, $target_id);
}
}
return $str;
@ -85,7 +85,7 @@ class GraphvizDOTTool
private static function __edge($source_id, $target_id)
{
return sprintf('%s -> %s [%s]' . PHP_EOL, $source_id, $target_id, GraphvizDOTTool::__arrayToAttributes(GraphvizDOTTool::EDGE_STYLE));
return sprintf('%s -> %s [%s]' . PHP_EOL, $source_id, $target_id, self::__arrayToAttributes(self::EDGE_STYLE));
}
private static function __arrayToAttributes(array $list)

View File

@ -0,0 +1,79 @@
<?php
App::uses('FontAwesomeHelper', 'View/Helper');
class MermaidFlowchartTool
{
const NODE_STYLE = [
'trigger' => '{{%s}}',
'logic' => '[/%s/]',
'action' => '[%s]',
];
/**
* dot Get DOT language format of the provided graph
*
* @return string
*/
public static function mermaid(array $graph_data)
{
$parsedGraph = self::__parseGraph($graph_data);
$str = self::__header();
$str .= self::__nodes($parsedGraph['nodes'], $parsedGraph['edges']);
$str .= self::__footer();
return $str;
}
private static function __parseGraph($graph_data)
{
$graphUtil = new GraphUtil($graph_data);
$nodes = $graphUtil->graph;
$edges = $graphUtil->edgeList;
return [
'nodes' => $nodes,
'edges' => $edges,
];
}
private static function __header()
{
return 'flowchart LR' . PHP_EOL;
}
private static function __footer()
{
return '';
}
private static function __nodes($nodes, $edges)
{
$str = '';
foreach ($nodes as $node) {
$str .= self::__node($nodes, $node, $edges[$node['id']]);
}
return $str;
}
private static function __node(array $all_nodes, array $node, array $edges)
{
$str = '';
foreach ($edges as $target_id) {
$target_node = $all_nodes[$target_id];
$sourceNode = self::__singleNode($node);
$targetNode = self::__singleNode($target_node);
$str .= ' ' . sprintf('%s --> %s', $sourceNode, $targetNode) . PHP_EOL;
}
return $str;
}
private static function __singleNode(array $node)
{
$str = $node['id'];
$icon = sprintf("%s:fa-%s ", FontAwesomeHelper::findNamespace($node['data']['icon']), $node['data']['icon']);
$node_content = sprintf('"%s%s"',(!empty($node['data']['icon']) ? "$icon " : ''), $node['data']['name']);
$str .= sprintf(
self::NODE_STYLE[$node['data']['module_type']],
$node_content
);
return $str;
}
}

View File

@ -1020,4 +1020,12 @@ class Workflow extends AppModel
$dot = GraphvizDOTTool::dot($workflow['Workflow']['data']);
return $dot;
}
public function getMermaid($id)
{
App::uses('MermaidFlowchartTool', 'Tools');
$workflow = $this->fetchWorkflow($id);
$mermaid = MermaidFlowchartTool::mermaid($workflow['Workflow']['data']);
return $mermaid;
}
}

View File

@ -433,13 +433,13 @@ class FontAwesomeHelper extends AppHelper
'youtube-square' => true,
'zhihu' => true,
);
public function getClass($icon)
{
return $this->findNamespace($icon) . ' fa-' . $icon;
return self::findNamespace($icon) . ' fa-' . $icon;
}
public function findNamespace($icon)
public static function findNamespace($icon)
{
return isset(self::FAB_ICONS[$icon]) ? 'fab' : 'fas';
}