> For the complete documentation index, see [llms.txt](https://docs.cybus.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cybus.io/2-8-0/unified-namespace/example-factory.md). Agents can also search and read this documentation through its [MCP server](https://docs.cybus.io/~gitbook/mcp).

# Example Factory

Build the Unified Namespace of a fictional factory from services that different teams own, from a shared model library to a KPI.

ACME Hamburg is a fictional factory with CNC mills, a hardening oven, and a power meter. Its namespace covers the following:

* A model library that every site shares.
* A namespace with its tree, its rules, a production line as an asset, and access roles.
* Machines on S7 and Modbus/TCP that publish to their data properties instead of hand-written topics, including writable properties for commands.
* A legacy MQTT device that a mapping brings into the namespace.
* A machine that a central placement service places, instead of its own service.
* A KPI that a mapping calculates from the state of the machines.

## Install the Services

Install the services in the following order, because each one references resources of the services before it. For how to install a service, see [Installing Services](https://docs.cybus.io/2-8-0/data-flows/services/managing/installing.md).

Enable each service after you install it. See [Enabling Services](https://docs.cybus.io/2-8-0/data-flows/services/managing/enabling.md). The namespace, the assets, and their topics become active only while their services are enabled.

1. `acme-uns-models.yml`: The models.
2. `acme-uns-namespace.yml`: The namespace, which uses the `ProductionLine` model.
3. `acme-uns-powermeter01.yml`, `acme-uns-oven01.yml`, and `acme-uns-mill01.yml`: The machines that place themselves. Mill 01 relates to the power meter, so install the power meter first.
4. `acme-uns-mill02.yml`: A machine without a placement.
5. `acme-uns-placement.yml`: Places Mill 02.
6. `acme-uns-mill02-data.yml`: The S7 connection of Mill 02. Its endpoint needs Mill 02 to be placed.
7. `acme-uns-kpi.yml`: Reads the state of both mills, so they must be placed.

A service can only reference resources that are already installed. For example, a mount needs its asset, and an endpoint needs its asset to be placed. If a resource is missing, the installation fails with an error that names the reference. Install the service again once that resource is installed.

Replace the IP addresses of the PLCs and the Modbus/TCP device with the addresses of your devices.

## The Model Library

The models describe each kind of machine once. `CncMill` and `HardeningOven` inherit the `state` data property from `MachineState` through `implements`. Writable properties, such as `recipeId` and `targetTemperature`, also get a `/set` topic for commands and a `/res` topic for their results.

{% code title="acme-uns-models.yml" lineNumbers="true" expandable="true" %}

```yaml
description: ACME model library, the shared asset models of all sites

metadata:
  name: acmemodels
  version: 1.0.0

resources:
  machineStateModel:
    type: Cybus::Model
    properties:
      modelId: MachineState
      version: 1
      data:
        state:
          type: string
          enum: [running, idle, setup, error, maintenance]

  energyConsumerModel:
    type: Cybus::Model
    properties:
      modelId: EnergyConsumer
      version: 1
      data:
        power:
          type: number
          unit: W

  cncMillModel:
    type: Cybus::Model
    properties:
      modelId: CncMill
      version: 1
      description: CNC milling machine
      implements:
        - !ref machineStateModel
        - !ref energyConsumerModel
      data:
        spindleSpeed:
          type: number
          unit: rpm
          minimum: 0
          maximum: 24000
        partCount:
          type: integer
        recipeId:
          type: string
          writable: true # gets recipeId/set for commands and recipeId/res for results
      attributes:
        manufacturer:
          type: string
          required: true
        serialNumber:
          type: string
          required: true
      relations:
        measuredBy:
          target: !ref powerMeterModel
          cardinality: one

  hardeningOvenModel:
    type: Cybus::Model
    properties:
      modelId: HardeningOven
      version: 1
      implements:
        - !ref machineStateModel
      data:
        temperature:
          type: number
          unit: Cel
        targetTemperature: # the setpoint, separate from the measured temperature
          type: number
          unit: Cel
          minimum: 0
          maximum: 950
          writable: true
      attributes:
        serialNumber:
          type: string
          required: true

  powerMeterModel:
    type: Cybus::Model
    properties:
      modelId: PowerMeter
      version: 1
      data:
        power:
          type: number
          unit: W

  productionLineModel:
    type: Cybus::Model
    properties:
      modelId: ProductionLine
      version: 1
      data:
        availability:
          type: number
          minimum: 0
          maximum: 1
      attributes:
        costCenter:
          type: string
          required: true
```

{% endcode %}

{% file src="/files/Za45A4bT8UNhu0mTHOU7" %}

## The Namespace

The namespace service owns the tree and its rules. With `placement: tree-only`, assets can only be placed at the declared nodes. With `modeling: required`, every asset needs a model. The `naming` policy allows lowercase letters, digits, and hyphens in topic segments, but no underscores.

The production line is an asset of the `ProductionLine` model. `promote` places it at the node `line-north` itself, so its `availability` topic is `acme/hamburg/assembly/line-north/availability`.

The `dashboardsHamburg` role grants read access to everything below `hamburg`. Assign it to users as usual. See [Cybus::Role](https://docs.cybus.io/2-8-0/data-flows/service-commissioning-files/resources/cybus-role.md#resource).

{% code title="acme-uns-namespace.yml" lineNumbers="true" %}

```yaml
description: ACME namespace with its tree, policy, line asset, and a dashboard role

metadata:
  name: acmenamespace
  version: 1.0.0

resources:
  acme:
    type: Cybus::Namespace
    properties:
      root: acme
      structure:
        levels: [site, area, line]
        tree: # the keys are the topic segments
          hamburg:
            assembly:
              line-north: {}
              line-south: {}
            hardening: {}
      policy:
        placement: tree-only
        modeling: required
        naming: '^[a-z0-9-]+$'

  # The production line is an asset too. Promoted, its topics live at the node itself.
  lineNorth:
    type: Cybus::Asset
    properties:
      model: ProductionLine@1
      promote: 'uns:acme/hamburg/assembly/line-north'
      attributes:
        costCenter: HH-4711

  dashboardsHamburg:
    type: Cybus::Role
    properties:
      permissions:
        - context: mqtt
          operation: read
          resource: 'uns:acme/hamburg'
```

{% endcode %}

{% file src="/files/o0hMrZI0Pggzw6OlS2by" %}

## The Machines

Each machine service declares the asset, its connection, and the endpoints. An endpoint references a data property, such as `mill01.spindleSpeed`, instead of a topic. Mill 01 places itself with `mount`, so its spindle speed is published on `acme/hamburg/assembly/line-north/mill01/spindleSpeed`.

References such as `mill02` or `asset:oven01` name assets by their ID. A reference such as `model: CncMill@1` names a model by its `modelId` and version. Both work across services, and Connectware resolves them when you install the service.

{% hint style="info" %}
The model references in this example name their version. While only one version of a model is installed, a reference without a version also selects it. The model library can only add a second version of a model once every reference to the model names its version. See [Referencing a Model](https://docs.cybus.io/2-8-0/data-flows/service-commissioning-files/resources/cybus-model.md#referencing-a-model).
{% endhint %}

The `recipeWrite` endpoint writes to the PLC. Because `recipeId` is writable, the endpoint takes its commands from `acme/hamburg/assembly/line-north/mill01/recipeId/set` and publishes the result on `recipeId/res`.

{% code title="acme-uns-mill01.yml" lineNumbers="true" expandable="true" %}

```yaml
description: Mill 01 with its S7 connection, placed by its own service

metadata:
  name: acmemill01
  version: 1.0.0

resources:
  mill01:
    type: Cybus::Asset
    properties:
      model: CncMill@1
      mount: 'uns:acme/hamburg/assembly/line-north'
      attributes:
        manufacturer: DMG Mori
        serialNumber: MX-2041-0017
      identity:
        globalAssetId: 'https://example.com/assets/mx-2041-0017'
        specificAssetIds:
          erpNumber: 'A-10045'
      relations:
        measuredBy: [powermeter01]

  plc:
    type: Cybus::Connection
    properties:
      protocol: S7
      targetState: connected
      connection:
        host: 192.168.1.100
        port: 102
        rack: 0
        slot: 1

  spindleSpeed:
    type: Cybus::Endpoint
    properties:
      protocol: S7
      connection: !ref plc
      asset: mill01.spindleSpeed # Connectware derives the topic from the asset
      subscribe:
        address: DB10,R0

  state:
    type: Cybus::Endpoint
    properties:
      protocol: S7
      connection: !ref plc
      asset: mill01.state
      subscribe:
        address: DB10,I8
      rules:
        - transform:
            expression: >-
              { "timestamp": timestamp,
                "value": ["running","idle","setup","error","maintenance"][value] }

  recipeWrite:
    type: Cybus::Endpoint
    properties:
      protocol: S7
      connection: !ref plc
      asset: mill01.recipeId # writable, so the endpoint executes commands from recipeId/set
      write:
        address: DB30,S0.32
```

{% endcode %}

{% file src="/files/KvMDhn0zZepNdbWh9ocQ" %}

The oven keeps the measured `temperature` and the `targetTemperature` setpoint apart, so a command never shows up as the measured state. The `ovenOperator` role can read every topic of the oven and send commands to `targetTemperature/set`, wherever the oven is placed. The role is part of the oven service, because an `asset:` permission needs a placed asset when you install the service.

{% code title="acme-uns-oven01.yml" lineNumbers="true" %}

```yaml
description: Oven 01 with its Modbus/TCP connection and a writable setpoint

metadata:
  name: acmeoven01
  version: 1.0.0

resources:
  oven01:
    type: Cybus::Asset
    properties:
      model: HardeningOven@1
      mount: 'uns:acme/hamburg/hardening'
      attributes:
        serialNumber: NB-88-1204

  modbus:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: 192.168.1.102
        port: 502

  temperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbus
      asset: oven01.temperature
      subscribe:
        fc: 3
        length: 2
        interval: 1000
        address: 100
        dataType: int16BE

  targetTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbus
      asset: oven01.targetTemperature
      write:
        fc: 6
        length: 2
        address: 200
        dataType: int16BE

  ovenOperator:
    type: Cybus::Role
    properties:
      permissions:
        - context: mqtt
          operation: readWrite
          resource: 'asset:oven01'
```

{% endcode %}

{% file src="/files/RUAkO49wtzswDZljZqNl" %}

The power meter publishes on a legacy topic that it cannot change. A mapping publishes its values to the `power` data property. So the meter gets a model, a place in the tree, and a `$meta` topic like every other asset.

{% code title="acme-uns-powermeter01.yml" lineNumbers="true" %}

```yaml
description: Power meter 01, an MQTT device that publishes on a legacy topic

metadata:
  name: acmepowermeter01
  version: 1.0.0

resources:
  powermeter01:
    type: Cybus::Asset
    properties:
      model: PowerMeter@1
      mount: 'uns:acme/hamburg/assembly/line-north'

  legacyBridge: # the device cannot change its topic, so a mapping brings it into the namespace
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: legacy/meters/7/power
          publish:
            asset: powermeter01.power
          rules:
            - transform:
                expression: '{ "timestamp": $millis(), "value": $number(p) }'
```

{% endcode %}

{% file src="/files/l4HpgV8sSjCawJpLUg8g" %}

## Central Placement

Mill 02 declares no mount. The placement service decides where it lives, so the team that owns the tree can move assets without changing the machine services.

To move Mill 02 to line south, change `at` to `uns:acme/hamburg/assembly/line-south` and update the placement service. Connectware then moves the endpoint of Mill 02 to the new topic. The KPI mapping reads Mill 02 by its ID, so also remove Mill 02 from the KPI of line north and adjust its divisor.

{% code title="acme-uns-mill02.yml" lineNumbers="true" %}

```yaml
description: Mill 02, placed by the placement service

metadata:
  name: acmemill02
  version: 1.0.0

resources:
  mill02: # no mount here, the placement service decides where mill02 lives
    type: Cybus::Asset
    properties:
      model: CncMill@1
      attributes:
        manufacturer: DMG Mori
        serialNumber: MX-2041-0018
```

{% endcode %}

{% file src="/files/fUqPvgVPI5enHIILRM7h" %}

{% code title="acme-uns-placement.yml" lineNumbers="true" %}

```yaml
description: Places assets of other services in the ACME namespace

metadata:
  name: acmeplacement
  version: 1.0.0

resources:
  mill02Mount:
    type: Cybus::Mount
    properties:
      asset: mill02
      at: 'uns:acme/hamburg/assembly/line-north'
```

{% endcode %}

{% file src="/files/2g50PaFfHA9G6Hr4wGvu" %}

The S7 connection of Mill 02 is its own service. An endpoint can only reference a data property of an asset that is placed, so install it after the placement service.

{% code title="acme-uns-mill02-data.yml" lineNumbers="true" %}

```yaml
description: The S7 connection of Mill 02, installed after the placement service

metadata:
  name: acmemill02data
  version: 1.0.0

resources:
  plc:
    type: Cybus::Connection
    properties:
      protocol: S7
      targetState: connected
      connection:
        host: 192.168.1.101
        port: 102
        rack: 0
        slot: 1

  state:
    type: Cybus::Endpoint
    properties:
      protocol: S7
      connection: !ref plc
      asset: mill02.state
      subscribe:
        address: DB10,I8
      rules:
        - transform:
            expression: >-
              { "timestamp": timestamp,
                "value": ["running","idle","setup","error","maintenance"][value] }
```

{% endcode %}

{% file src="/files/PeRz3fbFUwFf9ImVEB2E" %}

## A KPI on the Line

The KPI service is the only writer of the line's `availability`. Its mapping subscribes to the `state` of both mills by asset reference. The [collect](https://docs.cybus.io/2-8-0/data-flows/rule-engine/data-processing-rules.md#collect) rule keeps the latest message of each mill under its label.

The mapping then publishes the share of mills that are running. `$count([mill01.value, mill02.value][$ = "running"])` counts the mills whose state is `running`. With Mill 01 running and Mill 02 idle, the value is `0.5`. The expression divides by two mills, so change it when the mills on the line change.

{% code title="acme-uns-kpi.yml" lineNumbers="true" %}

```yaml
description: Availability of line north, calculated from the state of its mills

metadata:
  name: acmekpi
  version: 1.0.0

resources:
  availability:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            - asset: mill01.state
              label: mill01
            - asset: mill02.state
              label: mill02
          publish:
            asset: lineNorth.availability
          rules:
            - collect: {}
            - transform:
                expression: >-
                  { "timestamp": $millis(),
                    "value": $count([mill01.value, mill02.value][$ = "running"]) / 2 }
```

{% endcode %}

{% file src="/files/OeZxN7674V5N8ls3OIaE" %}

## Result

Once all services are installed and enabled, the namespace holds the following topics below `acme`, none of them written by hand:

```
acme/hamburg/
├── assembly/
│   ├── line-north/                  $meta, availability (the promoted line)
│   │   ├── mill01/                  $meta, state, power, spindleSpeed, partCount,
│   │   │                            recipeId, recipeId/set, recipeId/res
│   │   ├── mill02/                  $meta, state, power, spindleSpeed, partCount,
│   │   │                            recipeId, recipeId/set, recipeId/res
│   │   └── powermeter01/            $meta, power
│   └── line-south/
└── hardening/
    └── oven01/                      $meta, state, temperature, targetTemperature,
                                     targetTemperature/set, targetTemperature/res
```

A data property gets its topic as soon as its asset is placed. Connectware publishes the retained `$meta` topics right away. The other topics appear on the broker once an endpoint or a mapping writes to them. In this example, endpoints and mappings write the following:

* Mill 01: `spindleSpeed` and `state`
* Mill 02: `state`
* Oven: `temperature`
* Power meter: `power`
* Line: `availability`

The writable `recipeId` and `targetTemperature` carry commands on `/set` and results on `/res`. Add endpoints for the other data properties in the same way.

To check the namespace, subscribe to `acme/#` with an MQTT client. You receive the retained `$meta` message of every placed asset. Or request `GET /api/v2/uns/namespaces/acme/tree`, which lists the nodes and the base topics of the assets. See [Topic Explorer API](https://docs.cybus.io/2-8-0/reference/api-reference/topic-explorer-api.md).

For each resource, see [Where You Declare the Namespace](https://docs.cybus.io/2-8-0/unified-namespace/unified-namespace.md#where-you-declare-the-namespace).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.cybus.io/2-8-0/unified-namespace/example-factory.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
