Nodes

Node Attributes

Attributes required for graph layout and graph rendering are always stored as key value pairs in the form: g[key] or g.key, Only strings are allowed as keys. Any number of user defined string attributes, which are not necessarily used by the graphviz library can be set.

With it's creation each node receives  one of the following read-only attributes.

Attribute Key Value Type Description Notes
graph graph userdata Graph to which the given node belongs
id number Graphviz internal unique id for a node.
name string Name of the node.

Node Methods

result = n:degree(WHAT)

Returns the number of input edges to the node if the string parameter WHAT is set to "*i". If WHAT is set to "*o" the function returns the number of output edges. Finally if WHAT is set to "*a", the function returns the sum of input and output edges.

result = n:delete()

Deletes the given node. All edges associated with this node are  also  deleted. Lua userdata objects associated with the graphviz objects, are invalidated and eventually deleted during garbage collection. Any attempt to access the node will result in a runtime error.

The function returns a non-nil value on success.

e, tail, head = n:edge(HEAD [, LABEL [, NOCREATE]])

Retrieves or creates an edge between two nodes. The given node n is assumed as the TAIL of the edge. The HEAD of the edge is given as parameter either as node userdsata or by the node's name.The edge can be defined more precisely with the string parameter LABEL

Creation of the edge can be suppressed by passing true for the parameter NOCREATE.

The function returns an edge userdata object together with the edge's tail and head node. It returns nil plus an error message in case of failures.

e = n:nextedge(LAST)
e = n:nextinput(LAST)
e = n:nextoutput(LAST)

These iterator functions  retrieve the next edge of the given node n depending on the parameter LAST, which must be an edge userdata object. If LAST is nil the first edge is returned. If no more edges can be found the functions return nil.

The function nextedge() returns any edge, while the functions nextinput() and nexoutput() return either an input or an output edge.

All iterators return nil if no edge could be found.

result = n:rawget(KEY)

Retrieves an attributes from a node object n without using Lua metamethods. Attributes are always returned as strings.

The function returns nil if there is no attribute with  KEY could be found.

result = n:type()

See g:type().

iterfunc, n, nil = n:walkedges()
iterfunc, n, nil = n:walkinputs()
iterfunc, n, nil = n:walkoutputs()

These iterator functions return an edge, an input edge or an output edge of the given node n each time they are called. In fact they return the following triple: nextedge(), n, nil for usage in Lua's generic "for loop".

If no more edges can be found the functions return nil.

Example - Iterate over all edges of all nodes in a graph g.

for n in g:walknodes() do
for e in n:walkedges() do
print(e.label)
end
end

Node Metamethods

Comparison:     

n1 == n2

Nodes can be compared using the == operator. The comparison returns true if the nodes are identical (have equal attributes id). It returns false otherwise. 

Concatenation:      

e = n1 .. n2

Addition:

e = n1 + n2

Both operators create an edge. The edge does not receive a label and the name of the edge is automatically assigned to "edge@<id>".

Example:

goofy:luagraph$ lua -l graph -i
> g=graph.open("g")
> n1=g:node("n1")
> n2=g:node("n2")
> e1=n1+n2
> =e1
userdata: 0x8070628
> =e1.name
edge@1
> =e1.label
nil
> e2=n1..n2
> =e2
userdata: 0x8070628
> =e2.name
edge@1
> =e1==e2
true