lib/openwfe/expressions/flowexpression.rb in openwferu-0.9.8 vs lib/openwfe/expressions/flowexpression.rb in openwferu-0.9.9
- old
+ new
@@ -146,56 +146,87 @@
#
# Returns the parent expression (not as a FlowExpressionId but directly
# as the FlowExpression instance it is).
#
- def get_parent ()
- parent, parent_fei = get_expression_pool().fetch(@parent_id)
- parent
+ def get_parent
+ #parent, parent_fei = get_expression_pool.fetch @parent_id
+ #parent
+ get_expression_pool.fetch_expression @parent_id
end
#
# Stores itself in the expression pool.
# It's very important for expressions in persisted context to save
# themselves as soon as their state changed.
# Else this information would be lost at engine restart or
# simply if the expression got swapped out of memory and reloaded later.
#
- def store_itself ()
+ def store_itself
ldebug { "store_itself() for #{@fei.to_debug_s}" }
#ldebug { "store_itself() \n#{OpenWFE::caller_to_s(0, 6)}" }
- get_expression_pool().update(self)
+ get_expression_pool.update self
end
#
# Returns the environment instance this expression uses.
# An environment is a container (a scope) for variables in the process
# definition.
# Environments themselves are FlowExpression instances.
#
- def get_environment ()
- return nil if not @environment_id
- env, fei = get_expression_pool().fetch(@environment_id)
+ def get_environment
+
+ #return nil if not @environment_id
+ #env, fei = get_expression_pool().fetch(@environment_id)
+ #env
+
+ env = fetch_environment
+ env = get_expression_pool.fetch_engine_environment unless env
env
end
#
+ # A shortcut for fetch_environment.get_root_environment
+ #
+ # Returns the environment of the top process (the environement
+ # just before the engine environment in the hierarchy).
+ #
+ def get_root_environment
+ fetch_environment.get_root_environment
+ end
+
+ #
+ # A shortcut for fetch_environment.get_process_environment
+ #
+ #def get_subprocess_environment
+ # fetch_environment.get_subprocess_environment
+ #end
+
+ #
+ # Just fetches the environment for this expression.
+ #
+ def fetch_environment
+ #return nil unless @environment_id
+ get_expression_pool.fetch_expression @environment_id
+ end
+
+ #
# Returns true if the expression's environment was generated
# for itself (usually DefineExpression do have such envs)
#
- def owns_its_environment? ()
+ def owns_its_environment?
#ldebug do
# "owns_its_environment?()\n" +
# " #{@fei.to_debug_s}\n" +
# " #{@environment_id.to_debug_s}"
#end
return false if not @environment_id
- ei = @fei.dup()
- vi = @environment_id.dup()
+ ei = @fei.dup
+ vi = @environment_id.dup
ei.expression_name = "neutral"
vi.expression_name = "neutral"
#ldebug do
@@ -214,13 +245,17 @@
# The variable name may be prefixed by / to indicate process level scope
# or by // to indicate engine level (global) scope.
#
def set_variable (varname, value)
- #get_environment()[varname] = value
-
env, var = lookup_environment(varname)
+
+ #ldebug do
+ # "set_variable() '#{varname}' to '#{value}' " +
+ # "in #{env.fei.to_debug_s}"
+ #end
+
env[var] = value
end
#
# Looks up the value of a variable in the current environment.
@@ -230,12 +265,10 @@
# The variable name may be prefixed by / to indicate process level scope
# or by // to indicate engine level (global) scope.
#
def lookup_variable (varname)
- #get_environment()[varname]
-
env, var = lookup_environment(varname)
env[var]
end
#
@@ -244,12 +277,10 @@
# The variable name may be prefixed by / to indicate process level scope
# or by // to indicate engine level (global) scope.
#
def delete_variable (varname)
- #get_environment().delete(varname)
-
env, var = lookup_environment(varname)
env.delete var
end
#
@@ -284,10 +315,20 @@
OpenWFE::dosub(text, self, workitem)
end
#
+ # Like lookup_attribute() but returns the value downcased.
+ # Returns nil if no such attribute was found.
+ #
+ def lookup_downcase_attribute (attname, workitem, default=nil)
+ result = lookup_attribute attname, workitem, default
+ result = result.downcase if result
+ result
+ end
+
+ #
# Returns true if the expression has the given attribute.
# The attname parameter can be a String or a Symbol.
#
def has_attribute (attname)
@@ -367,11 +408,11 @@
end
#
# creates a new environment just for this expression
#
- def new_environment ()
+ def new_environment (initial_vars=nil)
ldebug { "new_environment() for #{@fei.to_debug_s}" }
@environment_id = @fei.dup
@environment_id.expression_name = EN_ENVIRONMENT
@@ -382,19 +423,36 @@
parent, _fei = 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)
+ env = Environment.new(
+ @environment_id, parent_fei, nil, @application_context, nil)
+
+ env.variables.merge!(initial_vars) if initial_vars
ldebug { "new_environment() is #{env.fei.to_debug_s}" }
return env.store_itself()
end
#
+ # This method is called in expressionpool.forget(). It duplicates
+ # the expression's current environment (deep copy) and attaches
+ # it as the expression own environment.
+ # Returns the duplicated environment.
+ #
+ def dup_environment
+ env = fetch_environment
+ env = env.dup
+ env.fei = @fei.dup
+ env.fei.expression_name = EN_ENVIRONMENT
+ @environment_id = env.fei
+ return env.store_itself
+ end
+
+ #
# Takes care of removing all the children of this expression, if any.
#
def clean_children
return unless @children
@@ -499,45 +557,53 @@
#
def to_s
s = "* #{@fei.to_debug_s}"
- if @parent_id
- s << "\n `--p--> #{@parent_id.to_debug_s}"
- end
+ s << "\n `--p--> #{@parent_id.to_debug_s}" \
+ if @parent_id
- if @environment_id
- s << "\n `--e--> #{@environment_id.to_debug_s}"
- end
+ s << "\n `--e--> #{@environment_id.to_debug_s}" \
+ if @environment_id
- 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}"
+ return s unless @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
- return s
+ s
end
#
# a nice 'names' tag/method for registering the names of the
# Expression classes.
#
def self.names (*exp_names)
+
exp_names = exp_names.collect do |n|
n.to_s
end
meta_def :expression_names do
exp_names
end
end
+ def self.is_definition?
+ false
+ end
+ def self.is_definition
+ meta_def :is_definition? do
+ true
+ end
+ end
+
protected
#
# If the varname starts with '//' will return the engine
# environment and the truncated varname...
@@ -558,10 +624,10 @@
get_environment.get_root_environment,
varname[1..-1]
]
end
- return [ get_environment, varname]
+ [ get_environment, varname]
end
#
# Returns the next sub process id available (this counter
# is stored in the local environment under the key :next_sub_id)