Sha256: b25332293704ed151901f9c78d1e34cecfbd98230cee92fa687539f1a519b2a0

Contents?: true

Size: 1.69 KB

Versions: 8

Compression:

Stored size: 1.69 KB

Contents

# graphxml.rb
#
# This file contains minimal support for creating RGL graphs from the GraphML
# format (see https://graphml.graphdrawing.org/). The main purpose is to
# have a rich set of example graphs to have some more tests.
#
# See the examples directory, which contains a subdirectory _north_ with the
# Graph catalog GraphViz (see
# https://www.research.att.com/sw/tools/graphviz/refs.html).
#
# We use REXML::StreamListener from the REXML library
# (https://www.germane-software.com/software/rexml) to parse the grapml files.

require 'rgl/mutable'
require 'rexml/document'
require 'rexml/streamlistener'

module RGL

  module MutableGraph

    # Used to parse a subset of GraphML into an RGL graph implementation.
    #
    class MutableGraphParser

      include REXML::StreamListener

      # First resets +graph+ to be empty and stores a reference for use with
      # #tag_start.
      #
      def initialize(graph)
        @graph = graph
        @graph.remove_vertices(@graph.vertices)
      end

      # Processes incoming edge and node elements from GraphML in order to
      # populate the graph given to #new.
      #
      def tag_start(name, attrs)
        case name
          when 'edge'
            @graph.add_edge(attrs['source'], attrs['target'])
          when 'node'
            @graph.add_vertex(attrs['id'])
        end
      end

    end # class MutableGraphParser

    # Initializes an RGL graph from a subset of the GraphML format given in
    # +source+ (see https://graphml.graphdrawing.org/).
    #
    def from_graphxml(source)
      listener = MutableGraphParser.new(self)
      REXML::Document.parse_stream(source, listener)
      self
    end

  end # module MutableGraph

end # module RGL

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rgl-0.6.6 lib/rgl/graphxml.rb
rgl-0.6.5 lib/rgl/graphxml.rb
rgl-0.6.4 lib/rgl/graphxml.rb
rgl-0.6.3 lib/rgl/graphxml.rb
rgl-0.6.2 lib/rgl/graphxml.rb
rgl-0.6.1 lib/rgl/graphxml.rb
rgl-0.6.0 lib/rgl/graphxml.rb
rgl-0.5.10 lib/rgl/graphxml.rb