Class: Zgomot::Comp::Markov

Inherits:
Object show all
Defined in:
lib/zgomot/comp/markov.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMarkov

Returns a new instance of Markov.



13
14
15
# File 'lib/zgomot/comp/markov.rb', line 13

def initialize
  @current_state, @states = 0, []
end

Instance Attribute Details

#current_stateObject (readonly)

Returns the value of attribute current_state.



11
12
13
# File 'lib/zgomot/comp/markov.rb', line 11

def current_state
  @current_state
end

#statesObject (readonly)

Returns the value of attribute states.



11
12
13
# File 'lib/zgomot/comp/markov.rb', line 11

def states
  @states
end

Class Method Details

.markObject



6
7
8
# File 'lib/zgomot/comp/markov.rb', line 6

def mark
  new
end

Instance Method Details

#add(trans, &blk) ⇒ Object



17
18
19
# File 'lib/zgomot/comp/markov.rb', line 17

def add(trans, &blk)
  @states << {:trans=>sum_trans(trans), :blk => blk}
end

#init(state, args = {}) ⇒ Object



21
22
23
24
# File 'lib/zgomot/comp/markov.rb', line 21

def init(state, args={})
  @current_state = state
  states[@current_state][:blk].call(args)
end

#next(args = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/zgomot/comp/markov.rb', line 26

def next(args={})
  r, state = rand, states[@current_state]
  @current_state = state[:trans].select{|t| r >= t}.count
  Zgomot.logger.info "CURRENT MARKOV STATE: #{current_state}"
  blk = states[@current_state][:blk]
  blk.arity > 0 ? blk.call(args) : blk.call
end