adopted to graphviz version 10.0.1 and MACOS Sonoma
[luagraph.git] / examples / exam1.lua
blob98a81ddcef266b6c3a1b3982fec93d24ad89b1ac
1 gr = require "graph"
3 --
4 -- Simple printing
5 --
6 local function printf(fmt, ...)
7 print(string.format(fmt, ...))
8 end
10 --
11 -- Convenience
13 local node, edge, subgraph, cluster, digraph =
14 gr.node, gr.edge, gr.subgraph, gr.cluster, gr.digraph
15 local s = tostring
18 -- A node constructor
20 xn = node{"xn", color="red"}
23 -- The graph
25 local g = digraph{"G",
26 -- Prototype
27 node = {shape = "box", color = "blue", style = "filled", fillcolor = "lightgrey"},
28 edge = {color = "red"},
29 graph = {style = "filled", label = ""},
31 -- General graph properties
32 rankdir = "LR",
33 compound = "1",
35 -- Graph objects
36 cluster{"SG1",
37 fillcolor = "grey",
38 node{"SomeNode", fillcolor = "cyan"},
39 edge{
40 node{"sn1", style = "filled", fillcolor = "green"},
41 node{"sn2"},
42 xn,
43 label = "hello world",
44 dir = "both"
47 cluster{"SG2",
48 fillcolor = "green",
49 node{"SomeOtherNode"},
51 edge{
52 node{"n1", shape = "box", color = "black"},
53 node{"n2", shape = "circle"},
54 node{"n3", shape = "ellipse"},
55 xn,
56 label = "n1=>n2=>n3",
57 color = "green"
59 edge{"sn2", "n1", label="sn1=>n1"},
60 edge{"SomeNode", "SomeOtherNode", label="this=>other", color="magenta"},
61 edge{"SomeNode", "SomeNode", label="this=>this", color="grey"},
62 edge{"SomeOtherNode", "sn1", ltail="cluster_SG2", lhead="cluster_SG1", label="compound"},
65 printf("GRAPH: %q", g.name)
67 -- Print subgraphs/clusters, nodes and edges
68 for sg in g:walk() do
69 printf(" SUBGRAPH: %q", sg.name)
70 for n in sg:walknodes() do
71 printf(" NODE: %q, shape=%q, color=%q",
72 s(n.name), s(n.shape), s(n.color))
73 for e in n:walkedges() do
74 printf(" EDGE: %q, color=%q", s(e.label), s(e.color))
75 end
76 end
77 end
79 printf("NUMBER OF NODES: %d nodes", g.nnodes)
80 printf("NUMBER OF EDGES: %d edges", g.nedges)
81 g:show()
82 g:close()