| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.model; |
| 27 |
|
| 28 |
import dr.inference.parallel.MPISerializable; |
| 29 |
import dr.util.Keywordable; |
| 30 |
import org.w3c.dom.Document; |
| 31 |
import org.w3c.dom.Element; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public abstract class AbstractModel implements Model, ModelListener, VariableListener, StatisticList, MPISerializable, Keywordable { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public AbstractModel(String name) { |
| 49 |
this.name = name; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public void addModel(Model model) { |
| 57 |
Model.CONNECTED_MODEL_SET.add(model); |
| 58 |
|
| 59 |
if (!models.contains(model)) { |
| 60 |
models.add(model); |
| 61 |
model.addModelListener(this); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public void removeModel(Model model) { |
| 66 |
models.remove(model); |
| 67 |
model.removeModelListener(this); |
| 68 |
} |
| 69 |
|
| 70 |
public int getModelCount() { |
| 71 |
return models.size(); |
| 72 |
} |
| 73 |
|
| 74 |
public final Model getModel(int i) { |
| 75 |
return models.get(i); |
| 76 |
} |
| 77 |
|
| 78 |
public final void addVariable(Variable variable) { |
| 79 |
if (variable instanceof Parameter) { |
| 80 |
Parameter.CONNECTED_PARAMETER_SET.add((Parameter)variable); |
| 81 |
} |
| 82 |
|
| 83 |
if (!variables.contains(variable)) { |
| 84 |
variables.add(variable); |
| 85 |
variable.addVariableListener(this); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if (variable instanceof Statistic) addStatistic((Statistic) variable); |
| 90 |
} |
| 91 |
|
| 92 |
public final void removeVariable(Variable variable) { |
| 93 |
variables.remove(variable); |
| 94 |
variable.removeVariableListener(this); |
| 95 |
|
| 96 |
|
| 97 |
if (variable instanceof Statistic) removeStatistic((Statistic) variable); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public final boolean hasVariable(Variable parameter) { |
| 105 |
return variables.contains(parameter); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void addModelListener(ModelListener listener) { |
| 112 |
listenerHelper.addModelListener(listener); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void removeModelListener(ModelListener listener) { |
| 119 |
listenerHelper.removeModelListener(listener); |
| 120 |
} |
| 121 |
|
| 122 |
public void addModelRestoreListener(ModelListener listener) { |
| 123 |
listenerHelper.addModelRestoreListener(listener); |
| 124 |
} |
| 125 |
|
| 126 |
public boolean isUsed() { |
| 127 |
return listenerHelper.getListenerCount() > 0; |
| 128 |
} |
| 129 |
|
| 130 |
public boolean isVariable() { return true; } |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public void fireModelChanged() { |
| 136 |
listenerHelper.fireModelChanged(this, this, -1); |
| 137 |
} |
| 138 |
|
| 139 |
public void fireModelChanged(Object object) { |
| 140 |
listenerHelper.fireModelChanged(this, object, -1); |
| 141 |
} |
| 142 |
|
| 143 |
public void fireModelChanged(Object object, int index) { |
| 144 |
listenerHelper.fireModelChanged(this, object, index); |
| 145 |
} |
| 146 |
|
| 147 |
public final int getVariableCount() { |
| 148 |
return variables.size(); |
| 149 |
} |
| 150 |
|
| 151 |
public final Variable getVariable(int i) { |
| 152 |
return variables.get(i); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public void sendState(int toRank) { |
| 161 |
|
| 162 |
|
| 163 |
for (Model model : models) { |
| 164 |
((AbstractModel) model).sendState(toRank); |
| 165 |
} |
| 166 |
|
| 167 |
for (Variable variable : variables) { |
| 168 |
if (variable instanceof Parameter.Abstract) ((Parameter.Abstract) variable).sendState(toRank); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
public void sendStateNoParameters(int toRank) { |
| 174 |
|
| 175 |
|
| 176 |
for (Model model : models) { |
| 177 |
((AbstractModel) model).sendState(toRank); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public void receiveStateNoParameters(int fromRank) { |
| 182 |
for (Model model : models) { |
| 183 |
((AbstractModel) model).receiveState(fromRank); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public void receiveState(int fromRank) { |
| 188 |
for (Model model : models) { |
| 189 |
((AbstractModel) model).receiveState(fromRank); |
| 190 |
} |
| 191 |
|
| 192 |
for (Variable variable : variables) { |
| 193 |
if (variable instanceof Parameter.Abstract) |
| 194 |
((Parameter.Abstract) variable).receiveState(fromRank); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public final void modelChangedEvent(Model model, Object object, int index) { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
handleModelChangedEvent(model, object, index); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
public void modelRestored(Model model) { |
| 220 |
} |
| 221 |
|
| 222 |
abstract protected void handleModelChangedEvent(Model model, Object object, int index); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public final void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 229 |
handleVariableChangedEvent(variable, index, type); |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
listenerHelper.fireModelChanged(this, variable, index); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
protected abstract void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public final void storeModelState() { |
| 252 |
if (isValidState) { |
| 253 |
|
| 254 |
|
| 255 |
for (Model m : models) { |
| 256 |
|
| 257 |
m.storeModelState(); |
| 258 |
} |
| 259 |
|
| 260 |
for (Variable variable : variables) { |
| 261 |
|
| 262 |
variable.storeVariableValues(); |
| 263 |
} |
| 264 |
|
| 265 |
storeState(); |
| 266 |
isValidState = false; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
public final void restoreModelState() { |
| 271 |
if (!isValidState) { |
| 272 |
|
| 273 |
|
| 274 |
for (Variable variable : variables) { |
| 275 |
variable.restoreVariableValues(); |
| 276 |
} |
| 277 |
for (Model m : models) { |
| 278 |
m.restoreModelState(); |
| 279 |
} |
| 280 |
|
| 281 |
restoreState(); |
| 282 |
isValidState = true; |
| 283 |
|
| 284 |
listenerHelper.fireModelRestored(this); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
public final void acceptModelState() { |
| 289 |
if (!isValidState) { |
| 290 |
|
| 291 |
|
| 292 |
for (Variable variable : variables) { |
| 293 |
variable.acceptVariableValues(); |
| 294 |
} |
| 295 |
|
| 296 |
for (Model m : models) { |
| 297 |
m.acceptModelState(); |
| 298 |
} |
| 299 |
|
| 300 |
acceptState(); |
| 301 |
|
| 302 |
isValidState = true; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
public boolean isValidState() { |
| 307 |
return isValidState; |
| 308 |
} |
| 309 |
|
| 310 |
public final String getModelName() { |
| 311 |
return name; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
protected abstract void storeState(); |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
protected abstract void restoreState(); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
protected abstract void acceptState(); |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public final void addStatistic(Statistic statistic) { |
| 338 |
if (!statistics.contains(statistic)) { |
| 339 |
statistics.add(statistic); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
public final void removeStatistic(Statistic statistic) { |
| 344 |
|
| 345 |
statistics.remove(statistic); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public int getStatisticCount() { |
| 352 |
|
| 353 |
return statistics.size(); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public Statistic getStatistic(int i) { |
| 360 |
|
| 361 |
return statistics.get(i); |
| 362 |
} |
| 363 |
|
| 364 |
public final Statistic getStatistic(String name) { |
| 365 |
|
| 366 |
for (int i = 0; i < getStatisticCount(); i++) { |
| 367 |
Statistic statistic = getStatistic(i); |
| 368 |
if (name.equals(statistic.getStatisticName())) { |
| 369 |
return statistic; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return null; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private String id = null; |
| 381 |
|
| 382 |
public void setId(String id) { |
| 383 |
this.id = id; |
| 384 |
} |
| 385 |
|
| 386 |
public String getId() { |
| 387 |
return id; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
public String toString() { |
| 392 |
if (id != null) { |
| 393 |
return id; |
| 394 |
} else if (name != null) { |
| 395 |
return name; |
| 396 |
} |
| 397 |
return super.toString(); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
@Override |
| 405 |
public void addKeyword(String keyword) { |
| 406 |
keywords.add(keyword); |
| 407 |
} |
| 408 |
|
| 409 |
@Override |
| 410 |
public List<String> getKeywords() { |
| 411 |
return keywords; |
| 412 |
} |
| 413 |
|
| 414 |
private final List<String> keywords = new ArrayList<String>(); |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public Element createElement(Document d) { |
| 421 |
throw new RuntimeException("Not implemented!"); |
| 422 |
} |
| 423 |
|
| 424 |
boolean isValidState = true; |
| 425 |
|
| 426 |
protected Model.ListenerHelper listenerHelper = new Model.ListenerHelper(); |
| 427 |
|
| 428 |
private final ArrayList<Model> models = new ArrayList<Model>(); |
| 429 |
private final ArrayList<Variable> variables = new ArrayList<Variable>(); |
| 430 |
private final ArrayList<Statistic> statistics = new ArrayList<Statistic>(); |
| 431 |
|
| 432 |
private final String name; |
| 433 |
} |