Class: Ruote::Exp::IfExpression

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

Overview

The ‘if’ construct found in many programming languages.

_if ?

Why the “_if” in all the examples below? Well, all the examples are using the Ruby DSL, using ‘if’ alone isn’t possible, the Ruby parser would think it’s the Ruby’s own if…

But process definitions written in Radial (see jmettraux.github.com/2012-09-03-ruote-2.3.0.html) don’t have this restriction:

if '${customer} == fred'
  salesman_henry  # then clause
  salesman_josh   # else clause

then / else clauses

The ‘if’ expression accepts two or three children branches, in pseudo-code it looks like:

_if do
  <condition>
  <then clause>
end

# or

_if <condition> do
  <then clause>
end

# or

_if do
  <condition>
  <then clause>
  <else clause>
end

# or

_if <condition> do
  <then clause>
  <else clause>
end

This piece of process definition:

_if '${customer} == fred' do
  salesman_henry
  salesman_josh
end

is thus interpreted as:

_if '${customer} == fred' do
  salesman_henry  # then clause
  salesman_josh   # else clause
end

If the intent was to express a sequence henry - josh, it should be written as:

_if '${customer} == fred' do
  sequence do # then clause
    salesman_henry
    salesman_josh
  end
end

Note this can be alternatively written as:

sequence :if => '${customer} == fred' do
  salesman_henry
  salesman_josh
end

examples

Here are some examples:

_if do
  equals :field_value => 'customer', :other_value => 'British Petroleum'
  participant :ref => 'Allister'
end

and:

_if :test => '${f:customer} == British Petroleum' do
  participant :ref => 'Allister'
end

An else clause is accepted:

_if do
  equals :field_value => 'customer', :other_value => 'British Petroleum'
  participant :ref => 'Allister'
  participant :ref => 'Bernardo'
end

or:

_if :test => '${f:customer} == British Petroleum' do
  participant :ref => 'Allister'
  participant :ref => 'Bernardo'
end

Note that any expression accepts an :if attribute:

participant :ref => 'Al', :if => '${f:customer} == British Petroleum'

shorter

The :test can be shortened to a :t :

_if :t => '${f:customer.name} == Fred' do
  subprocess 'premium_course'
  subprocess 'regular_course'
end

When using Ruby to generate the process definition tree, you can simply do:

_if '${f:customer.name} == Fred' do
  subprocess 'premium_course'
  subprocess 'regular_course'
end

Constant Summary

Constants inherited from FlowExpression

FlowExpression::COMMON_ATT_KEYS

Instance Attribute Summary

Attributes inherited from FlowExpression

#context, #error, #h

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_to_parent, #root, #root_id, #set_variable, #to_h, #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

included

Constructor Details

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

Instance Method Details

#applyObject



162
163
164
165
# File 'lib/ruote/exp/fe_if.rb', line 162

def apply

  reply(h.applied_workitem)
end

#reply(workitem) ⇒ Object

called by ‘else’, ‘then’ or perhaps ‘equals’



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ruote/exp/fe_if.rb', line 169

def reply(workitem)

  if workitem['fei'] == h.fei # apply --> reply

    h.test = attribute(:test)
    h.test = attribute(:t) if h.test.nil?
    h.test = attribute_text if h.test.nil?
    h.test = nil if h.test == ''

    offset = (h.test.nil? || Condition.true?(h.test)) ? 0 : 1

    apply_child(offset, workitem)

  else # reply from a child

    if h.test != nil || Ruote::FlowExpressionId.child_id(workitem['fei']) != 0

      reply_to_parent(workitem)

    else

      apply_child(workitem['fields']['__result__'] == true ? 1 : 2, workitem)
    end
  end
end