lib/openwfe/expressions/flowexpression.rb in openwferu-0.9.16 vs lib/openwfe/expressions/flowexpression.rb in openwferu-0.9.17
- old
+ new
@@ -1,8 +1,8 @@
#
#--
-# Copyright (c) 2006-2007, John Mettraux, OpenWFE.org
+# Copyright (c) 2006-2008, 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:
#
@@ -109,36 +109,50 @@
# When the FlowExpression instance is applied, this time stamp is set
# to the current date.
#
attr_accessor :apply_time
+ #
+ # Used by raw expressions to store the not yet interpreted branches
+ # of a process, used by other expressions to store their
+ # representation at 'eval time'.
+ #
+ attr_accessor :raw_representation
- def initialize (fei, parent_id, env_id, app_context, attributes)
- super()
+ #
+ # The classical no-params constructors.
+ #
+ def initialize
+
+ super
#
# very necessary as this class includes the MonitorMixin
+ end
- @fei = fei
- @parent_id = parent_id
- @environment_id = env_id
- @application_context = app_context
- @attributes = attributes
+ #
+ # Builds a new instance of an expression
+ #
+ def self.new_exp (fei, parent_id, env_id, app_context, attributes)
- @children = []
+ e = self.new
- @apply_time = nil
+ e.fei = fei
+ e.parent_id = parent_id
+ e.environment_id = env_id
+ e.application_context = app_context
+ e.attributes = attributes
- #ldebug do
- # "initialize()\n"+
- # "self : #{@fei}\n"+
- # "parent : #{@parent_id}"
- #end
+ e.children = []
+ e.apply_time = nil
+
+ e
end
- #
+ #--
# the two most important methods for flow expressions
+ #++
#
# this default implementation immediately replies to the
# parent expression
#
@@ -151,22 +165,22 @@
# this default implementation immediately replies to the
# parent expression
#
def reply (workitem)
- reply_to_parent(workitem)
+ reply_to_parent workitem
end
#
# Triggers the reply to the parent expression (of course, via the
# expression pool).
# Expressions do call this method when their job is done and the flow
# should resume without them.
#
def reply_to_parent (workitem)
- get_expression_pool.reply_to_parent(self, workitem)
+ get_expression_pool.reply_to_parent self, workitem
end
#
# a default implementation for cancel :
# cancels all the children
@@ -180,12 +194,12 @@
@children.each do |child|
next if child.kind_of?(String)
- i = get_expression_pool().cancel(child)
- inflowitem = i unless inflowitem
+ i = get_expression_pool.cancel child
+ inflowitem ||= i
end
inflowitem
end
@@ -212,10 +226,11 @@
#
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
end
#
# Returns the environment instance this expression uses.
@@ -223,17 +238,11 @@
# definition.
# Environments themselves are FlowExpression instances.
#
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
+ fetch_environment || get_expression_pool.fetch_engine_environment
end
#
# A shortcut for fetch_environment.get_root_environment
#
@@ -255,11 +264,11 @@
#
# Just fetches the environment for this expression.
#
def fetch_environment
-
+
get_expression_pool.fetch_expression @environment_id
end
#
# Returns true if the expression's environment was generated
@@ -285,19 +294,19 @@
# "owns_its_environment?()\n"+
# " exp #{ei.to_debug_s}\n"+
# " env #{vi.to_debug_s}"
#end
- ei == vi
+ (ei == vi)
end
#
# Returns true if this expression belongs to a paused flow
#
def paused?
- lookup_variable(VAR_PAUSED) == true
+ (lookup_variable(VAR_PAUSED) == true)
end
#
# Sets a variable in the current environment. Is usually
# called by the 'set' expression.
@@ -325,10 +334,13 @@
# The variable name may be prefixed by / to indicate process level scope
# or by // to indicate engine level (global) scope.
#
def lookup_variable (varname)
+ #puts "lv : #{varname}"
+ #puts OpenWFE.caller_to_s(0, 5)
+
env, var = lookup_environment(varname)
env[var]
end
#
@@ -362,10 +374,12 @@
# variable substitution.
# It's ok to pass a Symbol for the attribute name.
#
def lookup_attribute (attname, workitem, options={})
+ #p attname
+
default = options[:default]
escape = options[:escape]
attname = OpenWFE::symbol_to_name(attname) \
if attname.kind_of?(Symbol)
@@ -435,11 +449,11 @@
def has_attribute (attname)
attname = OpenWFE::symbol_to_name(attname) \
if attname.kind_of?(Symbol)
- @attributes[attname] != nil
+ (@attributes[attname] != nil)
end
#
# Returns a hash of all the FlowExpression attributes with their
# values having undergone dollar variable substitution.
@@ -510,23 +524,31 @@
@environment_id.expression_name = EN_ENVIRONMENT
parent_fei = nil
parent = nil
- parent, _fei = get_expression_pool().fetch(@parent_id) \
+ parent, _fei = get_expression_pool.fetch(@parent_id) \
if @parent_id
parent_fei = parent.environment_id if parent
- env = Environment.new(
+ env = Environment.new_env(
@environment_id, parent_fei, nil, @application_context, nil)
- env.variables.merge!(initial_vars) if initial_vars
+ env.variables.merge! initial_vars if initial_vars
+ env[@fei.wfname] = self.raw_representation \
+ if (not @parent_id) and (self.is_a?(RawExpression))
+ #
+ # keeping track of the raw representation
+ # of the top expression (for top recursion)
+
ldebug { "new_environment() is #{env.fei.to_debug_s}" }
- env.store_itself()
+ env.store_itself
+
+ env
end
#
# This method is called in expressionpool.forget(). It duplicates
# the expression's current environment (deep copy) and attaches
@@ -578,11 +600,11 @@
binding()
end
#
# Used like the classical Ruby synchronize, but as the OpenWFE
- # expression pool manages its own set of monitores, it's one of those
+ # expression pool manages its own set of monitors, it's one of those
# monitors that is used. But the synchronize code looks like the class
# just included the MonitorMixin. No hassle.
#
def synchronize
@@ -601,16 +623,22 @@
def fetch_text_content (workitem, escape=false)
text = ""
children.each do |child|
- if child.kind_of?(RawExpression)
+
+ if child.is_a?(RawExpression)
+
text << child.fei.to_s
- elsif child.kind_of?(FlowExpressionId)
+
+ elsif child.is_a?(FlowExpressionId)
+
text << get_expression_pool\
.fetch_expression(child).raw_representation.to_s
+
else
+
text << child.to_s
end
end
return nil if text == ""
@@ -625,48 +653,61 @@
# looks up for 'value', 'variable-value' and then for 'field-value'
# if necessary.
#
def lookup_value (workitem, options={})
- v = lookup_vf_attribute(workitem, 'value', options)
- v = lookup_vf_attribute(workitem, 'val', options) unless v
- v
+ lookup_vf_attribute(workitem, 'value', options) ||
+ lookup_vf_attribute(workitem, 'val', options)
end
#
# looks up for 'ref', 'variable-ref' and then for 'field-ref'
# if necessary.
#
def lookup_ref (workitem, prefix='')
- ref = lookup_vf_attribute(workitem, 'ref', :prefix => prefix)
+ ref = lookup_vf_attribute workitem, 'ref', :prefix => prefix
return ref.to_s if ref
nil
end
#
# Looks up for value attributes like 'field-ref' or 'variable-value'
#
def lookup_vf_attribute (workitem, att_name, options={})
+ att_name = att_name.to_s
+
prefix = options[:prefix] || ''
- prefix = "#{prefix.to_s}-" if prefix != ''
+ prefix = prefix.to_s
+ dash = (att_name.size > 0 and prefix.size > 0) ? "-" : ""
+
v = lookup_attribute(
- "#{prefix}#{att_name}", workitem, options)
+ "#{prefix}#{dash}#{att_name}", workitem, options)
+ att_name = "-#{att_name}" if att_name.size > 0
+ prefix = "#{prefix}-" if prefix.size > 0
+
return v if v
v = lookup_attribute(
- "#{prefix}variable-#{att_name}", workitem, options)
+ "#{prefix}variable#{att_name}", workitem, options) ||
+ lookup_attribute(
+ "#{prefix}var#{att_name}", workitem, options) ||
+ lookup_attribute(
+ "#{prefix}v#{att_name}", workitem, options)
return lookup_variable(v) if v
- v = lookup_attribute(
- "#{prefix}field-#{att_name}", workitem, options)
+ f = lookup_attribute(
+ "#{prefix}field#{att_name}", workitem, options) ||
+ lookup_attribute(
+ "#{prefix}f#{att_name}", workitem, options)
- return workitem.attributes[v] if v
+ #return workitem.attributes[f] if f
+ return workitem.attributes[f.to_s] if f
nil
end
#
@@ -741,10 +782,10 @@
get_environment.get_root_environment,
varname[1..-1]
]
end
- [ 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)