DT Engine & DT Instance

Now that we have created the main fundamental element of a DT (Physical Adapter, Shadowing Function and Digital Adapter) we can create Class file with a main to create the WLDT Engine with the created components and start the DT.

Create a new Java file called DemoDigitalTwin. The first step is to create a new Digital Twin instance, passing its id and its Shadowing Function, and to add to it the Physical and Digital Adapters (here in the version with a configuration, see Configurable Adapters):

// Create the new Digital Twin with its Shadowing Function
DigitalTwin digitalTwin = new DigitalTwin(digitalTwinId, new DemoShadowingFunction("test-shadowing-function"));

// Physical Adapter with Configuration
digitalTwin.addPhysicalAdapter(
        new DemoPhysicalAdapter(
                String.format("%s-%s", digitalTwinId, "test-physical-adapter"),
                new DemoPhysicalAdapterConfiguration()));

// Digital Adapter with Configuration
digitalTwin.addDigitalAdapter(
        new DemoDigitalAdapter(
                String.format("%s-%s", digitalTwinId, "test-digital-adapter"),
                new DemoDigitalAdapterConfiguration()));

DTs cannot be directly run: they have to be added to the DigitalTwinEngine in order to be executed through the WLDT Library

// Create the Digital Twin Engine
DigitalTwinEngine digitalTwinEngine = new DigitalTwinEngine();

// Add the Digital Twin to the Engine
digitalTwinEngine.addDigitalTwin(digitalTwin);

In order to start a DT from the Engine you can:

// Directly start it when you add it, passing a second boolean value = true
// (alternative to the previous addDigitalTwin call)
digitalTwinEngine.addDigitalTwin(digitalTwin, true);

// Start a single DT on the engine through its id
digitalTwinEngine.startDigitalTwin(DIGITAL_TWIN_ID);

// Start all the DTs registered on the engine
digitalTwinEngine.startAll();

To stop a single twin or all the twins registered on the engine:

// Stop a single DT on the engine through its id
digitalTwinEngine.stopDigitalTwin(DIGITAL_TWIN_ID);

// Stop all the DTs registered on the engine
digitalTwinEngine.stopAll();

It is also possible to remove a DT from the Engine with a consequent stop if it is active and the deletion of its reference from the engine:

// Remove a single DT on the engine through its id
digitalTwinEngine.removeDigitalTwin(DIGITAL_TWIN_ID);

// Remove all the DTs registered on the engine
digitalTwinEngine.removeAll();

The resulting code in our case, using the adapters without configuration created in the previous pages, is:

import it.wldt.core.engine.DigitalTwin;
import it.wldt.core.engine.DigitalTwinEngine;

public class DemoDigitalTwin {

    public static void main(String[] args)  {
        try{

            // Create the new Digital Twin
            DigitalTwin digitalTwin = new DigitalTwin(
                    "test-dt-id",
                    new DemoShadowingFunction("test-shadowing-function")
            );

            // Add the Physical and Digital Adapters
            digitalTwin.addPhysicalAdapter(new DemoPhysicalAdapter("test-physical-adapter"));
            digitalTwin.addDigitalAdapter(new DemoDigitalAdapter("test-digital-adapter"));

            // Create the Digital Twin Engine
            DigitalTwinEngine digitalTwinEngine = new DigitalTwinEngine();

            // Add the Digital Twin to the Engine
            digitalTwinEngine.addDigitalTwin(digitalTwin);

            // Start all the DTs registered on the engine
            digitalTwinEngine.startAll();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}