Class: GraphViz::GraphML

Inherits:
Object
  • Object
show all
Defined in:
lib/graphviz/graphml.rb

Constant Summary collapse

DEST =
{
  'node'  => [:nodes],
  'edge'  => [:edges],
  'graph' => [:graphs],
  'all'   => [:nodes, :edges, :graphs]
}
GTYPE =
{
  'directed' => :digraph,
  'undirected' => :graph
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_or_str) ⇒ GraphML

Create a new GraphViz object from a GraphML file of string



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphviz/graphml.rb', line 43

def initialize( file_or_str )
  data = ((File.file?( file_or_str )) ? File::new(file_or_str) : file_or_str) 
  @xmlDoc = REXML::Document::new( data )
  @attributs = {
    :nodes => {},
    :edges => {},
    :graphs => {}
  }
  @graph = nil
  @current_attr = nil
  @current_node = nil
  @current_edge = nil
  @current_graph = nil
  
  parse( @xmlDoc.root )
end

Instance Attribute Details

#attributsObject (readonly)

Returns the value of attribute attributs.



23
24
25
# File 'lib/graphviz/graphml.rb', line 23

def attributs
  @attributs
end

#graphObject

The GraphViz object



26
27
28
# File 'lib/graphviz/graphml.rb', line 26

def graph
  @graph
end

Instance Method Details

#graphml(node) ⇒ Object

:nodoc:



68
69
70
71
72
73
74
75
76
# File 'lib/graphviz/graphml.rb', line 68

def graphml( node ) #:nodoc:
  node.each_element( ) do |child|
    #begin
      send( "graphml_#{child.name}".to_sym, child )
    #rescue NoMethodError => e
    #  raise "ERROR node #{child.name} can be child of graphml"
    #end
  end
end

#graphml_graph(node) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/graphviz/graphml.rb', line 103

def graphml_graph( node ) #:nodoc:
  @current_node = nil
  
  if @current_graph.nil?
    @graph = GraphViz.new( node.attributes['id'], :type => GTYPE[node.attributes['edgedefault']] )
    @current_graph = @graph
    previous_graph = @graph
  else
    previous_graph = @current_graph      
    @current_graph = previous_graph.add_graph( node.attributes['id'] )
  end
  
  @attributs[:graphs].each do |id, data|
    @current_graph.graph[data[:name]] = data[:default] if data.has_key?(:default)
  end
  @attributs[:nodes].each do |id, data|
    @current_graph.node[data[:name]] = data[:default] if data.has_key?(:default)
  end
  @attributs[:edges].each do |id, data|
    @current_graph.edge[data[:name]] = data[:default] if data.has_key?(:default)
  end
      
  node.each_element( ) do |child|
    #begin
      send( "graphml_graph_#{child.name}".to_sym, child )
    #rescue NoMethodError => e
    #  raise "ERROR node #{child.name} can be child of graphml"
    #end
  end
  
  @current_graph = previous_graph
end

#graphml_graph_data(node) ⇒ Object

:nodoc:



136
137
138
# File 'lib/graphviz/graphml.rb', line 136

def graphml_graph_data( node ) #:nodoc:
  @current_graph[@attributs[:graphs][node.attributes['key']][:name]] = node.texts().to_s
end

#graphml_graph_edge(node) ⇒ Object

:nodoc:



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/graphviz/graphml.rb', line 181

def graphml_graph_edge( node ) #:nodoc:
  source = node.attributes['source']
  source = {source => node.attributes['sourceport']} if node.attributes['sourceport']
  target = node.attributes['target']
  target = {target => node.attributes['targetport']} if node.attributes['targetport']
  
  @current_edge = @current_graph.add_edge( source, target )
  
  node.each_element( ) do |child|
    #begin
      send( "graphml_graph_edge_#{child.name}".to_sym, child )
    #rescue NoMethodError => e
    #  raise "ERROR node #{child.name} can be child of graphml"
    #end
  end
  
  @current_edge = nil
end

#graphml_graph_edge_data(node) ⇒ Object

:nodoc:



200
201
202
# File 'lib/graphviz/graphml.rb', line 200

def graphml_graph_edge_data( node ) #:nodoc:
  @current_edge[@attributs[:edges][node.attributes['key']][:name]] = node.texts().to_s
end

#graphml_graph_hyperedge(node) ⇒ Object

:nodoc:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/graphviz/graphml.rb', line 204

def graphml_graph_hyperedge( node ) #:nodoc:
  list = []
  
  node.each_element( ) do |child|
    if child.name == "endpoint"
      if child.attributes['port']
        list << { child.attributes['node'] => child.attributes['port'] }
      else
        list << child.attributes['node']
      end
    end
  end
  
  list.each { |s|
    list.each { |t|
      @current_graph.add_edge( s, t ) unless s == t
    }
  } 
end

#graphml_graph_node(node) ⇒ Object

:nodoc:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/graphviz/graphml.rb', line 140

def graphml_graph_node( node ) #:nodoc:
  @current_node = {}
  
  node.each_element( ) do |child|
    case child.name
    when "graph"
      graphml_graph( child )
    else
      begin
        send( "graphml_graph_node_#{child.name}".to_sym, child )
      rescue NoMethodError => e
        raise "ERROR node #{child.name} can be child of graphml"
      end
    end
  end
  
  unless @current_node.nil?
    node = @current_graph.add_node( node.attributes['id'] )
    @current_node.each do |k, v|
      node[k] = v
    end
  end
  
  @current_node = nil
end

#graphml_graph_node_data(node) ⇒ Object

:nodoc:



166
167
168
# File 'lib/graphviz/graphml.rb', line 166

def graphml_graph_node_data( node ) #:nodoc:
  @current_node[@attributs[:nodes][node.attributes['key']][:name]] = node.texts().to_s
end

#graphml_graph_node_port(node) ⇒ Object

:nodoc:



170
171
172
173
174
175
176
177
178
179
# File 'lib/graphviz/graphml.rb', line 170

def graphml_graph_node_port( node ) #:nodoc:
  @current_node[:shape] = :record
  port = node.attributes['name']
  if @current_node[:label]
    label = @current_node[:label].gsub( "{", "" ).gsub( "}", "" )
    @current_node[:label] = "#{label}|<#{port}> #{port}"
  else
    @current_node[:label] = "<#{port}> #{port}"
  end
end

#graphml_key(node) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/graphviz/graphml.rb', line 78

def graphml_key( node ) #:nodoc:
  id = node.attributes['id']
  @current_attr = {
    :name => node.attributes['attr.name'],
    :type => node.attributes['attr.type']
  }    
  DEST[node.attributes['for']].each do |d|
    @attributs[d][id] = @current_attr
  end
  
  node.each_element( ) do |child|
    begin
      send( "graphml_key_#{child.name}".to_sym, child )
    rescue NoMethodError => e
      raise "ERROR node #{child.name} can be child of graphml"
    end
  end
  
  @current_attr = nil
end

#graphml_key_default(node) ⇒ Object

:nodoc:



99
100
101
# File 'lib/graphviz/graphml.rb', line 99

def graphml_key_default( node ) #:nodoc:
  @current_attr[:default] = node.texts().to_s
end

#parse(node) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
# File 'lib/graphviz/graphml.rb', line 60

def parse( node ) #:nodoc:
  #begin
    send( node.name.to_sym, node )
  #rescue NoMethodError => e
  #  raise "ERROR node #{node.name} can be root"
  #end
end