opennlp.maxent
Class ModelReplacementManager

java.lang.Object
  extended by opennlp.maxent.ModelReplacementManager

public class ModelReplacementManager
extends java.lang.Object

A object which can be used to ensure that a Maxent application can swap the model currently in use with a new one in a thread-safe manner without stopping the servicing of requests. Use this if your maxent application is a heavy-weight one or you have only one particular MaxentModel to use with your application. If your application class is lightweight and you will be creating multiple instances of it with different underlying models, consider using a DomainToModelMap object to ensure thread-safe model swapping.

For example, in your application, create a ModelReplacementManager as follows:

     private final ModelReplacementManager replacementManager =
          new ModelReplacementManager(
              new ModelSetter() {
                  public void setModel(MaxentModel m) {
                      model = m;
                  }
              }
          );
     
where "model" would be the actual variable name of the model used by your application which you wish to be able to swap (you might have other models which need their own ModelReplacementManager).

You'll also need a method to swap the model which calls the manager's replaceModel(MaxentModel m) method, e.g.,

     public void replaceModel (MaxentModel newmod) {
          replacementManager.replaceModel(newmod);
    }
     
Then, in the code that uses the model, you need to inform the ModelReplacementManager when a thread is beginning to use the model and when it no longer needs to be sure that the same model is being used. For example, it is quite common to evaluate a particular context, get back a double[] which has the normalized probabilities of each of the outcomes given that context, and then request the name of a particular outcome. The model cannot be swapped during that time since the mapping from outcome labels to unique will (probably) be different between the different models. So, do as follows:
          replacementManager.startUsingModel();
            // some code which evaluates the context, e.g.,
            double[] probs = model.eval(someContext);
            // some code which returns a particular outcome
            if (model.getBestOutcome(probs).equals("T") ...
          replacementManager.finishUsingModel();
     
The manager will then make sure that all requests which are currently being serviced are completed before the new model is swapped in. New requests which are made while the models are being swapped are forced to wait for the swap to finish. These requests will then be serviced by the new model.

Version:
$Revision: 1.1.1.1 $, $Date: 2001/10/23 14:06:53 $
Author:
Jason Baldridge

Constructor Summary
ModelReplacementManager(ModelSetter ms)
           
 
Method Summary
 void finishUsingModel()
          Inform the manager that a thread is done using the model, and thus is not dependending on it being unchanged.
 void replaceModel(MaxentModel model)
          Replace the old model with a new one, forcing the replacement to wait until all threads using the old model have finished using it.
 void startUsingModel()
          Inform the manager that a thread is using the model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModelReplacementManager

public ModelReplacementManager(ModelSetter ms)
Method Detail

startUsingModel

public void startUsingModel()
Inform the manager that a thread is using the model. If a replacement is underway, the thread is forced to join the replacement thread and thus wait until it is finished to begin using the model.


finishUsingModel

public void finishUsingModel()
Inform the manager that a thread is done using the model, and thus is not dependending on it being unchanged.


replaceModel

public void replaceModel(MaxentModel model)
Replace the old model with a new one, forcing the replacement to wait until all threads using the old model have finished using it.

Parameters:
model - The new model which is being swapped in.


Copyright © 2005 Jason Baldridge, Gann Bierner, and Thomas Morton. All Rights Reserved.