# # Copyright (c) 2006, 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 'ru/rudefinitions' require 'ru/contextual' require 'ru/dollar' require 'ru/logging' module OpenWFEru # # FlowExpression # class FlowExpression include Contextual, Logging attr_accessor \ :fei, \ :parent_id, \ :environment_id, \ :attributes, \ :children, \ :apply_time def initialize (fei, parent_id, env_id, app_context, attributes) @fei = fei @parent_id = parent_id @environment_id = env_id @application_context = app_context @attributes = attributes @apply_time = nil #ldebug do # "initialize()\n"+ # "self : #{@fei}\n"+ # "parent : #{@parent_id}" #end end # # the two most important methods for flow expressions def apply (workitem) get_parent().reply(workitem) if @parent_id end def reply (workitem) # # a default implementation # reply_to_parent(workitem) end def reply_to_parent (workitem) get_expression_pool.reply_to_parent(self, workitem) end # # some convenience methods def get_parent () get_expression_pool().fetch(@parent_id) end def get_expression_pool () @application_context[S_EXPRESSION_POOL] end def get_expression_map () @application_context[S_EXPRESSION_MAP] end def store_itself () get_expression_pool().update(self) end def get_environment () return nil if not @environment_id return get_expression_pool().fetch(@environment_id) end # # Returns true if the expression's environment was generated # for itself (usually DefineExpression do have such envs) # def owns_its_environment? () #ldebug { "owns_its_environment?() #{@fei.to_debug_s}" } return false if not @environment_id ei = @fei.dup() vi = @environment_id.dup() ei.expressionName = "neutral" vi.expressionName = "neutral" #ldebug do # "owns_its_environment?()\n"+ # " exp #{ei.to_debug_s}\n"+ # " env #{vi.to_debug_s}" #end return ei == vi end def set_variable (varname, value) get_environment()[varname] = value end def lookup_variable (varname) return get_environment()[varname] end def delete_variable (varname) get_environment().delete(varname) end def lookup_attribute (attname, workitem) text = @attributes[attname] return nil if not text return OpenWFEru::dosub(text, self, workitem) end def lookup_boolean_attribute (attname, workitem, default=false) value = lookup_attribute(attname, workitem) return default if not value return value.downcase == 'true' end def lookup_attributes (attributes, workitem) result = {} attributes.each do |k, v| result[k] = OpenWFEru::dosub(v, self, workitem) end return result end def list_attributes (workitem) result = {} @attributes.each do |k, v| result[k] = OpenWFEru::dosub(v, self, workitem) ldebug { "list_attributes() added '#{k}' -> '#{result[k]}'" } end return result end # # creates a new environment just for this expression # def new_environment () ldebug { "new_environment() for #{@fei.to_debug_s}" } @environment_id = @fei.dup @environment_id.expressionName = EN_ENVIRONMENT parent_fei = nil parent = nil parent = get_expression_pool().fetch(@parent_id) if @parent_id parent_fei = parent.environment_id if parent env = Environment\ .new(@environment_id, parent_fei, nil, @application_context, nil) ldebug { "new_environment() is #{env.fei.to_debug_s}" } env.store_itself() end # # takes care of removing all the children # def clean_children @children.each do |children_fei| get_expression_pool.remove(children_fei) end end # # some eye candy # def to_s s = "* #{@fei.to_debug_s}" if @parent_id s << "\n `--p--> #{@parent_id.to_debug_s}" end if @environment_id s << "\n `--e--> #{@environment_id.to_debug_s}" end if @children @children.each do |c| sc = if c.kind_of?(OpenWFE::FlowExpressionId) c.to_debug_s else ">#{c.to_s}<" end s << "\n `--c--> #{sc}" end end return s end end end