20

我正在尝试使用 Graphviz 可视化一个简单的有限状态机图。Graphviz 创建的布局并不完全符合我的喜好。我期待一个更紧凑的结果,边缘更短。

到目前为止,我已经尝试过使用组并改变边的权重,但运气不佳。我不清楚为什么 Graphviz 以它的方式绘制图形,以及如何根据我的喜好调整它的算法。我可以设置任何参数来实现这一目标吗?或者我应该使用另一个命令而不是dot?我试过neato了,但结果看起来完全一团糟,我真的不明白我在做什么......

这是我迄今为止最好的结果:

有限状态机,迄今为止最好的结果

尝试可视化比这更好的布局,我认为如果红色框的对齐方式不同,图形会看起来更好,更紧凑,例如如图中的箭头所示:

有限状态机,想象更好的结果

我用来dot创建图表,他的源代码如下:

  1 digraph JobStateDiagram
  2 {
  3   rankdir=LR;
  4   size="8,5";
  5 
  6   node  [style="rounded,filled,bold", shape=box, fixedsize=true, width=1.3, fontname="Arial"];
  7   Created   [fillcolor=black, shape=circle, label="", width=0.25];
  8   Destroyed [fillcolor=black, shape=doublecircle, label="", width=0.3];
  9   Empty     [fillcolor="#a0ffa0"];
 10   Announced [fillcolor="#a0ffa0"];
 11   Assigned  [fillcolor="#a0ffa0"];
 12   Working   [fillcolor="#a0ffa0"];
 13   Ready     [fillcolor="#a0ffa0"];
 14   TimedOut  [fillcolor="#ffa0a0"];
 15   Failed    [fillcolor="#ffa0a0"];
 16 
 17   {
 18     rank=source; Created Destroyed;
 19   }
 20 
 21   edge  [style=bold, fontname="Arial" weight=2]
 22   Empty     -> Announced [ label="announce"   ];
 23   Announced -> Assigned  [ label="assign"     ];
 24   Assigned  -> Working   [ label="start"      ];
 25   Working   -> Ready     [ label="finish"     ];
 26   Ready     -> Empty     [ label="revoke"     ];
 27 
 28   edge  [fontname="Arial" color="#aaaaaa" weight=1]
 29   Announced -> TimedOut  [ label="timeout"    ];
 30   Assigned  -> TimedOut  [ label="timeout"    ];
 31   Working   -> TimedOut  [ label="timeout"    ];
 32   Working   -> Failed    [ label="error"      ];
 33   TimedOut  -> Announced [ label="announce"   ];
 34   TimedOut  -> Empty     [ label="revoke"     ];
 35   Failed    -> Announced [ label="announce"   ];
 36   Failed    -> Empty     [ label="revoke"     ];
 37 
 38   edge  [style=bold, fontname="Arial" weight=1]
 39   Created   -> Empty     [ label="initialize" ];
 40   Empty     -> Destroyed [ label="finalize"   ];
 41   Announced -> Empty     [ label="revoke"     ];
 42   Assigned  -> Empty     [ label="revoke"     ];
 43   Working   -> Empty     [ label="revoke"     ];
 44 }

另外,如果我在上面的 Graphviz 文件中做了任何奇怪的事情,任何人都请告诉我——感谢任何反馈。


更新:

更多的实验和尝试一些建议,如用户 marapet 给出的端口,增加了我的困惑......例如,在下图中,为什么dot选择为 and 绘制这些奇怪的弯路,Working->FailedFailed->Announced不是直线?

具有奇怪长边的图

4

2 回答 2

18

对我来说,你的输出看起来不错。TimedOut并且Failed当然一直向右,因为从Working他们那里有一条边。这就是dot最好的方法,虽然您可以进行一些调整来调整 graphviz 布局,但我认为如果您想创建特定的图形布局并控制一切,最好使用其他工具。

话虽如此,我确实用graphviz快速尝试了一下。我更改了一些线以创建一条带有所有绿色节点的直线,并按照您的问题中的指示对齐红色节点。我还添加了边缘集中器 - 结果对我来说看起来并不好:

digraph JobStateDiagram
{
  rankdir=LR;
  size="8,5";
  concentrate=true;

  node  [style="rounded,filled,bold", shape=box, fixedsize=true, width=1.3, fontname="Arial"];
  Created   [fillcolor=black, shape=circle, label="", width=0.25];
  Destroyed [fillcolor=black, shape=doublecircle, label="", width=0.3];
  Empty     [fillcolor="#a0ffa0"];
  Failed    [fillcolor="#ffa0a0"];
  Announced [fillcolor="#a0ffa0"];
  Assigned  [fillcolor="#a0ffa0"];
  Working   [fillcolor="#a0ffa0"];
  Ready     [fillcolor="#a0ffa0"];
  TimedOut  [fillcolor="#ffa0a0"];

  {
    rank=source; Created; Destroyed;
  }
  {
    rank=same;Announced;Failed;
  }
  {
    rank=same;Assigned;TimedOut;
  }

  edge  [style=bold, fontname="Arial", weight=100]
  Empty     -> Announced [ label="announce"   ];
  Announced -> Assigned  [ label="assign"     ];
  Assigned  -> Working   [ label="start"      ];
  Working   -> Ready     [ label="finish"     ];
  Ready     -> Empty     [ label="revoke", weight=1     ];

  edge  [color="#aaaaaa", weight=1]
  Announced -> TimedOut  [ label="timeout"    ];
  Assigned  -> TimedOut  [ label="timeout"    ];
  Working   -> TimedOut  [ label="timeout"    ];
  Working   -> Failed    [ label="error"      ];
  TimedOut  -> Announced [ label="announce"   ];
  TimedOut  -> Empty     [ label="revoke"     ];
  Failed    -> Announced [ label="announce"   ];
  Failed    -> Empty     [ label="revoke"     ];

  Created   -> Empty     [ label="initialize" ];
  Empty     -> Destroyed [ label="finalize"   ];
  Announced -> Empty     [ label="revoke"     ];
  Assigned  -> Empty     [ label="revoke"     ];
  Working   -> Empty     [ label="revoke"     ];
}

图形输出

您还可以通过使用端口来改进以控制边缘的开始和结束位置。

至于你关于点文件中奇怪事物的问题:除了行号(这最终让我可以很好地使用我的文本编辑器的列模式)和对齐,你的文件对我来说看起来不错。我会尽可能地以类似方式构建我的点文件(图形属性、节点列表、分组、边)。请注意,节点首次出现的顺序可能会对最终布局产生影响。

于 2012-07-21T16:48:40.447 回答
3

虽然这是一个非常古老的问题,但我有类似的问题,并想分享我的结果。除了“weight”、“rank=same”的技巧,我发现这些方法可以用来调整布局结果:

  • 目录=返回
  • 添加更多边或节点并设置 style=invis

当涉及到问题中的这个特定图表时,实际上 rank=same 和 weight 将完成主要工作,而 style=invis 可以进行一些微调。所以通过添加这些行

 {
  rank=same;Announced;Failed;
 }
 {
  rank=same;Assigned;TimedOut;
 }

到文件并添加weight=1到“准备清空”边缘,并使用一些不可见的边缘来微调我得到的空间:

GraphViz 布局结果

完整的图点源:

digraph JobStateDiagram
{
  rankdir=LR;
  size="8,5";

  node  [style="rounded,filled,bold", shape=box, fixedsize=true, width=1.3, fontname="Arial"];
  Created   [fillcolor=black, shape=circle, label="", width=0.25];
  Destroyed [fillcolor=black, shape=doublecircle, label="", width=0.3];
  Empty     [fillcolor="#a0ffa0"];
  Announced [fillcolor="#a0ffa0"];
  Assigned  [fillcolor="#a0ffa0"];
  Working   [fillcolor="#a0ffa0"];
  Ready     [fillcolor="#a0ffa0"];
  TimedOut  [fillcolor="#ffa0a0"];
  Failed    [fillcolor="#ffa0a0"];

  {
    rank=source; Created Destroyed;
  }
  {
    rank=same;Announced;Failed; #change here
  }
  {
    rank=same;Assigned;TimedOut; #change here
  }

  edge  [style=bold, fontname="Arial" weight=20] #change here
  Empty     -> Announced [ label="announce"   ];
  Announced -> Assigned  [ label="assign"     ];
  Assigned  -> Working   [ label="start"      ];
  Working   -> Ready     [ label="finish"     ];
  Ready     -> Empty     [ label="revoke" weight=1 ]; #change here

  edge  [fontname="Arial" color="#aaaaaa" weight=2] #change here
  Announced -> TimedOut  [ label="timeout"    ];
  Assigned  -> TimedOut  [ label="timeout" weight=1]; #change here
  Working   -> TimedOut  [ label="timeout"    ];
  Working   -> Failed    [ label="error"      ];
  TimedOut  -> Announced [ label="announce"   ];
  TimedOut  -> Empty     [ label="revoke"     ];
  Failed    -> Announced [ label="announce"   ];
  Failed    -> Empty     [ label="revoke"     ];

  edge  [style=bold, fontname="Arial" weight=1]
  Created   -> Empty     [ label="initialize" ];
  Empty     -> Destroyed [ label="finalize"   ];
  Announced -> Empty     [ label="revoke"     ];
  Assigned  -> Empty     [ label="revoke"     ];
  Working   -> Empty     [ label="revoke"     ];
  
  Assigned  -> Working   [ label="start"  style=invis     ]; #change here 
  Assigned  -> Working   [ label="start"  style=invis     ]; #change here 
}

更新:与其将 'Failed' 和 'Announced' 放在同一等级,将 'Failed'、'Assigned' 和 'TimedOut' 放在同一等级可能会产生更好的结果,如下所示,IMO 更好地说明了 Failed 之间的相似性和差异和超时。(您必须删除 invis 边缘才能获得下图)

在此处输入图像描述

于 2020-10-29T11:07:29.053 回答