Module: Ruote

Defined in:
lib/ruote/version.rb,
lib/ruote/fei.rb,
lib/ruote/worker.rb,
lib/ruote/engine.rb,
lib/ruote/reader.rb,
lib/ruote/context.rb,
lib/ruote/workitem.rb,
lib/ruote/tree_dot.rb,
lib/ruote/util/tree.rb,
lib/ruote/util/look.rb,
lib/ruote/util/misc.rb,
lib/ruote/util/time.rb,
lib/ruote/reader/xml.rb,
lib/ruote/util/ometa.rb,
lib/ruote/log/pretty.rb,
lib/ruote/util/lookup.rb,
lib/ruote/svc/tracker.rb,
lib/ruote/util/filter.rb,
lib/ruote/exp/fe_error.rb,
lib/ruote/util/hashdot.rb,
lib/ruote/storage/base.rb,
lib/ruote/part/template.rb,
lib/ruote/receiver/base.rb,
lib/ruote/svc/dollar_sub.rb,
lib/ruote/util/subprocess.rb,
lib/ruote/reader/ruby_dsl.rb,
lib/ruote/svc/treechecker.rb,
lib/ruote/log/wait_logger.rb,
lib/ruote/log/test_logger.rb,
lib/ruote/util/serializer.rb,
lib/ruote/id/wfid_generator.rb,
lib/ruote/svc/dispatch_pool.rb,
lib/ruote/svc/error_handler.rb,
lib/ruote/storage/fs_storage.rb,
lib/ruote/svc/expression_map.rb,
lib/ruote/svc/expression_map.rb,
lib/ruote/log/default_history.rb,
lib/ruote/log/storage_history.rb,
lib/ruote/storage/hash_storage.rb,
lib/ruote/svc/participant_list.rb,
lib/ruote/engine/process_error.rb,
lib/ruote/part/smtp_participant.rb,
lib/ruote/part/null_participant.rb,
lib/ruote/engine/process_status.rb,
lib/ruote/part/local_participant.rb,
lib/ruote/part/no_op_participant.rb,
lib/ruote/part/block_participant.rb,
lib/ruote/id/mnemo_wfid_generator.rb,
lib/ruote/part/engine_participant.rb,
lib/ruote/part/storage_participant.rb,
lib/ruote/storage/composite_storage.rb

Overview

– Copyright © 2005-2011, John Mettraux, jmettraux@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Made in Japan. ++

Defined Under Namespace

Modules: Dollar, Exp, HashDot, LocalParticipant, Look, Mem, PrettyLogging, ReceiverMixin, RubyDsl, StorageBase, TemplateMixin, WithH, WithMeta, XmlReader Classes: BlankSlate, BlockParticipant, CompositeStorage, Context, DefaultHistory, DispatchPool, DollarSubstitution, Engine, EngineParticipant, EngineVariables, ErrorHandler, ExpressionMap, FlowExpressionId, ForcedError, FsStorage, HashStorage, MnemoWfidGenerator, NoOpParticipant, NullParticipant, ParticipantEntry, ParticipantList, ParticipantRegistrationProxy, ProcessError, ProcessStatus, Reader, Receiver, RuleSession, Serializer, SmtpParticipant, StorageHistory, StorageParticipant, TestLogger, Tracker, TreeChecker, ValidationError, WaitLogger, WfidGenerator, Worker, Workitem

Constant Summary

VERSION =
'2.1.12'
WIN =

Will be set to true if the Ruby runtime is on Windows

(RUBY_PLATFORM.match(/mswin|mingw/) != nil)
JAVA =

Will be set to true if the Ruby runtime is JRuby

(RUBY_PLATFORM.match(/java/) != nil)
SREGEX =

# Upon receiving something like

#
#   "(?-mix:nada)"
#
# will return
#
#   /nada/
#
def self.regex_from_s(s)

  if s.is_a?(String) && m = s.match(/^\(\?-mix:(.+)\)$/)
    Regexp.new(m[1])
  else
    nil
  end
end
/^\s*\/(.*)\/\s*$/

Class Method Summary (collapse)

Class Method Details

+ (Object) constantize(s)

(simpler than the one from active_support)



122
123
124
125
# File 'lib/ruote/util/misc.rb', line 122

def self.constantize(s)

  s.split('::').inject(Object) { |c, n| n == '' ? c : c.const_get(n) }
end

+ (Object) decompose_tree(t, pos = '0', h = {})



374
375
376
377
378
379
# File 'lib/ruote/engine/process_status.rb', line 374

def self.decompose_tree(t, pos='0', h={})

  h[pos] = t[0, 2]
  t[2].each_with_index { |c, i| decompose_tree(c, "#{pos}_#{i}", h) }
  h
end

+ (Object) define(*attributes, &block)

Not really a reader, more an AST builder.

pdef = Ruote.define :name => 'take_out_garbage' do
  sequence do
    take_out_regular_garbage
    take_out_glass
    take_out_paper
  end
end

engine.launch(pdef)


42
43
44
45
# File 'lib/ruote/reader/ruby_dsl.rb', line 42

def self.define(*attributes, &block)

  RubyDsl.create_branch('define', attributes, &block)
end

+ (Object) extract_id(o)

Will do its best to return a wfid (String) or a fei (Hash instance) extract from the given o argument.



65
66
67
68
69
70
# File 'lib/ruote/fei.rb', line 65

def self.extract_id(o)

  return o if o.is_a?(String) and o.index('!').nil? # wfid

  Ruote::FlowExpressionId.extract_h(o)
end

+ (Object) filter(filter, hash, double_caret = nil)

Given a filter (a list of rules) and a hash (probably workitem fields) performs the validations / transformations dictated by the rules.

See the Ruote::Exp::FilterExpression for more information.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruote/util/filter.rb', line 52

def self.filter(filter, hash, double_caret=nil)

  hash = Rufus::Json.dup(hash)

  hash['^'] = Rufus::Json.dup(hash)
  hash['^^'] = double_caret ? Rufus::Json.dup(double_caret) : hash['^']
    # the 'originals'

  filter.each do |rule|
    RuleSession.new(hash, rule).run
  end

  hash.delete('^')
  hash.delete('^^')
    # remove the 'originals'

  hash
end

+ (Object) fulldup(object)

Deep object duplication



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
90
91
# File 'lib/ruote/util/misc.rb', line 48

def self.fulldup(object)

  return object.fulldup if object.respond_to?(:fulldup)
    # trusting client objects providing a fulldup() implementation
    # Tomaso Tosolini 2007.12.11

  begin
    return Marshal.load(Marshal.dump(object))
      # as soon as possible try to use that Marshal technique
      # it's quite fast
  rescue TypeError => te
  end

  #if object.is_a?(REXML::Element)
  #  d = REXML::Document.new object.to_s
  #  return d if object.kind_of?(REXML::Document)
  #  return d.root
  #end
    # avoiding "TypeError: singleton can't be dumped"

  o = object.class.allocate

  # some kind of collection ?

  if object.is_a?(Array)
    object.each { |i| o << fulldup(i) }
  elsif object.is_a?(Hash)
    object.each { |k, v| o[fulldup(k)] = fulldup(v) }
  end

  # duplicate the attributes of the object

  object.instance_variables.each do |v|
    value = object.instance_variable_get(v)
    value = fulldup(value)
    begin
      o.instance_variable_set(v, value)
    rescue Exception => e
      # ignore, must be readonly
    end
  end

  o
end

+ (Object) generate_subid(salt)

This function is used to generate the subids. Each flow expression receives such an id (it's useful for cursors, loops and forgotten branches).

The salt's object_id is used when composing the string that gets digested. One should not pass always the same object as salt.



79
80
81
82
83
# File 'lib/ruote/fei.rb', line 79

def self.generate_subid(salt)

  Digest::MD5.hexdigest(
    "#{salt.object_id}-#{$$}-#{Thread.current.object_id}#{Time.now.to_f}")
end

+ (Boolean) is_a_fei?(o)

A shortcut for

Ruote::FlowExpressionId.is_a_fei?(o)

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/ruote/fei.rb', line 57

def self.is_a_fei?(o)

  Ruote::FlowExpressionId.is_a_fei?(o)
end

+ (Object) is_cron_string(s)

Waiting for a better implementation of it in rufus-scheduler 2.0.4



90
91
92
93
94
95
96
97
98
# File 'lib/ruote/util/time.rb', line 90

def self.is_cron_string(s)

  ss = s.split(' ')

  return false if ss.size < 5 || ss.size > 6
  return false if s.match(/\d{4}/)

  true
end

+ (Boolean) is_definition_tree?(arg)

Returns true if the argument is a process definition tree (whose root is 'define', 'process_definition' or 'workflow_definition'.

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/ruote/util/subprocess.rb', line 62

def self.is_definition_tree?(arg)

  Ruote::Exp::DefineExpression.is_definition?(arg) && is_tree?(arg)
end

+ (Boolean) is_pos_tree?(arg)

Mainly used by Ruote.lookup_subprocess, returns true if the argument is is an array [ position, tree ].

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/ruote/util/subprocess.rb', line 80

def self.is_pos_tree?(arg)

  arg.is_a?(Array) &&
  arg.size == 2 &&
  arg[0].is_a?(String) &&
  is_tree?(arg[1])
end

+ (Boolean) is_tree?(arg)

Returns true if the given argument is a process definition tree (its root doesn't need to be 'define' or 'process_definition' though).

Returns:

  • (Boolean)


70
71
72
73
74
75
# File 'lib/ruote/util/subprocess.rb', line 70

def self.is_tree?(arg)

  arg.is_a?(Array) && arg.size == 3 &&
  arg[0].is_a?(String) && arg[1].is_a?(Hash) && arg[2].is_a?(Array) &&
  (arg.last.empty? || arg.last.find { |e| ! is_tree?(e) }.nil?)
end

+ (Boolean) is_uri?(s)

Returns true if the string seems to correpond to a URI

TODO : wouldn't it be better to simply use URI.parse() ?

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/ruote/util/misc.rb', line 97

def self.is_uri?(s)

  s && (s.index('/') || s.match(/\.[^ ]+$/))
end

+ (Object) lookup(collection, key, container_lookup = false)

h = { 'a' => { 'b' => [ 1, 3, 4 ] } }

p Ruote.lookup(h, 'a.b.1') # => 3


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruote/util/lookup.rb', line 32

def Ruote.lookup(collection, key, container_lookup=false)

  return collection if key == '.'

  key, rest = pop_key(key)
  value = flookup(collection, key)

  return [ key, collection ] if container_lookup && rest.size == 0
  return [ rest.first, value ] if container_lookup && rest.size == 1
  return value if rest.size == 0
  return nil if value == nil

  lookup(value, rest)
end

+ (Object) lookup_subprocess(fexp, ref)

This method is used by the 'subprocess' expression and by the EngineParticipant.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruote/util/subprocess.rb', line 35

def self.lookup_subprocess(fexp, ref)

  val = fexp.lookup_variable(ref)

  # a classical subprocess stored in a variable ?

  return [ '0', val ] if is_tree?(val)
  return val if is_pos_tree?(val)

  # maybe subprocess :ref => 'uri'

  subtree = fexp.context.reader.read(ref) rescue nil

  if subtree && is_definition_tree?(subtree)
    _, subtree = Ruote::Exp::DefineExpression.reorganize(subtree)
  end

  return [ '0', subtree ] if is_tree?(subtree)

  # no luck ...

  raise "no subprocess named '#{ref}' found"
end

+ (Object) narrow_to_number(o)

Tries to return an Integer or a Float from the given input. Returns



111
112
113
114
115
116
117
118
# File 'lib/ruote/util/misc.rb', line 111

def self.narrow_to_number(o)

  return o if [ Fixnum, Bignum, Float ].include?(o.class)

  s = o.to_s

  (s.index('.') ? Float(s) : Integer(s)) rescue nil
end

+ (Object) neutralize(s)

Returns a neutralized version of s, suitable as a filename.



104
105
106
107
# File 'lib/ruote/util/misc.rb', line 104

def self.neutralize(s)

  s.to_s.strip.gsub(/[ \/:;\*\\\+\?]/, '_')
end

+ (Object) now_to_utc_s

Returns a parsable representation of the UTC time now.

like “2009/11/23 11:11:50.947109 UTC”



57
58
59
60
# File 'lib/ruote/util/time.rb', line 57

def self.now_to_utc_s

  time_to_utc_s(Time.now)
end

+ (Object) p_caller(*msg)

Prints the current call stack to stdout



38
39
40
41
42
43
44
# File 'lib/ruote/util/misc.rb', line 38

def self.p_caller(*msg)

  puts
  puts "  == #{msg.inspect} =="
  caller(1).each { |l| puts "  #{l}" }
  puts
end

+ (Object) process_definition(*attributes, &block)

Same as Ruote.define()

pdef = Ruote.process_definition :name => 'take_out_garbage' do
  sequence do
    take_out_regular_garbage
    take_out_paper
  end
end

engine.launch(pdef)


58
59
60
61
# File 'lib/ruote/reader/ruby_dsl.rb', line 58

def self.process_definition(*attributes, &block)

  define(*attributes, &block)
end

+ (Object) recompose_tree(h, pos = '0')



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/ruote/engine/process_status.rb', line 381

def self.recompose_tree(h, pos='0')

  t = h[pos]

  return nil unless t

  t << []
  i = 0

  loop do
    tt = recompose_tree(h, "#{pos}_#{i}")
    break unless tt
    t.last << tt
    i = i + 1
  end

  t
end

+ (Object) regex_or_s(s)

regex_or_s(“/nada/”) #==> /nada/

regex_or_s("nada") #==> "nada"
regex_or_s(/nada/) #==> /nada/


150
151
152
153
154
155
156
157
# File 'lib/ruote/util/misc.rb', line 150

def self.regex_or_s(s)

  if s.is_a?(String) && m = SREGEX.match(s)
    Regexp.new(m[1])
  else
    s
  end
end

+ (Object) s_to_at(s)

Turns a date or a duration to a Time object pointing AT a point in time…

(my prose is weak)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruote/util/time.rb', line 66

def self.s_to_at(s)

  at = if s.index(' ')
    #
    # date

    Rufus.to_ruby_time(s)# rescue nil

  else
    #
    # duration

    Time.now.utc.to_f + Rufus.parse_time_string(s)
  end

  case at
    when DateTime then at.to_time.utc
    when Float then Time.at(at).utc
    else at
  end
end

+ (Object) schedule_to_h(sched)

Refines a schedule as found in the ruote storage into something a bit easier to present.



887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'lib/ruote/engine.rb', line 887

def self.schedule_to_h(sched)

  h = sched.dup

  h.delete('_rev')
  h.delete('type')
  msg = h.delete('msg')
  owner = h.delete('owner')

  h['wfid'] = owner['wfid']
  h['action'] = msg['action']
  h['type'] = msg['flavour']
  h['owner'] = Ruote::FlowExpressionId.new(owner)
  h['target'] = Ruote::FlowExpressionId.new(msg['fei'])

  h
end

+ (Object) set(collection, key, value)

h = { 'customer' => { 'name' => 'alpha' } }

Ruote.set(h, 'customer.name', 'bravo')

h #=> { 'customer' => { 'name' => 'bravo' } }


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruote/util/lookup.rb', line 53

def Ruote.set(collection, key, value)

  k, c = lookup(collection, key, true)

  if c
    k = k.to_i if c.is_a?(Array)
    c[k] = value
  else
    collection[key] = value
  end
end

+ (Object) sid(fei)

A shorter shortcut for

Ruote::FlowExpressionId.to_storage_id(fei)


48
49
50
51
# File 'lib/ruote/fei.rb', line 48

def self.sid(fei)

  Ruote::FlowExpressionId.to_storage_id(fei)
end

+ (Object) time_to_utc_s(t)

Produces the UTC string representation of a Time

like “2009/11/23 11:11:50.947109 UTC”



48
49
50
51
# File 'lib/ruote/util/time.rb', line 48

def self.time_to_utc_s(t)

  "#{t.utc.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', t.usec)} UTC"
end

+ (Object) to_storage_id(fei)

A shortcut for

Ruote::FlowExpressionId.to_storage_id(fei)


39
40
41
42
# File 'lib/ruote/fei.rb', line 39

def self.to_storage_id(fei)

  Ruote::FlowExpressionId.to_storage_id(fei)
end

+ (Object) to_tree(&block)

Similar in purpose to Ruote.define and Ruote.process_definition but instead of returning a [process] definition, returns the tree.

tree = Ruote.process_definition :name => 'take_out_garbage' do
  sequence do
    take_out_regular_garbage
    take_out_paper
  end
end

p tree
  # => [ 'sequence', {}, [ [ 'take_out_regular_garbage', {}, [] ], [ 'take_out_paper', {}, [] ] ] ],

This is useful when modifying a process instance via methods like re_apply :

engine.re_apply(
  fei,
  :tree => Ruote.to_tree {
    sequence do
      participant 'alfred'
      participant 'bob'
    end
  })
    #
    # cancels the segment of process at fei and replaces it with
    # a simple alfred-bob sequence.


90
91
92
93
# File 'lib/ruote/reader/ruby_dsl.rb', line 90

def self.to_tree(&block)

  RubyDsl.create_branch('x', {}, &block).last.first
end

+ (Object) tree_to_dot(tree, name = 'ruote process definition')

Turns a process definition tree to a graphviz dot representation.

www.graphviz.org



32
33
34
35
36
37
# File 'lib/ruote/tree_dot.rb', line 32

def self.tree_to_dot(tree, name='ruote process definition')

  s = "digraph \"#{name}\" {\n"
  s << branch_to_dot('0', tree).join("\n")
  s << "\n}\n"
end

+ (Object) tree_to_s(tree, expid = '0')

Turning a tree into a numbered string view

require 'ruote/util/tree'
require 'ruote/reader/ruby_dsl'

pdef = Ruote.process_definition :name => 'def0' do
  sequence do
    alpha
    bravo
  end
end

p pdef
  # => ["define", {"name"=>"def0"}, [["sequence", {}, [["alpha", {}, []], ["bravo", {}, []]]]]]

puts Ruote.tree_to_s(pdef)
  # =>
  #    0  define {"name"=>"def0"}
  #      0_0  sequence {}
  #        0_0_0  alpha {}
  #        0_0_1  bravo {}


50
51
52
53
54
55
56
# File 'lib/ruote/util/tree.rb', line 50

def Ruote.tree_to_s(tree, expid='0')

  d = expid.split('_').size - 1
  s = "#{' ' * d * 2}#{expid}  #{tree[0]} #{tree[1].inspect}\n"
  tree[2].each_with_index { |t, i| s << tree_to_s(t, "#{expid}_#{i}") }
  s
end

+ (Object) unset(collection, key)

h = { 'customer' => { 'name' => 'alpha', 'rank' => '1st' } }

r = Ruote.unset(h, 'customer.rank')

h # => { 'customer' => { 'name' => 'alpha' } }
r # => '1st'


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruote/util/lookup.rb', line 71

def Ruote.unset(collection, key)

  k, c = lookup(collection, key, true)

  return collection.delete(key) unless c

  if c.is_a?(Array)
    c.delete_at(Integer(k)) rescue nil
  else
    c.delete(k)
  end
end