Class: OpenWFE::Environment

Inherits:
FlowExpression show all
Includes:
Rufus::Schedulable
Defined in:
lib/openwfe/expressions/environment.rb

Overview

An environment is a store for variables. It’s an expression thus it’s storable in the expression pool.

Constant Summary collapse

V_PAUSED =
VAR_PAUSED[1..-1]

Instance Attribute Summary collapse

Attributes inherited from FlowExpression

#apply_time, #attributes, #children, #environment_id, #fei, #parent_id, #raw_representation

Attributes included from Contextual

#application_context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FlowExpression

#apply, #cancel, #clean_children, #delete_variable, #dup_environment, #fetch_environment, #fetch_text_content, #get_binding, #get_environment, #get_parent, #has_attribute, is_definition, is_definition?, #lookup_attribute, #lookup_attributes, #lookup_boolean_attribute, #lookup_comma_list_attribute, #lookup_downcase_attribute, #lookup_ref, #lookup_string_attribute, #lookup_sym_attribute, #lookup_value, #lookup_variable, #lookup_vf_attribute, names, #new_environment, new_exp, #owns_its_environment?, #paused?, #remove_child, #reply, #reply_to_parent, #set_variable, #store_itself, #synchronize, #to_s, #to_yaml_properties

Methods included from Contextual

#get_work_directory, #init_service, #lookup

Methods included from Logging

#ldebug, #ldebug_callstack, #lerror, #lfatal, #linfo, #llog, #lunknown, #lwarn

Methods included from OwfeServiceLocator

#get_engine, #get_error_journal, #get_expool, #get_expression_map, #get_expression_pool, #get_expression_storage, #get_expression_storages, #get_journal, #get_participant_map, #get_scheduler, #get_wfid_generator

Methods inherited from ObjectWithMeta

#class_def, meta_def, meta_eval, metaclass

Constructor Details

#initializeEnvironment

def initialize (

fei, parent, environment_id, application_context, attributes)
super(fei, parent, environment_id, application_context, attributes)
@variables = {}

end



69
70
71
72
73
74
# File 'lib/openwfe/expressions/environment.rb', line 69

def initialize

    super

    @variables = {}
end

Instance Attribute Details

#variablesObject

the variables stored in this environment.



61
62
63
# File 'lib/openwfe/expressions/environment.rb', line 61

def variables
  @variables
end

Class Method Details

.new_env(fei, parent_id, environment_id, app_context, attributes) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openwfe/expressions/environment.rb', line 76

def self.new_env (
    fei, parent_id, environment_id, app_context, attributes)

    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
end

Instance Method Details

#[](key) ⇒ Object

Looks up for the value of a variable in this environment.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/openwfe/expressions/environment.rb', line 93

def [] (key)

    value = @variables[key]

    return value \
        if @variables.has_key?(key) or is_engine_environment?

    return get_parent[key] if @parent_id

    get_expression_pool.fetch_engine_environment[key]
end

#[]=(key, value) ⇒ Object

Binds a variable in this environment.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openwfe/expressions/environment.rb', line 108

def []= (key, value)

    #ldebug do 
    #    "#{fei.to_debug_s} []= "+
    #    "'#{key}' => '#{value}' (#{value.class.name})"
    #end

    synchronize do

        @variables[key] = value
        store_itself
    end
end

#delete(key) ⇒ Object

Removes a variable from this environment.



125
126
127
128
129
130
131
132
133
# File 'lib/openwfe/expressions/environment.rb', line 125

def delete (key)
    synchronize do

        ldebug { "#{fei.to_debug_s} delete() '#{key}'" }

        @variables.delete key
        store_itself
    end
end

#dupObject

Returns a deep copy of this environment.



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/openwfe/expressions/environment.rb', line 224

def dup

    env = Environment.new_env(
        @fei.dup, 
        @parent_id, 
        @environment_id, 
        @application_context, 
        OpenWFE::fulldup(@attributes))

    env.variables = OpenWFE::fulldup self.variables

    env
end

#get_root_environmentObject

Returns the top environment for the process instance (the environment just before the engine environment).



204
205
206
207
208
209
210
211
# File 'lib/openwfe/expressions/environment.rb', line 204

def get_root_environment

    #ldebug { "get_root_environment\n#{self}" }

    return self unless @parent_id

    get_parent.get_root_environment
end

#is_engine_environment?Boolean

Returns true if this environment is the engine environment

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/openwfe/expressions/environment.rb', line 163

def is_engine_environment?

    (@fei == get_expression_pool.engine_environment_id)
end

#reschedule(scheduler) ⇒ Object

Will reschedule any ‘Schedulable’ variable found in this environment this method especially targets cron expressions that are stored as variables and need to be rescheduled upon engine restart.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/openwfe/expressions/environment.rb', line 182

def reschedule (scheduler)

    @variables.each do |key, value|

        #ldebug { "reschedule()  - item of class #{value.class}" }

        get_expression_pool.paused_instances[@fei.wfid] = true \
            if key == V_PAUSED

        next unless value.kind_of?(Rufus::Schedulable)

        value.application_context = @application_context
        value.reschedule(scheduler)
    end

    store_itself
end

#trigger(params) ⇒ Object

Should never get used, only the reschedule() method is relevant for the Schedulable aspect of an environment expression.



172
173
174
175
# File 'lib/openwfe/expressions/environment.rb', line 172

def trigger (params)

    raise "an environment should never get directly triggered"
end

#unbindObject

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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/openwfe/expressions/environment.rb', line 141

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)

            value.cancel
        end
    end
end