Context¶

Overview¶

The Context class is used to pass information about execution and state. It is generally created at runtime, and updated under various operating conditions. Its primary attribute is flags - a binary vector, the individual flags of which are specified using the ContextFlags enum. The flags attribute is divided functionally into the two fields:

Each field can be addressed using the corresponding property of the class; only one source flag may be set, but in some cases multiple execution_phase flags may be set (although see individual property documentation for exceptions).

Context and Logging¶

The flags attribute is used by Log to identify conditions for logging (see). Accordingly, the LogCondition(s) used to specify such conditions in the set_log_conditions method of Log are a subset of (and are aliased to) the flags in ContextFlags.

In addition to flags, execution_phase, and source, Context has four other attributes that record information relevant to the operating state of the Component:

owner
the Component to which the Context belongs (assigned to its context attribute;
flags_string
a string containing the names of the flags currently set in each of the fields of the flags attribute;
composition
the Composition in which the Component is currently being executed;
execution_id
the execution_id assigned to the Component by the Composition in which it is currently being executed;
execution_time
the current time of the scheduler running the Composition within which the Component is currently being executed;
string
contains message(s) relevant to a method of the Component currently invoked or that is referencing the Component. In general, this contains a copy of the context argument passed to method of the Component or one that references it, but it is possible that future uses will involve other messages.

Note

The string attribute of Context is not the same as, nor does it usually contain the same information as the string returned by the flags_string method of Context.

Class Reference¶

class psyneulink.core.globals.context.Context(owner=None, composition=None, flags=None, execution_phase=<ContextFlags.IDLE: 1024>, source=<ContextFlags.NONE: 32768>, runmode=<ContextFlags.DEFAULT_MODE: 65536>, execution_id=NotImplemented, string='', time=None, rpc_pipeline=None)

Used to indicate the state of initialization and phase of execution of a Component, as well as the source of call of a method; also used to specify and identify conditions for logging.

owner

Component – Component to which the Context belongs.

flags

binary vector – represents the current operating context of the owner; contains two fields execution_phase, and source (described below).

flags_string

str – contains the names of the flags currently set in each of the fields of the flags attribute; note that this is not the same as the string attribute (see note).

execution_phase

field of flags attribute – indicates the phase of execution of the Component; one or more of the following flags can be set:

• PREPARING
• PROCESSING
• LEARNING
• CONTROL
• IDLE

If IDLE is set, the Component is not being executed at the current time, and flags_string will include IDLE in the string. In some circumstances all of the execution_phase flags may be set (other than IDLE and PREPARING), in which case flags_string will include EXECUTING in the string.

source

field of the flags attribute – indicates the source of a call to a method belonging to or referencing the Component; one of the following flags is always set:

• CONSTRUCTOR
• COMMAND_LINE
• COMPOSITION
composition

Composition – the Composition in which the owner is currently being executed.

execution_id

str – the execution_id assigned to the Component by the Composition in which it is currently being executed.

execution_time

TimeScale – current time of the Scheduler running the Composition within which the Component is currently being executed.

string

str – contains message(s) relevant to a method of the Component currently invoked or that is referencing the Component. In general, this contains a copy of the context argument passed to method of the Component or one that references it, but it is possible that future uses will involve other messages. Note that this is not the same as the flags_string attribute (see note).

rpc_pipeline

Queue – queue to populate with messages for external environment in cases where execution was triggered via RPC call (e.g. through PsyNeuLinkView).

class psyneulink.core.globals.context.ContextFlags(*args, **kwargs)

Used to identify the initialization and execution status of a Component.

Used when a Component’s value or one of its attributes is being accessed. Also used to specify the context in which a value of the Component or its attribute is logged..

_generate_next_value_(start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initital start value or None count: the number of existing members last_value: the last value assigned or None

classmethod _get_context_string(condition_flags, fields={'execution_phase', 'source'}, string=None)

Return string with the names of flags that are set in condition_flags

If fields is specified, then only the names of the flag(s) in the specified field(s) are returned. The fields argument must be the name of a field (EXECUTION_PHASE or SOURCE) or a set or list of them.

If string is specified, the string returned is prepended by string.

_member_type_

alias of int

psyneulink.core.globals.context._get_context(context)

Set flags based on a string of ContextFlags keywords If context is already a ContextFlags mask, return that Otherwise, return mask with flags set corresponding to keywords in context

psyneulink.core.globals.context.handle_external_context(source=<ContextFlags.COMMAND_LINE: 2048>, execution_phase=<ContextFlags.IDLE: 1024>, execution_id=None, fallback_most_recent=False, fallback_default=False, **context_kwargs)
Parameters: source – default ContextFlags to be used for source field when Context is not specified execution_phase – default ContextFlags to be used for execution_phase field when Context is not specified context_kwargs – additional keyword arguments to be given to Context.__init__ when Context is not specified a decorator that ensures a Context argument is passed in to the decorated method