如何在 Graphviz Dot 中创建从节点到子图的边?

How to create an edge from a node to a subgraph in Graphviz Dot?

我想创建一个看起来像这样的图表,我。 e.其中一条边从节点 Manufacturer of means of production 到同名子图。

我为此编写了以下代码:

digraph G {
    rankdir=LR;
    compound=true;

    graph [fontname="Liberation Mono"];
    node [fontname="Liberation Mono"];
    edge [fontname="Liberation Mono"];

    subgraph cluster0 {
        label="System components";
        mmp [label="Manufacturer of means of production", shape=box];
    }

    subgraph cluster1 {
        t1start [label="Start of tact 1", shape=point]
        t1end [label="End of tact 1", shape=point ]
        subgraph cluster1_mmp {
            label="Manufacturer of means of production"

            cluster1_1 [label="Node 1", color=white]


            subgraph cluster1_1_1 {
                label="Technological cycle 1"

                cluster1_1_1 [label="Node 2", color=white]
            }
            subgraph cluster1_1_2 {
                label="Technological cycle 2"

                cluster1_1_2 [label="Node 2", color=white]
            }
        }
    }

    subgraph cluster2 {
        label="Такт 2"
        t2start [label="Start of tact 2", shape=point]
        t2end [label="End of tact 2", shape=point]

    }
    t1end -> t2start
    mmp -> cluster1_1 [ltail=cluster1_mmp]; 
}

如果我尝试编译此代码 ("C:\Program Files (x86)\Graphviz2.38\bin\"dot.exe -Tpng -ograph.png graph.dot ),我会收到警告 Warning: mmp -> cluster1_1: tail not inside tail cluster cluster1_mmp.

如何修复它并使边缘转到子图?

更新 1:

您可以在下面找到预期结果的图像 -- 从节点到子图(子图,而不是子图中的节点)的边。下图中这条边是红色的。

更新 2:更改了如下所示的代码。

digraph G {
    rankdir=LR;
    compound=true;

    graph [fontname="Liberation Mono"];
    node [fontname="Liberation Mono"];
    edge [fontname="Liberation Mono"];

    subgraph cluster0 {
        label="System components";
        mmp [label="Manufacturer of means of production", shape=box];
    }

    subgraph cluster1 {
        t1start [label="Start of tact 1", shape=point]
        t1end [label="End of tact 1", shape=point ]
        subgraph cluster1_mmp {
            label="Manufacturer of means of production"

            testNode [label="Node 1", color=white]

            subgraph cluster1_1_1 {
                label="Technological cycle 1"

                cluster1_1_1 [label="Node 2", color=white]
            }
            subgraph cluster1_1_2 {
                label="Technological cycle 2"

                cluster1_1_2 [label="Node 2", color=white]
            }
        }
    }

    subgraph cluster2 {
        label="Такт 2"
        t2start [label="Start of tact 2", shape=point]
        t2end [label="End of tact 2", shape=point]

    }
    t1end -> t2start
    mmp -> cluster1 [ltail=cluster0, lhead=cluster1, label="           "];

}

嗯,你的意思是 "lhead=cluster1_mmp" 而不是 ltail?

您的优势指定为:

mmp -> cluster1_1 [ltail=cluster1_mmp]; 

您收到的错误消息是"Warning: mmp -> cluster1_1: tail not inside tail cluster cluster1_mmp"

这表示你的尾巴不在尾巴簇内。尾簇是cluster1_mmp。 cluster1_1,您要连接的肯定在 cluster1_mmp 内。这解释了你的困惑。

只有在使用 GraphvizFiddle 进行大量调查后,我才最终记起带箭头的尖端是 head(即语法是 tail -> head)。

因此,cluster1_1,您要说的节点在 cluster1_mmp 中是箭头的头部。这就是您的 ltail 规范不起作用的原因。将其更改为 lhead 可以消除错误消息并生成一个看起来像您的图片的图形。箭头指向子图,正是您在问题中要求的。

这里有两个 GraphvizFiddle,其中一个 of the original code generating the original error and one with ltail changed to lhead 与您的图片匹配。

(我确定我已经花了很长时间在我自己的图表中调试相同的问题。也许 Graphviz 可以获得更新以检查 ltail 参数是否对箭头有意义,反之亦然并吐出更有帮助的错误消息。)

您需要更改最后一行

mmp -> cluster1_1 [ltail=cluster1_mmp];

mmp -> cluster1_1 [lhead=cluster1 label="           "]

然后图表如期出现

此外,如果您希望边缘从框外开始,那么您可以这样做

mmp -> cluster1_1 [ltail=cluster0 lhead=cluster1 label="           "];

编辑

最终使用的代码

digraph G {
    rankdir=LR;
    compound=true;

    graph [fontname="Liberation Mono"];
    node [fontname="Liberation Mono"];
    edge [fontname="Liberation Mono"];

    subgraph cluster0 {
        label="System components";
        mmp [label="Manufacturer of means of production", shape=box];
    }

    subgraph cluster1 {
        t1start [label="Start of tact 1", shape=point]
        t1end [label="End of tact 1", shape=point ]
        subgraph cluster1_mmp {
            label="Manufacturer of means of production"

            cluster1_1 [label="Node 1", color=white]


            subgraph cluster1_1_1 {
                label="Technological cycle 1"

                cluster1_1_1 [label="Node 2", color=white]
            }
            subgraph cluster1_1_2 {
                label="Technological cycle 2"

                cluster1_1_2 [label="Node 2", color=white]
            }
        }
    }

    subgraph cluster2 {
        label="Такт 2"
        t2start [label="Start of tact 2", shape=point]
        t2end [label="End of tact 2", shape=point]

    }
    t1end -> t2start
    mmp -> cluster1_1 [lhead=cluster1 label="           "]
}

fiddlelink