Class: Ruote::Exp::ApplyExpression

Inherits:
FlowExpression show all
Defined in:
lib/ruote/exp/fe_apply.rb

Overview

The 'apply' expression is an advanced expression.

It takes as input an AST and applies it. The AST may be placed in a field or a variable or passed directly to the apply.

These apply examples :

apply :tree => [ 'echo', { 'nada' => nil }, [] ]

sequence do
  set :var => 'tree', :val => [ 'echo', { 'nada' => nil }, [] ]
  apply # looks by default in variable 'tree'
end

sequence do
  set :var => 't', :val => [ 'echo', { 'nada' => nil }, [] ]
  apply :tree_var => 't'
end

sequence do
  set :field => 't', :val => [ 'echo', { 'nada' => nil }, [] ]
  apply :tree_field => 't'
end

All are equivalent to

echo 'nada'

apply and subprocesses

There is an interesting way of using 'apply', it's close to the way of the Ruby “yield” expression.

pdef = Ruote.process_definition 'test' do
  sequence do
    handle do
      participant 'alpha'
    end
    handle do
      participant 'bravo'
    end
  end
  define 'handle' do
    sequence do
      participant 'prepare_data'
      apply
      participant 'rearrange_data'
    end
  end
end

With this process definition, the particpant alpha and bravo are handed a workitem in sequence, but each time, the data gets prepared and re-arranged.

'apply' simply picks the value of the tree to apply in the local variable 'tree'.

Passing variables to applied trees is possible :

pdef = Ruote.process_definition do
  handle do
    participant '${v:target}', :message => 'x'
  end
  define 'handle' do
    sequence do
      participant 'prepare_data'
      apply :v => 'alpha'
      apply :v => 'bravo'
      participant 'rearrange_data'
    end
  end
end

on_error

It's OK, to place an on_error on the apply

pdef = Ruote.process_definition do
  handle do
    sequence do
      echo 'in'
      nemo
    end
  end
  define 'handle' do
    apply :on_error => 'notify' # <==
    echo 'over.'
  end
  define 'notify' do
    echo 'error'
  end
end

Constant Summary

Constants inherited from FlowExpression

FlowExpression::COMMON_ATT_KEYS

Instance Attribute Summary

Attributes inherited from FlowExpression

#context, #error

Instance Method Summary collapse

Methods inherited from FlowExpression

#ancestor?, #applied_workitem, #att, #att_text, #attribute, #attribute_text, #attributes, #await, #cancel, #cancel_flanks, #cfei_at, #child_id, #child_ids, #compile_atts, #compile_variables, #debug_id, #deflate, #do, do_action, #do_apply, #do_cancel, #do_fail, #do_pause, #do_persist, #do_reply, #do_reply_to_parent, #do_resume, #do_unpersist, dummy, #fei, fetch, from_h, #handle_on_error, #has_attribute, #initial_persist, #initialize, #is_concurrent?, #iterative_var_lookup, #launch_sub, #lookup_val, #lookup_val_prefix, #lookup_variable, #name, names, #parent, #parent_id, #pause_on_apply, #persist_or_raise, #reply, #reply_to_parent, #root, #root_id, #set_variable, #tree, #tree_children, #try_persist, #try_unpersist, #unpersist_or_raise, #unset_variable, #update_tree, #variables, #wfid

Methods included from WithMeta

#class_def, included

Methods included from WithH

#h, #h=, included, #to_h

Constructor Details

This class inherits a constructor from Ruote::Exp::FlowExpression

Instance Method Details

#applyObject

TODO : apply [ 'echo', { 'nada' => nil }, [] ]


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ruote/exp/fe_apply.rb', line 134

def apply

  #
  # find 'tree'

  tree =
    lookup_val_prefix('tree', :escape => true) ||
    lookup_variable('tree')

  return reply_to_parent(h.applied_workitem) unless tree

  #
  # apply 'tree'

  launch_sub(
    "#{h.fei['expid']}_0", tree,
    :variables => compile_atts(:escape => true))
end