• Github
Table of Contents
0.16.1.0+92
  • Welcome to PsyNeuLink
  • Basics and Primer
  • Quick Reference
  • Core
  • Library
  • Contributors Guide
  • Docs >
  • Context
Shortcuts

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 the flags_string method of Context.

Class Reference

class psyneulink.core.globals.context.Context(owner=None, composition=None, flags=None, execution_phase=<ContextFlags.IDLE: 2048>, source=<ContextFlags.NONE: 65536>, runmode=<ContextFlags.DEFAULT_MODE: 131072>, 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 to which the Context belongs.

Type:

Component

flags

represents the current operating context of the owner; contains two fields execution_phase, and source (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 the string 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:

  • 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.

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:

  • CONSTRUCTOR

  • COMMAND_LINE

  • COMPOSITION

Type:

field of the flags attribute

composition

the Composition in which the owner is currently being executed.

Type:

Composition

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:

TimeScale

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)

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.

DISPLAYING = 1024

Set during call to Composition’s show_graph`show_graph method.

IDLE = 2048

Identifies condition in which no flags in the execution_phase are set.

COMMAND_LINE = 4096

Direct call by user (either interactively from the command line, or in a script).

CONSTRUCTOR = 8192

Call from Component’s constructor method.

METHOD = 16384

Call by method of the Component other than its constructor.

COMPOSITION = 32768

Call by a/the Composition to which the Component belongs.

NONE = 65536

Call by a/the Composition to which the Component belongs.

DEFAULT_MODE = 131072

Default mode

LEARNING_MODE = 262144

Set during composition.learn

SIMULATION_MODE = 524288

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.

_generate_next_value_(start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None

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: 4096>, execution_phase=<ContextFlags.IDLE: 2048>, 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

Return type:

a decorator that ensures a Context argument is passed in to the decorated method


© Copyright 2016, Jonathan D. Cohen.

Built with Sphinx using a theme provided by Read the Docs.
  • Context
    • Overview
    • Context and Logging
    • Additional Attributes
    • Class Reference
    • Context
      • Context.owner
      • Context.flags
      • Context.flags_string
      • Context.execution_phase
      • Context.source
      • Context.composition
      • Context.execution_id
      • Context.execution_time
      • Context.string
      • Context.rpc_pipeline
    • ContextFlags
      • ContextFlags.DEFERRED_INIT
      • ContextFlags.INITIALIZING
      • ContextFlags.VALIDATING
      • ContextFlags.INITIALIZED
      • ContextFlags.RESET
      • ContextFlags.UNINITIALIZED
      • ContextFlags.PREPARING
      • ContextFlags.PROCESSING
      • ContextFlags.LEARNING
      • ContextFlags.CONTROL
      • ContextFlags.DISPLAYING
      • ContextFlags.IDLE
      • ContextFlags.COMMAND_LINE
      • ContextFlags.CONSTRUCTOR
      • ContextFlags.METHOD
      • ContextFlags.COMPOSITION
      • ContextFlags.NONE
      • ContextFlags.DEFAULT_MODE
      • ContextFlags.LEARNING_MODE
      • ContextFlags.SIMULATION_MODE
      • ContextFlags._get_context_string()
      • ContextFlags._generate_next_value_()
    • _get_context()
    • handle_external_context()
  • Github