|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectopennlp.maxent.ModelReplacementManager
public class ModelReplacementManager
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.
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 |
---|
public ModelReplacementManager(ModelSetter ms)
Method Detail |
---|
public void startUsingModel()
public void finishUsingModel()
public void replaceModel(MaxentModel model)
model
- The new model which is being swapped in.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |