GateNLP / gateplugin-LearningFramework

A plugin for the GATE language technology framework for training and using machine learning models. Currently supports Mallet (MaxEnt, NaiveBayes, CRF and others), LibSVM, Scikit-Learn, Weka, and DNNs through Pytorch and Keras.
https://gatenlp.github.io/gateplugin-LearningFramework/
GNU Lesser General Public License v2.1
26 stars 6 forks source link

Re-implement how we handle duplication #107

Open johann-petrak opened 5 years ago

johann-petrak commented 5 years ago

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