UserSpace.relref#

UserSpace.relref(**kwargs)[source]#

Set references in relative mode.

Creates references that adjust their bindings based on inheritance context. When a space with relative references is inherited, the references are resolved relative to the derived space’s position in the hierarchy. The mode of a reference is relevant only when modelx objects, such as cells or spaces, are assigned to the reference.

This is useful for creating reusable space templates where references should point to “local” versions of objects in derived contexts.

This is a convenience method equivalent to calling set_ref() with refmode="relative" for each reference.

Relative vs Absolute:

  • Relative: Reference adjusts based on inheritance context

  • Absolute: Reference always points to the same object

Parameters:

**kwargs – Keyword arguments where each key is the reference name and each value is the object to reference. Multiple references can be set in a single call.

Example

In the example below, a cells foo is defined in Space S. abs_foo in Space S is defined as an absolute reference to foo, while rel_foo is defined as a relative reference.

When Space S is inherited by S[y], abs_foo continues to point to S.foo, while rel_foo points to S[y].foo, which uses the parameter y in stead of S.y:

>>> import modelx as mx

>>> s  = mx.new_space('S')

>>> @mx.defcells
... def foo(x):
...     return x + y

>>> s.absref(abs_foo=foo)
>>> s.relref(rel_foo=foo)
>>> s.y = 0

>>> s.parameters = 'y'

>>> s[1].abs_foo(10)  # Absolute reference to S.foo
10

>>> s[1].rel_foo(10)  # Relative reference to S[1].foo
11

See also

  • absref(): Set references in absolute mode

  • set_ref(): Set individual reference with explicit mode

  • refs: View all references