apply

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