public class ModelReplacementManager extends Object
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 and Description |
|---|
ModelReplacementManager(ModelSetter ms) |
| Modifier and Type | Method and Description |
|---|---|
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.
|
public ModelReplacementManager(ModelSetter ms)
public void startUsingModel()
public void finishUsingModel()
public void replaceModel(MaxentModel model)
model - The new model which is being swapped in.Copyright © 2019 The Apache Software Foundation. All rights reserved.