Class: MARS::Rendering::Graph::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/mars/rendering/graph/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



9
10
11
12
13
# File 'lib/mars/rendering/graph/builder.rb', line 9

def initialize
  @adjacency = Hash.new { |h, k| h[k] = [] }
  @nodes = {}
  @subgraphs = {}
end

Instance Attribute Details

#adjacencyObject (readonly)

Returns the value of attribute adjacency.



7
8
9
# File 'lib/mars/rendering/graph/builder.rb', line 7

def adjacency
  @adjacency
end

#nodesObject (readonly)

Returns the value of attribute nodes.



7
8
9
# File 'lib/mars/rendering/graph/builder.rb', line 7

def nodes
  @nodes
end

#subgraphsObject (readonly)

Returns the value of attribute subgraphs.



7
8
9
# File 'lib/mars/rendering/graph/builder.rb', line 7

def subgraphs
  @subgraphs
end

Instance Method Details

#add_edge(from, to, value = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/mars/rendering/graph/builder.rb', line 15

def add_edge(from, to, value = nil)
  return unless from && to

  # can we avoid visiting the node twice instead?
  adjacency[from] << [to, value] unless adjacency[from].include?([to, value])
  adjacency[to] = [] unless adjacency[to]
end

#add_node(id, value, type) ⇒ Object



23
24
25
26
27
# File 'lib/mars/rendering/graph/builder.rb', line 23

def add_node(id, value, type)
  return if nodes.key?(id)

  nodes[id] = Node.new(id, value, type)
end

#add_node_to_subgraph(id, node_id) ⇒ Object



35
36
37
38
39
# File 'lib/mars/rendering/graph/builder.rb', line 35

def add_node_to_subgraph(id, node_id)
  return if subgraphs[id]&.nodes&.include?(node_id)

  subgraphs[id].nodes << node_id
end

#add_subgraph(id, name) ⇒ Object



29
30
31
32
33
# File 'lib/mars/rendering/graph/builder.rb', line 29

def add_subgraph(id, name)
  return if subgraphs.key?(id)

  subgraphs[id] = Subgraph.new(id, name, [])
end