Class: Relyze::Graph::Node
- Inherits:
-
Object
- Object
- Relyze::Graph::Node
- Defined in:
- C:/Program Files/Relyze/lib/relyze/core/graph.rb
Overview
A base class representing a node in a graph.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#control_dependencies ⇒ Object
- nil, Array<Relyze::Graph::Node>
-
An array of nodes belonging to this nodes control dependencies or nil if it has not yet been calculated.
-
#data ⇒ Object
- Object
-
A data object to associate with this node.
-
#display ⇒ Object
readonly
- Hash
-
A hash of display properties for this node.
-
#dominance_frontier ⇒ Object
- nil, Array<Relyze::Graph::Node>
-
An array of nodes belonging to this nodes dominance frontier or nil if it has not yet been calculated.
-
#dominator_children ⇒ Object
[nil, Array<Relyze::Graph::Node>].
-
#edges ⇒ Object
readonly
- Array<Relyze::Graph::Edge>
-
An array of edge objects.
-
#graph ⇒ Object
readonly
- Relyze::Graph::Graph
-
The graph this node belongs to.
-
#id ⇒ Object
readonly
- Integer
-
A unique id number for this node.
-
#immediate_dominator ⇒ Object
- nil, Relyze::Graph::Node
-
This nodes immediate dominator or nil if it has not yet been calculated.
-
#immediate_post_dominator ⇒ Object
- nil, Relyze::Graph::Node
-
This nodes immediate post dominator or nil if it has not yet been calculated.
-
#name ⇒ Object
- String
-
Gets this node's name.
-
#post_dominance_frontier ⇒ Object
- nil, Array<Relyze::Graph::Node>
-
An array of nodes belonging to this nodes dominance frontier or nil if it has not yet been calculated.
-
#post_dominator_children ⇒ Object
[nil, Array<Relyze::Graph::Node>].
Instance Method Summary collapse
-
#degree ⇒ Integer
Get this nodes degree.
-
#edge?(node) ⇒ true, false
Check if this node contains an edge to another node.
-
#indegree ⇒ Integer
Get this nodes in degree.
-
#initialize(graph, id, data = nil) ⇒ Node
constructor
Initialize a new node object.
-
#outdegree ⇒ Integer
Get this nodes out degree.
-
#remove ⇒ nil
Remove this node from the graph.
-
#to_dot ⇒ String
Export this node object as a DOT node.
-
#to_s ⇒ String
Get this nodes text content.
-
#to_svg ⇒ String
Export this node object as a SVG element, suitable for embedding in HTML.
Constructor Details
#initialize(graph, id, data = nil) ⇒ Node
Initialize a new node object.
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_dependencies ⇒ Object
- 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 |
#data ⇒ Object
- 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 |
#display ⇒ Object (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_frontier ⇒ Object
- 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_children ⇒ Object
- 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 |
#edges ⇒ Object (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 |
#graph ⇒ Object (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 |
#id ⇒ Object (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_dominator ⇒ Object
- 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_dominator ⇒ Object
- 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 |
#name ⇒ Object
- 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_frontier ⇒ Object
- 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_children ⇒ Object
- 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
#degree ⇒ Integer
Get this nodes degree.
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
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 |
#indegree ⇒ Integer
Get this nodes in degree.
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 |
#outdegree ⇒ Integer
Get this nodes out degree.
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 |
#remove ⇒ nil
Remove this node from the graph.
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_dot ⇒ String
Export this node object as a DOT node.
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_s ⇒ String
Get this nodes text content. If a data object is available we use this to generate the content, otherwise this nodes name is used.
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_svg ⇒ String
Export this node object as a SVG element, suitable for embedding in HTML.
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 |