lib/openwfe/expressions/raw.rb in ruote-0.9.18 vs lib/openwfe/expressions/raw.rb in ruote-0.9.19

- old
+ new

@@ -42,506 +42,505 @@ require 'openwfe/rudefinitions' module OpenWFE + # + # A class storing bits (trees) of process definitions just + # parsed. Upon application (apply()) these raw expressions get turned + # into real expressions. + # + class RawExpression < FlowExpression + # - # A class storing bits (trees) of process definitions just - # parsed. Upon application (apply()) these raw expressions get turned - # into real expressions. + # A [static] method for creating new RawExpression instances. # - class RawExpression < FlowExpression + def self.new_raw ( + fei, parent_id, env_id, app_context, raw_representation) - # - # A [static] method for creating new RawExpression instances. - # - def self.new_raw ( - fei, parent_id, env_id, app_context, raw_representation) + re = self.new - re = self.new + re.fei = fei + re.parent_id = parent_id + re.environment_id = env_id + re.application_context = app_context + re.attributes = nil + re.children = [] + re.apply_time = nil - re.fei = fei - re.parent_id = parent_id - re.environment_id = env_id - re.application_context = app_context - re.attributes = nil - re.children = [] - re.apply_time = nil + re.raw_representation = raw_representation + re + end - re.raw_representation = raw_representation - re - end + #-- + # A duplication method that duplicates everything, except + # the application context + # + #def dup + # self.class.new_raw( + # @fei.dup, + # @parent_id ? @parent_id.dup : nil, + # @environment_id ? @environment_id.dup : nil, + # @application_context, + # raw_representation) + #end + #alias :fulldup :dup + #++ - #-- - # A duplication method that duplicates everything, except - # the application context - # - #def dup - # self.class.new_raw( - # @fei.dup, - # @parent_id ? @parent_id.dup : nil, - # @environment_id ? @environment_id.dup : nil, - # @application_context, - # raw_representation) - #end - #alias :fulldup :dup - #++ + def instantiate_real_expression ( + workitem, exp_name=nil, exp_class=nil, attributes=nil) - def instantiate_real_expression ( - workitem, exp_name=nil, exp_class=nil, attributes=nil) + exp_name ||= expression_name + exp_class ||= expression_class - exp_name ||= expression_name - exp_class ||= expression_class + raise "unknown expression '#{exp_name}'" \ + unless exp_class - raise "unknown expression '#{exp_name}'" \ - unless exp_class + #ldebug do + # "instantiate_real_expression() exp_class is #{exp_class}" + #end - #ldebug do - # "instantiate_real_expression() exp_class is #{exp_class}" - #end + attributes ||= raw_representation[1] - attributes ||= raw_representation[1] + exp = exp_class.new + exp.fei = @fei + exp.parent_id = @parent_id + exp.environment_id = @environment_id + exp.application_context = @application_context + exp.attributes = attributes - exp = exp_class.new - exp.fei = @fei - exp.parent_id = @parent_id - exp.environment_id = @environment_id - exp.application_context = @application_context - exp.attributes = attributes + exp.raw_representation = raw_representation + exp.raw_rep_updated = raw_rep_updated + # + # keeping track of how the expression look at apply / + # instantiation time - exp.raw_representation = raw_representation - exp.raw_rep_updated = raw_rep_updated - # - # keeping track of how the expression look at apply / - # instantiation time + consider_tag workitem, exp - consider_tag workitem, exp + exp.children = extract_children \ + unless exp_class.uses_template? - exp.children = extract_children \ - unless exp_class.uses_template? + exp + end - exp - end + # + # When a raw expression is applied, it gets turned into the + # real expression which then gets applied. + # + def apply (workitem) - # - # When a raw expression is applied, it gets turned into the - # real expression which then gets applied. - # - def apply (workitem) + exp_name, exp_class, attributes = determine_real_expression - exp_name, exp_class, attributes = determine_real_expression + expression = instantiate_real_expression( + workitem, exp_name, exp_class, attributes) - expression = instantiate_real_expression( - workitem, exp_name, exp_class, attributes) + expression.apply_time = Time.now + expression.store_itself - expression.apply_time = Time.now - expression.store_itself + expression.apply workitem + end - expression.apply workitem - end + # + # This method is called by the expression pool when it is about + # to launch a process, it will interpret the 'parameter' statements + # in the process definition and raise an exception if the requirements + # are not met. + # + def check_parameters (workitem) - # - # This method is called by the expression pool when it is about - # to launch a process, it will interpret the 'parameter' statements - # in the process definition and raise an exception if the requirements - # are not met. - # - def check_parameters (workitem) + extract_parameters.each do |param| + param.check(workitem) + end + end - extract_parameters.each do |param| - param.check(workitem) - end - end + #-- + #def reply (workitem) + # no implementation necessary + #end + #++ - #-- - #def reply (workitem) - # no implementation necessary - #end - #++ + def is_definition? - def is_definition? + get_expression_map.is_definition?(expression_name()) + end - get_expression_map.is_definition?(expression_name()) - end + def expression_class - def expression_class + get_expression_map.get_class(expression_name()) + end - get_expression_map.get_class(expression_name()) - end + def definition_name - def definition_name + (raw_representation[1]['name'] || raw_children.first).to_s + end - (raw_representation[1]['name'] || raw_children.first).to_s - end + def expression_name - def expression_name + raw_representation.first + end - raw_representation.first - end + # + # Forces the raw expression to load the attributes and set them + # in its @attributes instance variable. + # Currently only used by FilterDefinitionExpression. + # + def load_attributes - # - # Forces the raw expression to load the attributes and set them - # in its @attributes instance variable. - # Currently only used by FilterDefinitionExpression. - # - def load_attributes + @attributes = raw_representation[1] + end - @attributes = raw_representation[1] - end + # + # This method has been made public in order to have quick look + # at the attributes of an expression before it's really + # 'instantiated'. + # + def extract_attributes - # - # This method has been made public in order to have quick look - # at the attributes of an expression before it's really - # 'instantiated'. - # - def extract_attributes + raw_representation[1] + end - raw_representation[1] - end + protected - protected + # + # looks up a participant in the participant map, considers + # "my-participant" and "my_participant" as the same + # (by doing two lookups). + # + def lookup_participant (name) - # - # looks up a participant in the participant map, considers - # "my-participant" and "my_participant" as the same - # (by doing two lookups). - # - def lookup_participant (name) + p = get_participant_map.lookup_participant(name) - p = get_participant_map.lookup_participant(name) + unless p + name = OpenWFE::to_underscore(name) + p = get_participant_map.lookup_participant(name) + end - unless p - name = OpenWFE::to_underscore(name) - p = get_participant_map.lookup_participant(name) - end + return name if p - return name if p + nil + end - nil - end + # + # Determines if this raw expression points to a classical + # expression, a participant or a subprocess, or nothing at all... + # + def determine_real_expression - # - # Determines if this raw expression points to a classical - # expression, a participant or a subprocess, or nothing at all... - # - def determine_real_expression + exp_name = expression_name() + exp_class = expression_class() + var_value = lookup_variable exp_name + attributes = extract_attributes - exp_name = expression_name() - exp_class = expression_class() - var_value = lookup_variable exp_name - attributes = extract_attributes + unless var_value + # + # accomodating "sub_process_name" and "sub-process-name" + # + alt = OpenWFE::to_underscore exp_name + var_value = lookup_variable(alt) if alt != exp_name - unless var_value - # - # accomodating "sub_process_name" and "sub-process-name" - # - alt = OpenWFE::to_underscore exp_name - var_value = lookup_variable(alt) if alt != exp_name + exp_name = alt if var_value + end - exp_name = alt if var_value - end + var_value = exp_name \ + if (not exp_class and not var_value) - var_value = exp_name \ - if (not exp_class and not var_value) + if var_value.is_a?(String) - if var_value.is_a?(String) + participant_name = lookup_participant var_value - participant_name = lookup_participant var_value + if participant_name + exp_name = participant_name + exp_class = ParticipantExpression + attributes['ref'] = participant_name + end - if participant_name - exp_name = participant_name - exp_class = ParticipantExpression - attributes['ref'] = participant_name - end + elsif var_value.is_a?(FlowExpressionId) \ + or var_value.is_a?(RawExpression) - elsif var_value.is_a?(FlowExpressionId) \ - or var_value.is_a?(RawExpression) + exp_class = SubProcessRefExpression + attributes['ref'] = exp_name + end + # else, it's a standard expression - exp_class = SubProcessRefExpression - attributes['ref'] = exp_name - end - # else, it's a standard expression + [ exp_name, exp_class, attributes ] + end - [ exp_name, exp_class, attributes ] - end + def extract_children - def extract_children + i = 0 + result = [] + raw_representation.last.each do |child| - i = 0 - result = [] - raw_representation.last.each do |child| + if is_not_a_node?(child) - if is_not_a_node?(child) + result << child + else - result << child - else + cname = child.first.intern - cname = child.first.intern + next if cname == :param + next if cname == :parameter + #next if cname == :description - next if cname == :param - next if cname == :parameter - #next if cname == :description + cfei = @fei.dup + cfei.expression_name = child.first + cfei.expression_id = "#{cfei.expression_id}.#{i}" - cfei = @fei.dup - cfei.expression_name = child.first - cfei.expression_id = "#{cfei.expression_id}.#{i}" + efei = @environment_id - efei = @environment_id + rawexp = RawExpression.new_raw( + cfei, @fei, efei, @application_context, child) - rawexp = RawExpression.new_raw( - cfei, @fei, efei, @application_context, child) + get_expression_pool.update rawexp - get_expression_pool.update rawexp + i = i + 1 - i = i + 1 + result << rawexp.fei + end + end + result + end - result << rawexp.fei - end - end - result - end + def extract_parameters - def extract_parameters + r = [] + raw_representation.last.each do |child| - r = [] - raw_representation.last.each do |child| + #next unless child.is_a?(SimpleExpRepresentation) + #next unless child.is_a?(Array) + next if is_not_a_node?(child) - #next unless child.is_a?(SimpleExpRepresentation) - #next unless child.is_a?(Array) - next if is_not_a_node?(child) + name = child.first.to_sym + next unless (name == :parameter or name == :param) - name = child.first.to_sym - next unless (name == :parameter or name == :param) + attributes = child[1] - attributes = child[1] + r << Parameter.new( + attributes['field'], + attributes['match'], + attributes['default'], + attributes['type']) + end + r + end - r << Parameter.new( - attributes['field'], - attributes['match'], - attributes['default'], - attributes['type']) - end - r - end + def is_not_a_node? (child) - def is_not_a_node? (child) + (( ! child.is_a?(Array)) || + child.size != 3 || + ( ! child.first.is_a?(String))) + end - (( ! child.is_a?(Array)) || - child.size != 3 || - ( ! child.first.is_a?(String))) - end + # + # Expressions can get tagged. Tagged expressions can easily + # be cancelled (undone) or redone. + # + def consider_tag (workitem, new_expression) - # - # Expressions can get tagged. Tagged expressions can easily - # be cancelled (undone) or redone. - # - def consider_tag (workitem, new_expression) + tagname = new_expression.lookup_string_attribute :tag, workitem - tagname = new_expression.lookup_string_attribute :tag, workitem + return unless tagname - return unless tagname + ldebug { "consider_tag() tag is '#{tagname}'" } - ldebug { "consider_tag() tag is '#{tagname}'" } + set_variable tagname, Tag.new(self, workitem) + # + # keep copy of raw expression and workitem as applied - set_variable tagname, Tag.new(self, workitem) - # - # keep copy of raw expression and workitem as applied + new_expression.attributes["tag"] = tagname + # + # making sure that the value of tag doesn't change anymore + end - new_expression.attributes["tag"] = tagname - # - # making sure that the value of tag doesn't change anymore - end + # + # A small class wrapping a tag (a raw expression and the workitem + # it received at apply time. + # + class Tag - # - # A small class wrapping a tag (a raw expression and the workitem - # it received at apply time. - # - class Tag + attr_reader \ + :raw_expression, + :workitem - attr_reader \ - :raw_expression, - :workitem + def flow_expression_id + @raw_expression.fei + end + alias :fei :flow_expression_id - def flow_expression_id - @raw_expression.fei - end - alias :fei :flow_expression_id + def initialize (raw_expression, workitem) - def initialize (raw_expression, workitem) + @raw_expression = raw_expression.dup + @workitem = workitem.dup + end + end - @raw_expression = raw_expression.dup - @workitem = workitem.dup - end - end + # + # Encapsulating + # <parameter field="x" default="y" type="z" match="m" /> + # + # Somehow I have that : OpenWFEru is not a strongly typed language + # ... Anyway I implemented that to please Pat. + # + class Parameter - # - # Encapsulating - # <parameter field="x" default="y" type="z" match="m" /> - # - # Somehow I have that : OpenWFEru is not a strongly typed language - # ... Anyway I implemented that to please Pat. - # - class Parameter + def initialize (field, match, default, type) - def initialize (field, match, default, type) + @field = to_s field + @match = to_s match + @default = to_s default + @type = to_s type + end - @field = to_s field - @match = to_s match - @default = to_s default - @type = to_s type - end + # + # Will raise an exception if this param requirement is not + # met by the workitem. + # + def check (workitem) - # - # Will raise an exception if this param requirement is not - # met by the workitem. - # - def check (workitem) + unless @field + raise \ + OpenWFE::ParameterException, + "'parameter'/'param' without a 'field' attribute" + end - unless @field - raise \ - OpenWFE::ParameterException, - "'parameter'/'param' without a 'field' attribute" - end + field_value = workitem.attributes[@field] + field_value = @default unless field_value - field_value = workitem.attributes[@field] - field_value = @default unless field_value + unless field_value + raise \ + OpenWFE::ParameterException, + "field '#{@field}' is missing" \ + end - unless field_value - raise \ - OpenWFE::ParameterException, - "field '#{@field}' is missing" \ - end + check_match(field_value) - check_match(field_value) + enforce_type(workitem, field_value) + end - enforce_type(workitem, field_value) - end + protected - protected + # + # Used in the constructor to flatten everything to strings. + # + def to_s (o) + return nil unless o + o.to_s + end - # - # Used in the constructor to flatten everything to strings. - # - def to_s (o) - return nil unless o - o.to_s - end + # + # Will raise an exception if it cannot coerce the type + # of the value to the one desired. + # + def enforce_type (workitem, value) - # - # Will raise an exception if it cannot coerce the type - # of the value to the one desired. - # - def enforce_type (workitem, value) + value = if not @type + value + elsif @type == "string" + value.to_s + elsif @type == "int" or @type == "integer" + Integer(value) + elsif @type == "float" + Float(value) + else + raise + "unknown type '#{@type}' for field '#{@field}'" + end - value = if not @type - value - elsif @type == "string" - value.to_s - elsif @type == "int" or @type == "integer" - Integer(value) - elsif @type == "float" - Float(value) - else - raise - "unknown type '#{@type}' for field '#{@field}'" - end + workitem.attributes[@field] = value + end - workitem.attributes[@field] = value - end + def check_match (value) - def check_match (value) + return unless @match - return unless @match - - unless value.to_s.match(@match) - raise \ - OpenWFE::ParameterException, - "value of field '#{@field}' doesn't match" - end - end + unless value.to_s.match(@match) + raise \ + OpenWFE::ParameterException, + "value of field '#{@field}' doesn't match" end - end + end + end + end - # - # This class is only present to ensure that OpenWFEru 0.9.17 can read - # previous (<= 0.9.16) expools. - # - class ProgRawExpression < RawExpression + # + # This class is only present to ensure that OpenWFEru 0.9.17 can read + # previous (<= 0.9.16) expools. + # + class ProgRawExpression < RawExpression - def raw_representation + def raw_representation - @raw_representation.to_a - end + @raw_representation.to_a end + end - # - # This class is only present to ensure that OpenWFEru 0.9.17 can read - # previous (<= 0.9.16) expools. - # - class XmlRawExpression < RawExpression + # + # This class is only present to ensure that OpenWFEru 0.9.17 can read + # previous (<= 0.9.16) expools. + # + class XmlRawExpression < RawExpression - def raw_representation + def raw_representation - #SimpleExpRepresentation.from_xml @raw_representation_s - DefParser.parse_xml @raw_representation_s - end + get_def_parser.parse @raw_representation_s end + end - # - # This class is only present to ensure that OpenWFEru 0.9.17 can read - # previous (<= 0.9.16) expools. - # - class SimpleExpRepresentation + # + # This class is only present to ensure that OpenWFEru 0.9.17 can read + # previous (<= 0.9.16) expools. + # + class SimpleExpRepresentation - def to_a + def to_a - children = @children.collect do |c| - if c.is_a?(SimpleExpRepresentation) - c.to_a - else - c - end - end - - a = [ @name, @attributes, children ] + children = @children.collect do |c| + if c.is_a?(SimpleExpRepresentation) + c.to_a + else + c end + end + + a = [ @name, @attributes, children ] end + end - private + private - # - # OpenWFE process definitions do use some - # Ruby keywords... The workaround is to put an underscore - # just before the name to 'escape' it. - # - # 'undo' isn't reserved by Ruby, but lets keep it in line - # with 'do' and 'redo' that are. - # - KEYWORDS = [ - :if, :do, :redo, :undo, :print, :sleep, :loop, :break, :when - #:until, :while - ] + # + # OpenWFE process definitions do use some + # Ruby keywords... The workaround is to put an underscore + # just before the name to 'escape' it. + # + # 'undo' isn't reserved by Ruby, but lets keep it in line + # with 'do' and 'redo' that are. + # + KEYWORDS = [ + :if, :do, :redo, :undo, :print, :sleep, :loop, :break, :when + #:until, :while + ] - # - # Ensures the method name is not conflicting with Ruby keywords - # and turn dashes to underscores. - # - def OpenWFE.make_safe (method_name) + # + # Ensures the method name is not conflicting with Ruby keywords + # and turn dashes to underscores. + # + def OpenWFE.make_safe (method_name) - method_name = OpenWFE::to_underscore(method_name) + method_name = OpenWFE::to_underscore(method_name) - return "_" + method_name \ - if KEYWORDS.include? eval(":"+method_name) + return "_" + method_name \ + if KEYWORDS.include? eval(":"+method_name) - method_name - end + method_name + end - def OpenWFE.to_expression_name (method_name) + 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 = OpenWFE::to_dash(method_name) - method_name - end + method_name = method_name.to_s + method_name = method_name[1..-1] if method_name[0, 1] == "_" + method_name = OpenWFE::to_dash(method_name) + method_name + end end