python - 将某种 XML/Json 文件编译成 Graphiz/有限状态自动机。有什么建议么?

标签 python turing-machines depth-first-search automata

我有一个任务,我需要拍摄一些现有的图片[显示一些自动机(DFA、NFA、图灵机)]并以某种方式将它们转换为一种格式,这使我能够使用数据将其表示为自动机以及将其编译成某种图形表示形式。你们中有人以前做过类似的事情吗?是否有任何 Python 库/框架可以让我以图形方式呈现一些自动机数据?

最佳答案

Graphviz 可以提供解决方案。从图中可以看出,有向无环图 (DAG) 的数据表示是直接且简单的。如果您按照您的评论建议手动进行操作,则可以轻松地从图表中“读取”它。下面列出了复杂图的表示(几个不同的面板,每个面板包含一个独立的 DAG)。正如 @Constantin 所说,DFA 和 NFA 可以表示为 DAG。我不确定图灵机使用什么符号,但有几种结构化图的结构可以以类似的方式读取,例如树结构;无向图。我还附上了结果图的副本。.dot 文件的各个行是您正在寻找的数据项。

    Digraph {
graph [label="Problem Frame\nmapping editor\n",labelloc=t,fontsize=18,compound=true];
node[shape = record,fontsize = 10];
edge[arrowtail=none,arrowhead=none,arrowsize=0.8,color=ivory4,fontsize=8];

subgraph "cluster0" {
graph [label = "Model Fragment"];
A01 [label = "{Domain|class::marking\lisTemplate::boolean default false\lname::name\ltype::domain type\l}"];
A02 [label = "{Requirement|isTemplate::boolean default false\lname::name\l}"];
A03 [label = "{Requirement Reference\n\<\<associative\>\>|content::name\lis template::boolean default false\ltype::requirement reference type\l}",shape=Mrecord,style=dotted];
A04 [label = "{Shared Phenomena Set\n\<\<associative\>\>|content::name\lis template::boolean default false\ltype::phenomena type\l}",shape=Mrecord,style=dotted];

/* 1:1-0:M */
edge[dir=both,arrowhead=crowodot,arrowtail=none];
A01 -> A03 [style=dashed];
A01 -> A04 [style = dashed];
A01 -> A04 [style = dashed];
A02 -> A03 [style = dashed];
}

subgraph "cluster1" {
graph [label = "\>\>\>",fontsize = 24];
B01 [label = "{Domain}"];
B02 [label = "{Requirement}"];
B03 [label = "{Requirement\nReference\n}",shape=Mrecord,style=dotted];
B04 [label = "{Shared\nPhenomena\nSet\n}",shape=Mrecord,style=dotted];
F01 [label = "{C0001|if \[-\> controls -\> describes.isTemplate\]\l}"];
F02 [label = "{C0002|if not \[-\> controls -\> describes.isTemplate\]\l}"];
F03 [label = "{C0003|if \[-\> controls -\> describes.type = designed\]\l}"];
F04 [label = "{C0004|if \[-\> controls -\> describes.type = given\]\l}"];
F05 [label = "{C0005|if \[-\> controls -\> describes.type = machine\]\l}"];
F06 [label = "{C0006|if \[-\> controls -\> describes.marking = biddable\]\l}"];
F07 [label = "{C0007|if \[-\> controls -\> describes.marking = causal\]\l}"];
F08 [label = "{C0008|if \[-\> controls -\> describes.marking = lexical\]\l}"];
F09 [label = "{C0009|if \[-\> controls -\> describes.marking = null\]\l}"];
F10 [label = "{C0010|if \[-\> controls -\> describes.isTemplate\]\l}"];
F11 [label = "{C0011|if not \[-\> controls -\> describes.isTemplate\]\l}"];
F12 [label = "{C0012|if \[-\> controls -\> describes.isTemplate\]\l}"];
F13 [label = "{C0013|if not \[-\> controls -\> describes.isTemplate\]\l}"];
F14 [label = "{C0014|if \[-\> controls -\> describes.type = non-constraining\]\l}"];
F15 [label = "{C0015|if not \[-\> controls -\> describes.type = constraining\]\l}"];
F16 [label = "{C0016|if \[-\> controls -\> describes.isTemplate\]\l}"];
F17 [label = "{C0017|if not \[-\> controls -\> describes.isTemplate\]\l}"];
F18 [label = "{C0018|if \[-\> controls -\> describes.type = causal\]\l}"];
F19 [label = "{C0019|if \[-\> controls -\> describes.type = event\]\l}"];
F20 [label = "{C0020|if \[-\> controls -\> describes.type = symbolic\]\l}"];

edge [style = solid];
B01 -> F01 -> F02 -> F03 -> F04 -> F05 -> F06 -> F07 -> F08 -> F09;
B02 -> F10 -> F11;
B03 -> F12 -> F13 -> F14 -> F15;
B04 -> F16 -> F17 -> F18 -> F19 -> F20;

edge [style = invis];
B01 -> B02 -> B03 -> B04;
}

subgraph "cluster2" {
graph [label = "\<\<\<",fontsize = 24];
C01 [label = "{Edge|name := Constraining Reference\larrowtail := normal\ldir := both\lpermitted node1 := domain icon\lpermitted node2 := requirement icon\lstyle := dotted\l}"];
D02 [label = "{Attribute|name::oName\lvalue::-\> describes\l-\> described by.content\l}"];
C02 [label = "{Diagram|name := Frame Diagram\l}"];
C03 [label = "{Node|name := Domain Icon\lcolor = gray\lfillcolor = gold\lfontsize := 12\llabel := describes.preLabel\l + oName + describes.postLabel\lshape := Mrecord\lstyle := filled\l}"];
D03 [label = "{Attribute|name::oClass\lvalue :=-\> describes\l-\> described by.class\l}"];
D04 [label = "{Attribute|name::oName\lvalue := -\> describes\l-\> described by.name\l}"];
D05 [label = "{Attribute|name::postlabel\lvalue := \}\"\l}"];
D06 [label = "{Attribute|name::postlabel\lvalue := \|\{\|b\}\}\"\l}"];
D07 [label = "{Attribute|name::postlabel\lvalue := \|\{\|c\}\}\"\l}"];
D08 [label = "{Attribute|name::postlabel\lvalue := \|\{\|x\}\}\"\l}"];
D09 [label = "{Attribute|name::prelabel\lvalue := \"\{\|\l}"];
D10 [label = "{Attribute|name::prelabel\lvalue := \"\{\l}"];
D11 [label = "{Attribute|name::prelabel\lvalue := \"\{\|\|\l}"];
D12 [label = "{Attribute|name::oType\lvalue := -\> describes \l-\> described by.type\l}"];
C04 [label = "{Holding Box|name := Domain Template\lcolor := slategray\lfillcolor := white\lfontcolor := slategray\lfontsize := 9\llabel := oName\lreadonly := true\l}"];
D13 [label = "{Attribute|name::oName\lvalue := -\> describes \l-\> described by.name\l}"];
C05 [label = "{Edge|name := Edge Template\lcolor := white\llabel = oName\lstyle := invis\l}"];
D14 [label = "{Attribute|name::oName\lvalue := -\> describes \l-\> described by.contents\l}"];
C06 [label = "{Node|name := Phenomena\l}"];
D15 [label = "{Attribute|name::oName\lvalue::-\> describes\l\-\> described by.contents\l}"];
C07 [label = "{Edge|name := Reference\l}"];
D16 [label = "{Attribute|name::oName\lvalue := -\> describes \l-\> described by.contents\l}"];
C08 [label = "{Node|name := Requirement Icon\l}"];
D17 [label = "{Attribute|name::oName\lvalue := -\> describes \l-\> described by.name\l}"];
C09 [label = "{Edge|name := Shared Phenomena\l}"];
D18 [label = "{Attribute|name::oName\lvalue := -\> describes \l-\> described by.contents\l}"];
D19 [label = "{Attribute|name::oType\lvalue := C\l}"];
D20 [label = "{Attribute|name::oType\lvalue := E\l}"];
D21 [label = "{Attribute|name::oType\lvalue := Y\l}"];

C01 -> D02;
C03 -> D03 -> D04 -> D05 -> D06 -> D07 -> D08 -> D09 -> D10 -> D11 -> D12;
C04 -> D13;
C05 -> D14;
C06 -> D15;
C07 -> D16;
C08 -> D17;
C09 -> D18 -> D19 -> D20 -> D21;

edge[style="invis"];
C01 -> C02 -> C03 -> C04 -> C05 -> C06 -> C07 -> C08 -> C09;
}

subgraph "cluster5" {
graph [label = "Editor Elements"];
E01 [label = "{Node\n|color::color\lfillcolor::fillcolor\lfontname::font\lfontsize::fontsize\llabel::name\lname::name\lreadonly::boolean default false\lshape::shape\lstyle::style\l}"];
E02 [label = "{Edge\n|arrowtail::edge end\ldir::dir\lname::name\lpermitted node1::name\lpermitted node2::name\lstyle::style\l}"];
E03 [label = "{Attribute\n|name::name\lvalue::text\l}"];
E04 [label = "{Diagram\n|defaults::attributes\ledge defaults::attributes\lname::name\lnode attributes::attributes\l}"];
E05 [label = "{Holding Box|color::color\lfillcolor::fillcolor\lfontname::font\lfontsize::fontsize\llabel::name\lname::name\lreadonly::boolean default false\lshape::shape\lstyle::style\l}"];

/* 0:1-N:M */
E01 -> E02 [arrowhead = crowodot, label = "links", taillabel = " 2:2"];

/* 1:1-0:M  */
edge[dir=both,arrowtail=none,arrowhead=crowodot];
E04 -> E01 [label = nodes];
E04 -> E02 [label = edges];
E04 -> E05 [label = "holding boxes"];

/* 0:1-0:M  */
edge[dir=both,arrowtail=odot,arrowhead=crowodot];
E05 -> E01 [label = "contained nodes"];
E05 -> E02 [label = "contained edges"];
E05 -> E05 [label = contains];

/* 0:1-0:M  */
edge[dir=both,arrowtail=odot,arrowhead=crowodot];
E01 -> E03 [label = characteristics];
E02 -> E03 [label = parameters];
E04 -> E03 [label = attributes];
E04 -> E03 [label = attributes];
E04 -> E03 [label = attributes];
}

{rank = min B01 C01}

edge[style="solid"];
F01 -> C04 [ltail = cluster1];
F02 -> C03 [ltail = cluster1];
F03 -> D09 [ltail = cluster1];
F04 -> D10 [ltail = cluster1];
F05 -> D11 [ltail = cluster1];
F06 -> D06 [ltail = cluster1];
F07 -> D07 [ltail = cluster1];
F08 -> D08 [ltail = cluster1];
F09 -> D05 [ltail = cluster1];
F10 -> C04 [ltail = cluster1];
F11 -> C06 [ltail = cluster1];
F11 -> C08 [ltail = cluster1];
F12 -> C05 [ltail = cluster1];
F14 -> C07 [ltail = cluster1];
F15 -> C01 [ltail = cluster1];
F16 -> C05 [ltail = cluster1];
F17 -> C06 [ltail = cluster1];
F17 -> C09 [ltail = cluster1];
F18 -> D19 [ltail = cluster1];
F19 -> D20 [ltail = cluster1];
F20 -> D21 [ltail = cluster1]; 
}

alt text

关于python - 将某种 XML/Json 文件编译成 Graphiz/有限状态自动机。有什么建议么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4077117/

相关文章:

java - 在非常大的树上执行 DFS 的最佳方法是什么?

scala - 函数式风格提前退出深度优先递归

python - 如何在 Keras 中创建随时间变化的损失函数

prolog - 是纯Prolog Turing-complete,如果是,为什么不能实现列表交集?

python - 使用另一个多索引系列屏蔽数据框

complexity-theory - 程序在确定性和非确定性图灵机上的运行时间

language-agnostic - 我的简单图灵机

javascript - 使用深度优先搜索算法的无限循环错误

python - 如何在 Django 1.11 中导入和使用 csrf token ?

Python - 创建索引时出现KeyError