class JavaClass::Dependencies::GraphmlSerializer

Serializes a Graph of Node to GraphML (XML). To see the graph, use yED, www.yworks.com/en/products_yed_about.html to

Author

Peter Kofler

Public Class Methods

new(options = { :edges => :with_counts }) click to toggle source

Create a serializer with options hash:

edges

how to chart edge labes, either :no_text or :with_counts

# File lib/javaclass/dependencies/graphml_serializer.rb, line 17
def initialize(options = { :edges => :with_counts })
  @options = options
end

Public Instance Methods

add_node_as_xml(container, node) click to toggle source

Add the node as XML to the container .

# File lib/javaclass/dependencies/graphml_serializer.rb, line 57
def add_node_as_xml(container, node)
  add_node_element(container, node)
    
  node.dependencies.keys.each do |dep|
    add_edge_element(container, node, dep)
  end
end
graph_to_xml(graph) click to toggle source

Return an XML document of the GraphML serialized graph .

# File lib/javaclass/dependencies/graphml_serializer.rb, line 31
def graph_to_xml(graph)
  doc = create_xml_doc
  container = add_graph_element(doc)
  graph.to_a.each { |node| add_node_as_xml(container, node) }
  doc
end
save(filename, graph) click to toggle source

Save the graph to filename .

# File lib/javaclass/dependencies/graphml_serializer.rb, line 22
def save(filename, graph)
  File.open(filename + '.graphml', 'w') do |f|
    doc = graph_to_xml(graph)
    doc.write(out_string = '', 2)
    f.print out_string
  end
end

Private Instance Methods

add_edge_element(container, node, dep) click to toggle source
# File lib/javaclass/dependencies/graphml_serializer.rb, line 75
def add_edge_element(container, node, dep)
  edge = container.add_element('edge')
  edge.add_attribute('id', node.name + '.' + dep.name)
  edge.add_attribute('source', node.name)
  edge.add_attribute('target', dep.name)
  
  add_edge_label(edge, node.dependencies[dep])
end
add_edge_label(edge, dependencies_2_dep) click to toggle source
# File lib/javaclass/dependencies/graphml_serializer.rb, line 84
def add_edge_label(edge, dependencies_2_dep)
  if @options[:edges] == :with_counts
    number_total_dependencies = dependencies_2_dep.size.to_s
    number_unique_dependencies = dependencies_2_dep.collect { |d| d.target }.uniq.size.to_s
    edge.add_element('data', 'key' => 'e1').
      add_element('y:PolyLineEdge').
        add_element('y:EdgeLabel').
          add_text("#{number_total_dependencies} (#{number_unique_dependencies})")
  elsif @options[:edges] == :no_text
    # do nothing
  else
    raise "unknown option for edge labels #{@options[:edges]}"
  end
end
add_graph_element(doc) click to toggle source
# File lib/javaclass/dependencies/graphml_serializer.rb, line 44
def add_graph_element(doc)
  root = doc.add_element('graphml', 
    'xmlns' => 'http://graphml.graphdrawing.org/xmlns',  
    'xmlns:y' => 'http://www.yworks.com/xml/graphml')
  root.add_element('key', 'id' => 'n1', 'for' => 'node', 'yfiles.type' => 'nodegraphics')
  root.add_element('key', 'id' => 'e1', 'for' => 'edge', 'yfiles.type' => 'edgegraphics')
  
  root.add_element('graph', 'edgedefault' => 'directed')
end
add_node_element(container, node) click to toggle source
# File lib/javaclass/dependencies/graphml_serializer.rb, line 67
def add_node_element(container, node)
  elem = container.add_element('node', 'id' => node.name)
  elem.add_element('data', 'key' => 'n1').
    add_element('y:ShapeNode').
      add_element('y:NodeLabel').
        add_text(node.to_s)
end
create_xml_doc() click to toggle source
# File lib/javaclass/dependencies/graphml_serializer.rb, line 40
def create_xml_doc
  REXML::Document.new
end