Skip to content

Commit 14e95f3

Browse files
committed
Additional comments and minor changes/refactoring added
1 parent 2115652 commit 14e95f3

14 files changed

Lines changed: 197 additions & 28 deletions

src/main/java/it/wldt/core/engine/DigitalTwin.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import it.wldt.core.event.DefaultWldtEventLogger;
1010
import it.wldt.core.event.EventManager;
1111
import it.wldt.core.event.WldtEventBus;
12-
import it.wldt.core.model.ModelEngine;
12+
import it.wldt.core.model.DigitalTwinModel;
1313
import it.wldt.core.model.ShadowingFunction;
1414
import it.wldt.core.model.ShadowingModelListener;
1515
import it.wldt.core.state.DigitalTwinState;
@@ -101,7 +101,7 @@ public class DigitalTwin implements ShadowingModelListener, PhysicalAdapterListe
101101
/**
102102
* Instance of the Model Engine of the current Digital Twin
103103
*/
104-
private ModelEngine modelEngine = null;
104+
private DigitalTwinModel digitalTwinModel = null;
105105

106106
/**
107107
* List of Life Cycle Listener for the current Digital Twin
@@ -232,11 +232,11 @@ private void init(ShadowingFunction shadowingFunction) throws ModelException, Wl
232232
this.shadowingFunction = shadowingFunction;
233233
this.shadowingFunction.setShadowingModelListener(this);
234234

235-
// Initialize the Digital Twin Model with digital twin ID, state manager, and shadowing function
236-
this.modelEngine = new ModelEngine(this.digitalTwinId, this.digitalTwinStateManager, this.shadowingFunction);
235+
// Initialize the Digital Twin Model with digital twin ID, state manager, shadowing function, and storage manager
236+
this.digitalTwinModel = new DigitalTwinModel(this.digitalTwinId, this.digitalTwinStateManager, this.shadowingFunction, this.storageManager);
237237

238238
//Save the Model Engine as Digital Twin Life Cycle Listener
239-
addLifeCycleListener(this.modelEngine);
239+
addLifeCycleListener(this.digitalTwinModel);
240240

241241
// Execute Storage Manager
242242
executeStorageManager();
@@ -246,7 +246,7 @@ private void init(ShadowingFunction shadowingFunction) throws ModelException, Wl
246246
* Executes the model engine in a dedicated thread.
247247
*/
248248
private void executeModelEngine(){
249-
modelEngineThread = new Thread(this.modelEngine);
249+
modelEngineThread = new Thread(this.digitalTwinModel);
250250
modelEngineThread.setName(String.format("%s-model-engine", this.getId()));
251251
modelEngineThread.start();
252252
}
@@ -632,8 +632,8 @@ protected void stopLifeCycle(){
632632
//Stop and Notify Model Engine
633633
this.modelEngineThread.interrupt();
634634
this.modelEngineThread = null;
635-
this.modelEngine.onWorkerStop();
636-
removeLifeCycleListener(this.modelEngine);
635+
this.digitalTwinModel.onWorkerStop();
636+
removeLifeCycleListener(this.digitalTwinModel);
637637

638638
//Stop and Notify Physical Adapters
639639
this.physicalAdapterExecutor.shutdownNow();

src/main/java/it/wldt/core/model/ModelEngine.java renamed to src/main/java/it/wldt/core/model/DigitalTwinModel.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import it.wldt.adapter.physical.PhysicalAssetDescription;
44
import it.wldt.core.engine.LifeCycleListener;
5-
import it.wldt.core.event.EventManager;
65
import it.wldt.core.state.DigitalTwinState;
76
import it.wldt.core.state.DigitalTwinStateManager;
87
import it.wldt.exception.ModelException;
98
import it.wldt.exception.WldtRuntimeException;
109
import it.wldt.core.engine.DigitalTwinWorker;
1110
import it.wldt.exception.WldtWorkerException;
11+
import it.wldt.storage.StorageManager;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

@@ -19,19 +19,29 @@
1919
* Marco Picone, Ph.D. (picone.m@gmail.com)
2020
* Date: 01/02/2023
2121
* Project: White Label Digital Twin Java Framework - (whitelabel-digitaltwin)
22-
*
2322
* This a fundamental core component responsible to handle the Model associated to the DT instance
2423
* maintaining its internal state and executing/coordinating its shadowing function
2524
*/
26-
public class ModelEngine extends DigitalTwinWorker implements LifeCycleListener {
25+
public class DigitalTwinModel extends DigitalTwinWorker implements LifeCycleListener {
2726

28-
private static final Logger logger = LoggerFactory.getLogger(ModelEngine.class);
27+
private static final Logger logger = LoggerFactory.getLogger(DigitalTwinModel.class);
2928

3029
private static final String MODEL_ENGINE_PUBLISHER_ID = "model_engine";
3130

31+
private String digitalTwinId = null;
32+
3233
private final ShadowingFunction shadowingFunction;
3334

34-
public ModelEngine(String digitalTwinId, DigitalTwinStateManager digitalTwinStateManager, ShadowingFunction shadowingFunction) throws ModelException, WldtWorkerException {
35+
/**
36+
* Digital Twin Model Constructor
37+
* @param digitalTwinId Digital Twin ID
38+
* @param digitalTwinStateManager Digital Twin State Manager
39+
* @param shadowingFunction Shadowing Function to be executed by the Model
40+
* @param storageManager Storage Manager to be used by the Model
41+
* @throws ModelException Model Exception
42+
* @throws WldtWorkerException Wldt Worker Exception
43+
*/
44+
public DigitalTwinModel(String digitalTwinId, DigitalTwinStateManager digitalTwinStateManager, ShadowingFunction shadowingFunction, StorageManager storageManager) throws ModelException, WldtWorkerException {
3545

3646
super();
3747

@@ -44,7 +54,7 @@ public ModelEngine(String digitalTwinId, DigitalTwinStateManager digitalTwinStat
4454

4555
//Init the Shadowing Model Function with the current Digital Twin State and call the associated onCreate method
4656
this.shadowingFunction = shadowingFunction;
47-
this.shadowingFunction.init(digitalTwinStateManager);
57+
this.shadowingFunction.init(digitalTwinStateManager, storageManager);
4858
this.shadowingFunction.onCreate();
4959
}
5060
else {

src/main/java/it/wldt/core/model/ShadowingFunction.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import it.wldt.exception.EventBusException;
1111
import it.wldt.exception.ModelException;
1212
import it.wldt.adapter.physical.event.*;
13+
import it.wldt.storage.StorageManager;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
1516
import java.util.List;
@@ -35,17 +36,44 @@ public abstract class ShadowingFunction implements WldtEventListener {
3536

3637
private String id = null;
3738

39+
/**
40+
* Event Filter used to manage the subscription to the Physical Events
41+
*/
3842
private WldtEventFilter physicalEventsFilter = null;
3943

44+
/**
45+
* Reference to the Digital Twin State Manager
46+
*/
4047
protected DigitalTwinStateManager digitalTwinStateManager = null;
4148

49+
/**
50+
* Reference to the Storage Manager
51+
*/
52+
protected StorageManager storageManager = null;
53+
54+
/**
55+
* Reference to the Shadowing Model Listener
56+
*/
4257
private ShadowingModelListener shadowingModelListener;
4358

59+
/**
60+
* Default Constructor
61+
* @param id Unique Identifier of the Shadowing Model Function
62+
*/
4463
public ShadowingFunction(String id){
4564
this.id = id;
4665
this.physicalEventsFilter = new WldtEventFilter();
4766
}
4867

68+
/**
69+
* Initialize the Shadowing Model Function with the current Digital Twin State Manager
70+
* @param digitalTwinStateManager DigitalTwinStateManager instance
71+
*/
72+
protected void init(DigitalTwinStateManager digitalTwinStateManager, StorageManager storageManager){
73+
this.digitalTwinStateManager = digitalTwinStateManager;
74+
this.storageManager = storageManager;
75+
}
76+
4977
/**
5078
*
5179
* @param physicalAssetProperty
@@ -391,14 +419,6 @@ public void onEvent(WldtEvent<?> wldtEvent) {
391419

392420
}
393421

394-
/**
395-
* Initialize the Shadowing Model Function with the current Digital Twin State Manager
396-
* @param digitalTwinStateManager DigitalTwinStateManager instance
397-
*/
398-
protected void init(DigitalTwinStateManager digitalTwinStateManager){
399-
this.digitalTwinStateManager = digitalTwinStateManager;
400-
}
401-
402422
abstract protected void onCreate();
403423

404424
abstract protected void onStart();

src/main/java/it/wldt/storage/StorageManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import it.wldt.storage.query.QueryResult;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
29+
import java.util.ArrayList;
2930
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
@@ -462,4 +463,19 @@ public void setQueryManager(QueryManager queryManager) throws StorageException {
462463
else
463464
throw new StorageException("The query manager cannot be null !");
464465
}
466+
467+
/**
468+
* Check if a target Storage Id is available in the Storage Manager
469+
*/
470+
public boolean isStorageAvailable(String storageId){
471+
return this.storageMap.containsKey(storageId);
472+
}
473+
474+
/**
475+
* Return the list of id of the WldtStorage in the StorageManager
476+
* @return The list of id of the WldtStorage in the StorageManager
477+
*/
478+
public List<String> getStorageIdList() {
479+
return new ArrayList<>(this.storageMap.keySet());
480+
}
465481
}

src/main/java/it/wldt/storage/query/DefaultQueryManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
import java.util.Map;
1111
import java.util.Optional;
1212

13+
/**
14+
* Authors:
15+
* Marco Picone, Ph.D. (picone.m@gmail.com)
16+
* Date: 25/07/2024
17+
* Default Query Manager Class
18+
* This class is responsible to manage the query request received by the Query Engine.
19+
* The class is responsible to handle the query request and return the query result to the caller.
20+
* This class extends the QueryManager class and implements the default behavior for the query management.
21+
*/
1322
public class DefaultQueryManager extends QueryManager{
1423

1524
private static final Logger logger = LoggerFactory.getLogger(DefaultQueryManager.class);

src/main/java/it/wldt/storage/query/IQueryResultListener.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package it.wldt.storage.query;
22

3+
/**
4+
* Authors:
5+
* Marco Picone, Ph.D. (picone.m@gmail.com)
6+
* Date: 25/07/2024
7+
* This interface represents the Query Result Listener used to receive the query results
8+
*/
39
public interface IQueryResultListener {
410

511
public void onQueryResult(QueryResult<?> queryResult);

src/main/java/it/wldt/storage/query/QueryExecutor.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
import java.util.concurrent.TimeUnit;
1313
import java.util.concurrent.TimeoutException;
1414

15+
/**
16+
* Authors:
17+
* Marco Picone, Ph.D. (picone.m@gmail.com)
18+
* Date: 25/07/2024
19+
* This class represents the Query Executor used to execute queries on the storage system
20+
* supporting both synchronous and asynchronous query execution. Internally is implemented through
21+
* an event-based mechanism to handle the query request and response.
22+
*/
1523
public class QueryExecutor implements WldtEventListener {
1624

1725
private static final Logger logger = LoggerFactory.getLogger(WldtEventObserver.class);
@@ -28,6 +36,11 @@ public class QueryExecutor implements WldtEventListener {
2836

2937
private Map<String, IQueryResultListener> queryResultListenerMap = null;
3038

39+
/**
40+
* Default Constructor
41+
* @param digitalTwinId Digital Twin Id
42+
* @param queryExecutorId Query Executor Id
43+
*/
3144
public QueryExecutor(String digitalTwinId, String queryExecutorId) {
3245
// Set the Digital Twin Id and the Query Executor Id and create the Query Result Filter
3346
this.digitalTwinId = digitalTwinId;
@@ -153,6 +166,11 @@ public void onEvent(WldtEvent<?> wldtEvent) {
153166
}
154167
}
155168

169+
/**
170+
* Asynchronous Query Execution
171+
* @param queryRequest Query Request Object
172+
* @param queryResultListener Query Result Listener to be used to receive the query result
173+
*/
156174
public void asyncQueryExecute(QueryRequest queryRequest, IQueryResultListener queryResultListener) {
157175
try{
158176

src/main/java/it/wldt/storage/query/QueryManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
import java.util.Map;
88
import java.util.Optional;
99

10+
11+
/**
12+
* Authors:
13+
* Marco Picone, Ph.D. (picone.m@gmail.com)
14+
* Date: 25/07/2024
15+
* This class represents the Query Manager responsible to handle the query request and manage the query execution.
16+
* This class is designed to be extended by the user to implement the desired query management logic.
17+
*/
1018
public class QueryManager {
1119

1220
private static final Logger logger = LoggerFactory.getLogger(DefaultQueryManager.class);

src/main/java/it/wldt/storage/query/QueryRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import java.util.UUID;
44

5+
/**
6+
* Authors:
7+
* Marco Picone, Ph.D. (picone.m@gmail.com)
8+
* Date: 25/07/2024
9+
* This class represents a Query Request to be sent to the Query Executor
10+
* The Query Request contains all the information needed to perform a query on the storage system
11+
*/
512
public class QueryRequest {
613

714
// Query Resource Type

src/main/java/it/wldt/storage/query/QueryRequestType.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package it.wldt.storage.query;
22

3+
/**
4+
* Authors:
5+
* Marco Picone, Ph.D. (picone.m@gmail.com)
6+
* Date: 25/07/2024
7+
* This Enum represents the Query Request Type used to specify the type of query to be performed
8+
* on the storage system (e.g., Time Range Query, Sample Range Query, Last Value Query, Count Query)
9+
*/
310
public enum QueryRequestType {
411

512
TIME_RANGE("TIME_RANGE"),

0 commit comments

Comments
 (0)