Class: DAG::Vertex

Inherits:
Object
  • Object
show all
Defined in:
lib/dag/vertex.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dagObject (readonly)

Returns the value of attribute dag.



6
7
8
# File 'lib/dag/vertex.rb', line 6

def dag
  @dag
end

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'lib/dag/vertex.rb', line 6

def payload
  @payload
end

Instance Method Details

#[](key) ⇒ Object

Retrieve a value from the vertex’s payload. This is a shortcut for vertex.payload.

Parameters:

  • key (Object)

    the payload key

Returns:

  • the corresponding value from the payload Hash, or nil if not found



73
74
75
# File 'lib/dag/vertex.rb', line 73

def [](key)
  @payload[key]
end

#ancestors(result_set = Set.new) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/dag/vertex.rb', line 77

def ancestors(result_set = Set.new)
  predecessors.each do |v|
    unless result_set.include? v
      result_set.add(v)
      v.ancestors(result_set)
    end
  end
  return result_set
end

#descendants(result_set = Set.new) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/dag/vertex.rb', line 87

def descendants(result_set = Set.new)
  successors.each do |v|
    unless result_set.include? v
      result_set.add(v)
      v.descendants(result_set)
    end
  end
  return result_set
end

#has_ancestor?(other) ⇒ Boolean Also known as: is_reachable_from?

Is there a path from other to here following edges in the DAG?

Parameters:

Returns:

  • (Boolean)

    true iff there is a path following edges within this DAG

Raises:

  • (ArgumentError)

    if other is not a Vertex in the same DAG



58
59
60
61
62
# File 'lib/dag/vertex.rb', line 58

def has_ancestor?(other)
  raise ArgumentError.new('You must supply a vertex in this DAG') unless
    is_vertex_in_my_dag?(other)
  predecessors.include?(other) || predecessors.any? {|v| v.has_ancestor?(other) }
end

#has_path_to?(other) ⇒ Boolean Also known as: has_descendant?, has_descendent?

Is there a path from here to other following edges in the DAG?

Parameters:

Returns:

  • (Boolean)

    true iff there is a path following edges within this DAG

Raises:

  • (ArgumentError)

    if other is not a Vertex in the same DAG



42
43
44
45
46
# File 'lib/dag/vertex.rb', line 42

def has_path_to?(other)
  raise ArgumentError.new('You must supply a vertex in this DAG') unless
    is_vertex_in_my_dag?(other)
  successors.include?(other) || successors.any? {|v| v.has_path_to?(other) }
end

#incoming_edgesObject



19
20
21
# File 'lib/dag/vertex.rb', line 19

def incoming_edges
  @dag.edges.select {|e| e.destination == self}
end

#inspectObject



31
32
33
# File 'lib/dag/vertex.rb', line 31

def inspect
  "DAG::Vertex:#{@payload.inspect}"
end

#outgoing_edgesObject



15
16
17
# File 'lib/dag/vertex.rb', line 15

def outgoing_edges
  @dag.edges.select {|e| e.origin == self}
end

#predecessorsObject



23
24
25
# File 'lib/dag/vertex.rb', line 23

def predecessors
  incoming_edges.map(&:origin).uniq
end

#successorsObject



27
28
29
# File 'lib/dag/vertex.rb', line 27

def successors
  outgoing_edges.map(&:destination).uniq
end