Class: Seafoam::Node

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

Overview

A node, with properties, input edges, and output edges.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, props = nil) ⇒ Node

Returns a new instance of Node.



57
58
59
60
61
62
63
# File 'lib/seafoam/graph.rb', line 57

def initialize(id, props = nil)
  props ||= {}
  @id = id
  @inputs = []
  @outputs = []
  @props = props
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



55
56
57
# File 'lib/seafoam/graph.rb', line 55

def id
  @id
end

#inputsObject (readonly)

Returns the value of attribute inputs.



55
56
57
# File 'lib/seafoam/graph.rb', line 55

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



55
56
57
# File 'lib/seafoam/graph.rb', line 55

def outputs
  @outputs
end

#propsObject (readonly)

Returns the value of attribute props.



55
56
57
# File 'lib/seafoam/graph.rb', line 55

def props
  @props
end

Instance Method Details

#adjacentObject

All adjacent nodes - from input and output edges.



79
80
81
# File 'lib/seafoam/graph.rb', line 79

def adjacent
  (inputs.map(&:from) + outputs.map(&:to)).uniq
end

#edgesObject

All edges - input and output.



74
75
76
# File 'lib/seafoam/graph.rb', line 74

def edges
  inputs + outputs
end

#id_and_labelObject

id (label)



84
85
86
87
88
89
90
# File 'lib/seafoam/graph.rb', line 84

def id_and_label
  if props[:label]
    "#{id} (#{props[:label]})"
  else
    id.to_s
  end
end

#inspectObject

Inspect.



93
94
95
# File 'lib/seafoam/graph.rb', line 93

def inspect
  "<Node #{id} #{node_class}>"
end

#node_classObject



65
66
67
68
69
70
71
# File 'lib/seafoam/graph.rb', line 65

def node_class
  if @props[:synthetic] == true
    @props[:synthetic_class]
  else
    @props.dig(:node_class, :node_class)
  end
end

#visit(&block) ⇒ Object



97
98
99
# File 'lib/seafoam/graph.rb', line 97

def visit(&block)
  block.call(self)
end

#visit_outputs(search_strategy, &block) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/seafoam/graph.rb', line 101

def visit_outputs(search_strategy, &block)
  if search_strategy == :bfs
    BFS.new(self).search(&block)
  else
    raise "Unknown search strategy: #{search_strategy}"
  end
end