Graphviz 语法快捷方式

Graphviz syntax shortcuts

在 Graphviz 中编写巨大的图表很乏味。有一天我会以自定义格式编写图表,然后将其转换为 DOT format, but currently I want to find out the ways to write DOT files manually as succinctly as possible. I currently know of only one syntax shortcuta -- {b c}; 等同于 a -- b; a -- c;。目前DOT文件中有很多重复的模式,比如很多边有相同的label。我可以这样写吗:

// something that expands -m> into [label=meaning]
"English/cat" -m> "Meaning/cat_(Felidae)";
"English/cat" -m> "Meaning/domestic_cat";
"English/cat" -m> "Meaning/catfish";
"English/cat" -m> "Meaning/jazz_player";
"English/cat" -m> "Meaning/cat_(nautical)";

这样它就相当于下面的:

"English/cat" -> "Meaning/cat_(Felidae)" [label=meaning];
"English/cat" -> "Meaning/domestic_cat" [label=meaning];
"English/cat" -> "Meaning/catfish" [label=meaning];
"English/cat" -> "Meaning/jazz_player" [label=meaning];
"English/cat" -> "Meaning/cat_(nautical)" [label=meaning];

这可能吗? Graphviz 中是否有任何其他可能的语法快捷方式可以使 DOT 文件更简单和更短?如果你能在答案中编译所有这些方法,我会很高兴。

dot language 允许 graphsnodesedges 的默认属性。这些默认属性对在默认属性之后定义的任何新 graph/node/edge 有效,或者直到定义其他默认属性为止。

您的示例可以这样重写:

// label for all edges from here on
edge [label=meaning];
"English/cat" -> "Meaning/cat_(Felidae)";
"English/cat" -> "Meaning/domestic_cat";
"English/cat" -> "Meaning/catfish";
"English/cat" -> "Meaning/jazz_player";
"English/cat" -> "Meaning/cat_(nautical)";

// no/empty label from here on
edge[label=""];
"English/cat" -> "Other";

同样适用于节点和图形,只需使用 graph [a=b, c=d, ...]node [a=b, c=d, ...]

展开, you can put default node/edge/graph attributes into a subgraph (see DOT language documentation),所以属性的改变只会影响子图的范围。在这里,只有边 c -> de -> f 将附加标签 "meaning",而边 a -> bg -> h 将没有标签:

a -> b;
subgraph {
  edge[label="meaning"];
  c -> d;
  e -> f;
}
g -> h;