Class: XamplGenerator::GraphMLOut

Inherits:
Object
  • Object
show all
Defined in:
lib/xamplr-gen/graphml-out.rb

Instance Method Summary collapse

Constructor Details

#initialize(elements_map) ⇒ GraphMLOut

Returns a new instance of GraphMLOut.



6
7
8
# File 'lib/xamplr-gen/graphml-out.rb', line 6

def initialize(elements_map)
  @elements_map = elements_map
end

Instance Method Details

#generate_class_nodes(element, include_mixins) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xamplr-gen/graphml-out.rb', line 10

def generate_class_nodes(element, include_mixins)

  node = @element_to_node_map[element.nstag]

  class_name = "#{ element.package }::#{ element.class_name }"
  mixin_name = "#{ element.package }::#{ element.class_name }"

  if element.persisted then
    write_entity_node(node, class_name, element.kind)
  else
    write_internal_node(node, class_name, element.kind)
  end

  if include_mixins then
    mixin_node = @mixed_in[element.nstag]
    #puts "#{ element.nstag } => #{ node }, mixin: [#{ mixin_node }]"
    if mixin_node then
      write_mixin_node(mixin_node, class_name)
    end
  end
#      puts "NODE #{ node } #{ class_name }"
  #      puts "NODE #{ mixin_node } #{ mixin_name }"
end

#generate_edges(element, include_mixins) ⇒ Object



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
# File 'lib/xamplr-gen/graphml-out.rb', line 34

def generate_edges(element, include_mixins)
  # for each child, generate an entity-ref or internal-ref edge
  # for each child, generate a mixin-ref edge

  return if ignore_package(element.package)

  element.child_element_child.each do | celement |
    next if ignore_package(celement.package)

    cnstag = "{{#{ celement.namespace }}}#{ celement.element_name }"

    referenced_element = @ns_to_element_map[ cnstag ]
    next unless referenced_element

    this_node = @element_to_node_map[ element.nstag ]

    other_node = @element_to_node_map[ cnstag ]
    other_mixin = @mixed_in[ cnstag ]

    if referenced_element.persisted then
      @current_edge += 1
      write_entity_ref_edge(@current_edge, this_node, other_node)
#          puts "ER EDGE #{ @current_edge }, #{ this_node } --> #{ other_node } :: #{ element.class_name } --> #{ referenced_element.class_name }"
    else
      @current_edge += 1
      write_internal_ref_edge(@current_edge, this_node, other_node)
#          puts "IR EDGE #{ @current_edge }, #{ this_node } --> #{ other_node } :: #{ element.class_name } --> #{ referenced_element.class_name }"
    end
    if include_mixins then
      @current_edge += 1
      write_mixin_ref_edge(@current_edge, this_node, other_mixin)
      #        puts "MI EDGE #{ @current_edge }, #{ this_node } --> #{ other_mixin } :: #{ element.class_name } --> #{ referenced_element.class_name }"
    end
  end
end

#ignore_package(package) ⇒ Object



70
71
72
73
74
75
# File 'lib/xamplr-gen/graphml-out.rb', line 70

def ignore_package(package)
  return true if @excluded_packages.member?(package)
  return false if @included_packages.nil?
  return true unless @included_packages.member?(package)
  return false
end

#write_entity_node(node, class_name, kind) ⇒ Object



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
# File 'lib/xamplr-gen/graphml-out.rb', line 147

def write_entity_node(node, class_name, kind)
  @out << <<EOS
    <node id="n#{ node }">
        <data key="d0">
            <y:UMLClassNode>
                <y:Geometry height="102.0"
                            width="111.0"
                            x="-5.5"
                            y="174.0"/>
                <y:Fill color="#99CCFF"
                        transparent="false"/>
                <y:BorderStyle color="#000000"
                               type="line"
                               width="2.0"/>
                <y:NodeLabel alignment="center"
                             autoSizePolicy="content"
                             fontFamily="Dialog"
                             fontSize="13"
                             fontStyle="bold"
                             hasBackgroundColor="false"
                             hasLineColor="false"
                             height="19.310546875"
                             modelName="internal"
                             modelPosition="c"
                             textColor="#000000"
                             visible="true"
                             width="49.6904296875"
                             x="30.65478515625"
                             y="26.1328125">#{ class_name }
                </y:NodeLabel>
                <y:UML clipContent="true"
                       constraint=""
                       omitDetails="false"
                       stereotype="#{ kind }"
                       use3DEffect="false">
                    <!--y:AttributeLabel>bar
bar2</y:AttributeLabel>
                    <y:MethodLabel>foo()</y:MethodLabel-->
                </y:UML>
            </y:UMLClassNode>
        </data>
        <data key="d1">UMLClass</data>
    </node>
EOS
end

#write_entity_ref_edge(edge, class_node, external_node) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/xamplr-gen/graphml-out.rb', line 283

def write_entity_ref_edge(edge, class_node, external_node)
  @out << <<EOS
    <edge id="e#{ edge }"
          source="n#{ class_node }"
          target="n#{ external_node }">
        <data key="d2">
            <y:PolyLineEdge>
                <y:Path sx="0.0"
                        sy="0.0"
                        tx="0.0"
                        ty="0.0"/>
                <y:LineStyle color="#000000"
                             type="line"
                             width="2.0"/>
                <y:Arrows source="none"
                          target="short"/>
                <y:EdgeLabel alignment="center"
                             distance="2.0"
                             fontFamily="Dialog"
                             fontSize="12"
                             fontStyle="plain"
                             hasBackgroundColor="false"
                             hasLineColor="false"
                             height="4.0"
                             modelName="six_pos"
                             modelPosition="tail"
                             preferredPlacement="anywhere"
                             ratio="0.5"
                             textColor="#000000"
                             visible="true"
                             width="4.0"
                             x="30.000732421875"
                             y="2.0"></y:EdgeLabel>
                <y:BendStyle smoothed="false"/>
            </y:PolyLineEdge>
        </data>
        <data key="d3">UMLuses</data>
    </edge>
EOS
end

#write_graph_ml(base_filename, excluded_packages = [ ], included_packages = nil, include_mixins = true) ⇒ Object



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
# File 'lib/xamplr-gen/graphml-out.rb', line 77

def write_graph_ml(base_filename, excluded_packages=[ ], included_packages=nil, include_mixins=true)
  filename = "#{base_filename}.graphml"

  @excluded_packages = Set.new(excluded_packages)
  @included_packages = included_packages ? Set.new(included_packages) : nil

  @element_to_node_map = {}
  @ns_to_element_map = {}
  @element_to_child_element_map = {}
  @mixed_in = {}

  nodes = 0
  edges = 0
  mixins = 0

  @elements_map.each_value do |elements|
    elements.element_child.each do |element|
      next if ignore_package(element.package)
      nodes += 1

      @element_to_node_map[element.nstag] = nodes
      @ns_to_element_map[element.nstag] = element
      @element_to_child_element_map[element.nstag] = map = {}

      element.child_element_child.each do | celement |
        edges += 1
        cnstag = "{{#{ celement.namespace }}}#{ celement.element_name }"
        map[cnstag] = edges
        unless @mixed_in.include?(cnstag) then
          mixins += 1
          @mixed_in[cnstag] = mixins
        end
      end
    end
  end

  @mixed_in.each do | k, v |
    @mixed_in[k] = v + nodes
  end

  #      puts "#{File.basename(__FILE__)}:#{__LINE__} #{ @element_to_node_map.inspect }"

  @reference_edges = edges

  File.open(filename, "w") do | out |
    @out = out

    if include_mixins then
      write_graphml_start(nodes + mixins, 2 * edges)
    else
      write_graphml_start(nodes, edges)
    end
    @elements_map.each_value do |elements|
      elements.element_child.each do |element|
        generate_class_nodes(element, include_mixins)
      end
    end

    @current_edge = 0
    @elements_map.each_value do |elements|
      elements.element_child.each do |element|
        generate_edges(element, include_mixins)
      end
    end
    write_graphml_end
#        puts "#{File.basename(__FILE__)}:#{__LINE__} EDGES:: predicted: #{ 2 * edges }, actual: #{ @current_edge }"
  end
  return nil
end

#write_graphml_endObject



438
439
440
441
442
443
444
445
446
# File 'lib/xamplr-gen/graphml-out.rb', line 438

def write_graphml_end
  @out << <<EOS
</graph>
<data key="d4">
    <y:Resources/>
</data>
</graphml>
EOS
end

#write_graphml_start(nodes, edges) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/xamplr-gen/graphml-out.rb', line 406

def write_graphml_start(nodes, edges)
  @out << <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:y="http://www.yworks.com/xml/graphml"
     xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd">
<key for="node"
     id="d0"
     yfiles.type="nodegraphics"/>
<key attr.name="description"
     attr.type="string"
     for="node"
     id="d1"/>
<key for="edge"
     id="d2"
     yfiles.type="edgegraphics"/>
<key attr.name="description"
     attr.type="string"
     for="edge"
     id="d3"/>
<key for="graphml"
     id="d4"
     yfiles.type="resources"/>
<graph edgedefault="directed"
       id="G"
       parse.edges="#{ edges }"
       parse.nodes="#{ nodes }"
       parse.order="free">
EOS
end

#write_internal_node(node, class_name, kind) ⇒ Object



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
# File 'lib/xamplr-gen/graphml-out.rb', line 193

def write_internal_node(node, class_name, kind)
  @out << <<EOS
    <node id="n#{ node }">
        <data key="d0">
            <y:UMLClassNode>
                <y:Geometry height="102.0"
                            width="91.0"
                            x="4.5"
                            y="-1.0"/>
                <y:Fill color="#CCFFCC"
                        transparent="false"/>
                <y:BorderStyle color="#000000"
                               type="line"
                               width="1.0"/>
                <y:NodeLabel alignment="center"
                             autoSizePolicy="content"
                             fontFamily="Dialog"
                             fontSize="13"
                             fontStyle="bold"
                             hasBackgroundColor="false"
                             hasLineColor="false"
                             height="19.310546875"
                             modelName="internal"
                             modelPosition="c"
                             textColor="#000000"
                             visible="true"
                             width="39.83251953125"
                             x="25.583740234375"
                             y="26.1328125">#{ class_name }
                </y:NodeLabel>
                <y:UML clipContent="true"
                       constraint=""
                       omitDetails="false"
                       stereotype="#{ kind }"
                       use3DEffect="false">
                    <!--y:AttributeLabel>bar</y:AttributeLabel>
                    <y:MethodLabel>foo()</y:MethodLabel-->
                </y:UML>
            </y:UMLClassNode>
        </data>
        <data key="d1">UMLClass</data>
    </node>
EOS
end

#write_internal_ref_edge(edge, class_node, internal_node) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/xamplr-gen/graphml-out.rb', line 324

def write_internal_ref_edge(edge, class_node, internal_node)
  @out << <<EOS
    <edge id="e#{ edge }"
          source="n#{ class_node }"
          target="n#{ internal_node }">
        <data key="d2">
            <y:PolyLineEdge>
                <y:Path sx="0.0"
                        sy="0.0"
                        tx="0.0"
                        ty="0.0"/>
                <y:LineStyle color="#000000"
                             type="line"
                             width="1.0"/>
                <y:Arrows source="none"
                          target="short"/>
                <y:EdgeLabel alignment="center"
                             distance="2.0"
                             fontFamily="Dialog"
                             fontSize="12"
                             fontStyle="plain"
                             hasBackgroundColor="false"
                             hasLineColor="false"
                             height="4.0"
                             modelName="six_pos"
                             modelPosition="tail"
                             preferredPlacement="anywhere"
                             ratio="0.5"
                             textColor="#000000"
                             visible="true"
                             width="4.0"
                             x="2.0"
                             y="-38.529541015625"></y:EdgeLabel>
                <y:BendStyle smoothed="false"/>
            </y:PolyLineEdge>
        </data>
        <data key="d3">UMLuses</data>
    </edge>
EOS
end

#write_mixin_node(node, class_name) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/xamplr-gen/graphml-out.rb', line 238

def write_mixin_node(node, class_name)
  @out << <<EOS
    <node id="n#{ node }">
        <data key="d0">
            <y:UMLClassNode>
                <y:Geometry height="102.0"
                            width="136.0"
                            x="-18.0"
                            y="349.0"/>
                <y:Fill color="#FFCC99"
                        transparent="false"/>
                <y:BorderStyle color="#000000"
                               type="line"
                               width="1.0"/>
                <y:NodeLabel alignment="center"
                             autoSizePolicy="content"
                             fontFamily="Dialog"
                             fontSize="13"
                             fontStyle="bold"
                             hasBackgroundColor="false"
                             hasLineColor="false"
                             height="19.310546875"
                             modelName="internal"
                             modelPosition="c"
                             textColor="#000000"
                             visible="true"
                             width="110.818359375"
                             x="12.5908203125"
                             y="26.1328125">#{ class_name }
                </y:NodeLabel>
                <y:UML clipContent="true"
                       constraint=""
                       omitDetails="false"
                       stereotype="mixin"
                       use3DEffect="false">
                    <!--y:AttributeLabel></y:AttributeLabel>
                    <y:MethodLabel></y:MethodLabel-->
                </y:UML>
            </y:UMLClassNode>
        </data>
        <data key="d1">UMLClass</data>
    </node>
EOS
end

#write_mixin_ref_edge(edge, class_node, mixin_node) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/xamplr-gen/graphml-out.rb', line 365

def write_mixin_ref_edge(edge, class_node, mixin_node)
  @out << <<EOS
    <edge id="e#{ edge }"
          source="n#{ mixin_node }"
          target="n#{ class_node }">
        <data key="d2">
            <y:PolyLineEdge>
                <y:Path sx="0.0"
                        sy="0.0"
                        tx="0.0"
                        ty="0.0"/>
                <y:LineStyle color="#000000"
                             type="line"
                             width="1.0"/>
                <y:Arrows source="white_delta"
                          target="none"/>
                <y:EdgeLabel alignment="center"
                             distance="2.0"
                             fontFamily="Dialog"
                             fontSize="12"
                             fontStyle="plain"
                             hasBackgroundColor="false"
                             hasLineColor="false"
                             height="4.0"
                             modelName="six_pos"
                             modelPosition="tail"
                             preferredPlacement="anywhere"
                             ratio="0.5"
                             textColor="#000000"
                             visible="true"
                             width="4.0"
                             x="2.0"
                             y="34.529541015625"></y:EdgeLabel>
                <y:BendStyle smoothed="false"/>
            </y:PolyLineEdge>
        </data>
        <data key="d3">UMLinherits</data>
    </edge>
EOS
end