Class: Ruote::ProcessStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/engine/process_status.rb

Overview

A ‘view’ on the status of a process instance.

Returned by the #process and the #processes methods of the Engine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, expressions, stored_workitems, errors, schedules) ⇒ ProcessStatus

Called by Ruote::Engine#processes or Ruote::Engine#process.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruote/engine/process_status.rb', line 64

def initialize(context, expressions, stored_workitems, errors, schedules)

  #
  # preparing data

  @expressions = expressions.collect { |e|
    Ruote::Exp::FlowExpression.from_h(context, e) }
  @expressions.sort! { |a, b| a.fei.expid <=> b.fei.expid }

  @stored_workitems = stored_workitems.collect { |h|
    Ruote::Workitem.new(h)
  }

  @errors = errors.sort! { |a, b| a.fei.expid <=> b.fei.expid }
  @schedules = schedules.sort! { |a, b| a['owner'].sid <=> b['owner'].sid }

  @root_expression = root_expressions.first

  #
  # linking errors and expressions for easy navigation

  @errors.each do |err|
    err.flow_expression = @expressions.find { |fexp| fexp.fei == err.fei }
    err.flow_expression.error = err if err.flow_expression
  end
end

Instance Attribute Details

#errorsObject (readonly)

An array of errors currently plaguing the process instance. Hopefully, this array is empty.



55
56
57
# File 'lib/ruote/engine/process_status.rb', line 55

def errors
  @errors
end

#expressionsObject (readonly)

The expressions that compose the process instance.



39
40
41
# File 'lib/ruote/engine/process_status.rb', line 39

def expressions
  @expressions
end

#root_expressionObject (readonly)

Returns the expression at the root of the process instance.



43
44
45
# File 'lib/ruote/engine/process_status.rb', line 43

def root_expression
  @root_expression
end

#schedulesObject (readonly)

An array of schedules (open structs yielding information about the schedules of this process)



60
61
62
# File 'lib/ruote/engine/process_status.rb', line 60

def schedules
  @schedules
end

#stored_workitemsObject (readonly)

An array of the workitems currently in the storage participant for this process instance.

Do not confuse with #workitems



50
51
52
# File 'lib/ruote/engine/process_status.rb', line 50

def stored_workitems
  @stored_workitems
end

Instance Method Details

#all_tagsObject

Returns a hash tagname => array of feis of all the tags set in the process instance.



149
150
151
152
153
154
155
# File 'lib/ruote/engine/process_status.rb', line 149

def all_tags

  all_variables.inject({}) do |h, (fei, vars)|
    vars.each { |k, v| (h[k] ||= []) << v if FlowExpressionId.is_a_fei?(v) }
    h
  end
end

#all_variablesObject

Returns a hash fei => variable_hash containing all the variable bindings (expression by expression) of the process instance.



130
131
132
133
134
135
136
# File 'lib/ruote/engine/process_status.rb', line 130

def all_variables

  @expressions.inject({}) do |h, exp|
    h[exp.fei] = exp.variables if exp.variables
    h
  end
end

#current_treeObject

Returns the current version of the process definition tree. If no manipulation (gardening) was performed on the tree, this method yields the same result as the #original_tree method.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/ruote/engine/process_status.rb', line 386

def current_tree

  h = Ruote.decompose_tree(original_tree)

  @expressions.sort { |e0, e1|

    e0.fei.expid <=> e1.fei.expid

  }.each { |e|

    trigger = e.tree[1]['_triggered']

    tree = if trigger && trigger != 'on_re_apply'
      t = original_tree_from_parent(e).dup
      t[1]['_triggered'] = trigger
      t
    else
      e.tree
    end

    h.merge!(Ruote.decompose_tree(tree, e.fei.expid))
  }

  Ruote.recompose_tree(h)
end

#definition_nameObject

For a process

Ruote.process_definition :name => 'review', :revision => '0.1' do
  author
  reviewer
end

will yield ‘review’.



173
174
175
176
177
178
# File 'lib/ruote/engine/process_status.rb', line 173

def definition_name

  @root_expression && (
    @root_expression.attribute('name') ||
    @root_expression.attribute_text)
end

#definition_revisionObject

For a process

Ruote.process_definition :name => 'review', :revision => '0.1' do
  author
  reviewer
end

will yield ‘0.1’.



189
190
191
192
193
194
# File 'lib/ruote/engine/process_status.rb', line 189

def definition_revision

  @root_expression && (
    @root_expression.attribute('revision') ||
    @root_expression.attribute('rev'))
end

#inspectObject



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/ruote/engine/process_status.rb', line 316

def inspect

  vars = variables rescue nil
  avars = all_variables.inject({}) { |h, (k, v)| h[Ruote.sid(k)] = v; h }

  s = [ "== #{self.class} ==" ]
  s << "   expressions : #{@expressions.size}"
  @expressions.each do |e|
    s << "     #{e.fei.to_storage_id}"
    s << "       | #{e.name}"
    s << "       | * #{e.state} *" if e.state
    s << "       | #{e.attributes.inspect}"
    s << "       `-parent--> #{e.h.parent_id ? e.parent_id.to_storage_id : 'nil'}"
  end
  s << "   schedules : #{@schedules.size}"
  s << "   stored workitems : #{@stored_workitems.size}"
  s << "   variables :     #{vars.inspect}"
  s << "   all_variables : #{avars.inspect}"
  s << "   errors : #{@errors.size}"
  @errors.each do |e|
    s << "     ***"
    s << "     #{e.fei.to_storage_id} :" if e.fei
    s << "     action : #{e.action}"
    s << "     message : #{e.message}"
    s << "     trace :"
    e.trace.split("\n").each do |line|
      s << "       #{line}"
    end
    s << "     fields : #{e.fields.inspect}"
  end

  s.join("\n") + "\n"
end

#last_activeObject

Returns a parseable UTC datetime string which indicates when the process was last active.



286
287
288
289
# File 'lib/ruote/engine/process_status.rb', line 286

def last_active

  @expressions.collect { |fexp| fexp.h.put_at }.max
end

#launched_timeObject

Returns a Time instance indicating when the process instance was launched.



301
302
303
304
# File 'lib/ruote/engine/process_status.rb', line 301

def launched_time

  @root_expression && @root_expression.created_time
end

#leavesObject

Returns the expressions where the flow is currently, ak the leaves of the execution tree.

Whereas #position only looks at participant expressions (and errors), #leaves looks at any expressions that is a leave (which has no child at this point).

Returns an array of FlowExpression instances. (Note that they may have their attribute #error set).



243
244
245
246
247
248
# File 'lib/ruote/engine/process_status.rb', line 243

def leaves

  expressions.inject([]) { |a, exp|
    a.select { |e| ! exp.ancestor?(e.fei) } + [ exp ]
  }
end

#original_treeObject

Returns the process definition tree as it was when this process instance was launched.



294
295
296
297
# File 'lib/ruote/engine/process_status.rb', line 294

def original_tree

  @root_expression && @root_expression.original_tree
end

#positionObject

Returns the ‘position’ of the process.

pdef = Ruote.process_definition do
  alpha :task => 'clean car'
end
wfid = engine.launch(pdef)

sleep 0.500

engine.process(wfid) # => [["0_0", "alpha", {"task"=>"clean car"}]]

A process with concurrent branches will yield multiple ‘positions’.

It uses #workitems underneath.

If you want to list all the expressions where the “flow currently is” regardless they are participant expressions or errors, look at the #leaves method.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ruote/engine/process_status.rb', line 215

def position

  workitems.collect { |wi|

    r = [ wi.fei.sid, wi.participant_name ]

    params = (wi.fields['params'] || {}).dup
    params.delete('ref')

    if err = errors.find { |e| e.fei == wi.fei }
      params['error'] = err.message
    end

    r << params
    r
  }
end

#root_expression_for(fei) ⇒ Object

Given an expression id, returns the root (top ancestor) for its expression.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruote/engine/process_status.rb', line 108

def root_expression_for(fei)

  sfei = Ruote.sid(fei)

  exp = @expressions.find { |fe| sfei == Ruote.sid(fe.fei) }

  return nil unless exp
  return exp if exp.parent_id.nil?

  root_expression_for(exp.parent_id)
end

#root_expressionsObject

Returns a list of all the expressions that have no parent expression. The list is sorted with the deeper (closer to the original root) first.



94
95
96
97
98
99
100
101
102
103
# File 'lib/ruote/engine/process_status.rb', line 94

def root_expressions

  roots = @expressions.select { |e| e.h.parent_id == nil }

  roots = roots.inject({}) { |h, e|
    h["#{e.h.fei['expid']}__#{e.h.fei['subid']}"] = e; h
  }

  roots.keys.sort.collect { |k| roots[k] }
end

#root_workitemObject

Returns the workitem as was applied at the root expression.



252
253
254
255
# File 'lib/ruote/engine/process_status.rb', line 252

def root_workitem

  Ruote::Workitem.new(root_expression.h.applied_workitem)
end

#tagsObject

Returns a hash tagname => fei of tags set at the root of the process instance.



141
142
143
144
# File 'lib/ruote/engine/process_status.rb', line 141

def tags

  Hash[variables.select { |k, v| FlowExpressionId.is_a_fei?(v) }]
end

#to_dot(opts = {}) ⇒ Object

Returns a ‘dot’ representation of the process. A graph describing the tree of flow expressions that compose the process.



353
354
355
356
357
358
359
360
361
# File 'lib/ruote/engine/process_status.rb', line 353

def to_dot(opts={})

  s = [ "digraph \"process wfid #{wfid}\" {" ]
  @expressions.each { |e| s.push(*e.send(:to_dot, opts)) }
  @errors.each { |e| s.push(*e.send(:to_dot, opts)) }
  s << "}"

  s.join("\n")
end

#to_sObject



306
307
308
309
310
311
312
313
314
# File 'lib/ruote/engine/process_status.rb', line 306

def to_s

  "(process_status wfid '#{wfid}', " +
  "expressions #{@expressions.size}, " +
  "stored_workitems #{@stored_workitems.size}, " +
  "errors #{@errors.size}, " +
  "schedules #{@schedules.size}, " +
  ")"
end

#variablesObject

Returns the process variables set for this process instance.



122
123
124
125
# File 'lib/ruote/engine/process_status.rb', line 122

def variables

  @root_expression && @root_expression.variables
end

#wfidObject

Returns the unique identifier for this process instance.



159
160
161
162
# File 'lib/ruote/engine/process_status.rb', line 159

def wfid

  @expressions.any? ? @expressions.first.fei.wfid : @errors.first.fei.wfid
end

#workitemsObject

Returns a list of the workitems currently ‘out’ to participants

For example, with an instance of

Ruote.process_definition do
  concurrence do
    alpha :task => 'clean car'
    bravo :task => 'sell car'
  end
end

calling engine.process(wfid).workitems will yield two workitems (alpha and bravo).

Warning : do not confuse the workitems here with the workitems held in a storage participant or equivalent.



274
275
276
277
278
279
280
281
# File 'lib/ruote/engine/process_status.rb', line 274

def workitems

  @expressions.select { |fexp|
    fexp.is_a?(Ruote::Exp::ParticipantExpression)
  }.collect { |fexp|
    Ruote::Workitem.new(fexp.h.applied_workitem)
  }
end