Augmentation Functions

In this page, we provide a comprehensive overview of the Augmentation Function framework in WLDT, covering its architecture, implementation, and operational aspects.

The documentation explores the hierarchical structure of the augmentation system, detailing how the Digital Twin Kernel, Augmentation Manager, Function Handlers, and individual Augmentation Functions interact to extend Digital Twin capabilities beyond basic shadowing functions. We examine the two main categories of Augmentation Functions—Stateless and Stateful—explaining their characteristics, lifecycle management, state handling, and typical use cases.

The invocation mechanisms are covered in detail, including direct execution for stateless functions and continuous operation modes (push and polling) for stateful functions, showing how the Digital Twin Model triggers function execution through the Augmentation Manager. We describe how augmentation results are structured using Augmentation Result and how the Digital Twin Model processes these results.

Practical guidance is provided for implementing custom Augmentation Functions, including the base abstract class structure, context handling, result production, and lifecycle callbacks. Finally, we cover the technical details of implementing Augmentation Function Handlers, which manage execution, lifecycle, and result publication, including handler registration, context provisioning, and coordination with the Augmentation Manager.

The documentation is structured in the following subsections:

  • Architecture & Components: Overview of the hierarchical augmentation architecture, including DT Kernel, Augmentation Manager, Function Handlers, and data flow patterns
  • Function Types: Detailed comparison of Stateless and Stateful augmentation functions, their characteristics, execution models, and use cases
  • Handler & Manager: AugmentationFunctionHandler abstract class, DefaultAugmentationFunctionHandler, listener interfaces (AugmentationLifeCycleListener, StatefulAugmentationListener), and AugmentationManager
  • Lifecycle & Registration Flow: Event-driven registration and unregistration lifecycle, synchronization phases, and DTM discovery callbacks
  • Registration API: Programmatic API for obtaining the AugmentationManager, unregistering functions and handlers, and querying registered components
  • Context & Context Request: Description of AugmentationFunctionContext, AugmentationFunctionContextRequest, AugmentationFunctionRequest, and AugmentationFunctionRequestType
  • Discovery Callbacks: DTM callbacks for tracking function availability (onAugmentationNewFunctionAvailable, onAugmentationFunctionUnAvailable, onAugmentationFunctionListAvailable) and when they are invoked
  • Function Invocation: Methods for executing stateless functions and starting/stopping stateful functions from the Digital Twin Model, including all method overloads
  • Result Structure: Format and types of augmentation function results, including AugmentationFunctionResult, AugmentationFunctionResultType, and AugmentationFunctionResultMetrics
  • Error Handling: How augmentation functions report errors using AugmentationFunctionError and AugmentationFunctionErrorType
  • Implementation Guide: Base classes (AugmentationFunction, AugmentationFunctionType), abstract implementations, and practical examples for both stateless and stateful augmentation functions
  • WLDT Event Bus Integration: Internal event classes used by the framework for communication between the DTM, Augmentation Manager, and Handlers
  • Monitoring Integration: How augmentation functions participate in the WLDT monitoring system — automatic injection, the handleMetricsRegistration() hook, and function-level custom metrics

About the code examples: they focus on the augmentation APIs. Where they use logger, the class declares its own private static final WldtLogger logger = WldtLoggerProvider.getLogger(MyClass.class);. Some of them use Map.of(...), that requires Java 9 or later: with Java 8 build the map with a HashMap.

Augmentation Function - Architecture & Components

The WLDT augmentation architecture is organized in a hierarchical structure that enables scalable and modular management of augmentation functions within the Digital Twin Kernel.

aug_function_arch.png

DT Kernel Layer

The DT Kernel contains the core components of the Digital Twin:

  • Model: The Digital Twin Model component that implements the shadowing function and manages the DT state
  • Augmentation Manager: Central coordinator for all augmentation capabilities

The Model and Augmentation Manager interact bidirectionally:

  • Model → Augmentation Manager: Sends queries and uses augmentation functions
  • Augmentation Manager → Model: Returns results from augmentation functions

Augmentation Manager

The Augmentation Manager acts as the central orchestrator, providing:

  • Function Handler Management: Manages multiple Augmentation Function Handlers ([1], [...], [n])
  • Result Aggregation: Collects results from all handlers and forwards them to the Model
  • Lifecycle Control: Coordinates the lifecycle of all registered augmentation functions

Interactions:

  • Manages: Controls multiple Augmentation Function Handlers
  • Results: Receives results from handlers and publishes them to the Model

Augmentation Function Handler Layer

Multiple Augmentation Function Handlers ([1], [...], [n]) exist, each responsible for managing one or more augmentation functions.

Each handler:

  • Manages: Controls the execution and lifecycle of its assigned functions (both stateless and stateful)
  • Handles: Coordinates execution requests and context provisioning
  • Results: Collects and forwards results to the Augmentation Manager

Function Layer

The bottom layer contains the actual Augmentation Functions (f1, f2, etc.):

  • Function f1: Represents stateless or stateful augmentation functions
  • Function f2: Represents stateful augmentation functions with internal loops
  • Multiple instances of the same function type can exist across different handlers

Interactions:

  • Results: Functions produce results that flow upward through their handler
  • Handles: Functions are controlled and invoked by their respective handlers

Data Flow

Downward Flow (Control & Context)

  1. Model sends queries and invocation requests to Augmentation Manager
  2. Augmentation Manager delegates to appropriate Function Handlers
  3. Function Handlers invoke and manage Functions with context provisioning

Upward Flow (Results)

  1. Functions produce results and send them to their Function Handler
  2. Function Handlers forward results to the Augmentation Manager
  3. Augmentation Manager aggregates and delivers results to the Model

Augmentation Function Types

Augmentation Functions in WLDT are distinguished into two main types based on internal state management: Stateless and Stateful. Both types interact with the Augmentation Function Handler (implemented by the Augmentation Manager), which coordinates execution, lifecycle management, and result publication.

aug_function_types.png

Stateless Function

Stateless Functions (e.g., f1) are functions that do not maintain memory between successive executions. Each invocation is completely independent from previous ones.

Characteristics:

  • Execution: Invoked via execute call from the Digital Twin Model through the Handler
  • Lifecycle: No start/stop management required
  • State: No persistent internal state
  • Output: Produce a single list of results per execution
  • Use Cases: Ideal for instantaneous calculations, validations, threshold checking

Flow:

  1. The Digital Twin Model sends an execution request for the target Augmentation Function
  2. The associated Handler receives the request and invokes execute on the stateless function with the target context
  3. The function computes the result based only on the received context
  4. The result is immediately returned to the Handler
  5. The Handler publishes the result (results upward)
  6. The result is received by the Digital Twin Model

Stateful Function

Stateful Functions (e.g., f2) maintain internal state between successive executions, accumulating a history of results (result 1, result [...], result n).

Characteristics:

  • Execution: Managed via start/stop triggered from the model and managed by the Handler
  • Lifecycle: Continuous lifecycle with explicit initialization and termination
  • State: Maintain history of results and/or internal state
  • Output: Produce results asynchronously and continuously
  • Update: Receives automatic updates from the Digital Twin core associated to new State and/or Notification events in order to be used for its internal computation
  • Use Cases: Ideal for pattern recognition, trend analysis, adaptive AI models

Flow:

  1. The Digital Twin Model sends a start request for the target Augmentation Function
  2. The Handler receives the request and invokes start on the stateful function
  3. The function initializes its internal state and starts the function loop
  4. The function will receive:
    • New Digital Twin State
    • Digital Twin Event Notifications
  5. The function can also send requests for a context computation update
  6. Results are produced according to the function logic (result 1, result [...], result n)
  7. The Handler receives and publishes results as they are generated
  8. When necessary, the Model through the Handler can invoke stop to terminate the function

Important Note: Result production is not necessarily synchronized with the reception of new contexts or notifications. The function autonomously decides when and how to generate output based on its internal logic.


Augmentation Function Handler

The Augmentation Function Handler acts as a central coordinator for all augmentation functions, providing:

  • Execution Management: Invocation of execute for stateless, management of start/stop for stateful
  • Context Provisioning: Construction and provision of AugmentationFunctionContext to functions
  • Result Management: Collection and publication of results to the Digital Twin Model
  • Lifecycle Management: Control of the lifecycle of stateful functions

The Handler abstracts the complexity of managing different function types, offering a uniform interface to the Digital Twin Model.


AugmentationFunctionHandler

AugmentationFunctionHandler is the abstract base class for all augmentation function handlers. It extends DigitalTwinWorker and implements StatefulAugmentationListener, WldtEventListener, and AugmentationLifeCycleListener, providing the common infrastructure for function registration, event subscription, and result publication.

Public Registration Methods

// Register an augmentation function in this handler
public void registerAugmentationFunction(AugmentationFunction augmentationFunction)
    throws AugmentationFunctionException

// Unregister a registered function by its id
public void unRegisterAugmentationFunction(String augmentationFunctionId)
    throws AugmentationFunctionException

// Retrieve a single registered function by its id
public Optional<AugmentationFunction> getAugmentationFunction(String augmentationFunctionId)

// Retrieve all registered functions
public List<AugmentationFunction> getAllAugmentationFunctions()

Abstract Methods

When creating a custom handler by extending AugmentationFunctionHandler, the following abstract methods must be implemented:

// Called after a function is added to the handler's internal map
protected abstract void handleAugmentationFunctionRegistration(AugmentationFunction function)
    throws AugmentationFunctionException;

// Called after a function is removed from the handler's internal map
protected abstract void handleAugmentationFunctionUnRegistration(String augmentationFunctionId)
    throws AugmentationFunctionException;

// Called to start a stateful function — should invoke function.start(request)
protected abstract void handleAugmentationFunctionStart(StatefulAugmentationFunction function,
                                                        AugmentationFunctionRequest request)
    throws AugmentationFunctionException;

// Called to stop a running stateful function — should invoke function.stop(request)
protected abstract void handleAugmentationFunctionStop(StatefulAugmentationFunction function,
                                                       AugmentationFunctionRequest request)
    throws AugmentationFunctionException;

// Called to execute a stateless function — should invoke function.run(request) and return results
protected abstract AugmentationFunctionResultList handleAugmentationFunctionExecution(
    StatelessAugmentationFunction function, AugmentationFunctionRequest request)
    throws AugmentationFunctionException;

// Called to deliver a refreshed query result to a running stateful function
protected abstract void handleAugmentationFunctionQueryResultRefresh(
    StatefulAugmentationFunction function, Query query, QueryResult<?> queryResult)
    throws AugmentationFunctionException;

The two state callbacks are called once for each running stateful function and the library has already applied the filters of that function (State Filters): the function is skipped if it does not observe the state, the description is always complete, and the values are delivered only if the trigger filter accepts at least one change and they are already limited by the content filter. A custom handler only decides how the update is delivered:

// Called for each running stateful function that observes the state: the description is complete and never filtered
protected abstract void onStateDescriptionUpdate(StatefulAugmentationFunction function,
                                                 DigitalTwinStateDescription description,
                                                 List<DigitalTwinStateChange> changes)
    throws AugmentationFunctionException;

// Called for each running stateful function whose filters accept the update: the values are already filtered
protected abstract void onStateValuesUpdate(StatefulAugmentationFunction function,
                                            DigitalTwinStateValues filteredValues,
                                            List<DigitalTwinStateChange> changes)
    throws AugmentationFunctionException;

The remaining abstract methods deliver the event notifications and the life cycle of the handler and of the Digital Twin:

// Called with the running stateful functions that observe the event notifications and accept the event
protected abstract void onEventNotificationReceived(ArrayList<StatefulAugmentationFunction> functions,
                                                    DigitalTwinStateEventNotification<?> notification);

// Handler start / stop
public abstract void onManagerStart();
public abstract void onManagerStop();

// Digital Twin life cycle
public abstract void onDigitalTwinLifeCycleCreate();
public abstract void onDigitalTwinLifeCycleStart();
public abstract void onDigitalTwinLifeCycleBound();
public abstract void onDigitalTwinLifeCycleUnBound();
public abstract void onDigitalTwinLifeCycleSync(DigitalTwinStateDescription description, DigitalTwinStateValues values);
public abstract void onDigitalTwinLifeCycleUnSync(DigitalTwinStateDescription description, DigitalTwinStateValues values);
public abstract void onDigitalTwinLifeCycleStop();
public abstract void onDigitalTwinLifeCycleDestroy();

A custom handler can read the complete local copy of the state with the protected methods getDigitalTwinStateDescription() and getDigitalTwinStateValues().

Note: For most use cases, DefaultAugmentationFunctionHandler is sufficient. Extend AugmentationFunctionHandler only when you need custom execution logic, instrumentation, filtering, or specialized lifecycle management.


DefaultAugmentationFunctionHandler

DefaultAugmentationFunctionHandler is the ready-to-use concrete implementation of AugmentationFunctionHandler. It provides standard handling for all lifecycle operations—registration, unregistration, stateless execution, and stateful start/stop—and automatically forwards DT state updates and event notifications to the running stateful functions, applying the optional trigger and content filters (done by the abstract AugmentationFunctionHandler) defined in each function’s AugmentationFunctionContextRequest (see State Filters); without filters every update is forwarded with the complete state.

Developers can use it directly without writing any custom handler code:

// Create a handler with a unique ID
DefaultAugmentationFunctionHandler handler = new DefaultAugmentationFunctionHandler("my-handler-id");

// Register one or more augmentation functions
handler.registerAugmentationFunction(new MyStatelessFunction());
handler.registerAugmentationFunction(new MyStatefulFunction());

// Add the handler to the AugmentationManager
augmentationManager.addAugmentationFunctionHandler(handler);

For more advanced scenarios—custom execution logic, filtering, instrumentation, or specialized lifecycle management—extend AugmentationFunctionHandler directly and override only the abstract methods needed.


Handler Listener Interfaces

The augmentation framework defines two listener interfaces that decouple augmentation functions from their handlers, enabling a clean publish-subscribe communication pattern.

AugmentationLifeCycleListener

AugmentationLifeCycleListener is a focused subset of the WLDT LifeCycleListener tailored for augmentation components. AugmentationFunctionHandler implements this interface and receives relevant DT lifecycle events from the AugmentationManager, without handling the full granularity of the core DT lifecycle.

public interface AugmentationLifeCycleListener {
    void onCreate();
    void onStart();
    void onDigitalTwinBound();
    void onDigitalTwinUnBound();
    void onSync(DigitalTwinStateDescription digitalTwinStateDescription, DigitalTwinStateValues digitalTwinStateValues);
    void onUnSync(DigitalTwinStateDescription digitalTwinStateDescription, DigitalTwinStateValues digitalTwinStateValues);
    void onStop();
    void onDestroy();
}
CallbackWhen it is invoked
onCreate()The Digital Twin instance is created
onStart()The Digital Twin starts its lifecycle. It is notified before the Physical and Digital Adapters are executed, so it always precedes the binding of the adapters
onDigitalTwinBound()The DT has successfully bound to all Physical Adapters
onDigitalTwinUnBound()The DT has unbound from Physical Adapters
onSync(description, values)The DT reaches the Synchronized state (the current description and values of the state are provided)
onUnSync(description, values)The DT exits the Synchronized state (the last known description and values are provided)
onStop()The Digital Twin stops
onDestroy()The Digital Twin instance is destroyed

StatefulAugmentationListener

StatefulAugmentationListener is implemented by AugmentationFunctionHandler and is automatically set on each registered stateful function. It receives results (including embedded errors) and query refresh requests from stateful functions.

public interface StatefulAugmentationListener {

    // Called when the stateful function produces new results (or reports an error)
    void onStatefulAugmentationFunctionResult(String augmentationFunctionId,
                                              AugmentationFunctionResultList augmentationFunctionResultList);

    // Called when the stateful function requests a query result refresh from DT storage
    void onStatefulAugmentationFunctionQueryResultRefresh(String augmentationFunctionId,
                                                         Query query);
}

Note: Since v0.7.1, functions report errors by embedding them in an AugmentationFunctionResultList, so this listener has no separate error method. When a result list has an error, the handler publishes only the error, that reaches the Digital Twin Model through onAugmentationFunctionError() (see Error Handling). This interface is part of the internal communication channel between functions and their handlers — implemented by AugmentationFunctionHandler and set automatically; developers do not typically implement it directly.


AugmentationManager

AugmentationManager is the top-level coordinator for all augmentation capabilities within a Digital Twin instance. It manages a collection of AugmentationFunctionHandler instances, propagates DT lifecycle events to each handler, and runs each handler in its own thread via an internal ExecutorService.

The AugmentationManager is created automatically by the WLDT engine for each Digital Twin and is accessible via DigitalTwin.getAugmentationManager().

Key Methods

// Add a handler. Add it before the DT is started: the handlers are executed when the DT starts,
// a handler added later is not executed
public void addAugmentationFunctionHandler(AugmentationFunctionHandler handler)
    throws AugmentationFunctionException, WldtWorkerException

// Remove a handler by its id
public void removeAugmentationFunctionHandler(String handlerId)
    throws AugmentationFunctionException

// Retrieve the map of all registered handlers (id → handler)
public Map<String, AugmentationFunctionHandler> getAugmentationFunctionHandlerMap()

Thread Pool Limit

⚠️ Warning: The AugmentationManager caps the number of concurrently registered handlers at 5. Attempting to add more throws an AugmentationFunctionException. Design your handler topology accordingly.

For a complete setup example, see Augmentation Function Registration API.


Registering and Unregistering Augmentation Functions

aug_function_registration.png

This section describes the interaction procedure between a Digital Twin and its Augmentation Functions, covering registration, lifecycle synchronization, and dynamic updates with augmentation functions registered and unregistered at runtime as illustrated in the following sequence diagram:

augmentation_function_registration.png

The procedure is designed around loose coupling and event-driven reactivity. The Digital Twin Model never polls for changes; instead, it subscribes once to the Event Bus and reacts to events as they occur. The catch-up performed when the model starts guarantees consistency between the DTM state and any registrations that happened before the Digital Twin was active.

The main involved phases are:

Phase 1 — Pre-Start Registration

Before the Digital Twin is started, the Developer retrieves an AugmentationFunctionHandler from the AugmentationManager and registers the first Augmentation Function. The handler publishes a RegistrationEvent on the Event Bus, but since no subscribers are active yet, the event is silently ignored.

Phase 2 — Digital Twin Start

The Developer starts the Digital Twin, which starts the Digital Twin Model. At its start, the DTM subscribes to RegistrationEvent and UnregistrationEvent notifications on the Event Bus, making it ready to react to future changes.

Right after its onStart() callback, the DTM performs a catch-up procedure: it queries the Augmentation Manager to retrieve all registered handlers, then fetches the list of already-registered Augmentation Functions from each handler, invoking onAugmentationFunctionListAvailable() once for each handler. This ensures that any functions registered before the Digital Twin started are properly acknowledged.

Phase 3 — Dynamic Registration by the Developer

Once the system is running, the Developer can register additional Augmentation Functions at any time. When a new function is registered on the handler, a RegistrationEvent is published on the Event Bus and the DTM receives it immediately, keeping its internal state up to date without requiring a restart.

Phase 4 — Dynamic Unregistration by the Developer

Augmentation Functions can also be removed at runtime. When the Developer unregisters a function, the handler publishes an UnregistrationEvent on the Event Bus. The DTM receives the notification and updates its internal state, ensuring it no longer references the removed function.

Augmentation Function Registration API

The registration API allows developers to add, remove, and retrieve augmentation functions and handlers at any point during the Digital Twin lifecycle. For the basic setup pattern (create handler → register functions → add to manager) see DefaultAugmentationFunctionHandler.


Obtaining the AugmentationManager

The AugmentationManager is created automatically by the WLDT engine for each DigitalTwin instance and is exposed via getAugmentationManager():

DigitalTwin digitalTwin = new DigitalTwin("my-dt-id", new MyDigitalTwinModel());

AugmentationManager augmentationManager = digitalTwin.getAugmentationManager();

Unregistering Functions and Handlers

Individual functions can be removed from a handler at runtime without stopping the handler:

// Remove a specific function from its handler
handler.unRegisterAugmentationFunction(MyStatelessFunction.FUNCTION_ID);

An entire handler can be removed from the manager:

// Remove a handler and all its functions
augmentationManager.removeAugmentationFunctionHandler("sensor-handler");

Querying Registered Handlers and Functions

// Retrieve a specific handler by id
Optional<AugmentationFunctionHandler> h =
    augmentationManager.getAugmentationFunctionHandler("sensor-handler");

// Retrieve all registered handlers
List<AugmentationFunctionHandler> allHandlers =
    augmentationManager.getAllAugmentationFunctionHandlers();

// Find a function by id across all handlers (returns a map handlerId → function)
Map<String, AugmentationFunction> found =
    augmentationManager.getAugmentationFunctionWithId(MyStatelessFunction.FUNCTION_ID);

// Retrieve a function from a specific handler
Optional<AugmentationFunction> fn =
    handler.getAugmentationFunction(MyStatelessFunction.FUNCTION_ID);

// Retrieve all functions registered on a handler
List<AugmentationFunction> allFunctions = handler.getAllAugmentationFunctions();

Augmentation Function Context & Context Request

The Augmentation Function framework provides two key classes for managing the data and configuration that augmentation functions need during execution: AugmentationFunctionContext and AugmentationFunctionContextRequest.


AugmentationFunctionContext

The AugmentationFunctionContext encapsulates the runtime data provided to an augmentation function during execution. It contains the current Digital Twin state (its description and its values) and optional query results from the DT storage.

Structure

public class AugmentationFunctionContext {
    private DigitalTwinStateDescription digitalTwinStateDescription;
    private DigitalTwinStateValues digitalTwinStateValues;
    private QueryResult<?> queryResult;
}

Fields

FieldTypeDescription
digitalTwinStateDescriptionDigitalTwinStateDescriptionThe structure of the Digital Twin State at the time of execution. It is always complete: the filters of the context request never apply to the description
digitalTwinStateValuesDigitalTwinStateValuesThe values of the Digital Twin State at the time of execution: all the values, or only the resources selected by the content filters of the context request (see State Filters)
queryResultQueryResult<?>Results from storage queries defined in the AugmentationFunctionContextRequest (optional)

Constructors

// Default constructor
public AugmentationFunctionContext()

// Constructor with state only
public AugmentationFunctionContext(DigitalTwinStateDescription digitalTwinStateDescription, DigitalTwinStateValues digitalTwinStateValues)

// Constructor with state and query results
public AugmentationFunctionContext(DigitalTwinStateDescription digitalTwinStateDescription, DigitalTwinStateValues digitalTwinStateValues, QueryResult<?> queryResult)

Usage in Functions

The context is provided to augmentation functions automatically:

  • Stateless Functions: Received as a parameter in the run() method
  • Stateful Functions: Received during start() initialization and can be requested for updates during execution

AugmentationFunctionContextRequest

The AugmentationFunctionContextRequest defines what data and notifications an augmentation function wants to receive during its lifecycle. It acts as a configuration that the Augmentation Manager and the associated Handler use to prepare the appropriate context.

Structure

public class AugmentationFunctionContextRequest {
    private boolean observeState;
    private boolean observeEventNotifications;
    private DigitalTwinStateFilter stateFilter;     // content filter, default = accepts everything
    private List<DigitalTwinStateChangeFilter> stateChangeFilters;   // trigger filter, null/empty = every update
    private Query query;
    private boolean refreshQueryOnStateUpdate;   // default false
}

Fields

FieldTypeDefaultDescription
observeStatebooleantrueWhether the function wants to observe DT state changes
observeEventNotificationsbooleantrueWhether the function wants to observe DT event notifications
stateFilterDigitalTwinStateFilterDigitalTwinStateFilter.all()Content filter by name (properties, actions, events, relationships; per category null = all, empty = none). The event names also select the event notifications delivered
stateChangeFiltersList<DigitalTwinStateChangeFilter>nullTrigger filter (stateful only): a state update is pushed only if one of its changes matches one of the rules (null/empty = every update)
queryQuerynullQuery to execute on DT storage for historical data (see Storage & Query System)
refreshQueryOnStateUpdatebooleanfalseIf true the query is executed and its result delivered to the function before every state update (stateful functions only, see below)

observeState: false stops state updates being pushed to a stateful function and leaves the state out of the start/stop/execute context; observeEventNotifications: false stops event notifications being pushed.

State Filters

Both filters are optional and disabled by default: with no filter the function receives every values update with the complete values. They apply only to the values of the state: the description (the structure) is never filtered, and a stateful function is notified of every change of the description.

1. Trigger filter (stateChangeFilters, stateful functions only) — decides when a values update is pushed. Each DigitalTwinStateChangeFilter rule has optional criteria on operation, resource type and resource name (empty criterion = wildcard, non-empty criteria are AND-ed). The update is pushed if any change of the update matches any rule (and, if a content filter is also defined, concerns an accepted resource, see below).

2. Content filter (stateFilter, a DigitalTwinStateFilter, stateful and stateless) — decides what is delivered. The function receives the DigitalTwinStateValues containing only the listed resources (onStateValuesUpdate(DigitalTwinStateValues) and the context of start / stop / execute). Each category is independent: null keeps it all, an empty list removes it. The event names of the filter also drop the event notifications whose name is not listed.

AugmentationFunctionContextRequest request = new AugmentationFunctionContextRequest()
        // push only when the value of "temperature" or "humidity" is updated...
        .withStateChangeFilters(
                DigitalTwinStateChangeFilter.onPropertyValueUpdate("temperature", "humidity"),
                DigitalTwinStateChangeFilter.builder()
                        .operations(DigitalTwinStateChange.Operation.OPERATION_REMOVE)
                        .resourceTypes(DigitalTwinStateChange.ResourceType.RELATIONSHIP_INSTANCE)
                        .build())
        // ...and receive only those two properties and no action
        .withStateFilter(DigitalTwinStateFilter.all()
                .withPropertyNames("temperature", "humidity")
                .withActionNames());

The content filter has a double role. Besides selecting what is delivered, for stateful functions it also decides when: with a content filter, a state update is pushed only if at least one of its changes concerns an accepted resource. When both filters are defined, the same change must satisfy both (a change matching only the trigger filter plus another one matching only the content filter do not push the update). Examples:

FiltersonStateValuesUpdate is invoked when
noneevery state update
trigger only (OPERATION_UPDATE_VALUE on property values)any property value update, complete values delivered
content only (withStateFilter(DigitalTwinStateFilter.all().withPropertyNames("temperature", "humidity")))a change concerns temperature, humidity or any other category left unset (unset = accepts all)
botha value update on temperature or on humidity (each one alone is enough)

Without change information (null/empty change list) the update is pushed only if no trigger filter is defined. Stateless functions are request-driven, so for them the content filter only restricts the state in the context.

The content filter is the generic DigitalTwinStateFilter (package it.wldt.core.state), the same class used by the Digital Adapter and by DigitalTwinStateValues.filter(filter): request.getStateFilter() returns it. DigitalTwinStateChangeFilter lives in the same package. A DigitalTwinStateChangeFilter rule is a cross product: inside each criterion (operations, resource types, names) the values are alternatives (OR) and the criteria are AND-ed, so {OPERATION_UPDATE_VALUE} x {PROPERTY_VALUE, EVENT} x {"temperature", "alarm"} accepts any combination of the three sets (names are not tied to a resource type); use several rules to pair a type with a name exactly. The delivered filtered values keep the evaluationInstant and the description id of the complete values. The trigger filter does not apply to event notifications (use the event names of the content filter).

The two filters, together with the WldtEventFilter (event types of an event bus subscription) and the query Filter (conditions of a storage query), are four unrelated concepts that share the word “filter”.

A function that needs the structure of the state receives it, always complete, in onStateDescriptionUpdate(DigitalTwinStateDescription) and in the context (getDigitalTwinStateDescription()); the values are in onStateValuesUpdate(DigitalTwinStateValues) and in the context (getDigitalTwinStateValues()), already filtered. The values carry the id of the description they belong to (getDescriptionId()).

The handler keeps a complete, never filtered, local copy of the description and of the values, updated with the description and values events of the event bus (see Digital Twin State events on the event bus): the filters are applied for each function only when the function is notified. A function without filters receives, at every values update, the complete values (not just the delta carried by the event) and, at every description update, the complete new description.

The handler also applies the event notification filters (observeEventNotifications and the event names of the content filter) before calling the handler hook onEventNotificationReceived(functions, notification), which receives only the functions that must be notified: a custom handler does not need to repeat them. The state hooks run on the event bus thread of the handler while holding the monitor of the local copy of the state, so a slow hook, or one that keeps a lock needed by other threads, blocks the arrival of the following state events.

Constructors

// Default constructor: observes state and events, no query
public AugmentationFunctionContextRequest()

// Constructor with query only
public AugmentationFunctionContextRequest(Query query)

// Constructor with observation flags and query
public AugmentationFunctionContextRequest(boolean observeState,
                                         boolean observeEventNotifications,
                                         Query query)

Filters are set with the fluent methods withStateChangeFilters(...) and withStateFilter(...), and can be inspected with hasTriggerFilter() / hasContentFilter(). The request offers also the methods the handler uses to apply them: shouldTrigger(changes), acceptsEventNotification(notification) and filterValues(values).

Usage in Function Constructors

The context request is typically configured once in the function’s constructor and tells the framework what data to provide at runtime.

// Stateless function that needs the last 10 minutes of DT state history
public class HistoryAwareStatelessFunction extends StatelessAugmentationFunction {

    public HistoryAwareStatelessFunction() {
        // ⚠️ Warning: timestamps are computed once at construction time and stay fixed.
        // A Query is immutable: for a truly sliding "last 10 minutes" window, build a new Query
        // with fresh timestamps and pass it to refreshQueryResult(Query) at each invocation
        // (available in stateful functions).
        super("history-fn", "History Function", "Uses DT state history", "1.0.0",
              new AugmentationFunctionContextRequest(
                  Query.on(QueryResourceType.DIGITAL_TWIN_STATE)
                      .where(and(gte(QueryFieldNames.TIMESTAMP, System.currentTimeMillis() - 600_000),
                                 lte(QueryFieldNames.TIMESTAMP, System.currentTimeMillis())))
                      .build()
              ));
    }

    @Override
    protected AugmentationFunctionResultList run(AugmentationFunctionRequest request)
            throws AugmentationFunctionException {
        // The result of the query defined in the context request: a single DigitalTwinStateHistoryRecord
        // that maps every description (by description id) to the values stored for it in the interval
        QueryResult<?> history = request.getContext().getQueryResult();
        if (history != null && history.isSuccessful() && !history.getItems().isEmpty()) {
            DigitalTwinStateHistoryRecord states = history.as(DigitalTwinStateHistoryRecord.class).first();
            for (DigitalTwinStateHistoryEntry entry : states.getEntries().values()) {
                // entry.getDescription() is the structure, entry.getValuesList() the values stored for it
            }
        }
        return new AugmentationFunctionResultList();
    }
}

// Stateful function that only observes state changes (no event notifications needed)
public class StateOnlyStatefulFunction extends StatefulAugmentationFunction {

    public StateOnlyStatefulFunction() {
        super("state-only-fn", "State Only Function", "Observes state only", "1.0.0",
              new AugmentationFunctionContextRequest(true, false, null));
    }
    // ...
}

Query refresh on every state update (opt-in)

A stateful function that has a Query in its context request receives the result of that query when it is started, and then only when a refresh is requested. By default a Digital Twin state update does not execute any query: the function receives the new values in onStateValuesUpdate() and decides by itself if and when it needs fresh data from the storage. A refresh can be requested in three ways:

  • By the function (recommended): the function calls refreshQueryResult() (re-executes the query of the context request) or refreshQueryResult(Query) (executes a new query, e.g., a sliding time window) whenever it needs it, for example inside onStateValuesUpdate() only when a condition is met, or from a timer. The result arrives in onQueryResultRefresh().
  • By the Digital Twin Model (shadowing function): refreshAugmentationFunctionQueryResult(augmentationFunctionHandlerId, augmentationFunctionId) triggers the execution of the query of the context request of a stateful function.
  • Automatically on every state update (opt-in): when refreshQueryOnStateUpdate is set to true in the context request, the handler executes the query and calls onQueryResultRefresh() before calling onStateValuesUpdate() for each values update, so the function always sees fresh history together with the new values.
public class LastHourAverageFunction extends StatefulAugmentationFunction {

    private static final WldtLogger logger = WldtLoggerProvider.getLogger(LastHourAverageFunction.class);

    public LastHourAverageFunction() {
        super("last-hour-average", "Last Hour Average", "Average of the temperature on every state update", "1.0.0",
              buildContextRequest());
    }

    private static AugmentationFunctionContextRequest buildContextRequest() {
        // The query is executed by the handler before every state update
        AugmentationFunctionContextRequest contextRequest = new AugmentationFunctionContextRequest(
                Query.on(QueryResourceType.PHYSICAL_ASSET_PROPERTY_VARIATION)
                        .where(eq(QueryFieldNames.PROPERTY_KEY, "temperature"))
                        .aggregate(avg(QueryFieldNames.BODY).as("avg"))
                        .build());

        // Disabled by default
        contextRequest.setRefreshQueryOnStateUpdate(true);
        return contextRequest;
    }

    @Override
    public void onQueryResultRefresh(Query query, QueryResult<?> queryResult) throws AugmentationFunctionException {
        // Called first: the aggregation result is a single AggregationRecord
        if (queryResult != null && queryResult.isSuccessful())
            logger.info("Average temperature: {}", queryResult.as(AggregationRecord.class).first().getDouble("avg"));
    }

    @Override
    public void onStateDescriptionUpdate(DigitalTwinStateDescription digitalTwinStateDescription) throws AugmentationFunctionException {
        // The structure of the state changed (never filtered, no query refresh)
    }

    @Override
    public void onStateValuesUpdate(DigitalTwinStateValues digitalTwinStateValues) throws AugmentationFunctionException {
        // Called right after onQueryResultRefresh() for the same values update
    }

    // start(), stop() and onEventNotificationReceived() omitted
}

⚠️ Warning: the query is executed synchronously on the handler thread for every state update, so a heavy query slows down the processing of all the functions of that handler and loads the storage (that executes queries and writes on the same thread, see Threading and performance). Moreover, if the results of the function modify the Digital Twin state (e.g., through the shadowing function), each result produces a new state update that executes the query again: avoid this feedback loop, or enable the option only for light and bounded queries (filters, limit, aggregations). The handler waits for the result with the default timeout of the QueryExecutor (5 seconds): a query that takes longer returns a failed QueryResult, delivered to onQueryResultRefresh() as any other result.


AugmentationFunctionRequest

AugmentationFunctionRequest is the runtime request object passed to every augmentation function lifecycle method (run(), start(), stop()). It bundles an AugmentationFunctionContext with metadata identifying the specific invocation.

Structure

public class AugmentationFunctionRequest {
    private final String requestId;
    private final AugmentationFunctionContext context;
    private final AugmentationFunctionRequestType type;
    private final Long timestamp;
}

Fields

FieldTypeDescription
requestIdStringAuto-generated UUID uniquely identifying this request
contextAugmentationFunctionContextRuntime context (current DT state + optional query results)
typeAugmentationFunctionRequestTypeThe type of operation requested (EXECUTE, START, or STOP)
timestampLongMilliseconds since epoch when the request was created

Constructors

// Full constructor with explicit timestamp
public AugmentationFunctionRequest(String requestId,
                                   AugmentationFunctionContext context,
                                   AugmentationFunctionRequestType requestType,
                                   Long timestamp)

// Constructor with automatic timestamp (timestamp = now)
public AugmentationFunctionRequest(String requestId,
                                   AugmentationFunctionContext context,
                                   AugmentationFunctionRequestType requestType)

Accessing Request Data Inside a Function

@Override
public AugmentationFunctionResultList run(AugmentationFunctionRequest request)
        throws AugmentationFunctionException {

    // Retrieve the request type
    AugmentationFunctionRequestType type = request.getType(); // EXECUTE

    // Retrieve the context from the request
    AugmentationFunctionContext context = request.getContext();

    // Access the current DT state: the structure (always complete) and the values (limited by the content filters, if any)
    DigitalTwinStateDescription description = context.getDigitalTwinStateDescription();
    DigitalTwinStateValues values = context.getDigitalTwinStateValues();

    // Access optional query results from DT storage
    QueryResult<?> queryResult = context.getQueryResult();

    // ...
    return new AugmentationFunctionResultList();
}

AugmentationFunctionRequestType

AugmentationFunctionRequestType is an enum that identifies which operation triggered the invocation of an augmentation function.

public enum AugmentationFunctionRequestType {
    EXECUTE,   // Stateless function execution request
    START,     // Stateful function start request
    STOP       // Stateful function stop request
}
ValueTriggered byTarget
EXECUTEexecuteAugmentationFunction() called from the Digital Twin ModelStateless functions
STARTstartAugmentationFunction() called from the Digital Twin ModelStateful functions
STOPstopAugmentationFunction() called from the Digital Twin ModelStateful functions

Context Lifecycle: Stateless vs Stateful

The behavior of context and context request differs significantly between stateless and stateful augmentation functions.

Stateless Functions: Single-Use Context

For stateless functions, the context and context request are used only once per execution:

  1. Context Request Definition: The function defines its context requirements (typically in the descriptor or during registration)
  2. Execution Invocation: When executeAugmentationFunction() is called from the Digital Twin Model
  3. Context Preparation: The Augmentation Manager builds the context based on the request (current state + optional query results)
  4. Single Execution: The run() method receives the context, executes, and returns results
  5. Context Disposal: The context is discarded after execution completes

Example:

public class StatelessPredictionFunction extends StatelessAugmentationFunction {
    
    public StatelessPredictionFunction() {
        super("prediction-fn", "Prediction Function", "Predicts from state and history", "1.0.0");
    }
    
    @Override
    public AugmentationFunctionResultList run(AugmentationFunctionRequest request)
            throws AugmentationFunctionException {
        // Context used once per execution
        DigitalTwinStateValues currentValues = request.getContext().getDigitalTwinStateValues();
        QueryResult<?> history = request.getContext().getQueryResult();
        
        // Perform prediction using current state and history
        // Return results
        return new AugmentationFunctionResultList();
    }
}

Each time the function is executed, a fresh context is prepared with the latest state and query results.


Stateful Functions: Initial Context + Update Requests

For stateful functions, the context lifecycle is more complex:

  1. Context Request Definition: The function defines its initial context requirements
  2. Start Invocation: When startAugmentationFunction() is called
  3. Initial Context: The start() method receives an initial context built from the context request
  4. Runtime Updates: The function automatically receives onStateDescriptionUpdate(), onStateValuesUpdate() and onEventNotificationReceived() callbacks with fresh state/events. By default every values update is pushed with the complete values; the optional State Filters of the context request restrict which part of the values is delivered and, together, when an update is pushed (an update is pushed only if at least one change satisfies both the trigger filter and the content filter; the same content filters also shape the initial context). The description is never filtered
  5. Context Update Request: The function can explicitly request context updates when needed
  6. Stop: The stop() method may receive a final context

Example: Push Mode (Automatic Updates)

public class StatefulAnomalyDetector extends StatefulAugmentationFunction {

    private static final WldtLogger logger = WldtLoggerProvider.getLogger(StatefulAnomalyDetector.class);
    
    private List<Double> temperatureHistory = new ArrayList<>();
    
    public StatefulAnomalyDetector() {
        super("anomaly-detector", "Anomaly Detector", "Detects temperature anomalies", "1.0.0");
    }
    
    @Override
    public void start(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        // Receive initial context at startup
        DigitalTwinStateValues initialValues = request.getContext().getDigitalTwinStateValues();
        
        // Initialize with current temperature
        if (initialValues.getPropertyValues().containsKey("temperature"))
            temperatureHistory.add((Double) initialValues.getPropertyValue("temperature"));
        
        logger.info("Started with initial temperature history: {}", temperatureHistory);
    }
    
    @Override
    public void onStateDescriptionUpdate(DigitalTwinStateDescription newDescription) {
        // The structure of the state changed: e.g., a property has been added or removed
    }

    @Override
    public void onStateValuesUpdate(DigitalTwinStateValues newValues) {
        // Automatically receive values updates (no explicit context request needed)
        if (newValues.getPropertyValues().containsKey("temperature"))
            Optional.ofNullable(newValues.getPropertyValue("temperature"))
            .ifPresent(value -> {
                double temp = (Double) value;
                temperatureHistory.add(temp);
                
                // Detect anomaly
                if (isAnomaly(temp)) {
                    notifyResult(createAnomalyResult(temp));
                }
            });
    }
    
    @Override
    public void stop(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        logger.info("Stopped after processing {} temperature readings", 
                    temperatureHistory.size());
    }
    
    @Override
    public void onEventNotificationReceived(DigitalTwinStateEventNotification<?> notification) {
        // Not used by this function
    }
    
    @Override
    public void onQueryResultRefresh(Query query, QueryResult<?> queryResult) {
        // This function does not use storage queries
    }
    
    private boolean isAnomaly(double temperature) {
        return temperature > 50.0;
    }
    
    private AugmentationFunctionResultList createAnomalyResult(double temperature) {
        return new AugmentationFunctionResultList(new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.EVENT_RESULT, "temperature_anomaly", temperature, null, null));
    }
}

Example: Polling Mode (Explicit Context Requests)

A stateful function that periodically requests a fresh snapshot of historical data from DT storage using refreshQueryResult() and receives the result asynchronously via onQueryResultRefresh():

public class StatefulPollingAugmentationFunction extends StatefulAugmentationFunction {

    private static final WldtLogger logger = WldtLoggerProvider.getLogger(StatefulPollingAugmentationFunction.class);

    public static final String FUNCTION_ID = "polling-augmentation-function";
    private static final long POLLING_INTERVAL_MS = 5000;

    private Timer pollingTimer;

    public StatefulPollingAugmentationFunction() {
        super(FUNCTION_ID,
              "Polling Augmentation Function",
              "Periodically queries DT storage for the last 60 seconds of state history",
              "1.0.0",
              // The initial query of the context request: the last 60 seconds at construction time.
              // A Query is immutable, so the window is slid at each polling cycle by building a new Query
              // inside the TimerTask and passing it to refreshQueryResult(Query).
              new AugmentationFunctionContextRequest(lastSecondsQuery(60)));
    }

    private static Query lastSecondsQuery(int seconds) {
        long now = System.currentTimeMillis();
        return Query.on(QueryResourceType.DIGITAL_TWIN_STATE)
                .where(and(gte(QueryFieldNames.TIMESTAMP, now - seconds * 1000L), lte(QueryFieldNames.TIMESTAMP, now)))
                .build();
    }

    @Override
    public void start(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        logger.info("Starting polling function, interval: {} ms", POLLING_INTERVAL_MS);

        pollingTimer = new Timer(true);
        pollingTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                // Request a fresh query result from DT storage with a sliding window of the last 60 seconds
                refreshQueryResult(lastSecondsQuery(60));
            }
        }, 0, POLLING_INTERVAL_MS);
    }

    @Override
    public void onQueryResultRefresh(Query query, QueryResult<?> queryResult)
            throws AugmentationFunctionException {
        if (queryResult == null || !queryResult.isSuccessful() || queryResult.getItems().isEmpty()) {
            return;
        }

        // Process the refreshed historical data
        logger.debug("Received query refresh with {} records", queryResult.getItems().size());

        // Compute result from the historical snapshot
        AugmentationFunctionResult<Integer> result = new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.GENERIC_RESULT,
                "history_record_count",
                queryResult.getItems().size(),
                null,
                Map.of("query_timestamp", System.currentTimeMillis())
        );

        notifyResult(new AugmentationFunctionResultList(result));
    }

    @Override
    public void stop(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        if (pollingTimer != null) {
            pollingTimer.cancel();
            pollingTimer = null;
        }
        logger.info("Polling function stopped");
    }

    @Override
    public void onStateDescriptionUpdate(DigitalTwinStateDescription digitalTwinStateDescription) {
        // Not used in pure polling mode
    }

    @Override
    public void onStateValuesUpdate(DigitalTwinStateValues digitalTwinStateValues) {
        // Not used in pure polling mode — data is retrieved via scheduled refreshQueryResult()
    }

    @Override
    public void onEventNotificationReceived(DigitalTwinStateEventNotification<?> notification) {
        // Not used in pure polling mode
    }
}

In polling mode the function drives its own data retrieval rhythm independently of DT state changes. refreshQueryResult() re-executes the Query defined in the AugmentationFunctionContextRequest and delivers the result to onQueryResultRefresh(). A custom Query can also be passed directly to refreshQueryResult(Query) when the query parameters need to change at runtime (e.g., to slide a time window, as in the example above). The new query replaces the one of the context request, so it is also the one executed by the following refreshQueryResult() calls.


Key Differences: Stateless vs Stateful Context Usage

AspectStateless FunctionsStateful Functions
Context FrequencyOnce per execute() callInitial context + runtime updates
Context SourceBuilt fresh each executionInitial + push notifications or polling requests
Context RequestDefines single execution needsDefines initial setup + observation preferences
State ObservationInitial/execution context contains the state if observeState = true (optionally narrowed by content filters)Via onStateDescriptionUpdate() (always the complete description) and onStateValuesUpdate() if observeState = true; the values update only for updates with at least one change matching the trigger filter (if any) and concerning a resource accepted by the content filters (if any), with the values narrowed by the content filters
Event ObservationNot applicable (no lifecycle)Via onEventNotificationReceived() if observeEventNotifications = true, only for events accepted by the event names of the stateFilter (if set)
Context UpdatesNot possible (stateless)Automatic (push) or explicit (polling)
Query ExecutionOnce per executionInitial + can be refreshed on request

Augmentation Function Discovery

The DigitalTwinModel provides callback methods to discover and track the availability of Augmentation Functions throughout the Digital Twin lifecycle. These callbacks enable the Model to react to function registration, unregistration, and availability changes.

Discovery Callbacks

onAugmentationNewFunctionAvailable

protected void onAugmentationNewFunctionAvailable(String handlerId, 
                                                  AugmentationFunction augmentationFunction)

Invoked when a new Augmentation Function has been registered and becomes available for use.

Parameters:

  • handlerId: The identifier of the Augmentation Function Handler that registered the function
  • augmentationFunction: The Augmentation Function that became available

onAugmentationFunctionUnAvailable

protected void onAugmentationFunctionUnAvailable(String handlerId, 
                                                 AugmentationFunction augmentationFunction)

Invoked when an Augmentation Function has been unregistered and is no longer available.

Parameters:

  • handlerId: The identifier of the Augmentation Function Handler that unregistered the function
  • augmentationFunction: The Augmentation Function that became unavailable

onAugmentationFunctionListAvailable

protected void onAugmentationFunctionListAvailable(String handlerId, 
                                                   List<AugmentationFunction> augmentationFunctionList)

Invoked at Digital Twin Model startup to notify all Augmentation Functions already registered and available. This method is called once per Handler with its associated list of registered functions.

Parameters:

  • handlerId: The identifier of the Augmentation Function Handler
  • augmentationFunctionList: List of Augmentation Functions registered in the Handler

When the notifications are delivered

The availability notifications do not depend on the synchronization state of the Digital Twin:

  • Functions registered before the DT is started: notified with onAugmentationFunctionListAvailable() when the Digital Twin Model starts, right after its onStart() callback
  • Functions registered while the DT is running: notified immediately with onAugmentationNewFunctionAvailable()

A notification does not mean that the state of the Digital Twin is already synchronized: if a function needs a synchronized state, execute or start it after notifyShadowingSync() has been called. Since the model can learn about a function in two different ways, it is good practice to handle possible duplicated notifications.


Override Augmentation Function Discovery Methods

All discovery callbacks are abstract methods of DigitalTwinModel: every model has to implement them, with an empty body if it is not interested in the availability of the functions.

Example override:

@Override
protected void onAugmentationNewFunctionAvailable(String handlerId,
                                                  AugmentationFunction augmentationFunction) {
    logger.info("New augmentation function available: {} from handler: {}",
                augmentationFunction.getId(),
                handlerId);

    // Custom logic: auto-start stateful functions
    if (augmentationFunction.getType() == AugmentationFunctionType.STATEFUL) {
        try {
            this.startAugmentationFunction(augmentationFunction.getId());
        } catch (Exception e) {
            logger.error("Failed to start stateful function", e);
        }
    }
}

Augmentation Function Invocation

The DigitalTwinModel class provides several methods to interact with both Stateless and Stateful augmentation functions during runtime.

Execute Augmentation Function (Stateless)

// Search across all handlers for the function with the given id
protected void executeAugmentationFunction(String augmentationFunctionId)
    throws EventBusException, AugmentationFunctionException

// Same, but with a custom request id for traceability
protected void executeAugmentationFunction(String augmentationFunctionId,
                                           String augmentationFunctionRequestId)
    throws EventBusException, AugmentationFunctionException

// Target a specific handler explicitly
protected void executeAugmentationFunction(String augmentationFunctionHandlerId,
                                           String augmentationFunctionId,
                                           String augmentationFunctionRequestId)
    throws EventBusException, AugmentationFunctionException

Triggers the execution of a stateless augmentation function. The framework searches all registered handlers for the function unless a specific augmentationFunctionHandlerId is provided.


Start Augmentation Function (Stateful)

// Search across all handlers
protected void startAugmentationFunction(String augmentationFunctionId)
    throws EventBusException, AugmentationFunctionException

// Same, but with a custom request id
protected void startAugmentationFunction(String augmentationFunctionId,
                                         String augmentationFunctionRequestId)
    throws EventBusException, AugmentationFunctionException

// Target a specific handler explicitly
protected void startAugmentationFunction(String augmentationFunctionHandlerId,
                                         String augmentationFunctionId,
                                         String augmentationFunctionRequestId)
    throws EventBusException, AugmentationFunctionException

Initiates a stateful augmentation function, starting its internal loop and enabling continuous operation. The function begins processing and producing results asynchronously.


Query Running State (Stateful)

// Search across all handlers for the function and return its running state
public boolean isStatefulAugmentationFunctionRunning(String augmentationFunctionId)

// Target a specific handler explicitly
public boolean isStatefulAugmentationFunctionRunning(String augmentationFunctionHandlerId,
                                                     String augmentationFunctionId)

Returns true if the specified stateful function has been started and not yet stopped. Returns false, logging a warning, if the handler or the function is not found or the function is not of type STATEFUL.

Note: startAugmentationFunction() is a no-op if the target function is already running, and stopAugmentationFunction() is a no-op if the target function is already stopped. Guard checks happen automatically — you do not need to call isStatefulAugmentationFunctionRunning() before start/stop unless your application logic requires it.


Stop Augmentation Function (Stateful)

// Search across all handlers
protected void stopAugmentationFunction(String augmentationFunctionId)
    throws EventBusException, AugmentationFunctionException

// Same, but with a custom request id
protected void stopAugmentationFunction(String augmentationFunctionId,
                                        String augmentationFunctionRequestId)
    throws EventBusException, AugmentationFunctionException

// Target a specific handler explicitly
protected void stopAugmentationFunction(String augmentationFunctionHandlerId,
                                        String augmentationFunctionId,
                                        String augmentationFunctionRequestId)
    throws EventBusException, AugmentationFunctionException

Terminates a running stateful augmentation function, stopping its internal loop and cleaning up resources.


Example 1: Executing Stateless Function on Physical Property Changes

Execute an augmentation function each time a physical property variation is received:

@Override
protected void onPhysicalAssetPropertyVariation(PhysicalAssetPropertyWldtEvent<?> physicalPropertyEventMessage) {
    try {

        // Implementation of the management of a new Physical Event in the Digital Twin Model
        //[...]
        
        // Execute augmentation function for each received physical event notification
        this.executeAugmentationFunction(RandomNumberAugmentationFunction.FUNCTION_ID);
        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Example 2: Starting Stateful Function After Digital Twin Binding

Start a stateful augmentation function once the Digital Twin has completed its binding phase:

@Override
protected void onDigitalTwinBound(Map<String, PhysicalAssetDescription> adaptersPhysicalAssetDescriptionMap) {
    try {
        
        // Implementation of the binding phase in the Model
        //[...]
        
        // Start stateful augmentation function after DT initialization
        this.startAugmentationFunction(
            StatefulPeriodicRandomNumberAugmentationFunction.FUNCTION_ID
        );
        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Example 3: Stopping a Stateful Function on Unbind

Stop a running stateful function when the Digital Twin loses its physical asset binding:

@Override
protected void onDigitalTwinUnBound(Map<String, PhysicalAssetDescription> adaptersPhysicalAssetDescriptionMap,
                                    String errorMessage) {
    try {
        this.stopAugmentationFunction(StatefulPeriodicRandomNumberAugmentationFunction.FUNCTION_ID);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Augmentation Function Results

Augmentation Functions produce results encapsulated in the AugmentationFunctionResult<T> class, which provides a flexible and type-safe way to return computed outputs to the Digital Twin Model.

The AugmentationFunctionResult<T> is a generic container that structures the output of augmentation function executions.

Structure

public class AugmentationFunctionResult<T> {
    private AugmentationFunctionResultType type;
    private String key;
    private T value;
    private AugmentationFunctionResultMetrics augmentationFunctionResultMetrics;
    private Map<String, Object> metadata;
    private AugmentationFunctionRequest request;
    private final Long timestamp;
}

Fields

FieldTypeDescription
typeAugmentationFunctionResultTypeCategorizes the result type (PROPERTY_RESULT, EVENT_RESULT, RELATIONSHIP_RESULT, RELATIONSHIP_INSTANCE_RESULT, GENERIC_RESULT)
keyStringIdentifier for the result (e.g., property name, event key, relationship name)
valueTThe actual computed value, generic type allows flexibility
augmentationFunctionResultMetricsAugmentationFunctionResultMetricsOptional execution metrics (timing, records processed/generated, execution ID)
metadataMap<String, Object>Optional metadata providing additional context (e.g., confidence score, timestamp, source)
requestAugmentationFunctionRequestThe request that triggered this result, set automatically by the handler
timestampLongCreation timestamp in milliseconds since epoch, set automatically

AugmentationFunctionResultList

AugmentationFunctionResultList extends ArrayList<AugmentationFunctionResult<?>> and is the standard container returned by augmentation functions and received by the Digital Twin Model. It can carry either a list of results or an AugmentationFunctionError — not both. When an error is set the list becomes immutable: all mutating operations (add, set, remove, clear) throw IllegalStateException, while read operations remain available.

Structure

public class AugmentationFunctionResultList extends ArrayList<AugmentationFunctionResult<?>> {
    private AugmentationFunctionError augmentationFunctionError;
}

Constructors

// Empty list — add results with the standard List API
public AugmentationFunctionResultList()

// Single-result convenience constructor
public AugmentationFunctionResultList(AugmentationFunctionResult<?> result)

// Error-state constructor — list starts immutable with the given error
public AugmentationFunctionResultList(AugmentationFunctionError augmentationFunctionError)

Key Methods

MethodDescription
hasError()Returns true if the list is in error state
getAugmentationFunctionError()Returns the AugmentationFunctionError, or null if none
setAugmentationFunctionError(AugmentationFunctionError)Sets the error and locks all mutations

Usage

// Return a single result from a stateless function
@Override
protected AugmentationFunctionResultList run(AugmentationFunctionRequest request)
        throws AugmentationFunctionException {
    AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
            AugmentationFunctionResultType.GENERIC_RESULT, "value", 42.0, null, null);
    return new AugmentationFunctionResultList(result);
}

// Return multiple results
AugmentationFunctionResultList results = new AugmentationFunctionResultList();
results.add(result1);
results.add(result2);
return results;

// Return an error instead of results
return new AugmentationFunctionResultList(
    new AugmentationFunctionError(AugmentationFunctionErrorType.ERROR, "computation failed")
);

// In the Digital Twin Model: the results...
@Override
protected void onAugmentationFunctionResultEvent(String handlerId, String functionId,
                                                 AugmentationFunctionResultList resultList) {
    for (AugmentationFunctionResult<?> result : resultList) {
        // process result ...
    }
}

// ...and the errors are received by two different callbacks
@Override
protected void onAugmentationFunctionError(String handlerId, String functionId,
                                           AugmentationFunctionError error) {
    logger.warn("Function {} reported error: {}", functionId, error.getMessage());
}

AugmentationFunctionResultType Enum

The result type enum defines the categories of outputs that augmentation functions can produce:

public enum AugmentationFunctionResultType {
    PROPERTY_RESULT,
    EVENT_RESULT,
    RELATIONSHIP_RESULT,
    RELATIONSHIP_INSTANCE_RESULT,
    GENERIC_RESULT
}

Result Type Descriptions

TypeDescriptionDigital Twin ComponentExample Use Case
PROPERTY_RESULTSuggests new or updated property valuesDT State PropertiesPredicted temperature, calculated efficiency, derived metrics
EVENT_RESULTSuggests event notifications or registrationsDT State EventsAnomaly detected, threshold exceeded, pattern recognized
RELATIONSHIP_RESULTSuggests new relationship type declarationsDT State RelationshipsNew relationship type discovered between DT types
RELATIONSHIP_INSTANCE_RESULTSuggests concrete relationship instancesDT State Relationship InstancesSpecific connection discovered to another DT instance
GENERIC_RESULTUnstructured or custom resultsModel-defined handlingAnalysis data, recommendations, metrics, custom insights

AugmentationFunctionResultMetrics

AugmentationFunctionResultMetrics is an optional companion class that can be attached to any AugmentationFunctionResult to provide observability and performance data about the execution that produced the result.

Structure

public class AugmentationFunctionResultMetrics {
    private Long totalExecutionTimeMs;
    private Long startTimestamp;
    private Long endTimestamp;
    private Integer recordsProcessed;
    private Integer recordsGenerated;
    private String executionId;
    private String nodeId;
}

Fields

FieldTypeDescription
totalExecutionTimeMsLongTotal execution time in milliseconds
startTimestampLongExecution start time (milliseconds since epoch)
endTimestampLongExecution end time (milliseconds since epoch)
recordsProcessedIntegerNumber of input records/samples consumed during the execution
recordsGeneratedIntegerNumber of output records generated
executionIdStringUnique identifier for this specific execution run
nodeIdStringIdentifier of the node on which the function was executed (useful in distributed deployments)

Constructors

// Provide total execution time directly (throws AugmentationFunctionException if null or negative)
public AugmentationFunctionResultMetrics(Long totalExecutionTimeMs)
    throws AugmentationFunctionException

// Provide start and end timestamps; total time is auto-calculated
// (throws AugmentationFunctionException if timestamps are null or end < start)
public AugmentationFunctionResultMetrics(Long startTimestamp, Long endTimestamp)
    throws AugmentationFunctionException

Usage Example

long start = System.currentTimeMillis();

// ... perform computation ...

long end = System.currentTimeMillis();

AugmentationFunctionResultMetrics metrics = new AugmentationFunctionResultMetrics(start, end);
metrics.setRecordsProcessed(1440);
metrics.setRecordsGenerated(1);
metrics.setExecutionId(UUID.randomUUID().toString());

AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
    AugmentationFunctionResultType.PROPERTY_RESULT,
    "predicted_value",
    85.5,
    metrics,   // ← attach metrics
    null
);

Constructor

public AugmentationFunctionResult(AugmentationFunctionResultType type,
                                  String key,
                                  T value,
                                  AugmentationFunctionResultMetrics augmentationFunctionResultMetrics,
                                  Map<String, Object> metadata)

Creates a new augmentation function result. augmentationFunctionResultMetrics and metadata can be null when not needed.


Result Production in Augmentation Functions

Augmentation functions can produce multiple results in a single execution, returned as an AugmentationFunctionResultList. This allows a function to suggest multiple property updates, events, or relationships simultaneously.

Example: Multi-Result

  AugmentationFunctionResultList results = new AugmentationFunctionResultList();
  
  // Result 1: Predicted failure probability as property
  results.add(new AugmentationFunctionResult<>(
      AugmentationFunctionResultType.PROPERTY_RESULT,
      "predicted_failure_probability",
      0.78,
      null,
      Map.of("confidence", 0.92, "horizon_hours", 48)
  ));
  
  // Result 2: Maintenance recommendation as event
  results.add(new AugmentationFunctionResult<>(
      AugmentationFunctionResultType.EVENT_RESULT,
      "maintenance_required",
      Map.of("urgency", "HIGH", "estimated_hours", 48),
      null,
      Map.of("timestamp", System.currentTimeMillis())
  ));
  
  // Result 3: Generic analytics
  results.add(new AugmentationFunctionResult<>(
      AugmentationFunctionResultType.GENERIC_RESULT,
      "degradation_analysis",
      Map.of("trend", "increasing", "rate", 0.05),
      null,
      Map.of("samples_analyzed", 1000)
  ));

Processing Results in Digital Twin Model

The Digital Twin Model receives augmentation function results through the onAugmentationFunctionResultEvent callback method, which provides a list of results from a single function (both for Stateful and Stateless).

Callback Method Signature

protected void onAugmentationFunctionResultEvent(String augmentationFunctionHandlerId,
                                                 String augmentationFunctionId,
                                                 AugmentationFunctionResultList augmentationFunctionResult)

Parameters:

  • augmentationFunctionHandlerId: The ID of the handler managing the function
  • augmentationFunctionId: The ID of the function that produced the results
  • augmentationFunctionResult: AugmentationFunctionResultList produced by the function. Result lists that carry an error are not delivered here: the error is received by onAugmentationFunctionError()

Basic Implementation Example

@Override
protected void onAugmentationFunctionResultEvent(String augmentationFunctionHandlerId,
                                                 String augmentationFunctionId,
                                                 AugmentationFunctionResultList augmentationFunctionResult) {

    logger.info("Received {} results from function: {} (handler: {})",
                augmentationFunctionResult.size(),
                augmentationFunctionId,
                augmentationFunctionHandlerId);
    
    // Iterate over the augmentation function results
    for(AugmentationFunctionResult<?> result : augmentationFunctionResult) {
        logger.info("Processing result: {}", result);
        
        // Process based on result type
        switch (result.getType()) {
            case PROPERTY_RESULT:
                handlePropertyResult(result);
                break;
            case EVENT_RESULT:
                handleEventResult(result);
                break;
            case RELATIONSHIP_RESULT:
                handleRelationshipResult(result);
                break;
            case RELATIONSHIP_INSTANCE_RESULT:
                handleRelationshipInstanceResult(result);
                break;
            case GENERIC_RESULT:
                handleGenericResult(result);
                break;
        }
    }
}

Example 1: PROPERTY_RESULT (Predicted Value)

AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
    AugmentationFunctionResultType.PROPERTY_RESULT,
    "predicted_temperature",
    85.5,
    null,
    Map.of(
        "confidence", 0.92,
        "horizon_minutes", 10,
        "timestamp", System.currentTimeMillis()
    )
);

Example 2: EVENT_RESULT (Anomaly Detection)

AugmentationFunctionResult<Map<String, Object>> result = new AugmentationFunctionResult<>(
    AugmentationFunctionResultType.EVENT_RESULT,
    "temperature_anomaly_detected",
    Map.of(
        "anomaly", true,
        "severity", "HIGH",
        "temperature", 95.0,
        "threshold", 50.0
    ),
    null,
    Map.of(
        "detector_version", "1.2.3",
        "detection_time", System.currentTimeMillis()
    )
);

Example 3: RELATIONSHIP_RESULT (New Relationship Type)

AugmentationFunctionResult<Map<String, Object>> result = new AugmentationFunctionResult<>(
    AugmentationFunctionResultType.RELATIONSHIP_RESULT,
    "energySuppliedBy",
    Map.of(
        "description", "Indicates energy supply relationship",
        "bidirectional", false
    ),
    null,
    Map.of(
        "discovered_at", System.currentTimeMillis(),
        "discovery_method", "network_topology_analysis"
    )
);

Example 4: RELATIONSHIP_INSTANCE_RESULT (Concrete Connection)

AugmentationFunctionResult<String> result = new AugmentationFunctionResult<>(
    AugmentationFunctionResultType.RELATIONSHIP_INSTANCE_RESULT,
    "connectedTo",
    "machine-002-dt-id",
    null,
    Map.of(
        "discovered_at", System.currentTimeMillis(),
        "confidence", 0.95,
        "connection_type", "physical"
    )
);

Example 5: GENERIC_RESULT (Custom Analytics)

AugmentationFunctionResult<Map<String, Object>> result = new AugmentationFunctionResult<>(
    AugmentationFunctionResultType.GENERIC_RESULT,
    "performance_analysis",
    Map.of(
        "efficiency", 0.78,
        "uptime_percentage", 99.2,
        "energy_consumption_kwh", 150.5,
        "recommendation", "Schedule maintenance within 48 hours"
    ),
    null,
    Map.of(
        "analysis_period_hours", 24,
        "samples_analyzed", 1440
    )
);

Key Features

  • Multi-Result Support: Functions can return multiple results in a single execution via AugmentationFunctionResultList
  • Type Safety: Generic <T> allows strongly-typed values while maintaining flexibility
  • Categorization: Five distinct result types enable structured processing based on DT component
  • Batch Processing: The callback provides all results at once, enabling efficient batch operations (e.g., single state transaction)
  • Extensibility: metadata map allows arbitrary additional context without schema changes
  • Traceability: Results include handler and function IDs, plus optional metadata for full provenance
  • Model Autonomy: The Digital Twin Model retains full control over whether and how to apply results

Augmentation Function Error Handling

Augmentation functions can report errors to the Augmentation Manager and the Digital Twin Model. Errors are encapsulated in AugmentationFunctionError objects and categorized using AugmentationFunctionErrorType. A function reports an error with an AugmentationFunctionResultList built from it: a stateless function returns that list from run(), a stateful function passes it to notifyResult(). The handler recognizes the error and publishes it (instead of a result), and the Digital Twin Model receives it in its onAugmentationFunctionError(handlerId, functionId, augmentationFunctionError) callback.


AugmentationFunctionErrorType

AugmentationFunctionErrorType defines the severity levels of errors reported by augmentation functions.

public enum AugmentationFunctionErrorType {
    INFO,       // Informational messages, not necessarily problems
    WARNING,    // Non-blocking conditions that may require attention
    ERROR,      // Recoverable errors during function execution
    CRITICAL    // Critical failures that may compromise function operation
}
LevelDescription
INFOGeneral informational messages
WARNINGNon-blocking conditions that should be monitored
ERRORRecoverable errors encountered during execution
CRITICALSevere failures that may compromise the function’s ability to operate

AugmentationFunctionError

AugmentationFunctionError encapsulates all information about an error that occurred during the execution of an augmentation function.

Structure

public class AugmentationFunctionError {
    private final String errorId;
    private final Long timestamp;
    private String augmentationFunctionRequestId;
    private final AugmentationFunctionErrorType errorType;
    private final String message;
    private HashMap<String, Object> metadata;
}

Fields

FieldTypeDescription
errorIdStringAuto-generated UUID uniquely identifying this error instance
timestampLongMilliseconds since epoch when the error occurred
augmentationFunctionRequestIdStringID of the request being processed when the error occurred (set automatically by the framework when the result list is returned or notified)
errorTypeAugmentationFunctionErrorTypeSeverity level of the error
messageStringHuman-readable description of the error
metadataHashMap<String, Object>Optional additional context (input parameters, state snapshot, etc.)

Constructors

// Constructor with error type and message (timestamp = now, empty metadata)
public AugmentationFunctionError(AugmentationFunctionErrorType errorType, String message)

// Constructor with error type, message and explicit timestamp
public AugmentationFunctionError(AugmentationFunctionErrorType errorType, String message, Long timestamp)

// Full constructor with error type, message, timestamp and metadata
public AugmentationFunctionError(AugmentationFunctionErrorType errorType,
                                 String message,
                                 Long timestamp,
                                 HashMap<String, Object> metadata)

Reporting Errors from Stateless Functions

Inside a StatelessAugmentationFunction, return an AugmentationFunctionResultList initialized with an AugmentationFunctionError. The error is delivered to the onAugmentationFunctionError callback of the Digital Twin Model.

public class MyStatelessFunction extends StatelessAugmentationFunction {

    public MyStatelessFunction() {
        super("my-stateless-fn", "My Stateless Function", "Reports errors", "1.0.0");
    }

    @Override
    protected AugmentationFunctionResultList run(AugmentationFunctionRequest request)
            throws AugmentationFunctionException {

        try {
            // ... computation ...
            AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.GENERIC_RESULT, "value", 42.0, null, null);
            return new AugmentationFunctionResultList(result);
        } catch (Exception e) {
            // Report a non-fatal error via the result list
            return new AugmentationFunctionResultList(new AugmentationFunctionError(
                AugmentationFunctionErrorType.WARNING,
                "Failed to retrieve sensor data: " + e.getMessage()
            ));
        }
    }
}

Reporting Errors from Stateful Functions

A StatefulAugmentationFunction reports an error at any time by passing to notifyResult() a result list built from the error, for example inside onStateValuesUpdate() or from a timer.

public class MyStatefulFunction extends StatefulAugmentationFunction {

    public MyStatefulFunction() {
        super("my-stateful-fn", "My Stateful Function", "Reports errors", "1.0.0");
    }

    @Override
    public void onStateValuesUpdate(DigitalTwinStateValues digitalTwinStateValues)
            throws AugmentationFunctionException {

        try {
            // ... stateful computation ...
        } catch (Exception e) {
            // Report a critical error with additional metadata
            HashMap<String, Object> meta = new HashMap<>();
            meta.put("state_snapshot", digitalTwinStateValues.toString());

            notifyResult(new AugmentationFunctionResultList(new AugmentationFunctionError(
                AugmentationFunctionErrorType.CRITICAL,
                "State processing failed: " + e.getMessage(),
                System.currentTimeMillis(),
                meta
            )));
        }
    }

    // The other abstract methods are omitted
}

Augmentation Function Implementation

WLDT provides two abstract base classes for implementing augmentation functions: StatelessAugmentationFunction and StatefulAugmentationFunction. Both classes define the structure and lifecycle methods that developers must implement to create custom augmentation capabilities.


AugmentationFunction Base Class

All augmentation functions extend the abstract AugmentationFunction class, which defines the common fields and metadata shared by both stateless and stateful functions.

Structure

public abstract class AugmentationFunction {
    private String id;
    private String name;
    private String description;
    private String version;
    private AugmentationFunctionType type;
    private AugmentationFunctionContextRequest contextRequest;
}

Fields

FieldTypeDescription
idStringUnique identifier for the function
nameStringHuman-readable name
descriptionStringDescription of what the function does
versionStringVersion string (e.g., "1.0.0")
typeAugmentationFunctionTypeWhether the function is STATELESS or STATEFUL (set automatically by subclass constructors)
contextRequestAugmentationFunctionContextRequestDefines what data the function needs during execution (state, events, queries)

Constructors

// Full constructor — used internally by subclass constructors
public AugmentationFunction(String id, String name, String description, String version,
                            AugmentationFunctionType type,
                            AugmentationFunctionContextRequest contextRequest)

// Minimal constructor — description and version default to null
public AugmentationFunction(String id, String name, AugmentationFunctionType type)

Developers never instantiate AugmentationFunction directly—they extend StatelessAugmentationFunction or StatefulAugmentationFunction, which call super(...) with the correct type automatically.


AugmentationFunctionType

AugmentationFunctionType identifies whether a function is stateless (single-shot execution) or stateful (continuous lifecycle with internal state).

public enum AugmentationFunctionType {
    STATELESS,  // Single-shot execution, no persistent internal state
    STATEFUL    // Continuous lifecycle, maintains internal state
}

This value is set automatically by StatelessAugmentationFunction and StatefulAugmentationFunction constructors and should not be changed at runtime.


Stateless Augmentation Function

The StatelessAugmentationFunction class is designed for functions that perform independent computations without maintaining state between executions.

Base Class Structure

public abstract class StatelessAugmentationFunction extends AugmentationFunction {

    // Minimal constructor — description and version are null, default context request
    public StatelessAugmentationFunction(String id, String name) {
        super(id, name, null, null, AugmentationFunctionType.STATELESS, new AugmentationFunctionContextRequest());
    }

    // Default constructor — uses a default context request (observes state and events, no query)
    public StatelessAugmentationFunction(String id,
                                         String name,
                                         String description,
                                         String version) {
        super(id, name, description, version, AugmentationFunctionType.STATELESS, new AugmentationFunctionContextRequest());
    }

    // Constructor with custom context request — use when the function needs a specific
    // AugmentationFunctionContextRequest (e.g. to include a storage Query or to
    // disable state/event observation)
    public StatelessAugmentationFunction(String id,
                                            String name,
                                            String description,
                                            String version,
                                            AugmentationFunctionContextRequest contextRequest) {
        super(id, name, description, version, AugmentationFunctionType.STATELESS, contextRequest);
    }
    
    /**
     * Main execution method that must be implemented by concrete functions.
     * Called each time the function is invoked.
     *
     * @param request The augmentation function request containing context (DT state, query results) and metadata
     * @return AugmentationFunctionResultList — a list of results, or a list in error state if execution failed
     * @throws AugmentationFunctionException if execution fails with an unrecoverable exception
     */
    protected abstract AugmentationFunctionResultList run(AugmentationFunctionRequest request)
        throws AugmentationFunctionException;
}

Key Characteristics

  • Single Method: Only run() needs to be implemented
  • No Lifecycle: No start() or stop() methods required
  • Request Provided: Receives AugmentationFunctionRequest containing the AugmentationFunctionContext (current DT state + optional query results) on each invocation
  • Immediate Results: Returns results synchronously via AugmentationFunctionResultList
  • Error Reporting: Return a new AugmentationFunctionResultList(error) to signal errors
  • Custom Context: Use the 5-arg constructor (passing an AugmentationFunctionContextRequest) when the function needs a storage query or must disable state/event observation

Stateful Augmentation Function

The StatefulAugmentationFunction class is designed for functions that maintain internal state, run continuously, and can react to DT state changes or events.

Base Class Structure

public abstract class StatefulAugmentationFunction extends AugmentationFunction {

    // Default constructor — uses a default context request (observes state and events, no query)
    public StatefulAugmentationFunction(String id,
                                        String name,
                                        String description,
                                        String version) {
        super(id, name, description, version, AugmentationFunctionType.STATEFUL, new AugmentationFunctionContextRequest());
    }

    // Constructor with custom context request — use when the function needs a specific
    // AugmentationFunctionContextRequest (e.g. to include a storage Query or to
    // disable state/event observation)
    public StatefulAugmentationFunction(String id,
                                           String name,
                                           String description,
                                           String version,
                                           AugmentationFunctionContextRequest contextRequest) {
        super(id, name, description, version, AugmentationFunctionType.STATEFUL, contextRequest);
    }

    /**
     * Called when the function is started. Initialize internal state and resources here.
     *
     * @param request The augmentation function request containing the initial context
     * @throws AugmentationFunctionException if initialization fails
     */
    protected abstract void start(AugmentationFunctionRequest request)
        throws AugmentationFunctionException;

    /**
     * Called when the function is stopped. Clean up resources here.
     *
     * @param request The augmentation function request containing the final context
     * @throws AugmentationFunctionException if cleanup fails
     */
    protected abstract void stop(AugmentationFunctionRequest request)
        throws AugmentationFunctionException;

    /**
     * Called automatically when the description (structure) of the Digital Twin state changes.
     * The description is never filtered.
     *
     * @param digitalTwinStateDescription The new complete Digital Twin state description
     * @throws AugmentationFunctionException if processing fails
     */
    public abstract void onStateDescriptionUpdate(DigitalTwinStateDescription digitalTwinStateDescription)
        throws AugmentationFunctionException;

    /**
     * Called automatically when the values of the Digital Twin state change (subject to the filters of the
     * context request, if any).
     *
     * @param digitalTwinStateValues The new Digital Twin state values (complete, or limited by the content filter)
     * @throws AugmentationFunctionException if processing fails
     */
    public abstract void onStateValuesUpdate(DigitalTwinStateValues digitalTwinStateValues)
        throws AugmentationFunctionException;

    /**
     * Called automatically when a Digital Twin event is notified.
     *
     * @param digitalTwinStateEventNotification The event notification
     * @throws AugmentationFunctionException if processing fails
     */
    public abstract void onEventNotificationReceived(DigitalTwinStateEventNotification<?> digitalTwinStateEventNotification)
        throws AugmentationFunctionException;

    /**
     * Called when a refreshed query result becomes available from the DT storage, after a refresh has been
     * requested (by the function, by the Digital Twin Model or by the refreshQueryOnStateUpdate option).
     * It must be implemented by every function, with an empty body if it does not use queries.
     *
     * @param query The query whose result was refreshed
     * @param queryResult  The new query result
     * @throws AugmentationFunctionException if processing fails
     */
    public abstract void onQueryResultRefresh(Query query, QueryResult<?> queryResult)
        throws AugmentationFunctionException;

    /**
     * Request a refresh of the query result from the DT storage.
     * Uses the Query defined in AugmentationFunctionContextRequest.
     * The refreshed result is delivered asynchronously via onQueryResultRefresh().
     * Provided by the framework — call this method from your implementation, do not override it.
     */
    protected void refreshQueryResult() { /* provided by the framework */ }

    /**
     * Request a refresh of the query result using a custom Query, that replaces the one of the context request.
     * The refreshed result is delivered asynchronously via onQueryResultRefresh().
     * Provided by the framework — call this method from your implementation, do not override it.
     *
     * @param query The custom query to execute
     */
    protected void refreshQueryResult(Query query) { /* provided by the framework */ }

    /**
     * Returns whether this stateful function is currently running.
     * Automatically updated by the framework when handleStart() / handleStop() are called.
     * Provided by the framework — call this method from your implementation, do not override it.
     *
     * @return true if the function has been started and not yet stopped
     */
    public boolean isRunning() { /* provided by the framework */ return false; }

    /**
     * Notify results asynchronously to the Augmentation Manager.
     * Call this method whenever the function produces results.
     * Provided by the framework — call this method from your implementation, do not override it.
     *
     * @param results AugmentationFunctionResultList to publish
     */
    protected void notifyResult(AugmentationFunctionResultList results) { /* provided by the framework */ }
}

Key Characteristics

  • Lifecycle Management: Implements start() and stop() for initialization and cleanup
  • Running State: isRunning() reflects whether the function has been started. startAugmentationFunction() is a no-op if already running; stopAugmentationFunction() is a no-op if already stopped
  • Push Notifications: Receives automatic callbacks via onStateDescriptionUpdate(), onStateValuesUpdate() and onEventNotificationReceived()
  • Polling Support: onQueryResultRefresh() receives the refreshed query results; it is abstract, so push-mode and timer-based functions implement it with an empty body
  • Asynchronous Results: Produces results by calling notifyResult(AugmentationFunctionResultList) at any time, not necessarily synchronized with notifications
  • Error Reporting: Call notifyResult(new AugmentationFunctionResultList(error)) to report errors asynchronously
  • Internal State: Can maintain history, timers, or any internal data structures
  • Framework-provided methods: notifyResult(), refreshQueryResult(), isRunning() are concrete methods provided by the framework — call them from your implementation, do not override them

Implementation Examples

Example 1: Simple Stateless Function (Random Number Generator)

A basic stateless function that generates a random number on each invocation:

public class RandomNumberAugmentationFunction extends StatelessAugmentationFunction {

    public static final String FUNCTION_ID = "random-number-augmentation-function";

    public RandomNumberAugmentationFunction() {
        super(FUNCTION_ID,
                "Random Number Augmentation Function",
                "This augmentation function generates a random number between 0 and 100.",
                "1.0.0");
    }

    @Override
    public AugmentationFunctionResultList run(AugmentationFunctionRequest request)
            throws AugmentationFunctionException {
        
        // Generate a random number between 0 and 1
        double randomNumber = Math.random();

        // Create a generic result with the random number
        AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.GENERIC_RESULT,
                "randomNumber",
                randomNumber,
                null,
                null
        );

        return new AugmentationFunctionResultList(result);
    }
}

Use Case: Generate random data for testing or simulation purposes on-demand.


Example 2: Multi-Result Stateless Function (State Component Generator)

A stateless function that produces multiple results of different types in a single execution:

public class RandomStateResultAugmentationFunction extends StatelessAugmentationFunction {

    public static final String FUNCTION_ID = "random-state-result-function";
    public static final int RANDOM_STRING_LENGTH = 10;

    public RandomStateResultAugmentationFunction() {
        super(FUNCTION_ID,
                "Random State Result Augmentation Function",
                "Generates multiple state components with random values",
                "1.0.0");
    }

    @Override
    public AugmentationFunctionResultList run(AugmentationFunctionRequest request)
            throws AugmentationFunctionException {

        AugmentationFunctionResultList results = new AugmentationFunctionResultList();

        // Property Result: Random string property
        results.add(new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.PROPERTY_RESULT,
                "randomStringProperty",
                generateRandomString(RANDOM_STRING_LENGTH),
                null,
                null
        ));

        // Event Result: Random number event
        results.add(new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.EVENT_RESULT,
                "randomNumberEvent",
                Math.random(),
                null,
                null
        ));

        // Relationship Result: New relationship type
        results.add(new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.RELATIONSHIP_RESULT,
                "randomStringRelationship",
                generateRandomString(RANDOM_STRING_LENGTH),
                null,
                null
        ));

        // Relationship Instance Result: Concrete relationship
        results.add(new AugmentationFunctionResult<>(
                AugmentationFunctionResultType.RELATIONSHIP_INSTANCE_RESULT,
                "randomStringRelationshipInstance",
                generateRandomString(RANDOM_STRING_LENGTH),
                null,
                null
        ));

        return results;
    }

    private static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder randomString = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = (int) (Math.random() * characters.length());
            randomString.append(characters.charAt(index));
        }
        return randomString.toString();
    }
}

Use Case: Demonstrate how a single augmentation function can suggest multiple changes across different DT state components simultaneously.


Example 3: Periodic Stateful Function (Timer-Based)

A stateful function that generates results periodically using an internal timer (polling mode):

public class StatefulPeriodicRandomNumberAugmentationFunction extends StatefulAugmentationFunction {

    private static final WldtLogger logger = WldtLoggerProvider.getLogger(StatefulPeriodicRandomNumberAugmentationFunction.class);

    private static final long AUGMENTATION_FUNCTION_TIME_MS = 1000;
    public static final String FUNCTION_ID = "periodic-random-number-augmentation-function";
    
    private Timer timer;
    private TimerTask timerTask;

    public StatefulPeriodicRandomNumberAugmentationFunction() {
        super(FUNCTION_ID,
                "Periodic Random Number Augmentation Function",
                "Generates random numbers periodically",
                "1.0.0");
    }

    @Override
    public void start(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        try {
            logger.info("Starting periodic function with period {} ms", AUGMENTATION_FUNCTION_TIME_MS);
            createTimerTask(0, AUGMENTATION_FUNCTION_TIME_MS);
        } catch (Exception e) {
            throw new AugmentationFunctionException("Error starting function: " + e.getMessage());
        }
    }

    @Override
    public void stop(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        try {
            logger.info("Stopping periodic function");
            stopTimerTask();
        } catch (Exception e) {
            throw new AugmentationFunctionException("Error stopping function: " + e.getMessage());
        }
    }

    private void createTimerTask(long initialDelayMs, long periodMs) {
        stopTimerTask(); // Ensure no existing timer
        
        this.timerTask = new TimerTask() {
            @Override
            public void run() {
                // Generate random number
                double randomNumber = Math.random();
                
                AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
                        AugmentationFunctionResultType.GENERIC_RESULT,
                        "randomNumber",
                        randomNumber,
                        null,
                        null
                );
                
                // Notify result asynchronously
                notifyResult(new AugmentationFunctionResultList(result));
            }
        };

        this.timer = new Timer(true);
        this.timer.scheduleAtFixedRate(this.timerTask, initialDelayMs, periodMs);
    }

    private void stopTimerTask() {
        if (this.timerTask != null) {
            this.timerTask.cancel();
            this.timerTask = null;
        }
        if (this.timer != null) {
            this.timer.cancel();
            this.timer.purge();
            this.timer = null;
        }
    }

    @Override
    public void onStateDescriptionUpdate(DigitalTwinStateDescription digitalTwinStateDescription) {
        logger.debug("Received state description update: {}", digitalTwinStateDescription);
        // No action needed - timer handles result generation
    }

    @Override
    public void onStateValuesUpdate(DigitalTwinStateValues digitalTwinStateValues) {
        logger.debug("Received state values update: {}", digitalTwinStateValues);
        // No action needed - timer handles result generation
    }

    @Override
    public void onEventNotificationReceived(DigitalTwinStateEventNotification<?> notification) {
        logger.debug("Received event notification: {}", notification);
        // No action needed - timer handles result generation
    }

    @Override
    public void onQueryResultRefresh(Query query, QueryResult<?> queryResult) {
        // Not used in pure timer mode
    }
}

Use Case: Continuous monitoring or periodic forecasting where results are generated at fixed intervals regardless of DT state changes.


Example 4: State-Driven Stateful Function (Push Mode with History)

A stateful function that reacts to state changes and maintains internal history for computing aggregated results:

public class StatefulStateDrivenRandomNumberAugmentationFunction extends StatefulAugmentationFunction {

    private static final WldtLogger logger = WldtLoggerProvider.getLogger(StatefulStateDrivenRandomNumberAugmentationFunction.class);

    public static final String FUNCTION_ID = "state-driven-random-number-augmentation-function";
    
    private List<AugmentationFunctionResult<?>> lastResultList;

    public StatefulStateDrivenRandomNumberAugmentationFunction() {
        super(FUNCTION_ID,
                "State-Driven Random Number Augmentation Function",
                "Generates random numbers on state changes and computes running average",
                "1.0.0");
        
        this.lastResultList = new ArrayList<>();
    }

    @Override
    public void start(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        try {
            logger.info("Starting state-driven augmentation function");
            // Initialize internal state if needed
        } catch (Exception e) {
            throw new AugmentationFunctionException("Error starting function: " + e.getMessage());
        }
    }

    @Override
    public void stop(AugmentationFunctionRequest request) throws AugmentationFunctionException {
        try {
            logger.info("Stopping state-driven augmentation function");
            // Cleanup resources
        } catch (Exception e) {
            throw new AugmentationFunctionException("Error stopping function: " + e.getMessage());
        }
    }

    @Override
    public void onStateDescriptionUpdate(DigitalTwinStateDescription digitalTwinStateDescription) throws AugmentationFunctionException {
        logger.debug("Received state description update: {}", digitalTwinStateDescription);
    }

    @Override
    public void onStateValuesUpdate(DigitalTwinStateValues digitalTwinStateValues) throws AugmentationFunctionException {
        try {
            logger.debug("Received state values update: {}", digitalTwinStateValues);

            // Generate new random number in response to state change
            double randomNumber = Math.random();
            AugmentationFunctionResult<Double> result = new AugmentationFunctionResult<>(
                    AugmentationFunctionResultType.GENERIC_RESULT,
                    "randomNumber",
                    randomNumber,
                    null,
                    null
            );

            // Add to internal history (stateful behavior)
            this.lastResultList.add(result);

            // Compute average from history
            double averageRandomNumber = this.lastResultList.stream()
                    .filter(r -> r.getType() == AugmentationFunctionResultType.GENERIC_RESULT)
                    .mapToDouble(r -> (Double) r.getValue())
                    .average()
                    .orElse(0.0);

            // Create aggregated result
            AugmentationFunctionResult<Double> averageResult = new AugmentationFunctionResult<>(
                    AugmentationFunctionResultType.GENERIC_RESULT,
                    "averageRandomNumber",
                    averageRandomNumber,
                    null,
                    null
            );

            // Notify both results asynchronously
            AugmentationFunctionResultList resultList = new AugmentationFunctionResultList();
            resultList.add(result);
            resultList.add(averageResult);
            this.notifyResult(resultList);

        } catch (Exception e) {
            throw new AugmentationFunctionException("Error processing state update: " + e.getMessage());
        }
    }

    @Override
    public void onEventNotificationReceived(DigitalTwinStateEventNotification<?> notification) {
        logger.debug("Received event notification: {}", notification);
        // Could react to specific events if needed
    }

    @Override
    public void onQueryResultRefresh(Query query, QueryResult<?> queryResult) {
        // Not used in pure push mode
    }
}

Use Case: Real-time analytics that compute running statistics (averages, trends) based on each state update, demonstrating both state-driven execution and internal state management.


Key Implementation Patterns

Stateless Pattern

  1. Constructor: Initialize function metadata (id, name, description, version)
  2. run(): Implement computation logic and return results immediately
  3. No State: Avoid instance variables that persist across invocations

Stateful Polling Pattern

  1. Constructor: Initialize function metadata
  2. start(): Initialize internal state and start timer/thread
  3. stop(): Cancel timers and cleanup resources
  4. Timer/Thread: Generate results periodically using notifyResult()
  5. Callbacks: Optionally react to state/event notifications if needed

Stateful Push Pattern

  1. Constructor: Initialize function metadata and internal state structures
  2. start(): Initialize any resources needed
  3. stop(): Cleanup resources
  4. onStateDescriptionUpdate() / onStateValuesUpdate(): React to structure and values changes, compute results, call notifyResult()
  5. onEventNotificationReceived(): React to events if relevant

Augmentation Function Events

Augmentation Function Events

Those events map the interactions between the Digital Twin Model, the Augmentation Manager and the Augmentation Function Handlers that host the Augmentation Functions (stateless and stateful) registered in the DT. They are published on the same WLDT Event Bus used by all the other components and all share the base event type dt.augmentation.function, extended with the Handler Id and the Function Id when the event is related to a specific function. Involved events are related to:

  • Augmentation Function Registration: Associated to the availability of the Augmentation Functions in the DT. They are published by the Augmentation Function Handler and observed by the Digital Twin Model, which keeps an up-to-date list of the available functions and notifies the Shadowing Function through the discovery callbacks
    • Function Registered: Maps the registration of a new Augmentation Function in a Handler (AugmentationFunctionRegistrationWldtEvent, type dt.augmentation.function.registered)
    • Function Unregistered: Maps the removal of an Augmentation Function from a Handler (AugmentationFunctionUnRegistrationWldtEvent, type dt.augmentation.function.unregistered)
  • Augmentation Function Request: Associated to the requests generated by the Digital Twin Model (e.g., after a Physical Asset variation or a DT’s life cycle change) and sent to a specific function of a specific Handler. The payload is an AugmentationFunctionRequest containing the context of the execution
    • Execute Request: Maps the request to execute a Stateless Augmentation Function (AugmentationFunctionExecuteWldtEvent, type dt.augmentation.function.execution.execute.<handlerId>.<functionId>)
    • Start Request: Maps the request to start a Stateful Augmentation Function, which will then keep running and producing results over time (AugmentationFunctionStartWldtEvent, type dt.augmentation.function.execution.start.<handlerId>.<functionId>)
    • Stop Request: Maps the request to stop a running Stateful Augmentation Function (AugmentationFunctionStopWldtEvent, type dt.augmentation.function.execution.stop.<handlerId>.<functionId>)
  • Augmentation Function Output: Associated to the outcome of the execution of an Augmentation Function. They are published by the Handler and observed by the Digital Twin Model
    • Function Result: Maps the availability of new results produced by a function. The payload is an AugmentationFunctionResultList containing a list of AugmentationFunctionResult (e.g., a predicted property value, an anomaly event or a new relationship) (AugmentationFunctionResultWldtEvent, type dt.augmentation.function.result.<handlerId>.<functionId>)
    • Function Error: Maps an error notified by a function during its execution (e.g., an invalid context or an exception). The payload is an AugmentationFunctionError that also reports the identifier of the request that generated it (AugmentationFunctionErrorWldtEvent, type dt.augmentation.function.error)

Since all the events share the same base type, any component can observe the entire augmentation activity of the DT by using the wildcard dt.augmentation.function.*. This is the approach adopted, for example, by the Storage Layer to automatically persist registrations, requests, results and errors of the Augmentation Functions.


WLDT Event Bus Integration

The augmentation framework relies on the WLDT Event Bus for internal communication between the Digital Twin Model, the Augmentation Manager, and the Augmentation Function Handlers. Developers extending or monitoring the system should be aware of these event types. All augmentation events extend the generic WldtEvent<T> class.

Event Summary

Event ClassPayloadDirectionDescription
AugmentationFunctionRegistrationWldtEventAugmentationFunctionHandler → DTMPublished when a new function is registered in a handler
AugmentationFunctionUnRegistrationWldtEventAugmentationFunctionHandler → DTMPublished when a function is unregistered from a handler
AugmentationFunctionExecuteWldtEventAugmentationFunctionRequestDTM → HandlerTriggers the execution of a stateless function
AugmentationFunctionStartWldtEventAugmentationFunctionRequestDTM → HandlerTriggers the start of a stateful function
AugmentationFunctionStopWldtEventAugmentationFunctionRequestDTM → HandlerTriggers the stop of a stateful function
AugmentationFunctionResultWldtEventAugmentationFunctionResultListHandler → DTMCarries results produced by a function execution (a result list with an error is published as an error event instead)
AugmentationFunctionErrorWldtEventAugmentationFunctionErrorHandler → DTMCarries error notifications from a function execution

Key Event Classes

AugmentationFunctionRegistrationWldtEvent

Published on the Event Bus when a new augmentation function is registered in a handler. The Digital Twin Model subscribes to this event to maintain an up-to-date registry of available functions (see Registration & Discovery).

public class AugmentationFunctionRegistrationWldtEvent extends WldtEvent<AugmentationFunction> {
    private String augmentationHandlerId;
}

AugmentationFunctionUnRegistrationWldtEvent

Published when an augmentation function is removed from a handler at runtime.

public class AugmentationFunctionUnRegistrationWldtEvent extends WldtEvent<AugmentationFunction> {
    private String augmentationHandlerId;
}

AugmentationFunctionExecuteWldtEvent

Sent by the Digital Twin Model to trigger a stateless function execution. The payload is an AugmentationFunctionRequest with type EXECUTE.

public class AugmentationFunctionExecuteWldtEvent extends WldtEvent<AugmentationFunctionRequest> {
    private String augmentationFunctionId;
    private String augmentationHandlerId;
}

AugmentationFunctionStartWldtEvent / AugmentationFunctionStopWldtEvent

Sent by the Digital Twin Model to start or stop a stateful function. The payload is an AugmentationFunctionRequest with type START or STOP respectively.

public class AugmentationFunctionStartWldtEvent extends WldtEvent<AugmentationFunctionRequest> {
    private String augmentationFunctionId;
    private String augmentationHandlerId;
}

public class AugmentationFunctionStopWldtEvent extends WldtEvent<AugmentationFunctionRequest> {
    private String augmentationFunctionId;
    private String augmentationHandlerId;
}

AugmentationFunctionResultWldtEvent

Published by a handler when an augmentation function (stateless or stateful) produces results. The Digital Twin Model subscribes to this event to receive and process augmentation outputs via the onAugmentationFunctionResultEvent() callback. The payload is an AugmentationFunctionResultList with the results: when a function returns or notifies a list with an error, the handler publishes an AugmentationFunctionErrorWldtEvent instead.

public class AugmentationFunctionResultWldtEvent
        extends WldtEvent<AugmentationFunctionResultList> {
    private String augmentationFunctionId;
    private String augmentationHandlerId;
}

AugmentationFunctionErrorWldtEvent

Published by a handler when an augmentation function returns (stateless) or notifies (stateful) a result list with an error. The Digital Twin Model receives it through the onAugmentationFunctionError() callback.

public class AugmentationFunctionErrorWldtEvent extends WldtEvent<AugmentationFunctionError> {
    private String augmentationFunctionId;
    private String augmentationHandlerId;
}

Note: These event classes are used internally by the WLDT framework. Developers typically do not instantiate them directly—invocations of executeAugmentationFunction(), startAugmentationFunction(), and stopAugmentationFunction() in the Digital Twin Model automatically create and publish the corresponding events.

Augmentation Function Monitoring Integration

Augmentation functions participate in the WLDT monitoring system through built-in support in the AugmentationFunction base class. The MonitoringInterface is injected automatically by AugmentationFunctionHandler when a function is registered — developers do not manage this injection themselves.

For the full reference on monitoring integration — including the handleMetricsRegistration() hook, the METRIC_METADATA_AF_FUNCTION_ID_KEY constant for tagging function-level metrics, the list of framework-native augmentation metrics, and code examples — see Augmentation Function Monitoring Integration in the monitoring documentation.