Class | OpenWFE::ProcessDefinition |
In: |
lib/openwfe/expressions/rprocdef.rb
|
Parent: | Object |
Extend this class to create a programmatic process definition.
A short example :
class MyProcessDefinition < OpenWFE::ProcessDefinition def make process_definition :name => "test1", :revision => "0" do sequence do set :variable => "toto", :value => "nada" print "toto:${toto}" end end end end li = OpenWFE::LaunchItem.new(MyProcessDefinition) engine.launch(li)
ClassNameRex | = | Regexp.compile( " *class *([a-zA-Z0-9]*) *< .*ProcessDefinition") |
ProcessDefinitionRex | = | Regexp.compile( "^class *[a-zA-Z0-9]* *< .*ProcessDefinition") |
ProcessNameAndDefRex | = | Regexp.compile( "([^0-9_]*)_*([0-9].*)$") |
ProcessNameRex | = | Regexp.compile( "(.*$)") |
EndsInDefinitionRex | = | Regexp.compile( ".*Definition$") |
context | [R] |
A class method for actually "making" the process segment raw representation
# File lib/openwfe/expressions/rprocdef.rb, line 172 172: def self.do_make (instance=nil) 173: 174: context = if @ccontext 175: 176: @ccontext.discard 177: # preventing further additions in case of reevaluation 178: @ccontext 179: 180: elsif instance 181: 182: instance.make 183: instance.context 184: else 185: 186: pdef = self.new 187: pdef.make 188: pdef.context 189: end 190: 191: return context.top_expression if context.top_expression 192: 193: name, revision = 194: extract_name_and_revision(self.metaclass.to_s[8..-2]) 195: 196: top_expression = [ 197: "process-definition", 198: { "name" => name, "revision" => revision }, 199: context.top_expressions 200: ] 201: 202: top_expression 203: end
Turns a String containing a ProcessDefinition …
# File lib/openwfe/expressions/rprocdef.rb, line 224 224: def self.eval_ruby_process_definition (code, safety_level=2) 225: 226: #puts "\nin:\n#{code}\n" 227: 228: code, is_wrapped = wrap_code code 229: 230: o = Rufus::eval_safely code, safety_level, binding() 231: 232: o = extract_class(code) \ 233: if (o == nil) or o.is_a?(Array) 234: #if (o == nil) or o.is_a?(SimpleExpRepresentation) 235: # 236: # grab the first process definition class found 237: # in the given code 238: 239: #return o.do_make # if o.is_a?(ProcessDefinition) or o.is_a?(Class) 240: #o 241: 242: result = o.do_make 243: 244: #return result.first_child if is_wrapped 245: return result.last.first if is_wrapped 246: 247: result 248: end
Parses the string to find the class name of the process definition and returns that class (instance).
# File lib/openwfe/expressions/rprocdef.rb, line 209 209: def self.extract_class (ruby_proc_def_string) 210: 211: ruby_proc_def_string.each_line do |l| 212: 213: m = ClassNameRex.match l 214: 215: return eval(m[1]) if m 216: end 217: 218: nil 219: end
builds an actual expression representation (a node in the process definition tree).
# File lib/openwfe/expressions/rprocdef.rb, line 107 107: def self.make_expression (context, exp_name, params, &block) 108: 109: string_child = nil 110: #attributes = OpenWFE::SymbolHash.new 111: attributes = Hash.new 112: 113: #puts " ... params.class is #{params.class}" 114: 115: if params.kind_of?(Hash) 116: 117: params.each do |k, v| 118: 119: if k == '0' 120: string_child = v.to_s 121: else 122: #attributes[OpenWFE::symbol_to_name(k.to_s)] = v.to_s 123: attributes[OpenWFE::symbol_to_name(k.to_s)] = v 124: end 125: end 126: 127: elsif params 128: 129: string_child = params.to_s 130: end 131: 132: exp = [ exp_name, attributes, [] ] 133: 134: exp.last << string_child \ 135: if string_child 136: 137: if context.parent_expression 138: # 139: # adding this new expression to its parent 140: # 141: context.parent_expression.last << exp 142: else 143: # 144: # an orphan, a top expression 145: # 146: context.top_expressions << exp 147: end 148: 149: return exp unless block 150: 151: context.push_parent_expression exp 152: 153: result = block.call 154: 155: exp.last << result \ 156: if result and result.kind_of?(String) 157: 158: context.pop_parent_expression 159: 160: exp 161: end
# File lib/openwfe/expressions/rprocdef.rb, line 70 70: def self.metaclass; class << self; self; end; end
# File lib/openwfe/expressions/rprocdef.rb, line 91 91: def self.method_missing (m, *args, &block) 92: 93: @ccontext = Context.new \ 94: if (not @ccontext) or @ccontext.discarded? 95: 96: ProcessDefinition.make_expression( 97: @ccontext, 98: OpenWFE::to_expression_name(m), 99: ProcessDefinition.pack_args(args), 100: &block) 101: end
# File lib/openwfe/expressions/rprocdef.rb, line 74 74: def initialize 75: 76: super() 77: @context = Context.new 78: end
# File lib/openwfe/expressions/rprocdef.rb, line 305 305: def self.as_name (s) 306: 307: return s[0..-11] if EndsInDefinitionRex.match(s) 308: s 309: end
# File lib/openwfe/expressions/rprocdef.rb, line 311 311: def self.as_revision (s) 312: 313: s.gsub("_", ".") 314: end
# File lib/openwfe/expressions/rprocdef.rb, line 291 291: def self.extract_name_and_revision (s) 292: 293: i = s.rindex("::") 294: s = s[i+2..-1] if i 295: 296: m = ProcessNameAndDefRex.match s 297: return [ as_name(m[1]), as_revision(m[2]) ] if m 298: 299: m = ProcessNameRex.match s 300: return [ as_name(m[1]), '0' ] if m 301: 302: [ as_name(s), '0' ] 303: end
# File lib/openwfe/expressions/rprocdef.rb, line 276 276: def self.pack_args (args) 277: 278: return args[0] if args.length == 1 279: 280: a = {} 281: args.each_with_index do |arg, index| 282: if arg.is_a?(Hash) 283: a = a.merge(arg) 284: break 285: end 286: a[index.to_s] = arg 287: end 288: a 289: end
# File lib/openwfe/expressions/rprocdef.rb, line 264 264: def self.wrap_code (code) 265: 266: return [ code, false ] if ProcessDefinitionRex.match(code) 267: 268: s = "class NoName0 < ProcessDefinition" 269: s << "\n" 270: s << code 271: s << "\nend" 272: 273: [ s, true ] 274: end
# File lib/openwfe/expressions/rprocdef.rb, line 163 163: def do_make 164: 165: ProcessDefinition.do_make self 166: end