Class: BPEL_Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ BPEL_Transform

Returns a new instance of BPEL_Transform.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 7

def initialize(fname)
  @base = ::File.dirname(fname)
  @doc = XML::Smart.open(fname)
  @doc.namespaces = {
    'bpel' => 'http://docs.oasis-open.org/wsbpel/2.0/process/executable',
    'bpws' => 'http://schemas.xmlsoap.org/ws/2003/03/business-process/',
    'ext'  => 'http://www.activebpel.org/2006/09/bpel/extension/query_handling',
    'xsd'  => 'http://www.w3.org/2001/XMLSchema'
  }
  @acounter = 0
  @MULTI = 2
  @vars = {}
end

Instance Method Details

#bpel_copy_x(c, what, op) ⇒ Object

}}}



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 193

def bpel_copy_x(c,what,op) # {{{
  result = ''
  c.find(what).each do |to|
    text = if to.attributes['variable']
      if to.attributes['part']
        temp = "$#{to.attributes['variable']}"
        temp << '/' + @vars[to.attributes['variable']].to_s + to.attributes['part']
        result << transform_bpel_xpath(temp,op)
      else
        result << "data.#{to.attributes['variable']}"
      end
      temp
    else
      result << transform_bpel_xpath(to.text,op)
    end
  end
  result
end

#extract_varsObject

}}}



32
33
34
35
36
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 32

def extract_vars # {{{
  @doc.find("//bpel:variables/bpel:variable").each do |v|
    @vars[v.attributes['name']] = extract_ns_plus v.attributes['messageType']
  end  
end

}}}



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 178

def print_activity_assign(e,spaces) # {{{
  result = ''
  e.find('bpel:copy').each do |c|
    result << print_spaces(spaces)
    result << bpel_copy_x(c,'bpel:to','set')
    result << ' = '
    result << bpel_copy_x(c,'bpel:from','evaluate')
    result << "\n"
  end
  result
end

}}}



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 332

def print_activity_call(e,spaces) # {{{
  result = ", :call, :\"#{e.attributes['partnerLink']}.#{e.attributes['operation']}\", data.#{e.attributes['inputVariable']}"
  if e.attributes['outputVariable']
    result << " do |result|\n"
    result << print_spaces(spaces+@MULTI)
    result << "data.#{e.attributes['outputVariable']} = result\n"
    result << print_activity_end(spaces)
  else  
    result << "\n"
  end  
end

}}}



189
190
191
192
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 189

def print_activity_end(spaces) # {{{
  result = print_spaces(spaces)
  result << "end\n"
end

}}}



175
176
177
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 175

def print_activity_manipulate(e) # {{{
  result = ", :manipulate do\n"
end

}}}



169
170
171
172
173
174
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 169

def print_activity_plain(spaces) # {{{
  @acounter += 1
  result = print_spaces(spaces)
  result << 'activity :a'
  result << @acounter.to_s
end

}}}



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 225

def print_alternative(e,word,spaces) # {{{
  result = ''
  result << print_spaces(spaces) 
  case word 
    when 'alternative'
      result << word + " " 
      result << transform_bpel_xpath(e.find('string(bpel:condition)'),'evaluate')
    when 'otherwise'
      result << word
  end    
  result << " do\n"
  result << print_elements(e,spaces+@MULTI)
  result << print_spaces(spaces) << "end\n"
  result
end

}}}



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 212

def print_choose(e,spaces) # {{{
  result = ''
  result << print_spaces(spaces) << "choose do\n"
  result << print_alternative(e,'alternative',spaces+@MULTI)
  e.find('bpel:elseif').each do |ei|
    result << print_alternative(ei,'alternative',spaces+@MULTI)
  end
  e.find('bpel:else').each do |ei|
    result << print_alternative(ei,'otherwise',spaces+@MULTI)
  end
  result << print_spaces(spaces) << "end\n"
  result
end

}}}



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 134

def print_element(e,spaces) # {{{
  result = ''
  case e.name.name
    when 'invoke'
      result << print_activity_plain(spaces)
      result << print_activity_call(e,spaces)
    when 'receive'
      result << print_activity_plain(spaces)
      result << print_activity_call(e)
      result << print_activity_end(spaces)
    when 'reply'
      result << print_reply(e,spaces)
    when 'forEach'
      result << print_foreach(e,spaces)
    when 'pick'
    when 'sequence'
      result << print_elements(e,spaces)
    when 'pick'
    when 'scope'
    when 'wait'
    when 'assign'
      result << print_activity_plain(spaces)
      result << print_activity_manipulate(e)
      result << print_activity_assign(e,spaces+@MULTI)
      result << print_activity_end(spaces)
    when 'if'
      result << print_choose(e,spaces)
    when 'while'
      result << print_while(e,spaces)
    when 'flow'
      result << print_parallel(e,spaces) 
  end
  result
end

}}}



127
128
129
130
131
132
133
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 127

def print_elements(e,spaces) # {{{
  result = ''
  e.find("*[not(@createInstance) or @createInstance='no']").each do |e|
    result << print_element(e,spaces)
  end  
  result
end

}}}



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 252

def print_foreach(e,spaces) # {{{
  result = ''
  cname = e.find('string(@counterName)')
  parallel = e.find('boolean(@parallel[.="yes"])')
  sps = spaces

  if parallel
    result << print_spaces(sps) << "parallel(:wait) do\n"
    sps += @MULTI
  end  

  result << print_spaces(sps) << "#{cname} = " << e.find('string(bpel:startCounterValue)') << "\n"
  result << print_spaces(sps) << "loop pre_test{" 
  result << "#{cname} <= " << transform_bpel_xpath(e.find('string(bpel:finalCounterValue)'),'evaluate')
  result << "} do\n"
  if parallel
    sps += @MULTI
    result << print_spaces(sps) << "parallel_branch(#{cname}) do |#{cname}|\n"
  end  
  e.find("*[name()='bpel:scope']").each do |f|
    result << print_elements(f,sps+@MULTI)
  end  
  if parallel
    result << print_spaces(sps) << "end\n"
    sps -= @MULTI
  end  
  result << print_spaces(sps+@MULTI) << "#{cname} += 1\n"
  result << print_spaces(sps) << "end\n"

  if parallel
    result << print_spaces(spaces) << "end\n"
  end  
  result
end

}}}



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 294

def print_parallel(e,spaces) # {{{
  result = ''
  result << print_spaces(spaces) 
  result << "parallel do\n" 

  result << print_spaces(spaces+@MULTI) 
  result << "links = {}\n"

  e.find("*[name()!='bpel:links']").each do |e|
    result << print_spaces(spaces+@MULTI) 
    result << "parallel_branch do\n"

    e.find('bpel:targets/bpel:target').each do |l|
      result << print_spaces(spaces+@MULTI+@MULTI)
      result << "links[\"" + l.attributes['linkName'] + "\"] = Thread.current\n"
      result << print_spaces(spaces+@MULTI+@MULTI)
      result << "Thread.current.stop\n"
    end

    result << print_element(e,spaces+@MULTI+@MULTI)

    e.find('bpel:sources/bpel:source').each do |s|
      result << print_spaces(spaces+@MULTI+@MULTI)
      result << "until links.include?(\"" + s.attributes["linkName"] + "\") && links[\"" + s.attributes["linkName"] + "\"].stop?\n"
      result << print_spaces(spaces+@MULTI+@MULTI+@MULTI)
      result << "Thread.current.pass\n"
      result << print_spaces(spaces+@MULTI+@MULTI) << "end\n"
      result << print_spaces(spaces+@MULTI+@MULTI)
      result << "links[\"" + s.attributes["linkName"] + "\"].run\n"
    end

    result << print_spaces(spaces+@MULTI) << "end\n"
  end

  result << print_spaces(spaces) << "end\n"
  result
end

}}}



287
288
289
290
291
292
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 287

def print_reply(e,spaces) # {{{
  result = ''
  result << print_spaces(spaces) 
  result << "status.update(1,\"#{e.attributes['partnerLink']}.#{e.attributes['operation']};#{e.attributes['variable']}\")"
  result
end

}}}



344
345
346
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 344

def print_spaces(spaces) # {{{
  ' ' * spaces
end

}}}



241
242
243
244
245
246
247
248
249
250
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 241

def print_while(e,spaces) # {{{
  result = ''
  result << print_spaces(spaces) 
  result << "loop pre_test{" 
  result << transform_bpel_xpath(e.find('string(bpel:condition)'),'evaluate')
  result << "} do\n"
  result << print_elements(e,spaces+@MULTI)
  result << print_spaces(spaces) << "end\n"
  result
end

#transform_bpel_xpath(text, op) ⇒ Object

}}}



347
348
349
350
351
352
353
354
355
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 347

def transform_bpel_xpath(text,op) # {{{
  text.gsub!(/\$([a-z][a-zA-Z0-9]+)\.Document/,'/helper/\1')
  text.gsub!(/\$([a-z][a-zA-Z0-9]+)\.([a-z][a-zA-Z0-9]+)/) do
    t1,t2 = $1,$2
    "/helper/#{t1}/" + @vars[t1] + t2
  end 
  text.gsub!(/\$([a-z][a-zA-Z0-9]+)/,'/helper/\1')
  "XPATHHelper.#{op}(\"" + text.strip + "\")"
end

#transform_dataObject

}}}



37
38
39
40
41
42
43
44
45
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 37

def transform_data # {{{
  result = "<dataelements>\n"
  @doc.find("//bpel:variables/bpel:variable").each do |v|
    result << print_spaces(@MULTI)
    result << "<" + v.attributes['name'] + "/>\n"
  end  
  result << '</dataelements>'
  result
end

#transform_dslObject

{{{



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 21

def transform_dsl # {{{
  @acounter = 0
  @vars = {}
  extract_vars
  spaces = 0
  result = ''
  @doc.find("/bpel:process/bpel:sequence").each do |e|
    result << print_elements(e,spaces)
  end
  result
end

#transform_endpointsObject

}}}



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
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 46

def transform_endpoints # {{{
  result = XML::Smart.string('<endpoints/>')
  result.namespaces = {
    'xsd'  => 'http://www.w3.org/2001/XMLSchema',
  }
  @doc.find("//bpel:invoke").each do |e|
    op = e.attributes['operation']
    plnk_name = e.attributes['partnerLink']
    @doc.find("/bpel:process/bpel:partnerLinks/bpel:partnerLink[@name=\"#{plnk_name}\"]").each do |f|
      plnk_role = f.attributes['partnerRole'] 
      plnk_type = remove_ns f.attributes['partnerLinkType']
      @doc.find("/bpel:process/bpel:import[@importType=\"http://www.w3.org/ns/wsdl\"]").each do |g|
        XML::Smart.open(@base + "/" + g.attributes['location']) do |w|
          w.namespaces = {
            'wsdl' => 'http://www.w3.org/ns/wsdl',
            'plnk' => 'http://schemas.xmlsoap.org/ws/2003/05/partner-link/',
            'std'  => g.attributes['namespace'],
            'xsd'  => 'http://www.w3.org/2001/XMLSchema',
            'whttp' => 'http://www.w3.org/ns/wsdl/http'
          }
          w.find("/wsdl:description/plnk:partnerLinkType[@name='#{plnk_type}']/plnk:role[@name='#{plnk_role}']").each do |h|
            interface = remove_ns h.attributes['portType']
            method = w.find("string(/wsdl:description/wsdl:binding[@interface='#{interface}']/wsdl:operation[@ref='#{op}']/@whttp:method)")
            method = method == '' ? 'SOAP' : method
            n1 = result.root.add("#{plnk_name}.#{op}")
            w.find("/wsdl:description/wsdl:interface[@name='#{interface}']/wsdl:operation[@name='#{op}']/wsdl:*").each do |i|
              schema_nsn = extract_ns i.attributes['element'] 
              schema_root = remove_ns i.attributes['element']
              schema_ns = w.root.namespaces[schema_nsn]
              w.find("/wsdl:description/wsdl:types/xsd:schema[@targetNamespace='#{schema_ns}']").each do |s|
                n2 = n1.add(i.name.to_s, 'method' => method, 'targetNamespace' => w.root.attributes['targetNamespace'])
                n2.add(s)
                n2.find("xsd:schema/xsd:element[@name!='#{schema_root}']").delete_all!
              end
            end
          end
        end  
      end
    end
  end
  result.to_s
end

#transform_invocationObject

}}}



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
# File 'lib/cpee/processtransformation/bpel/lib/BPEL_Transform.rb', line 88

def transform_invocation # {{{
  result = XML::Smart.string('<invocations/>')
  @doc.find("/bpel:process/bpel:sequence/bpel:receive[@createInstance='yes']").each do |e|
    op = e.attributes['operation']
    plnk_name = e.attributes['partnerLink']
    @doc.find("/bpel:process/bpel:partnerLinks/bpel:partnerLink[@name=\"#{plnk_name}\"]").each do |f|
      plnk_role = f.attributes['myRole'] 
      plnk_type = remove_ns f.attributes['partnerLinkType']
      @doc.find("/bpel:process/bpel:import[@importType=\"http://www.w3.org/ns/wsdl\"]").each do |g|
        XML::Smart.open(@base + "/" + g.attributes['location']) do |w|
          w.namespaces = {
            'wsdl' => 'http://www.w3.org/ns/wsdl',
            'plnk' => 'http://schemas.xmlsoap.org/ws/2003/05/partner-link/',
            'std'  => g.attributes['namespace'],
            'xsd'  => 'http://www.w3.org/2001/XMLSchema',
            'whttp' => 'http://www.w3.org/ns/wsdl/http'
          }
          w.find("/wsdl:description/plnk:partnerLinkType[@name='#{plnk_type}']/plnk:role[@name='#{plnk_role}']").each do |h|
            interface = remove_ns h.attributes['portType']
            method = w.find("string(/wsdl:description/wsdl:binding[@interface='#{interface}']/wsdl:operation[@ref='#{op}']/@whttp:method)")
            method = method == '' ? 'SOAP' : method
            w.find("/wsdl:description/wsdl:interface[@name='#{interface}']/wsdl:operation[@name='#{op}']/wsdl:input").each do |i|
              schema_nsn = extract_ns i.attributes['element'] 
              schema_root = remove_ns i.attributes['element']
              schema_ns = w.root.namespaces[schema_nsn]
              w.find("/wsdl:description/wsdl:types/xsd:schema[@targetNamespace='#{schema_ns}']/xsd:element[@name!='#{schema_root}']").delete_all!
              w.find("/wsdl:description/wsdl:types/xsd:schema[@targetNamespace='#{schema_ns}']").each do |s|
                node = result.root.add('invocation', 'plnk_name' => plnk_name, 'plnk_role' => plnk_role, 'plnk_type' => plnk_type, 'interface' => interface, 'method' => method, 'targetNamespace' => w.root.attributes['targetNamespace'])
                node.add(s)
              end
            end
          end
        end  
      end
    end
  end
  result.to_s
end