lib/openwfe/expressions/fe_misc.rb in ruote-0.9.19 vs lib/openwfe/expressions/fe_misc.rb in ruote-0.9.20

- old
+ new

@@ -1,43 +1,29 @@ -# #-- -# Copyright (c) 2006-2008, John Mettraux, OpenWFE.org -# All rights reserved. +# Copyright (c) 2006-2009, John Mettraux, jmettraux@gmail.com # -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: +# 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: # -# . Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. # -# . Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. +# 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. # -# . Neither the name of the "OpenWFE" nor the names of its contributors may be -# used to endorse or promote products derived from this software without -# specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. +# Made in Japan. #++ -# -# -# "made in Japan" -# -# John Mettraux at openwfe.org -# require 'openwfe/expressions/flowexpression' require 'openwfe/expressions/value' require 'openwfe/util/treechecker' @@ -81,11 +67,11 @@ set_variable(vname, text) set_variable(DESC, text) unless lookup_variable(DESC) # set default if not set - reply_to_parent workitem + reply_to_parent(workitem) end end # # A debug/test expression (it's mostly used in the test suite @@ -109,11 +95,11 @@ # OpenWFEru test suite uses this expression). # class PrintExpression < FlowExpression include ValueMixin - names :print + names :print, :echo def reply (workitem) text = workitem.get_result.to_s text << "\n" @@ -124,11 +110,11 @@ tracer << text else puts text end - reply_to_parent workitem + reply_to_parent(workitem) end end # # Evals some Ruby code contained within the process definition @@ -160,19 +146,34 @@ # """ # end # end # # Don't embed too much Ruby into your process definitions, it might - # hurt... + # hurt... It's probably better to embed some ruby code in a BlockParticipant, + # like in # + # engine.register_participant :compute_total do |workitem| + # sum = workitem.items.inject(0) do |sum, item| + # sum += item['count'] * item['price'] + # end + # end + # + # 2 advantages : not too much ruby code in the process definition, and the + # participant can be reused for another process. + # # Reval can also be used with the 'code' attribute (or 'field-code' or # 'variable-code') : # # <reval field-code="f0" /> # # to eval the Ruby code held in the field named "f0". # + # Note that currently, the actual evaluation of the ruby code is done in + # the work thread, so while this ruby code is executing, there is no + # chance for other process instances to progress. Using a block participant + # (like explained a few paragraphs up here) avoids this problem altogether. + # class RevalExpression < FlowExpression include ValueMixin names :reval @@ -186,18 +187,18 @@ code = code.to_s wi = workitem # so that the ruby code being evaluated sees 'wi' and 'workitem' - get_tree_checker.check code + get_tree_checker.check(code) - result = eval code, binding() + result = eval(code, binding()) workitem.set_result(result) \ if result != nil # as 'false' is a valid result - reply_to_parent workitem + reply_to_parent(workitem) end end # # An advanced expression : it takes the value in a field or variable (or @@ -222,45 +223,25 @@ class EvalExpression < FlowExpression include ValueMixin names :eval - def reply (workitem) - raise "dynamic evaluation of process definitions is not allowed" \ + raise 'dynamic evaluation of process definitions is not allowed' \ if @application_context[:dynamic_eval_allowed] != true df = lookup_vf_attribute(workitem, 'def') || workitem.get_result return reply_to_parent(workitem) unless df # # currently, 'nothing to eval' means, 'just go on' - ldebug { "apply() def is >#{df}<" } + tree = get_def_parser.determine_rep(df) - raw_expression = build_raw_expression df - - #puts - #puts "======================================" - #puts raw_expression.to_s - #puts raw_expression.raw_representation - #puts "======================================" - #puts - - raw_expression.apply workitem + get_expression_pool.substitute_and_apply(self, tree, workitem) end - - protected - - def build_raw_expression (df) - - procdf = get_expression_pool.determine_rep df - - RawExpression.new_raw( - fei, parent_id, environment_id, application_context, procdf) - end end # # Some kind of limited 'eval' expression. # @@ -296,77 +277,57 @@ # class ExpExpression < RawExpression names :exp - #-- - #def initialize (fei, parent_id, env_id, app_context, att) - # # - # # this responds to the FlowExpression constructor... - # super fei, parent_id, env_id, app_context, nil - # # - # # but this triggers the RawExpression constructor :) - # @attributes = att - # # - # # as this is not done by the RawExpression constructor - #end - #++ - def apply (workitem) @applied_workitem = workitem super end protected - # - # Evaluates the 'name' attribute, if it's not present or empty, - # will return the value for the 'default' attribute. - # - def expression_name + # + # Evaluates the 'name' attribute, if it's not present or empty, + # will return the value for the 'default' attribute. + # + def expression_name - n = lookup_attribute :name, @applied_workitem + n = lookup_attribute(:name, @applied_workitem) - return lookup_attribute(:default, @applied_workitem) \ - if (not n) or (n.strip == '') + return lookup_attribute(:default, @applied_workitem) \ + if (not n) or (n.strip == '') + n + end - n - end + # + # If the 'attributes' attribute is present, will return its + # value. Else, will simply return the attributes of the 'exp' + # expression itself ('name' and 'default' included). + # + def extract_attributes - # - # If the 'attributes' attribute is present, will return its - # value. Else, will simply return the attributes of the 'exp' - # expression itself ('name' and 'default' included). - # - def extract_attributes + att = lookup_vf_attribute(@applied_workitem, :attributes) + # will currently only work with an attribute hash + # whose keys are strings... symbols :( - att = lookup_vf_attribute @applied_workitem, :attributes - # will currently only work with an attribute hash - # whose keys are strings... symbols :( + att || @attributes + end - att || @attributes - end + def extract_children + @children + end - #-- - #def extract_descriptions - # [] - #end - #++ - - def extract_children - @children - end - - def extract_parameters - [] - end + def extract_parameters + [] + end end # # This expression simply emits a message to the application - # log (by default logs/openwferu.log). + # log (by default logs/ruote.log). # # <sequence> # <log>before participant alpha</log> # <participant ref="alpha" /> # <log>after participant alpha</log>