lib/openwfe/expressions/environment.rb in ruote-0.9.19 vs lib/openwfe/expressions/environment.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 'rufus/scheduler' require 'openwfe/utils' require 'openwfe/expressions/flowexpression' @@ -58,96 +44,77 @@ # # the variables stored in this environment. # attr_accessor :variables - #def initialize ( - # fei, parent, environment_id, application_context, attributes) - # super(fei, parent, environment_id, application_context, attributes) - # @variables = {} - #end - def initialize super - @variables = {} end def self.new_env ( - fei, parent_id, environment_id, app_context, attributes) + fei, parent_id, environment_id, app_context, attributes, variables=nil) env = self.new env.fei = fei env.parent_id = parent_id env.environment_id = environment_id env.application_context = app_context env.attributes = attributes + env.variables = variables if variables env end # # Looks up for the value of a variable in this environment. # def [] (key) - value = @variables[key] - - return value \ + return @variables[key] \ if @variables.has_key?(key) or is_engine_environment? - return get_parent[key] if @parent_id + # else look in parent environment + return get_parent[key] \ + if @parent_id + + # finally look in engine (global) environment + get_expression_pool.fetch_engine_environment[key] end # # Binds a variable in this environment. # def []= (key, value) - #ldebug do - # "#{fei.to_debug_s} []= "+ - # "'#{key}' => '#{value}' (#{value.class.name})" - #end - - #synchronize do - @variables[key] = value store_itself - #end end # # Removes a variable from this environment. # def delete (key) - #synchronize do - ldebug { "#{fei.to_debug_s} delete() '#{key}'" } - - @variables.delete key + @variables.delete(key) store_itself - #end end # # This method is usually called before the environment gets wiped # out of the expression pool. # It takes care of removing subprocess templates pointed at by # variables in this environment. # def unbind - #ldebug { "unbind() for #{fei.to_s}" } - @variables.each do |key, value| - #ldebug { "unbind() '#{key}' => #{value.class}" } - if value.kind_of?(FlowExpressionId) get_expression_pool.remove(value) #elsif value.kind_of?(FlowExpression) @@ -207,39 +174,58 @@ # Returns the top environment for the process instance (the # environment just before the engine environment). # def get_root_environment - #ldebug { "get_root_environment\n#{self}" } + @parent_id ? get_parent.get_root_environment : self + end - return self unless @parent_id + # + # A shortcut to get_expression_pool.fetch_engine_environment + # + def get_engine_environment - get_parent.get_root_environment + get_expression_pool.fetch_engine_environment end - #-- - #def get_subprocess_environment - # return self if not @parent_id - # return self if @parent_id.sub_instance_id != @fei.sub_instance_id - # get_parent.get_subprocess_environment - #end - #++ + # + # Fetches in an array (stack) all the values for a given variable from + # this environment up to the engine environement (parent chain). + # + def lookup_variable_stack (varname, stack=[]) + val = self[varname] + stack << [ self, val ] unless val.nil? + + return stack if is_engine_environment? + return get_parent.lookup_variable_stack(varname, stack) if @parent_id + + get_engine_environment.lookup_variable_stack(varname, stack) + end + # + # Returns a brand new hash containing the variables as seen in the + # calling environment (doesn't include variables set at the engine level). + # + def lookup_all_variables + + return @variables unless @parent_id + + get_parent.lookup_all_variables.merge(@variables) + end + + # # Returns a deep copy of this environment. # def dup - env = Environment.new_env( + Environment.new_env( @fei.dup, @parent_id, @environment_id, @application_context, - OpenWFE::fulldup(@attributes)) - - env.variables = OpenWFE::fulldup self.variables - - env + OpenWFE::fulldup(@attributes), + OpenWFE::fulldup(self.variables)) end end end