# SEND

Sends a message, with an optional payload, to agents of an agent-based model so they can react, for example a customer calling a driver.

Family: control. Python builder: `m.send(...)`.

## Typical uses

Use SEND to let an entity reach an agent in an agent-based model. A customer calls a driver, an order wakes a machine agent, or a signal goes to a whole population. It is the bridge from the discrete flow to the agents. The agent's statechart reacts to the message in a `message` transition.

## Behaviour and rules

The `message` name is what an agent's transition matches. The default recipient `entity.agent` is the agent that the entity got when it entered the model. Other forms are `AGENT("Pop", i)` for one agent, or `{"population": "Pop"}` for a whole population. A `where` condition on the agents selects some, and `random` picks one of them from the block's own random stream.

The optional `payload` is an expression evaluated for the entity. The receiving transition reads it as `payload`. The message is delivered before the entity moves on, in order of sending, at the same instant. The block takes no time.

A model with no populations cannot use SEND, and the run stops with an error. A malformed `to` is caught by a check. The `agent_summary` frame reports the agents' states.

## Ports

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

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `message` | str | `''` | yes |  |  | Name of the message; an agent's "message" transition matches it. |
| `to` | object | `'entity.agent'` |  |  |  | Recipients: an expression giving an agent (entity.agent, AGENT("Pop", i)) or {"population": "Pop"[, "where": "<condition on self>"]\[, "random": true]}. |
| `payload` | str | `''` |  |  |  | Optional expression evaluated for the entity; the receiving transition reads it as `payload`. |

## Statistics

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

| Frame | Columns |
|---|---|
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |
| `agent_summary` | `Run`, `Agent State`, `Population`, `State`, `Avg Count`, `Min`, `Max`, `Final`, `Entries`, `Avg Sojourn`, `Dropped Messages` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_SEND_TARGET` | A send action's or SEND block's `to` (or a remove action's target) is malformed: not "self" (agents only), an index, an agent expression or {population, where?, random?}; or a SEND block in a model without populations. |
| `W_AGENT_MESSAGE_UNUSED` | A message transition waits for a name that no send action or SEND block sends. |

## 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=100)
m.add_agent_type(
    name="Patient", states=[{"name": "Waiting"}, {"name": "Treated"}], initial_state="Waiting",
    transitions=[{"from": "Waiting", "to": "Treated", "message": "treated"}])
m.add_population(name="Patients", type="Patient", count=0, max_count=50)
m.create(distribution=nb.Constant(value=5), population="Patients", max_arrivals=10) \
    >> m.delay(distribution=nb.Constant(value=2)) >> m.send(message="treated") >> m.dispose()

results = m.run(seed=1)
print(results["agent_summary"].round(2).to_string(index=False))
```

```text
 Run      Agent State Population   State  Avg Count  Min  Max  Final  Entries  Avg Sojourn  Dropped Messages
   1 Patients/Waiting   Patients Waiting       0.20    0    1      0        0          2.0                 0
   1 Patients/Treated   Patients Treated       7.55    0   10     10       10          NaN                 0
```

## Used in

Bundled models: [Emergency department: staffing game](../../gallery/games.md), [Emergency department with patient agents](../../gallery/services.md).

Guides: [Agent-based modelling](../../agent-based-modelling.md), [Coming from AnyLogic](../../coming-from/anylogic.md).

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
- Same family (control): [DECIDE](decide.md), [HOLD](hold.md), [LOGIC](logic.md), [ROUTE](route.md), [SIGNAL](signal.md)
