Skip to content

Instantly share code, notes, and snippets.

@alienrobotwizard
Created October 2, 2010 20:45
Show Gist options
  • Save alienrobotwizard/607973 to your computer and use it in GitHub Desktop.
Save alienrobotwizard/607973 to your computer and use it in GitHub Desktop.
module GMLParser
class Graph < Struct.new(
:nodes,
:edges
)
def edge_list
self.edges.map{|e| [e[:source], e[:target], e[:value]]}
end
end
class Parser
def self.parse gml_data
graph = gml_data.gsub(/\n/, '').scan(rgxp[:graph]).inject([]) do |g, s|
g << s.scan(rgxp[:node]).map do |n|
{
:id => n.match(rgxp[:id]).captures.first,
:label => n.match(rgxp[:label]).captures.first
}
end
g << s.scan(rgxp[:edge]).map do |e|
{
:source => e.match(rgxp[:source]).captures.first,
:target => e.match(rgxp[:target]).captures.first,
:value => e.match(rgxp[:value]).captures.first
}
end
g
end
Graph.new(*graph)
end
def self.rgxp
{
:graph => /graph.*\[[^\[\]]+\]/,
:edge => /edge\W+\[[^\[\]]+\]/,
:node => /node\W+\[[^\[\]]+\]/,
:id => /id\W+([A-Za-z0-9]+)/,
:label => /label\W+([A-Za-z0-9]+)/,
:source => /source\W+([A-Za-z0-9]+)/,
:target => /target\W+([A-Za-z0-9]+)/,
:value => /value\W+([A-Za-z0-9]+)/
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment