Building a Model

The following sample illustrates the steps to define a real-time digital twin model for streaming analytics consisting of the digital twin state object, message, and message processor classes. Note that both the digital twin state object and message classes need to be JSON serializable since the library uses JSON serialization for all user-defined types. Once these steps have been completed, you can use either the real-time digital twin cloud service or the on-premises APIs to deploy the model and start handling incoming messages from data sources.

The workflow for creating a real-time or simulation digital twin model in Java is as follows:

  1. Create a new Java project.

  2. Extend the DigitalTwinBase class.

  3. Define a message.

  4. Implement the message processor.

  5. (Optional) Implement the simulation processor.

The topics in this section illustrate how to create an example digital twin model – a real-time thermostat processing temperature change messages.

Begin by creating a new package. Using the archetype from com.scaleoutsoftware.archetypes:scaleout-realtime-digitaltwin:1.0.6 we can generate a Maven project with the RealTimeThermostat.java, RealTimeThermostatMessageProcessor.java, and TemperatureChangeMessage.java already created. To create the Thermostat project with the scaleout-realtime-digitaltwin archetype, execute the following command in your terminal:

mvn archetype:generate \
  -DarchetypeGroupId=com.scaleoutsoftware.archetypes \
  -DarchetypeArtifactId=scaleout-realtime-digitaltwin \
  -DarchetypeVersion=1.0.6 \
  -DgroupId=com.example.thermostat \
  -DartifactId=thermostat \
  -DmodelName=RealTimeThermostat \
  -DmessageName=TemperatureChangeMessage \
  -DmessageProcessorName=RealTimeThermostatMessageProcessor \
  -DinteractiveMode=false

What these options do:

Property

Description

groupId

Your organization or namespace

artifactId

Name of the generated project directory

modelName

The class representing each RealTimeThermostat

messageName

Telemetry message detailing the temperature change

messageProcessorName

Message processing logic for a RealTimeThermostat

This command generates a new folder (thermostat) containing:

  • Java class stubs for your Digital Twin classes

  • A pre-configured model.json metadata file

  • A Maven project file (pom.xml) with preconfigured dependencies

  • A build pipeline for packaging your project

Alternatively, you can create the project using your build tool of choice. The following class examples are using the package:

com.example.thermostat

Inside that package, create the following classes:

  • RealTimeThermostat.java

  • TemperatureChangeMessage.java

  • RealTimeThermostatMessageProcessor.java

The classes for the digital twin’s state object and message processor extend the abstract base classes defined in the core library; the message class can be any JSON-serializable class. For test purposes, you can copy and paste the sample code in the following subtopics into these three files.