scenic.core.lazy_eval

Support for lazy evaluation of expressions and specifiers.

Lazy evaluation is necessary for expressions like 30 deg relative to roadDirection where roadDirection is a vector field and so defines a different heading at different positions. Scenic defers evaluation of such expressions until they are used in the definition of an object, when the required context (here, a position) is available. This is implemented by representing lazy values as special objects which capture all operations applied to them (in a similar way to Distribution objects). The main class of such objects is DelayedArgument: in the above example, the relative to operator returns such an object. However, since lazy values can appear as arguments to distributions, Distribution objects can also require lazy evaluation (prior to sampling); therefore both of these classes derive from a common abstract class LazilyEvaluable.

Summary of Module Members

Functions

dependencies

Dependencies which must be sampled before this value.

isLazy

Whether this value requires either sampling or lazy evaluation.

makeDelayedFunctionCall

Utility function for creating a lazily-evaluated function call.

makeDelayedOperatorHandler

needsLazyEvaluation

Whether the given value requires lazy evaluation.

needsSampling

Whether this value requires sampling.

requiredProperties

Set of properties needed to evaluate the given value, if any.

toLazyValue

Wrap a Python object in a DelayedArgument if it needs lazy evaluation.

valueInContext

Evaluate something in the context of an object being constructed.

Classes

DelayedArgument

Specifier arguments requiring other properties to be evaluated first.

LazilyEvaluable

Values which may require evaluation in the context of an object being constructed.

Member Details

class LazilyEvaluable(requiredProps, dependencies=())[source]

Values which may require evaluation in the context of an object being constructed.

If a LazilyEvaluable specifies any properties it depends on, then it cannot be evaluated to a normal value except during the construction of an object which already has values for those properties.

Parameters:
  • requiredProps – sequence of strings naming all properties which this value can depend on (formally, which must exist in the object passed as the context to evaluateIn).

  • dependencies – for internal use only (see Samplable).

Attributes:

_requiredProperties – set of strings as above.

evaluateIn(context)[source]

Evaluate this value in the context of an object being constructed.

The object must define all of the properties on which this value depends.

evaluateInner(context)[source]

Actually evaluate in the given context, which provides all required properties.

Overridden by subclasses.

static makeContext(**props)[source]

Make a context with the given properties.

class DelayedArgument(requiredProps, value, _internal=False)[source]

Bases: LazilyEvaluable

Specifier arguments requiring other properties to be evaluated first.

The value of a DelayedArgument is given by a function mapping the context (object under construction) to a value.

Note

When called from a dynamic behavior, constructors for delayed arguments return actual evaluations, not DelayedArgument objects. The agent running the behavior is used as the evaluation context.

Parameters:
  • requiredProps – see LazilyEvaluable.

  • value – function taking a single argument (the context) and returning the corresponding evaluation of this object.

  • _internal (bool) – set to True for internal uses that need to suppress the exceptional handling of calls from dynamic behaviors above.

makeDelayedFunctionCall(func, args, kwargs={})[source]

Utility function for creating a lazily-evaluated function call.

valueInContext(value, context)[source]

Evaluate something in the context of an object being constructed.

toLazyValue(thing)[source]

Wrap a Python object in a DelayedArgument if it needs lazy evaluation.

requiredProperties(thing)[source]

Set of properties needed to evaluate the given value, if any.

needsLazyEvaluation(thing)[source]

Whether the given value requires lazy evaluation.

dependencies(thing)[source]

Dependencies which must be sampled before this value.

needsSampling(thing)[source]

Whether this value requires sampling.

isLazy(thing)[source]

Whether this value requires either sampling or lazy evaluation.