Mermaid is a markdown-like language which allows generating a wide selection of charts and diagrams and we’ll be using it to generate a flow chart of our knowledge base.
The simplest example in Mermaid is the flow chart. It starts by graph TD, then you can declare nodes and edges using ”—>“.
See? Very simple to declare a graph. Let’s see how it looks with a more complex chart. We’ll use the python code below to parse the knowledge base from the procedural story article and generate a flow chart.
withopen("story-data.pl", "r") as f: data = f.readlines()subset = []for elem in data:iflen(elem) <4:continueif elem[:4] =="edge": subset.append(elem)for i inrange(len(subset)): subset[i] = subset[i].replace("edge(", "") subset[i] = subset[i].replace(").\n", "") subset[i] = subset[i].replace("). \n", "") subset[i] = subset[i].split(", ")output = ["graph TD"]for elem in subset: output.append(elem[0] +" --> "+ elem[1])withopen("output.txt", "w+") as f:for line in output: f.write(f"{line}\n")
Thanks to our code we now get a valid mermaid flowchart definition.