gateplugin-LearningFramework icon indicating copy to clipboard operation
gateplugin-LearningFramework copied to clipboard

Re-implement how we handle duplication

Open johann-petrak opened this issue 6 years ago • 0 comments

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]);
  }
}

johann-petrak avatar Jan 31 '19 16:01 johann-petrak