Debian Codegraph @

src/dr/inference/model/AbstractModel.java

raw symbols SCIP
1 /*
2 * AbstractModel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * A model that brings together a number of model components
38 *
39 * @author Alexei Drummond
40 * @author Andrew Rambaut
41 * @version $Id: AbstractModel.java,v 1.13 2006/08/17 15:30:08 rambaut Exp $
42 */
43 public abstract class AbstractModel implements Model, ModelListener, VariableListener, StatisticList, MPISerializable, Keywordable {
44
45 /**
46 * @param name Model Name
47 */
48 public AbstractModel(String name) {
49 this.name = name;
50 }
51
52 /**
53 * Adds a sub-model to this model. If the model is already in the
54 * list then it does nothing.
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 // parameters are also statistics
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 // parameters are also statistics
97 if (variable instanceof Statistic) removeStatistic((Statistic) variable);
98 }
99
100 /**
101 * @param parameter
102 * @return true of the given parameter is contained in this model
103 */
104 public final boolean hasVariable(Variable parameter) {
105 return variables.contains(parameter);
106 }
107
108 /**
109 * Adds a model listener.
110 */
111 public void addModelListener(ModelListener listener) {
112 listenerHelper.addModelListener(listener);
113 }
114
115 /**
116 * remove a model listener.
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 * Fires a model changed event.
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 // MPI IMPLEMENTATION
157 // **************************************************************
158
159
160 public void sendState(int toRank) {
161
162 // Iterate through child models
163 for (Model model : models) {
164 ((AbstractModel) model).sendState(toRank);
165 }
166 // Send current model parameters
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 // Iterate through child models
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 // Send current model parameters
192 for (Variable variable : variables) {
193 if (variable instanceof Parameter.Abstract)
194 ((Parameter.Abstract) variable).receiveState(fromRank);
195 }
196
197
198 }
199
200 // **************************************************************
201 // ModelListener IMPLEMENTATION
202 // **************************************************************
203
204 public final void modelChangedEvent(Model model, Object object, int index) {
205
206 // String message = " model: " + getModelName() + "/" + getId() + " component: " + model.getModelName();
207 // if (object != null) {
208 // message += " object: " + object;
209 // }
210 // if (index != -1) {
211 // message += " index: " + index;
212 // }
213 // System.out.println(message);
214
215 handleModelChangedEvent(model, object, index);
216 }
217
218 // do nothing by default
219 public void modelRestored(Model model) {
220 }
221
222 abstract protected void handleModelChangedEvent(Model model, Object object, int index);
223
224 // **************************************************************
225 // VariableListener IMPLEMENTATION
226 // **************************************************************
227
228 public final void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
229 handleVariableChangedEvent(variable, index, type);
230
231 // todo AR - I am not sure this is required and may be overruling modelChange events on parts of the
232 // model. If a parameter changes it should be handleVariableChangedEvent() job to fireModelChanged
233 // events
234 listenerHelper.fireModelChanged(this, variable, index);
235 }
236
237 /**
238 * This method is called whenever a parameter is changed.
239 * <p/>
240 * It is strongly recommended that the model component sets a "dirty" flag and does no
241 * further calculations. Recalculation is typically done when the model component is asked for
242 * some information that requires them. This mechanism is 'lazy' so that this method
243 * can be safely called multiple times with minimal computational cost.
244 */
245 protected abstract void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type);
246
247 // **************************************************************
248 // Model IMPLEMENTATION
249 // **************************************************************
250
251 public final void storeModelState() {
252 if (isValidState) {
253 // System.out.println("STORE MODEL: " + getModelName() + "/" + getId() + "/" + getClass().getCanonicalName());
254
255 for (Model m : models) {
256 // System.out.println("\t" + m.getModelName() + "/" + m.getClass().getCanonicalName());
257 m.storeModelState();
258 }
259
260 for (Variable variable : variables) {
261 // System.out.println("\t" + variable.getVariableName() + "/" + variable.getClass().getCanonicalName());
262 variable.storeVariableValues();
263 }
264
265 storeState();
266 isValidState = false;
267 }
268 }
269
270 public final void restoreModelState() {
271 if (!isValidState) {
272 //System.out.println("RESTORE MODEL: " + getModelName() + "/" + getId());
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 //System.out.println("ACCEPT MODEL: " + getModelName() + "/" + getId());
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 * Additional state information, outside of the sub-model is stored by this call.
317 */
318 protected abstract void storeState();
319
320 /**
321 * After this call the model is guaranteed to have returned its extra state information to
322 * the values coinciding with the last storeState call.
323 * Sub-models are handled automatically and do not need to be considered in this method.
324 */
325 protected abstract void restoreState();
326
327 /**
328 * This call specifies that the current state is accept. Most models will not need to do anything.
329 * Sub-models are handled automatically and do not need to be considered in this method.
330 */
331 protected abstract void acceptState();
332
333 // **************************************************************
334 // StatisticList IMPLEMENTATION
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 * @return the number of statistics of this component.
350 */
351 public int getStatisticCount() {
352
353 return statistics.size();
354 }
355
356 /**
357 * @return the ith statistic of the component
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 // Identifiable IMPLEMENTATION
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 // Interface: Keywordable
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 // XMLElement IMPLEMENTATION
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 }