class Flor::Pro::Iterator

Public Instance Methods

add() click to toggle source
# File lib/flor/pcore/iterator.rb, line 30
def add

  elts = message['elements']

  elts = elts.inject([]) { |a, e| a.concat(e.to_a) } \
    if @node['ocol'].is_a?(Hash)

  @node['col'].concat(elts)

  [] # no new messages to queue
end
pre_execute() click to toggle source
# File lib/flor/pcore/iterator.rb, line 5
def pre_execute

  @node['vars'] ||= {}

  @node['args'] = [] # before iterating, arguments are collected

  @node['ocol'] = nil # original collection
  @node['fun'] = nil # function

  @node['col'] = nil # collection
  @node['idx'] = -1

  unatt_unkeyed_children
end
receive_non_att() click to toggle source
# File lib/flor/pcore/iterator.rb, line 20
def receive_non_att

  if @node['args']
    receive_argument
  else
    receive_iteration
    iterate
  end
end

Protected Instance Methods

apply_iteration() click to toggle source
# File lib/flor/pcore/iterator.rb, line 104
def apply_iteration

  #vars = determine_iteration_vars
  #args = vars.values
  #vars.each { |k, v| @node['vars'][k] = v }
  #
  #apply(@node['fun'], args, tree[2])

  apply(@node['fun'], determine_iteration_args, tree[2])
end
determine_iteration_args() click to toggle source
# File lib/flor/pcore/iterator.rb, line 115
def determine_iteration_args

  idx = @node['idx']
  elt = @node['col'][idx]
  len = @node['col'].length

  args =
    if @node['ocol'].is_a?(Array)
      [ [ 'elt', elt ] ]
    else
      [ [ 'key', elt[0] ], [ 'val', elt[1] ] ]
    end
  args << [ 'idx', idx ]
  args << [ 'len', len ]

  args
end
end_iterator() click to toggle source
# File lib/flor/pcore/iterator.rb, line 138
def end_iterator

  wrap_reply('ret' => iterator_result)
end
function_mandatory?() click to toggle source
# File lib/flor/pcore/iterator.rb, line 69
def function_mandatory?

  true
end
iterate() click to toggle source
# File lib/flor/pcore/iterator.rb, line 55
def iterate

  prepare_iterations unless @node['ocol']

  return no_iterate unless @node['fun']

  @node['idx'] += 1
  @node['mtime'] = Flor.tstamp

  return end_iterator if iterator_over?

  apply_iteration
end
iterator_over?() click to toggle source
# File lib/flor/pcore/iterator.rb, line 133
def iterator_over?

  @node['idx'] == @node['col'].size
end
prepare_iterations() click to toggle source
# File lib/flor/pcore/iterator.rb, line 74
def prepare_iterations

  prepare_iterator

  @node['args']
    .each { |a|
      if Flor.is_func_tree?(a)
        @node['fun'] ||= a
      elsif Flor.is_collection?(a)
        @node['ocol'] ||= a
      end }

  ocol = (@node['ocol'] ||= node_payload_ret)

  fail Flor::FlorError.new(
    "function not given to #{heap.inspect}", self
  ) if function_mandatory? && ( ! @node['fun'])
  fail Flor::FlorError.new(
    "collection not given to #{heap.inspect}", self
  ) unless Flor.is_collection?(ocol)

  @node['col'] = Flor.to_coll(ocol) if @node['fun']
  @node['args'] = nil
end
prepare_iterator() click to toggle source
# File lib/flor/pcore/iterator.rb, line 99
def prepare_iterator

  @node['res'] = []
end
receive_argument() click to toggle source
# File lib/flor/pcore/iterator.rb, line 44
def receive_argument

  @node['args'] << payload['ret']

  if children[@ncid]
    execute_child(@ncid)
  else
    iterate
  end
end