class Node
Represents a visual node in the graph, which can be connected to other nodes.
Definitions
def initialize(name, graph = nil, **attributes)
Initialize the node in the graph with the unique name.
Implementation
def initialize(name, graph = nil, **attributes)
@name = name
@attributes = attributes
@connections = []
# This sets up the connection between the node and the parent.
@graph = nil
graph << self if graph
end
def attach(parent)
Attach this node to the given graph:
Implementation
def attach(parent)
@graph = parent
end
attr :name
attr :connections
attr_accessor :attributes
def connect(destination, attributes = {})
Create an edge between this node and the destination with the specified options.
Implementation
def connect(destination, attributes = {})
edge = Edge.new(@graph, self, destination, attributes)
@connections << edge
return edge
end
def connected?(node)
Calculate if this node is connected to another. +O(N)+ search required.
Implementation
def connected?(node)
return @connections.find{|edge| edge.destination == node}
end
def add_node(name = nil, **attributes)
Add a node and #connect to it.
Implementation
def add_node(name = nil, **attributes)
node = @graph.add_node(name, **attributes)
connect(node)
return node
end
def dump_value(value)
Dump the value to dot text format.
Implementation
def dump_value(value)
if Symbol === value
value.to_s
else
value.inspect
end
end
def dump_attributes(attributes)
Dump the attributes to dot text format.
Implementation
def dump_attributes(attributes)
if attributes.size > 0
"[" + attributes.collect{|name, value| "#{name}=#{dump_value(value)}"}.join(", ") + "]"
else
""
end
end