Class: Relyze::Graph::Node

Inherits:
Object
  • Object
show all
Defined in:
C:/Program Files/Relyze/lib/relyze/core/graph.rb

Overview

A base class representing a node in a graph.

Direct Known Subclasses

TCG::TCGNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, id, data = nil) ⇒ Node

Initialize a new node object.

Parameters:

  • graph (Relyze::Graph::Graph)

    The graph this node belongs to.

  • id (Integer)

    A unique id number for this node.

  • data (Object) (defaults to: nil)

    An optional data object to associate with this node.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 423

def initialize( graph, id, data=nil )
    @graph                     = graph
    @id                        = id
    @name                      = "node_%d" % @id
    @data                      = data
    @immediate_dominator       = nil
    @dominator_children        = nil
    @dominance_frontier        = nil
    @immediate_post_dominator  = nil
    @post_dominator_children   = nil
    @post_dominance_frontier   = nil
    @control_dependencies      = nil
    @edges                     = []
    @display                   = {
        :x                     => 0.0,
        :y                     => 0.0,
        :width                 => 0.0,
        :height                => 0.0,
        :rva                   => nil,
        :node_shape            => @graph.display.fetch( :node_shape, :rectangle ),
        :node_border_width     => @graph.display.fetch( :node_border_width, 2.0 ),
        :node_border_color     => @graph.display.fetch( :node_border_color, '#000000' ),
        :node_background_color => @graph.display.fetch( :node_background_color, '#FFFFFF')
    }
end

Instance Attribute Details

#control_dependenciesObject

nil, Array<Relyze::Graph::Node>

An array of nodes belonging to this nodes control dependencies or nil if it has not yet been calculated.



416
417
418
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 416

def control_dependencies
  @control_dependencies
end

#dataObject

Object

A data object to associate with this node.



395
396
397
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 395

def data
  @data
end

#displayObject (readonly)

Hash

A hash of display properties for this node.



389
390
391
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 389

def display
  @display
end

#dominance_frontierObject

nil, Array<Relyze::Graph::Node>

An array of nodes belonging to this nodes dominance frontier or nil if it has not yet been calculated.



404
405
406
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 404

def dominance_frontier
  @dominance_frontier
end

#dominator_childrenObject

nil, Array<Relyze::Graph::Node>


401
402
403
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 401

def dominator_children
  @dominator_children
end

#edgesObject (readonly)

Array<Relyze::Graph::Edge>

An array of edge objects.



378
379
380
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 378

def edges
  @edges
end

#graphObject (readonly)

Relyze::Graph::Graph

The graph this node belongs to.



372
373
374
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 372

def graph
  @graph
end

#idObject (readonly)

Integer

A unique id number for this node.



375
376
377
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 375

def id
  @id
end

#immediate_dominatorObject

nil, Relyze::Graph::Node

This nodes immediate dominator or nil if it has not yet been calculated.



398
399
400
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 398

def immediate_dominator
  @immediate_dominator
end

#immediate_post_dominatorObject

nil, Relyze::Graph::Node

This nodes immediate post dominator or nil if it has not yet been calculated.



407
408
409
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 407

def immediate_post_dominator
  @immediate_post_dominator
end

#nameObject

String

Gets this node's name.



392
393
394
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 392

def name
  @name
end

#post_dominance_frontierObject

nil, Array<Relyze::Graph::Node>

An array of nodes belonging to this nodes dominance frontier or nil if it has not yet been calculated.



413
414
415
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 413

def post_dominance_frontier
  @post_dominance_frontier
end

#post_dominator_childrenObject

nil, Array<Relyze::Graph::Node>


410
411
412
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 410

def post_dominator_children
  @post_dominator_children
end

Instance Method Details

#degreeInteger

Get this nodes degree.

Returns:

  • (Integer)

    This node degree value.



486
487
488
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 486

def degree
    return @edges.size
end

#edge?(node) ⇒ true, false

Check if this node contains an edge to another node

Parameters:

  • node (Node)

    The node to test if an edge to it exists.

Returns:

  • (true, false)


476
477
478
479
480
481
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 476

def edge?( node )
    @edges.each do | edge |
        return true if edge.rhs == node
    end
    return false
end

#indegreeInteger

Get this nodes in degree.

Returns:

  • (Integer)

    This node in degree value.



493
494
495
496
497
498
499
500
501
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 493

def indegree
    count = 0
    @edges.each do | edge |
        if( edge.rhs == self )
            count += 1
        end
    end
    return count
end

#outdegreeInteger

Get this nodes out degree.

Returns:

  • (Integer)

    This node out degree value.



506
507
508
509
510
511
512
513
514
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 506

def outdegree
    count = 0
    @edges.each do | edge |
        if( edge.lhs == self )
            count += 1
        end
    end
    return count
end

#removenil

Remove this node from the graph.

Returns:

  • (nil)


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 452

def remove
    tmp_edges = @edges.dup

    tmp_edges.each do | edge |
        edge.remove
    end

    @graph.nodes.delete( self )

    if( not @data.nil? )
        @graph.data_map.delete( @data )
    end

    if( @graph.root == self )
        @graph.root = nil
    end

    return nil
end

#to_dotString

Export this node object as a DOT node.

Returns:



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 574

def to_dot

    dot  = "\t"

    dot << @name

    dot << " ["

    label = self.to_s

    label.gsub!( "\r", "" )

    if( @graph.display[:font_justify] == :left )
        label.gsub!( "\n", "\\l" )
    elsif( @graph.display[:font_justify] == :right )
        label.gsub!( "\n", "\\r" )
    else
        label.gsub!( "\n", "\\n" )
    end

    label.gsub!( "\"", "\\\"" )

    dot << "label=\"%s\" " % label

    dot << "style=filled "

    dot << "color=\"%s\" " % @display[:node_border_color]

    dot << "fillcolor=\"%s\" " % @display[:node_background_color]

    shape = 'box'

    if( @display[:node_shape] == :ellipse )
        shape = 'ellipse';
    elsif( @display[:node_shape] == :circle )
        shape = 'circle';
    elsif( @display[:node_shape] == :square )
        shape = 'square';
    end

    dot << "shape=\"%s\" " % shape

    dot << "fontname=\"%s\" " % @graph.display[:font_name]

    dot << "fontcolor=\"%s\" " % @graph.display[:font_color]

    dot << "fontsize=\"%d\" " % @graph.display[:font_size]

    dot << "]\r\n"

    return dot
end

#to_sString

Get this nodes text content. If a data object is available we use this to generate the content, otherwise this nodes name is used.

Returns:

  • (String)

    This nodes text content.



631
632
633
634
635
636
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 631

def to_s
    if( not @data.nil? )
        return @data.respond_to?( :render ) ? @data.render : @data.to_s
    end
    return @name
end

#to_svgString

Export this node object as a SVG element, suitable for embedding in HTML.

Returns:

  • (String)

    An SVG element.



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'C:/Program Files/Relyze/lib/relyze/core/graph.rb', line 519

def to_svg

    svg  = "<g class=\"relyze_graph_node\">"

    svg << "<title>%s</title>" % @name

    if( @display[:node_shape] == :ellipse or @display[:node_shape] == :circle )
        svg << "<ellipse  "
        svg << "cx=\"%f\" " % @display[:x]
        svg << "cy=\"%f\" " % @display[:y]
        svg << "rx=\"%f\" " % (@display[:width] / 2.0)
        svg << "ry=\"%f\" " % (@display[:height] / 2.0)
        svg << "style=\"fill:%s; stroke-width:%f; stroke:%s\" " % [ @display[:node_background_color], @display[:node_border_width], @display[:node_border_color] ]
        svg << ">"
        svg << "</ellipse >"
    else
        svg << "<rect "
        svg << "x=\"%f\" " % (@display[:x] - (@display[:width] / 2.0))
        svg << "y=\"%f\" " % (@display[:y] - (@display[:height] / 2.0))
        svg << "width=\"%f\" " % @display[:width]
        svg << "height=\"%f\" " % @display[:height]
        svg << "style=\"fill:%s; stroke-width:%f; stroke:%s\" " % [ @display[:node_background_color], @display[:node_border_width], @display[:node_border_color] ]
        svg << ">"
        svg << "</rect>"
    end

    text_lines = self.to_s.lines

    line_height = (@display[:height] - (@graph.display[:node_padding] * 2.0)) / text_lines.length

    text_y = (@display[:y] - (@display[:height] / 2.0)) + @graph.display[:node_padding] + @display[:node_border_width] + (@display[:node_border_width] / 2.0) + (line_height / 2.0)

    text_lines.each do | line |

        svg << "<text "
        svg << "text-anchor=\"start\" "
        svg << "x=\"%f\" " % (@display[:x] - (@display[:width] / 2.0) + @graph.display[:node_padding])
        svg << "y=\"%f\" " % text_y
        svg << "fill=\"%s\" " % @graph.display[:font_color]
        svg << "style=\"font-family:%s;font-size:%dpt\"" % [ @graph.display[:font_name], @graph.display[:font_size] ]
        svg << ">"
        svg << @graph.svg_escape( line )
        svg << "</text>"

        text_y += line_height
    end

    svg << "</g>"

    return svg
end
022 by yard 0.9.20 (ruby-2.6.5).