Class: Yargi::Decorate

Inherits:
Object
  • Object
show all
Defined in:
lib/yargi/decorate.rb

Constant Summary collapse

DEPTH =
Decorate.new{|d|
  d.key       = :depth
  d.bottom    = 1.0/0
  d.d0        = 0
  d.suppremum = lambda{|d1,d2| d1 < d2 ? d1 : d2}
  d.propagate = lambda{|d,e| d+1}
}
SHORTEST_PATH =
Decorate.new{|d|
  d.key       = :shortest_path
  d.bottom    = nil
  d.d0        = []
  d.suppremum = lambda{|d1,d2|
    d1.nil? ? d2 : (d2.nil? ? d1 : (d1.size < d2.size ? d1 : d2))
  }
  d.propagate = lambda{|d,e|
    d.nil? ? [e] : (d + [e])
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Decorate

Returns a new instance of Decorate.

Yields:

  • (_self)

Yield Parameters:



9
10
11
# File 'lib/yargi/decorate.rb', line 9

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



4
5
6
# File 'lib/yargi/decorate.rb', line 4

def bottom
  @bottom
end

#d0Object

Returns the value of attribute d0.



5
6
7
# File 'lib/yargi/decorate.rb', line 5

def d0
  @d0
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/yargi/decorate.rb', line 3

def key
  @key
end

#propagateObject

Returns the value of attribute propagate.



7
8
9
# File 'lib/yargi/decorate.rb', line 7

def propagate
  @propagate
end

#suppremumObject

Returns the value of attribute suppremum.



6
7
8
# File 'lib/yargi/decorate.rb', line 6

def suppremum
  @suppremum
end

Instance Method Details

#execute(digraph, initials = digraph.vertices(0)) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yargi/decorate.rb', line 13

def execute(digraph, initials = digraph.vertices(0))
  # all to bottom except initial states
  digraph.each_vertex{|s| s[key] = bottom}
  initials.each{|s| s[key] = d0}

  # main loop
  to_explore = initials
  until to_explore.empty?
    source = to_explore.pop
    source.out_edges.each do |edge|
      target = edge.target
      p_decor = propagate.call(source[key], edge)
      p_decor = suppremum.call(target[key], p_decor)
      unless p_decor == target[key]
        target[key] = p_decor
        to_explore << target unless to_explore.include?(target)
      end
    end
  end
end