public
Description: a ruby workflow engine
Homepage: http://ruote.rubyforge.org
Clone URL: git://github.com/jmettraux/ruote.git
ruote / lib / openwfe / expressions / fe_step.rb
100644 147 lines (121 sloc) 4.41 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#--
# Copyright (c) 2008-2009, 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.
#++
 
 
require 'openwfe/expressions/flowexpression'
 
 
module OpenWFE
 
  #
  # This expression takes its root in this "trouble ticket blog post" :
  #
  # http://jmettraux.wordpress.com/2008/01/04/the-trouble-ticket-process/
  #
  # In this post, the "step" was implemented directly in the OpenWFEru
  # process definition language.
  #
  # It's been turned into an expression and it's not limited anymore to
  # the concept "state is a participant, transition points to a subprocess",
  # state can now point to a subprocess as well as to a participant, idem
  # for a transition.
  #
  # In other words, this "step" expression allows you to write
  # state-transition process definitions in OpenWFEru (the Ruote workflow
  # engine). But don't abuse it. Classical OpenWFEru constructs can
  # do most of the job.
  #
  # An interesting aspect of the "step" expression is that it can remove
  # the need for some "if" expression constructs (well the fact
  #
  # class ProcDef0 < OpenWFE::ProcessDefinition
  #
  # sequence do
  #
  # step "Alfred", :outcomes => [ 'blue_pen', 'red_pen' ]
  # # Alfred gets to choose between a blue pen and a red pen
  #
  # participant "Bob"
  # # flow resumes with Bob in a classical way (sequence)
  # end
  #
  # define "blue_pen" do
  # # ... Alfred buying a blue pen
  # end
  # define "red_pen" do
  # # ... Alfred buying a red pen
  # end
  # end
  #
  # in XML it would look like :
  #
  # <sequence>
  # <step ref="Alfred" outcomes="blue_pen, red_pen" />
  # <participant ref="Bob" />
  # </sequence>
  #
  # You can specify a default outcome (else if the outcome doesn't correspond
  # to a participant or a subprocess, the flow will cease) :
  #
  # <step ref="toto" outcomes="left, right" default="right" />
  #
  # For some more discussions about Ruote and state-transition see
  #
  # http://groups.google.com/group/openwferu-dev/t/16e713c1313cb2fa
  #
  class StepExpression < FlowExpression
 
    names :step
 
    attr_accessor :outcomes
    attr_accessor :default
 
 
    def apply (workitem)
 
      step =
        lookup_attribute(:ref, workitem) ||
        fetch_text_content(workitem)
 
      # keeping track of outcomes and default as found at apply time
 
      @outcomes = lookup_array_attribute(
        :outcomes, workitem, :to_s => true)
 
      @default = lookup_attribute(
        :default, workitem, :to_s => true)
 
      #store_itself
        # now done in tlaunch_child()
 
      # launching the 'step' itself
 
      template = [
        step.to_s, # expression name
        lookup_attributes(workitem), # attributes
        [], # children
      ]
 
      get_expression_pool.tlaunch_child(
        self, template, 0, workitem, :register_child => true)
    end
 
    def reply (workitem)
 
      outcome = workitem.fields.delete('outcome')
      outcome = outcome.to_s if outcome
 
      outcome = @default \
        if @outcomes and (not @outcomes.include?(outcome))
 
      return reply_to_parent(workitem) \
        unless outcome
 
      template = [
        outcome.to_s, # expression name
        {}, # attributes
        [], # children
      ]
 
      get_expression_pool.substitute_and_apply(self, template, workitem)
    end
  end
 
end