-
Notifications
You must be signed in to change notification settings - Fork 6
/
GraphMLDocument.rb
executable file
·238 lines (202 loc) · 5.98 KB
/
GraphMLDocument.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
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
161
162
163
164
165
166
167
168
169
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# GraphMLDocument.rb
# Leonhard
#
# Created by greg on 04/11/10.
# Copyright 2010, 2011 Gregoire Lejeune. All rights reserved.
require 'rexml/document'
class GraphMLDocument
attr_reader :attributs
attr_accessor :graph
DEST = {
'node' => [:nodes],
'edge' => [:edges],
'graph' => [:graphs],
'all' => [:nodes, :edges, :graphs]
}
GTYPE = {
'directed' => 'digraph',
'undirected' => 'graph'
}
ETYPE = {
'directed' => "->",
'undirected' => "--"
}
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
@edge_type = nil
@indent = 0
parse( @xmlDoc.root )
end
def parse( node )
send( node.name.to_sym, node )
end
def dot
@graph
end
def graphml( node )
node.each_element( ) do |child|
send( "graphml_#{child.name}".to_sym, child )
end
end
def graphml_key( node )
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
def graphml_key_default( node )
@current_attr[:default] = node.texts()[-1].inspect
end
def graphml_graph( node )
@current_node = nil
if @current_graph.nil?
@graph = "#{GTYPE[node.attributes['edgedefault']]} \"#{node.attributes['id']}\" {\n"
@edge_type = ETYPE[node.attributes['edgedefault']]
@current_graph = @graph
previous_graph = @graph
@indent += 2
else
previous_graph = @current_graph
@current_graph << " "*@indent + "subgraph \"#{node.attributes['id']}\" {\n"
@indent += 2
end
@attributs[:graphs].each do |id, data|
@current_graph << " "*@indent + "#{data[:name]} = #{data[:default]};\n" if data.has_key?(:default)
end
sep = " "*@indent + "node["
@attributs[:nodes].each do |id, data|
if data.has_key?(:default)
@current_graph << "#{sep}#{data[:name]} = #{data[:default]}"
sep = ", "
end
end
@current_graph << "];\n" if sep == ", "
sep = " "*@indent + "edge["
@attributs[:edges].each do |id, data|
if data.has_key?(:default)
@current_graph << "#{sep}#{data[:name]} = #{data[:default]}"
sep == ", "
end
end
@current_graph << "];\n" if sep == ", "
node.each_element( ) do |child|
send( "graphml_graph_#{child.name}".to_sym, child )
end
@indent -= 2
@current_graph << " "*@indent + "}\n"
@current_graph = previous_graph
end
def graphml_graph_data( node )
@current_graph << " "*@indent + "#{@attributs[:graphs][node.attributes['key']][:name]} = #{node.texts()[-1].inspect};\n"
end
def graphml_graph_node( node )
@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?
@current_graph << " "*@indent + '"' + node.attributes['id'] + '"'
sep = "["
@current_node.each do |k, v|
# node[k] = v
if v.class == Array
@current_graph << "#{sep}#{k} = #{v[-1]}"
else
@current_graph << "#{sep}#{k} = #{v.inspect}"
end
sep = ", "
end
if sep == ", "
@current_graph << "];\n"
else
@current_graph << "\n"
end
end
@current_node = nil
end
def graphml_graph_node_data( node )
@current_node[@attributs[:nodes][node.attributes['key']][:name]] = node.texts()
end
def graphml_graph_node_port( node )
@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
def graphml_graph_edge( node )
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_graph << " "*@indent + "#{source} #{@edge_type} #{target}"
sep = "["
node.each_element( ) do |child|
@current_graph << sep
send( "graphml_graph_edge_#{child.name}".to_sym, child )
sep = ", "
end
if sep == ", "
@current_graph << "]\n"
else
@current_graph << "\n"
end
@current_edge = nil
end
def graphml_graph_edge_data( node )
@current_graph << "#{@attributs[:edges][node.attributes['key']][:name]} = #{node.texts()[-1].inspect}"
end
def graphml_graph_hyperedge( node )
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
@current_graph << " "*@indent + "#{s} #{@edge_type} #{t}\n" unless s == t
}
}
end
end