gateplugin-LearningFramework
gateplugin-LearningFramework copied to clipboard
Re-implement how we handle duplication
Instead of the current approach that uses CustomDuplication and relies on a specific order in which the instances get initialized and the controllerStarted callback invoked, do something as suggested by @ianroberts
private MLEngine[] engineHolder;
@Sharable
// getter and setter for engineHolder
public Resource init() {
if(engineHolder == null) {
engineHolder = new MLEngine[1];
}
}
public void reInit() {
engineHolder = null;
}
public void controllerExecutionStarted() {
synchronized(ThisClass.class) {
if(engineHolder[0] == null) {
engineHolder[0] = createEngine();
engineOwner = true;
} else {
engineOwner = false;
}
}
}
public void execute() {
// guaranteed to be not-null,
MLEngine engine = engineHolder[0];
}
public void controllerExecutionFinished() {
if(engineOwner) {
doStuffWith(engineHolder[0]);
}
}