Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reference Target

The Reference Target semantic module constrains an entity reference to a specific entity type. A targeted ref links one entity to another entity of a declared type. Here, the task’s started event records which Worker runs it.

YAML model

quent: alpha
model: references

entities:
  Worker:
    events:
      registered: {}

  Task:
    events:
      started:
        attributes:
          worker: { ref: Worker }
      ended: {}

Instrumentation API

The generated started method only accepts the target-language representation of a reference to a Worker.

use instrumentation::{Context, Noop, References, Task, Worker};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let context = Context::<References>::try_new(Noop)?;

    let mut worker = context.observer::<Worker>().handle();
    worker.registered()?;

    let mut task = context.observer::<Task>().handle();
    // A `Task` reference has the wrong target type, so this would not compile:
    // let other_task = context.observer::<Task>().handle();
    // task.started(other_task.as_entity_ref())?;
    task.started(worker.as_entity_ref())?;
    task.ended()?;

    Ok(())
}
#include "quent-tutorial-entity-references-cpp-bridge/gen/quent.hpp"

int main() {
  auto context = quent::Context::none();
  auto worker = context.worker_observer()->handle();
  worker.registered();

  auto task = context.task_observer()->handle();
  task.started(quent::task::Started{.worker = worker.id()});
  task.ended();
  return 0;
}
import quent_tutorial_entity_references as quent


def main() -> None:
    with quent.Context() as context:
        worker = context.worker_observer().handle()
        worker.registered()

        task = context.task_observer().handle()
        task.started(worker=worker)
        task.ended()


if __name__ == "__main__":
    main()
Key point

An entity reference identifies a specific entity and preserves its type.

Check yourself

Which entity type may the worker attribute target?

Does ref: Worker place Task under Worker in a hierarchy?

Full code