Class: GraphViz::GraphML

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

Constant Summary

DEST =
{
  'node'      => [:nodes],
  'edge'      => [:edges],
  'graph'     => [:graphs],
  'graphml'   => [:graphml],
  'hyperedge' => [:hyperedge],
  'port'      => [:port],
  'endpoint'  => [:endpoint],
  'all'       => [:nodes, :edges, :graphs, :graphml, :hyperedge, :port, :endpoint]
}
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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/graphviz/graphml.rb', line 52

def initialize( file_or_str )
  data = ((File.file?( file_or_str )) ? File::new(file_or_str) : file_or_str)
  @xmlDoc = REXML::Document::new( data )
  @attributes = {
    :nodes => {},
    :edges => {},
    :graphs => {},
    :graphml => {},
    :endpoint => {},
    :port => {},
    :hyperedge => {}
  }
  @ignored_keys = []
  @graph = nil
  @current_attr = nil
  @current_node = nil
  @current_edge = nil
  @current_graph = nil

  parse( @xmlDoc.root )
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes



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

def attributes
  @attributes
end

#graphObject

The GraphViz object



33
34
35
# File 'lib/graphviz/graphml.rb', line 33

def graph
  @graph
end

Instance Method Details

#attributsObject



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

def attributs
   warn "`GraphViz::GraphML#attributs` is deprecated, please, use `GraphViz::GraphML#attributes`"
   return @attributes
end

#graphml(node) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
# File 'lib/graphviz/graphml.rb', line 78

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

#graphml_data(node) ⇒ Object



88
89
90
# File 'lib/graphviz/graphml.rb', line 88

def graphml_data(node)
   warn "graphml/data not supported!"
end

#graphml_graph(node) ⇒ Object

:nodoc:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/graphviz/graphml.rb', line 121

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

  @attributes[:graphs].each do |id, data|
    begin
      @current_graph.graph[data[:name]] = data[:default] if data.has_key?(:default)
    rescue ArgumentError => e
      warn e
    end
  end
  @attributes[:nodes].each do |id, data|
    begin
      @current_graph.node[data[:name]] = data[:default] if data.has_key?(:default)
    rescue ArgumentError => e
      warn e
    end
  end
  @attributes[:edges].each do |id, data|
    begin
      @current_graph.edge[data[:name]] = data[:default] if data.has_key?(:default)
    rescue ArgumentError => e
      warn e
    end
  end

  node.each_element( ) do |child|
    send( "graphml_graph_#{child.name}".to_sym, child )
  end

  @current_graph = previous_graph
end

#graphml_graph_data(node) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
# File 'lib/graphviz/graphml.rb', line 162

def graphml_graph_data( node ) #:nodoc:
  begin
    @current_graph[@attributes[:graphs][node.attributes['key']][:name]] = node.texts().join('\n')
  rescue ArgumentError => e
    warn e
  end
end

#graphml_graph_edge(node) ⇒ Object

:nodoc:



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/graphviz/graphml.rb', line 220

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_edges( source, target )

  node.each_element( ) do |child|
    begin
      send( "graphml_graph_edge_#{child.name}".to_sym, child )
    rescue NoMethodError => e
      raise GraphMLError, "node #{child.name} can be child of edge"
    end
  end

  @current_edge = nil
end

#graphml_graph_edge_data(node) ⇒ Object

:nodoc:



239
240
241
242
243
244
245
246
# File 'lib/graphviz/graphml.rb', line 239

def graphml_graph_edge_data( node ) #:nodoc:
  return if @ignored_keys.include?(node.attributes['key'])
  begin
    @current_edge[@attributes[:edges][node.attributes['key']][:name]] = node.texts().join('\n')
  rescue ArgumentError => e
    warn e
  end
end

#graphml_graph_hyperedge(node) ⇒ Object

:nodoc:



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/graphviz/graphml.rb', line 248

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_edges( s, t ) unless s == t
    }
  }
end

#graphml_graph_node(node) ⇒ Object

:nodoc:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/graphviz/graphml.rb', line 170

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 GraphMLError, "node #{child.name} can be child of node"
      end
    end
  end

  unless @current_node.nil?
    node = @current_graph.add_nodes( node.attributes['id'] )
    @current_node.each do |k, v|
      begin
        node[k] = v
      rescue ArgumentError => e
        warn e
      end
    end
  end

  @current_node = nil
end

#graphml_graph_node_data(node) ⇒ Object

:nodoc:



200
201
202
203
204
205
206
207
# File 'lib/graphviz/graphml.rb', line 200

def graphml_graph_node_data( node ) #:nodoc:
  return if @ignored_keys.include?(node.attributes['key'])
  begin
    @current_node[@attributes[:nodes][node.attributes['key']][:name]] = node.texts().join('\n')
  rescue ArgumentError => e
    warn e
  end
end

#graphml_graph_node_port(node) ⇒ Object

:nodoc:



209
210
211
212
213
214
215
216
217
218
# File 'lib/graphviz/graphml.rb', line 209

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:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graphviz/graphml.rb', line 92

def graphml_key( node ) #:nodoc:
  id = node.attributes['id']
  @current_attr = {
    :name => node.attributes['attr.name'],
    :type => node.attributes['attr.type']
  }
  if @current_attr[:name].nil?
    @ignored_keys << id
  else
    DEST[node.attributes['for']].each do |d|
      @attributes[d][id] = @current_attr
    end

    node.each_element( ) do |child|
      begin
        send( "graphml_key_#{child.name}".to_sym, child )
      rescue NoMethodError => e
        raise GraphMLError, "node #{child.name} can be child of key"
      end
    end
  end

  @current_attr = nil
end

#graphml_key_default(node) ⇒ Object

:nodoc:



117
118
119
# File 'lib/graphviz/graphml.rb', line 117

def graphml_key_default( node ) #:nodoc:
  @current_attr[:default] = node.texts().join('\n')
end

#parse(node) ⇒ Object

:nodoc:



74
75
76
# File 'lib/graphviz/graphml.rb', line 74

def parse( node ) #:nodoc:
  send( node.name.to_sym, node )
end