# ASSIGN

Sets attributes on a part, order or customer, or a model variable, from expressions as it passes, for example a due date, a type or a counter.

Takes an ordered list of assignments, each is `{"target": "entity.<attr>" | "entity.agent.<attr>" | "<variable>", "expr": "<expression>"}`.

Assignments run in order, so a later one sees what an earlier one wrote. Targets and expressions both go through core/expressions.py -- a model file never executes Python.

Family: data. Python builder: `m.assign(...)`.

## Typical uses

Use ASSIGN to stamp data on an entity or to set a model variable as the entity passes. Typical jobs are a due date, a priority, a product type, a counter or a flag that a later DECIDE reads. It takes no simulated time. To measure something without changing the model, use [RECORD](record.md). To change a stock, use [TRANSFER](transfer.md).

## Behaviour and rules

Assignments run in the listed order, so a later one sees what an earlier one wrote. A target `entity.<name>` sets an attribute of the entity. A target `entity.agent.<name>` writes a column of the entity's agent. Any other name sets a model variable, which must be declared in `variables`.

Assigning `entity.sequence` restarts the entity's routing at step 0. Writing a variable wakes any [HOLD](hold.md) that waits on a condition reading it, and updates the levels and agents that depend on it.

A bad target or expression raises an error that names the block and the assignment. The error appears when the first entity reaches the block, not when the file loads, unless a check of the model catches it earlier.

## Ports

- Inputs: any number of incoming links.
- Outputs: one outgoing link.

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `assignments` | list | `[]` |  |  |  | Ordered list of {"target": "entity.<attr>" | "entity.agent.<attr>" | "<variable>", "expr": "<expression>"}; each runs in order. |

## Statistics

Every run fills `system` and `entity_summary` (see the [KPI reference](../kpis.md)). The example below also fills:

| Frame | Columns |
|---|---|
| [`resource_summary`](../kpis.md#resource_summary) | `Run`, `Resource`, `Total Seizes`, `Usage % (snapshot)`, `Idle %`, `Scheduled Capacity Avg`, `Down %`, `Busy Cost`, `Idle Cost`, `Usage Cost`, `Total Cost`, `Queue Length Avg`, `Queue Length Max`, `Queue Length at End`, `Queue Length Mode`, `Queue Length Mode %`, `Queue Len P95`, `Queue Len P99`, `Queue Len Std`, `Time in Queue Avg`, `Time in Queue Avg (incl. waiting)`, `Time in Queue Max`, `Time in Queue Mode`, `Time in Queue Mode %`, `Wait P95`, `Wait P99`, `Wait Std`, `Bottleneck Score`, `Bottleneck %` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_ASSIGN_KEY` | An ASSIGN entry has a key other than `target` and `expr` (it would be ignored). |
| `E_ASSIGN_TARGET` | An ASSIGN target is not a writable `entity.<attr>` or variable name. |
| `E_SEQUENCE_UNKNOWN` | A CREATE or ASSIGN names a sequence the model does not declare. |
| `E_UNDECLARED_VARIABLE` | An expression or ASSIGN target names a variable the model does not declare. |

## Example

The snippet seeds itself; its output is from a run made when this page was generated.

```python
from nagasim import build as nb

m = nb.Model(time_limit=300)
m.add_resource(name="Press")
arrive = m.create(distribution=nb.Exponential(rate=0.2))
size = m.assign(assignments=[{"target": "entity.size", "expr": "UNIF(1, 3)"}])
press = m.process(resource_name="Press", distribution=nb.Constant(value="entity.size"))
arrive >> size >> press >> m.dispose()

results = m.run(seed=1)
print(round(results.kpi("resource/Press/Usage % (snapshot)"), 1), "% busy; service time is entity.size, 2 on average")
```

```text
37.1 % busy; service time is entity.size, 2 on average
```

## Used in

Bundled models: [Berth: shore power](../../gallery/energy.md), [Biogas plant (library assets)](../../gallery/energy.md), [Emergency department: staffing game](../../gallery/games.md), [Supply line: order-rate management game](../../gallery/games.md), [Hub day: electrifying the ground support equipment](../../gallery/logistics.md), [Container terminal: electrification](../../gallery/logistics.md) and 9 more.

Guides: [Entities and attributes](../../modelling-guide/entities-attributes.md), [Continuous quantities: levels, flows and time-series inputs](../../modelling-guide/continuous-quantities.md), [Model file format](../../model-file-format.md), [Expressions and variables](../../modelling-guide/expressions-variables.md), [Coming from AnyLogic](../../coming-from/anylogic.md), [Coming from Arena](../../coming-from/arena.md) and 5 more.

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
