Class: Seafoam::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/seafoam/graph.rb

Overview

A graph, with properties, nodes, and edges. We don’t encapsulate the graph too much - be careful.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = nil) ⇒ Graph

Returns a new instance of Graph.



7
8
9
10
11
12
# File 'lib/seafoam/graph.rb', line 7

def initialize(props = nil)
  @props = props || {}
  @nodes = {}
  @edges = []
  @blocks = []
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



5
6
7
# File 'lib/seafoam/graph.rb', line 5

def blocks
  @blocks
end

#edgesObject (readonly)

Returns the value of attribute edges.



5
6
7
# File 'lib/seafoam/graph.rb', line 5

def edges
  @edges
end

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/seafoam/graph.rb', line 5

def nodes
  @nodes
end

#propsObject (readonly)

Returns the value of attribute props.



5
6
7
# File 'lib/seafoam/graph.rb', line 5

def props
  @props
end

Instance Method Details

#create_block(id, node_ids) ⇒ Object

Add a new basic block with given id and node id list.



33
34
35
36
37
38
# File 'lib/seafoam/graph.rb', line 33

def create_block(id, node_ids)
  nodes = node_ids.select { |n| @nodes.key? n }.map { |n| @nodes[n] }
  block = Block.new(id, nodes)
  @blocks.push block
  block
end

#create_edge(from, to, props = nil) ⇒ Object

Create an edge between two nodes.



23
24
25
26
27
28
29
30
# File 'lib/seafoam/graph.rb', line 23

def create_edge(from, to, props = nil)
  props ||= {}
  edge = Edge.new(from, to, props)
  @edges.push edge
  from.outputs.push edge
  to.inputs.push edge
  edge
end

#create_node(id, props = nil) ⇒ Object

Create a node.



15
16
17
18
19
20
# File 'lib/seafoam/graph.rb', line 15

def create_node(id, props = nil)
  props ||= {}
  node = Node.new(id, props)
  @nodes[id] = node
  node
end