# #-- # Copyright (c) 2006-2007, John Mettraux, OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # . Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # . 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. # # . 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. #++ # # $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $ # # # "made in Japan" # # John Mettraux at openwfe.org # require 'openwfe/workitem' require 'openwfe/flowexpressionid' require 'openwfe/expressions/flowexpression' require 'openwfe/expressions/fe_utils' # # expressions like 'set' and 'unset' and their utility methods # module OpenWFE class SetValueExpression < FlowExpression def apply (workitem) if @children.length < 1 workitem.attributes[FIELD_RESULT] = \ OpenWFE::lookup_value(self, workitem) reply(workitem) return end child = @children[0] if child.kind_of? OpenWFE::FlowExpressionId handle_child(child, workitem) return end #workitem.attributes[FIELD_RESULT] = child.to_s workitem.attributes[FIELD_RESULT] = \ OpenWFE::fetch_text_content(self, workitem) reply(workitem) end def reply (workitem) vkey = lookup_attribute("variable", workitem) fkey = lookup_attribute("field", workitem) value = workitem.attributes[FIELD_RESULT] #puts "value is '#{value}'" if vkey set_variable(vkey, value) elsif fkey workitem.attributes[fkey] = value else raise "'variable' or 'field' attribute missing from 'set' expression" end reply_to_parent(workitem) end protected def handle_child (child, workitem) child, _fei = get_expression_pool().fetch(child) if child.is_definition? fei = get_expression_pool().evaluate(child, workitem) workitem.attributes[FIELD_RESULT] = fei reply(workitem) else get_expression_pool().apply(child, workitem) end end #def determine_value (workitem) # if @children.length > 0 # child = @children[0] # if child.kind_of? FlowExpressionId # return "nada" # else # return child # end # end # return OpenWFE::lookup_value(self, workitem) #end end class UnsetValueExpression < FlowExpression def apply (workitem) vkey = lookup_attribute("variable", workitem) fkey = lookup_attribute("field", workitem) if vkey delete_variable(vkey) elsif fkey workitem.attributes.delete(fkey) else raise "attribute 'variable' or 'field' is missing for 'unset' expression" end reply_to_parent(workitem) end #def reply (workitem) #end end class ComparisonExpression < FlowExpression def apply (workitem) value_a = OpenWFE::lookup_value(self, workitem) value_b = OpenWFE::lookup_value(self, workitem, 'other') result = compare(value_a, value_b) ldebug { "apply() result is '#{result}' #{@fei.to_debug_s}" } OpenWFE::set_result(workitem, result) reply_to_parent(workitem) end protected def compare (a, b) raise "not yet implemented : '#{@fei.expressionName}'" end end # # # class EqualsExpression < ComparisonExpression protected def compare (a, b) #ldebug { "compare() #{fei.to_debug_s}" } #ldebug { "compare() '#{a}' == '#{b}'" } return a == b end end # # # class IfExpression < FlowExpression attr_accessor \ :condition_replied def apply (workitem) reply_to_parent(workitem) if @children.length < 1 @condition_replied = false store_itself() get_expression_pool.apply(@children[0], workitem) end def reply (workitem) if @condition_replied reply_to_parent(workitem) return end result = workitem.attributes[FIELD_RESULT] @condition_replied = true store_itself() if result apply_consequence(1, workitem) else apply_consequence(2, workitem) end end def reply_to_parent(workitem) clean_children() super(workitem) end protected def apply_consequence (index, workitem) if index >= @children.length reply_to_parent(workitem) else get_expression_pool.apply(@children[index], workitem) end end end end