Class: Ruote::Exp::IncExpression

Inherits:
SequenceExpression show all
Defined in:
lib/ruote/exp/fe_inc.rb

Overview

Increments or decrements the value found in a process variable or a workitem field.

One points to a var or a field in these various ways :

sequence do
  inc :var => 'counter'
  inc :field => 'counter'
  inc 'v:counter'
  inc 'f:counter'
end

'inc' and 'dec' work with two types of values : numbers (the default) and arrays.

numbers

The vanilla case of inc/dec is increasing/decreasing a value by 1.

dec :var => 'x'

will decrease the value in variable 'x' by 1.

inc/dec works with two kind of numbers, integers and floats.

If the target var or field is not set, it will be assumed to be at zero.

The default increment is 1 (or 1.0 for floats). It can be changed by passing a value to the inc/dec expression.

inc 'v:x', :val => 3
inc 'v:y', :val => 2.4

arrays

inc/dec can be used to push/pop elements in arrays held in process variables or workitem fields.

This fragment of process definition

sequence do
  set 'v:x' => %w[ a b c d ]
  repeat do
    dec 'v:x', :pos => 'head'
    _break :unless => '${__result__}'
    participant '${__result__}'
  end
end

is equivalent to

iterator :on => 'a, b, c, d', :to_var => 'x' do
  participant '${v:x}'
end

More details : the inc expression expects a value and it will place it at the end of the current array. The :pos or :position attribute can be set to 'head' to let the inc expression place the value at the head of the array.

set 'v:x' => [ 'alfred', 'bryan' ]
set 'v:customer_name' => 'charles'

inc 'v:x', :val => '${v:customer_name}'
  # the variable 'x' now holds [ 'alfred', 'bryan', 'charles' ]
inc 'v:y', :val => '${v:customer_name}', :pos => 'head'
  # the variable 'x' now holds [ 'charles', 'alfred', 'bryan', 'charles' ]

The 'dec' / 'decrement' variant of the expression will remove the tail value (by default) of the array, or the head value if :pos is set to 'head'.

It's also possible to remove a specific value by passing it to 'dec' :

set 'v:x' => [ 'alfred', 'bryan', 'carl' ]
dec 'v:x', :val => 'bryan'
  # the variable 'x' now holds [ 'alfred', 'carl' ]

'dec' places the removed value in workitem field '__result__'. This trick was used in the above iterator example.

A specific variable or field can be specified via the :to_var / :to_field attributes :

dec 'v:x', :to_v => 'a'
participant :ref => '${v:a}'

nested value

(Since ruote 2.3.0)

If nested expressions are provided the __result__ workitem field is used for inc.

inc 'v:x' do
  set '__result__' => 3
end

will increase the value of the variable x by 3.

push and pop

push and pop are aliases for inc and dec respectively. There is a major difference though: they'll force the target value into an array.

sequence do
  set 'v:x' => 2
  push 'v:x' => 3
end

will result in a variable x holding [ 2, 3 ] as value.

Likewise,

pop 'v:x'

will force a value of [] into the variable x if it wasn't previously set or its value was not an array with more than one element.

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 SequenceExpression

#reply

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, #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



155
156
157
158
159
160
# File 'lib/ruote/exp/fe_inc.rb', line 155

def apply

  h.variables ||= {} # ensures a local scope

  reply(h.applied_workitem)
end

#reply_to_parent(workitem) ⇒ Object



162
163
164
165
166
167
168
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
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ruote/exp/fe_inc.rb', line 162

def reply_to_parent(workitem)

  h.applied_workitem['fields'] = workitem['fields']

  key, value = if var_key = has_attribute(:v, :var, :variable)

    var = attribute(var_key)

    [ "v:#{var}", new_value(:var, var, nil) ]

  elsif field_key = has_attribute(:f, :fld, :field)

    field = attribute(field_key)

    [ field, new_value(:field, field, nil) ]

  elsif k = att_text

    [ k, new_value(nil, k, nil) ]

  elsif kv = find_kv

    k, v = kv

    [ k, new_value(nil, k, v) ]

  else

    raise(ArgumentError.new('no variable or field to increment/decrement'))
  end

  h.variables = nil
    # the local scope is over,
    # variables set here will be set in the parent scope

  if dec? && value.is_a?(Array)
    k, car, value = value
    set_vf(k || '__result__', car)
  end

  set_vf(key, value)

  super(h.applied_workitem)
end