# MATCH

Waits until one entity of each of several kinds is present (a part and its kit, an order and its customer), then releases them together.

Match `number_to_match` *distinct* entity types; with `match_on` set, only entities whose value of that attribute is equal (Arena's attribute match).

The arriving entity is always stored first, so it can never be dropped ; the match is then taken atomically from the stores, oldest waiting type first, FIFO within a type (and within a value).

Family: group. Python builder: `m.match(...)`.

## Typical uses

Use MATCH when different kinds of entity must meet before work can start: a part and its kit, an order and its customer, a driver and a vehicle. It waits for one entity of each of several types. For a count of entities of any kind, such as a pallet of ten, use [BATCH](batch.md).

## Behaviour and rules

`number_to_match` is the number of distinct entity types that must be present, at least 2. Two entities of the same type never match each other. Each arrival is stored first, so it can never be lost. The block then checks whether that many types are waiting.

When they are, it takes the oldest waiting entity of each type, starting with the type that has waited longest. Within a type the order is first in first out. With `match_on` set to an attribute, only entities with an equal value of that attribute match, and a missing attribute stops the run with an error.

With `batch_option` on, the matched entities become one batch entity of type `batched_entity_type`, which a SEPARATE can split. With it off, they leave one after the other in the same instant along the single outgoing link. Unmatched entities wait for ever, and the `store_summary` frame shows them.

## Ports

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

## Parameters

| Parameter | Type | Default | Required | Quantity | Unit key | Description |
|---|---|---|---|---|---|---|
| `number_to_match` | int | `2` |  | count |  | Number of distinct entity types to match together. |
| `batch_option` | bool | `False` |  |  |  | True: combine the matched entities into one batch entity. False: forward them separately. |
| `batched_entity_type` | str | `'BATCH'` |  |  |  | Entity type assigned to the batch entity, when batch_option is True. |
| `type_store_map` | dict | `{}` |  |  |  | Optional {e_type: store index} mapping; default is automatic assignment. |
| `match_on` | str | `''` |  |  |  | An entity attribute (e.g. "order"); entities are matched only with others whose value of it is equal -- Arena's attribute match -- still one per distinct type, FIFO within a value. Empty = match by type alone. |

## Statistics

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

| Frame | Columns |
|---|---|
| [`store_summary`](../kpis.md#store_summary) | `Run`, `Store`, `Capacity`, `Average Size`, `Max Size`, `Mode Size`, `Mode Size %`, `P95 Size`, `P99 Size`, `Std Size`, `Avg Time in Store`, `Mode Time in Store`, `Mode Time in Store %`, `P95 Time in Store`, `P99 Time in Store`, `Std Time in Store`, `Util % (time-weighted)` |
| `material_flow_summary` | `Run`, `From`, `To`, `Entities` |

## Problem codes

A check of the model reports these about this block:

| Code | Meaning |
|---|---|
| `E_MATCH_ATTRIBUTE` | A MATCH match_on is not an attribute name (an identifier, optionally written entity.<name>). |

## 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_entity(name="FRAME")
m.add_entity(name="WHEEL")
frames = m.create("Frames", e_type="FRAME", distribution=nb.Constant(value=4), max_arrivals=20)
wheels = m.create("Wheels", e_type="WHEEL", distribution=nb.Constant(value=3), max_arrivals=20)
pair = m.match("Pair", number_to_match=2)     # waits for one FRAME and one WHEEL, then releases both
frames >> pair
wheels >> pair
pair >> m.dispose()

results = m.run(seed=1)
print(int(results.kpi("system/Total Entities Completed")), "entities released, in 20 pairs")
print(round(results.kpi("entity/WHEEL/Avg Time"), 2), "time units a wheel waits for its frame, on average (wheels arrive faster)")
```

```text
40 entities released, in 20 pairs
9.5 time units a wheel waits for its frame, on average (wheels arrive faster)
```

## Used in

Bundled models: [Hub day: electrifying the ground support equipment](../../gallery/logistics.md), [Hub day: turnaround workload (crews, ground equipment, on-time departures)](../../gallery/logistics.md).

Guides: [Digital twin](../../digital-twin.md), [How do I make two kinds of entity wait for each other?](../../how-to/recipes/pair-flows.md), [Batching and matching](../../modelling-guide/batching-matching.md), [Coming from AnyLogic](../../coming-from/anylogic.md), [Coming from Arena](../../coming-from/arena.md), [Coming from salabim](../../coming-from/salabim.md) and 4 more.

## See also

- [All blocks](index.md), [KPI reference](../kpis.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
- Same family (group): [BATCH](batch.md), [DUPLICATE](duplicate.md), [SEPARATE](separate.md)
