lib/openwfe/expressions/raw_prog.rb in openwferu-0.9.6 vs lib/openwfe/expressions/raw_prog.rb in openwferu-0.9.7
- old
+ new
@@ -39,10 +39,11 @@
# John Mettraux at openwfe.org
#
require 'rexml/document'
+require 'openwfe/utils'
require 'openwfe/expressions/fe_raw'
require 'openwfe/expressions/fe_utils'
module OpenWFE
@@ -175,122 +176,136 @@
# engine.launch(li)
# </tt>
#
class ProcessDefinition
- attr_reader :top_expressions
+ def self.metaclass; class << self; self; end; end
- #def initialize (exp_names=nil)
- def initialize ()
+ attr_reader :context
+ def initialize ()
super()
-
- @top_expressions = []
- @previous_parent_expression = []
+ @context = Context.new
end
def method_missing (m, *args, &block)
- methodname = m.to_s
+ #puts "__i_method_missing >>>#{m}<<<<"
- expname = OpenWFE.to_expression_name(methodname)
+ ProcessDefinition.make_expression(
+ @context,
+ OpenWFE::to_expression_name(m),
+ ProcessDefinition.pack_args(args),
+ &block)
+ end
- args = pack_args(args)
+ def self.method_missing (m, *args, &block)
- make_expression(expname, args, &block)
+ #puts "__c_method_missing >>>#{m}<<<<"
+
+ @ccontext = Context.new() unless @ccontext
+
+ ProcessDefinition.make_expression(
+ @ccontext,
+ OpenWFE::to_expression_name(m),
+ ProcessDefinition.pack_args(args),
+ &block)
end
#
# This method has to be overriden in order to define
# a process definition.
#
def make
raise "make() implementation is missing, please provide one"
end
- def make_expression (exp_name, params, &block)
+ def self.make_expression (context, exp_name, params, &block)
- exp_name = exp_name.to_s
-
string_child = nil
- attributes = {}
+ attributes = OpenWFE::SymbolHash.new
#puts " ... params.class is #{params.class}"
if params.kind_of? Hash
params.each do |k, v|
#attributes[k.to_s] = v.to_s
- attributes[OpenWFE.to_dash(k.to_s)] = v.to_s
+ attributes[OpenWFE::symbol_to_name(k.to_s)] = v.to_s
end
elsif params
string_child = params.to_s
end
exp = ProgExpRepresentation.new(exp_name, attributes)
exp.children << string_child \
if string_child
- if @parent_expression
- @parent_expression << exp
+ if context.parent_expression
+ #
+ # adding this new expression to its parent
+ #
+ context.parent_expression << exp
else
- @top_expressions << exp
+ #
+ # an orphan, a top expression
+ #
+ context.top_expressions << exp
end
return exp if not block
- @previous_parent_expression.push(@parent_expression) \
- if @parent_expression
+ context.push_parent_expression(exp)
- @parent_expression = exp
-
result = block.call
- #if result and result.kind_of? String and result.length > 0
- # puts " ... child is >#{result}<"
- # exp.children << result
- #end
- #exp.children << result \
- # if result and not result.kind_of? ProgExpRepresentation
+
exp.children << result \
if result and result.kind_of? String
- @parent_expression = @previous_parent_expression.pop
+ context.pop_parent_expression
return exp
end
#
# A class method for actually "making" the process
# segment raw representation
#
def ProcessDefinition.do_make ()
- pdef = self.new
- exp = pdef.make
+ context = nil
- if pdef.top_expressions.size == 1 and \
- exp.name == "process-definition"
-
- return exp
+ if @ccontext
+ context = @ccontext
+ else
+ pdef = self.new
+ pdef.make
+ context = pdef.context
end
- name, revision = extract_name_and_revision(pdef.class.name)
+ exp = context.top_expression
+
+ return exp if exp
+
+ name, revision =
+ extract_name_and_revision(self.metaclass.to_s[8..-2])
+
attributes = {}
attributes["name"] = name
attributes["revision"] = revision
top_expression = ProgExpRepresentation.new(
"process-definition", attributes)
- top_expression.children = pdef.top_expressions
+ top_expression.children = context.top_expressions
- top_expression
+ return top_expression
end
protected
- def pack_args (args)
+ def ProcessDefinition.pack_args (args)
return args[0] if args.length == 1
a = {}
args.each_with_index do |arg, index|
if arg.is_a? Hash
a = a.merge(arg)
@@ -321,10 +336,52 @@
end
def ProcessDefinition.as_revision (s)
s.gsub("_", ".")
end
+
+ class Context
+
+ attr_accessor :parent_expression
+ attr_reader :top_expressions, :previous_parent_expressions
+
+ def initialize
+ @parent_expression = nil
+ @top_expressions = []
+ @previous_parent_expressions = []
+ end
+
+ #
+ # puts the current parent expression on top of the 'previous
+ # parent expressions' stack, the current parent expression
+ # is replaced with the supplied parent expression.
+ #
+ def push_parent_expression (exp)
+ @previous_parent_expressions.push(@parent_expression) \
+ if @parent_expression
+ @parent_expression = exp
+ end
+
+ #
+ # Replaces the current parent expression with the one found
+ # on the top of the previous parent expression stack (pop).
+ #
+ def pop_parent_expression
+ @parent_expression = @previous_parent_expressions.pop
+ end
+
+ #
+ # This method returns the top expression among the
+ # top expressions...
+ #
+ def top_expression
+ return nil if @top_expressions.size > 1
+ exp = @top_expressions[0]
+ return exp if exp.name == "process-definition"
+ return nil
+ end
+ end
end
#
# The actual 'programmatic' raw expression.
# Its raw_representation being an instance of ProgExpRepresentation.
@@ -346,20 +403,44 @@
end
protected
def extract_attributes ()
- return raw_representation.attributes
+ raw_representation.attributes
end
+ def extract_descriptions ()
+
+ result = []
+ raw_representation.children.each do |child|
+
+ next unless child.is_a?(ProgExpRepresentation)
+ next if child.name.intern != :description
+
+ lang = child.attributes[:language]
+ lang = child.attributes[:lang] unless lang
+ lang = "default" unless lang
+
+ result << [ lang, child.children[0] ]
+ end
+ result
+ end
+
def extract_children ()
i = 0
- raw_representation.children.collect do |child|
+ result = []
+ raw_representation.children.each do |child|
if child.kind_of? ProgExpRepresentation
+ cname = child.name.intern
+
+ next if cname == :param
+ next if cname == :parameter
+ next if cname == :description
+
cfei = @fei.dup
cfei.expression_name = child.name
cfei.expression_id = "#{cfei.expression_id}.#{i}"
efei = @environment_id
@@ -369,24 +450,44 @@
get_expression_pool.update(rawexp)
i = i + 1
- rawexp.fei
+ result << rawexp.fei
else
- child
+ result << child
end
end
+ result
end
def extract_text_children ()
raw_representation.children.collect do |child|
next if child.is_a? ProgExpRepresentation
child.to_s
end
end
+
+ def extract_parameters ()
+
+ r = []
+ raw_representation.children.each do |child|
+
+ next unless child.is_a? ProgExpRepresentation
+
+ name = child.name.intern
+ next unless (name == :parameter or name == :param)
+
+ r << Parameter.new(
+ child.attributes[:field],
+ child.attributes[:match],
+ child.attributes[:default],
+ child.attributes[:type])
+ end
+ r
+ end
end
private
#
@@ -404,18 +505,19 @@
#
# Ensures the method name is not conflicting with Ruby keywords
# and turn dashes to underscores.
#
def OpenWFE.make_safe (method_name)
- method_name = to_underscore(method_name)
+ method_name = OpenWFE::to_underscore(method_name)
return "_" + method_name \
if KEYWORDS.include? eval(":"+method_name)
return method_name
end
def OpenWFE.to_expression_name (method_name)
+ method_name = method_name.to_s
method_name = method_name[1..-1] if method_name[0, 1] == "_"
- method_name = to_dash(method_name)
+ method_name = OpenWFE::to_dash(method_name)
return method_name
end
end