Module 6 β€” Schema Types, Enums & Complex Models


🎯 Module Objective

By the end of this module, you will:

This module transforms your DTDL from basic to professional.


1️⃣ What Is a Schema?

In DTDL, a schema defines the type of data.

Example:

{
  "@type": "Property",
  "name": "temperature",
  "schema": "double"
}

Here:

double = schema

Schemas define:

Think of schema as:

Data contract.


2️⃣ Primitive Schema Types

These are the simplest types.

Common primitives:

Example:

{
  "@type": "Property",
  "name": "isOperational",
  "schema": "boolean"
}

Use primitive types when:


3️⃣ Enum β€” Controlled Value Sets

Enum restricts values to a predefined list.

Example:

{
  "@type": "Property",
  "name": "operationalStatus",
  "schema": {
    "@type": "Enum",
    "valueSchema": "string",
    "enumValues": [
      { "name": "Running", "enumValue": "Running" },
      { "name": "Stopped", "enumValue": "Stopped" },
      { "name": "Maintenance", "enumValue": "Maintenance" }
    ]
  }
}

Now only those values are valid.

Why this is powerful:

Always use Enum when values are predictable.


4️⃣ Object β€” Structured Data

Objects allow grouping multiple related fields.

Example:

{
  "@type": "Property",
  "name": "location",
  "schema": {
    "@type": "Object",
    "fields": [
      {
        "name": "latitude",
        "schema": "double"
      },
      {
        "name": "longitude",
        "schema": "double"
      }
    ]
  }
}

Now location is structured.

Instead of two separate properties:

You have a single structured object.

Use Object when:


5️⃣ Array β€” Multiple Values

Array allows storing multiple values.

Example:

{
  "@type": "Property",
  "name": "supportedProtocols",
  "schema": {
    "@type": "Array",
    "elementSchema": "string"
  }
}

Now the twin can store:

[β€œMQTT”, β€œHTTP”, β€œAMQP”]

Use Array when:


6️⃣ Map β€” Key-Value Structure

Map stores dynamic key-value pairs.

Example:

{
  "@type": "Property",
  "name": "configuration",
  "schema": {
    "@type": "Map",
    "mapKey": {
      "name": "configName",
      "schema": "string"
    },
    "mapValue": {
      "name": "configValue",
      "schema": "string"
    }
  }
}

This allows flexible configuration storage.

Use Map when:


7️⃣ Complex Example β€” Smart Machine Model

Here is a production-style model combining everything:

{
  "@id": "dtmi:com:smartfactory:Machine;1",
  "@type": "Interface",
  "@context": "dtmi:dtdl:context;2",
  "displayName": "Machine",
  "contents": [
    {
      "@type": "Property",
      "name": "operationalStatus",
      "schema": {
        "@type": "Enum",
        "valueSchema": "string",
        "enumValues": [
          { "name": "Running", "enumValue": "Running" },
          { "name": "Stopped", "enumValue": "Stopped" },
          { "name": "Maintenance", "enumValue": "Maintenance" }
        ]
      }
    },
    {
      "@type": "Property",
      "name": "location",
      "schema": {
        "@type": "Object",
        "fields": [
          { "name": "x", "schema": "double" },
          { "name": "y", "schema": "double" }
        ]
      }
    },
    {
      "@type": "Property",
      "name": "supportedModes",
      "schema": {
        "@type": "Array",
        "elementSchema": "string"
      }
    }
  ]
}

This is real-world ready modeling.


8️⃣ When to Use What?

Use:

Primitive β†’ Simple values
Enum β†’ Controlled states
Object β†’ Grouped structured data
Array β†’ Lists
Map β†’ Flexible dynamic key-value

Design rule:

Use the simplest schema that solves the problem.


9️⃣ Over-Engineering Warning

Avoid:

❌ Using Object when primitive is enough
❌ Using Map for everything
❌ Making deeply nested structures
❌ Creating overly complex schemas

Remember:

Digital Twin modeling is about clarity.

Not showing complexity.


πŸ”Ÿ Schema Validation Thinking

Ask:

Model intentionally.


1️⃣1️⃣ Enterprise Modeling Example β€” Energy System

PowerUnit:

This creates:

Strong validation
Clear semantics
Query-friendly structure


🧠 Key Takeaways

Now your DTDL is expressive and controlled.


πŸ§ͺ Practice Exercise

Model a Smart Elevator.

Think:

Design the correct schema types.

That is professional modeling thinking.


πŸš€ What’s Next?

In Module 7 we build:

A complete Real-World Smart Factory Case Study

You will:

This is where everything comes together.

You are now modeling like an enterprise architect.

πŸ’¬
AI Learning Assistant