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:
execution_phase
- phase of execution of the Component;
source
- source of a call to a method belonging to or operating on the Component.
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
.
Additional Attributes¶
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 theflags_string
method of Context.
Class Reference¶
- class psyneulink.core.globals.context.Context(owner=None, composition=None, flags=None, execution_phase=ContextFlags.IDLE, source=ContextFlags.NONE, runmode=ContextFlags.DEFAULT_MODE, 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.
- flags¶
represents the current operating context of the
owner
; contains two fieldsexecution_phase
, andsource
(described below).- Type
binary vector
- flags_string¶
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 thestring
attribute (see note).- Type
str
- execution_phase¶
indicates the phase of execution of the Component; one or more of the following flags can be set:
If
IDLE
is set, the Component is not being executed at the current time, andflags_string
will include IDLE in the string. In some circumstances all of theexecution_phase
flags may be set (other than IDLE and PREPARING), in which caseflags_string
will include EXECUTING in the string.- Type
field of flags attribute
- source¶
indicates the source of a call to a method belonging to or referencing the Component; one of the following flags is always set:
- Type
field of the flags attribute
- composition¶
the Composition in which the
owner
is currently being executed.- Type
- execution_id¶
the execution_id assigned to the Component by the Composition in which it is currently being executed.
- Type
str
- execution_time¶
current time of the Scheduler running the Composition within which the Component is currently being executed.
- Type
- 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 that this is not the same as the
flags_string
attribute (see note).- Type
str
- rpc_pipeline¶
queue to populate with messages for external environment in cases where execution was triggered via RPC call (e.g. through PsyNeuLinkView).
- Type
Queue
- class psyneulink.core.globals.context.ContextFlags(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
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..- DEFERRED_INIT = 1¶
Set if flagged for deferred initialization.
- INITIALIZING = 2¶
Set during initialization of the Component.
- VALIDATING = 4¶
Set during validation of the value of a Component or its attribute.
- INITIALIZED = 8¶
Set after completion of initialization of the Component.
- RESET = 16¶
Set on stateful Components when they are re-initialized.
- UNINITIALIZED = 32¶
Default value set before initialization
- PREPARING = 64¶
Set while Composition is preparing to `execute.
- PROCESSING = 128¶
Set while Composition is `executing ProcessingMechanisms.
- LEARNING = 256¶
Set while Composition is `executing LearningMechanisms.
- CONTROL = 512¶
Set while Composition’s
controller
or its ObjectiveMechanism is executing.
- IDLE = 1024¶
Identifies condition in which no flags in the
execution_phase
are set.
- COMMAND_LINE = 2048¶
Direct call by user (either interactively from the command line, or in a script).
- CONSTRUCTOR = 4096¶
Call from Component’s constructor method.
- METHOD = 8192¶
Call by method of the Component other than its constructor.
- COMPOSITION = 16384¶
Call by a/the Composition to which the Component belongs.
- NONE = 32768¶
Call by a/the Composition to which the Component belongs.
- DEFAULT_MODE = 65536¶
Default mode
- LEARNING_MODE = 131072¶
Set during
composition.learn
- SIMULATION_MODE = 262144¶
Set during simulation by Composition.controller
- classmethod _get_context_string(cls, 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.
- 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, execution_phase=ContextFlags.IDLE, 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
- Returns
- Return type
a decorator that ensures a Context argument is passed in to the decorated method