Class: Nodepile::GraphVisualizer

Inherits:
Object
  • Object
show all
Defined in:
lib/nodepile/gviz.rb

Overview

This class converts a set of nodes and edges into a visualization rendered by the graphviz tool.

Note: At present this is a wrapper around the ruby-graphviz gem as interface
to the graphviz software tool.  In order for this class to work
the graphviz software package must be installed on the system.  At some
point I'm probably going to want to pull out that dependency because
I'm a classic NIH bigot.  But I always think about Luke and his lightsaber.

Constant Summary collapse

NODE_ATTR_MAPS =
{
 '_shape' =>  :shape,
 '_color' => :color,
 '_fillcolor' => Proc.new{|obj,s| obj[:fillcolor] = s; obj[:style] ||= 'filled'},
 '_fontcolor' => :fontcolor,
 '_label' => :label,
}
EDGE_ATTR_MAPS =
{
 '_color' => :color,
 '_fontcolor' => :fontcolor,
 '_label' => :label,
}

Instance Method Summary collapse

Constructor Details

#initializeGraphVisualizer

Returns a new instance of GraphVisualizer.



18
19
20
# File 'lib/nodepile/gviz.rb', line 18

def initialize
    @graph = nil
end

Instance Method Details

#emit_file(fpath, configs: nil, file_format: nil) ⇒ String

Generate a file based on the load specified

Parameters:

  • fpath (String)

    filepath location at which to create the file. Filename to create/overwrite with the generated visualization.

  • file_format (nil, :png, :gif, :svg, :dot) (defaults to: nil)

    Note that the desired output will be deduced from the fpath extension if it exactly matches one of the valid arguments here (e.g. “xyz.png”).

  • configs (#[]) (defaults to: nil)

    may be interrogated for config info such as the preferred rendering engine

Returns:

  • (String)

    returns the path of the file created



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nodepile/gviz.rb', line 71

def emit_file(fpath,configs: nil ,file_format: nil)
    configs ||= Hash.new
    extension = /.*\.(png|svg|dot)$/.match(fpath)&.[](1)
    fmt = file_format || extension&.to_sym
    raise "Output file format is unspecified and can't be deduced from file extension" unless fmt
    oargs = Hash.new
    oargs[fmt] = fpath
    oargs[:use] = configs[:layout_engine] if configs[:layout_engine]
    @graph.output(oargs)
    return fpath
end

#inspectObject



22
# File 'lib/nodepile/gviz.rb', line 22

def inspect = "#<#{self.class}:0x#{object_id}"

#load(node_record_enum, edge_record_enum, configs: Hash.new) ⇒ void

This method returns an undefined value.

Given edge and node packets,

Parameters:

  • node_packet_enum (Enumerable<EntityPacket>)

    all nodes

  • edge_packet_enum (Enumerable<EntityPacket>)

    all edges

  • configs (#[]) (defaults to: Hash.new)

    may be interrogated for config info such as the preferred rendering engine



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nodepile/gviz.rb', line 45

def load(node_record_enum, edge_record_enum,configs: Hash.new)
    @graph = GraphViz.new(:G)
    @graph.type = configs[:directionality] if configs[:directionality]
    nodes = Hash.new # temporary cache
    node_record_enum.each{|nr| 
                n = nodes[nr['@key']] = @graph.add_nodes(nr['@key']) 
                self.class._apply_attrs(n,nr,NODE_ATTR_MAPS)
               }
    edge_record_enum.each{|er| 
            node_pair = er['@key'].map{|k| nodes[k] }
            e = @graph.add_edges(*node_pair)
            self.class._apply_attrs(e,er,EDGE_ATTR_MAPS)
           }
end