# submodels

Reusable definitions (parameters, inner blocks/links/ports), flattened before a run.

In the model file this key holds an object that maps a definition's name to its definition.

## Typical uses

These sections organise a model and leave notes for people. `submodels` are reusable definitions that SUBMODEL blocks instantiate. `info` holds the description and author. `annotations` and `background` draw notes, frames and a floor plan on the canvas, and `building` declares the floor plan itself: rooms, wall openings and labels in metres. `cranes` and `stockpiles` say how playback draws lifts and stores on that plan. `view` remembers where the editor was. Only `submodels` changes what a run does.

## Behaviour and rules

A submodel definition holds parameters, inner blocks, links and named ports. Before a run, every instance is replaced by a copy of the definition's blocks, named `<instance>/<inner>`, and the links are rewired through the ports. A definition may be reused with different arguments, and a definition that contains itself is an error.

The engine ignores `annotations`, `background`, `building`, `cranes`, `stockpiles`, `view` and `info`, apart from showing `info` in the report header. They never change a result. A background image has a position, a scale, an opacity, a lock and a real-world scale. A building's walls are not stored: they are the outlines of its rooms with `walls` true, merged per wall line and cut by its openings. The editor writes `view` on every save.

A crane's lift is any traced move with a duration between two blocks it serves. Playback splits that move into phases (the hook travels empty, lowers, carries the load, sets it down) without changing its start or its length. A `stockpiles` area draws its block's entities as slabs on a few piles instead of a queue row. Each entity's pile comes from `keys`, one per entity created at `source`, in creation order. Past `max_layers` a pile stops growing and its label counts.

## Fields

| Field | Type | Required | Default | Unit | Description |
|---|---|---|---|---|---|
| `info` | object |  |  |  | The definition's own details, such as its title and description. |
| `parameters` | list of object |  |  |  | The arguments an instance sets; blocks inside read them by name. |
| `scoped` | object |  |  |  | Per-instance field overrides on `$`-scoped records. |
| `repeat` | list of object |  |  |  | Repeat groups (Phase 15 M7): per instance, the named blocks and every link touching them are stamped `count` times, `{i}` = 1..count in every string inside them. A group with no `{i}` in its names is optional (count 0 or 1). In a list param, an item {"$repeat": &lt;count&gt;, "item": &lt;value&gt;} is spliced in as count copies the same way. |
| `extends` | string |  |  |  | Inherit another definition: this one is the parent's with its own blocks and parameters merged in by name (child wins), links by source/target/ports, `scoped` per record. Chains allowed; a cycle is E_EXTENDS_CYCLE. |
| `remove` | list of string |  |  |  | With `extends`: parent blocks to drop, with every parent link touching one; a `src->tgt` entry drops the parent links between those two blocks. |
| `blocks` | list of block |  |  |  | The blocks of the definition. |
| `links` | list of link |  |  |  | The links between the blocks of the definition. |
| `annotations` | list of annotation |  |  |  | The canvas notes of the definition; the engine ignores them. |
| `code` | object |  |  |  | The item's own Python logic: a class with event hooks (on_entry, on_exit, on_timer, on_state, on_signal) that reads the instance's parameters. It runs only after a person has trusted this exact code on their computer. |

A field of a nested object is described in the JSON Schema (`naga schema`).

## Example

```json
{"submodels": {"Cell": {"blocks": [{"name": "In", "type": "SUB_ENTRY", "position": [0, 0]}, {"name": "Work", "type": "PROCESS", "position": [200, 0], "params": {"distribution": "Exponential", "dist_params": {"rate": 1.0}}}, {"name": "Out", "type": "SUB_EXIT", "position": [400, 0]}], "links": [{"source": "In", "target": "Work"}, {"source": "Work", "target": "Out"}]}}}
```

In the Python builder `m.definition(name)` adds a definition and returns the canvas to build it on.

## Used in

Bundled models: [Boids flock (continuous, heading)](../../gallery/agent-based.md), [Nagel-Schreckenberg traffic (ring)](../../gallery/agent-based.md), [Schelling segregation (grid)](../../gallery/agent-based.md), [SIR on a small-world network](../../gallery/agent-based.md), [Sugarscape with regrowth (grid, patches)](../../gallery/agent-based.md), [Wolf-Sheep predation with grass (grid)](../../gallery/agent-based.md) and 27 more.

Guides: [Model file format](../../model-file-format.md), [Submodels](../../modelling-guide/submodels.md), [Semantic diff](../../semantic-diff.md).

## See also

- [All sections](index.md), [Model file keys](../schema.md), [Model file format](../../model-file-format.md)
