Debian Codegraph @

src/dr/inference/model/Model.java

raw symbols SCIP
1 /*
2 * Model.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.util.Identifiable;
29
30 import java.io.Serializable;
31 import java.util.*;
32
33 /**
34 * An interface that describes a model of some data.
35 *
36 * @version $Id: Model.java,v 1.6 2005/05/24 20:26:00 rambaut Exp $
37 *
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 */
41
42 public interface Model extends Identifiable, Serializable {
43
44 /**
45 * Adds a listener that is notified when the this model changes.
46 */
47 void addModelListener(ModelListener listener);
48
49 /**
50 * Remove a listener previously addeed by addModelListener
51 * @param listener
52 */
53 void removeModelListener(ModelListener listener);
54
55 /**
56 * This function should be called to store the state of the
57 * entire model. This makes the model state invalid until either
58 * an acceptModelState or restoreModelState is called.
59 */
60 void storeModelState();
61
62 /**
63 * This function should be called to restore the state of the entire model.
64 */
65 void restoreModelState();
66
67 /**
68 * This function should be called to accept the state of the entire model
69 */
70 void acceptModelState();
71
72 /**
73 * @return whether this model is in a valid state
74 */
75 boolean isValidState();
76
77 /**
78 * @return the total number of sub-models
79 */
80 int getModelCount();
81
82 /**
83 * @return the ith sub-model
84 */
85 Model getModel(int i);
86
87 /**
88 * @return the total number of variable in this model
89 */
90 int getVariableCount();
91
92 /**
93 * @return the ith variable
94 */
95 Variable getVariable(int i);
96
97 /**
98 * @return the variable of the component with a given name
99 */
100 //Parameter getParameter(String name);
101
102 /**
103 * @return the name of this model
104 */
105 String getModelName();
106
107 /**
108 * is the model being listened to by another or by a likelihood?
109 * @return
110 */
111 boolean isUsed();
112
113 /**
114 * A helper class for storing listeners and firing events.
115 */
116 public class ListenerHelper implements Serializable {
117
118 public void fireModelChanged(Model model) {
119 fireModelChanged(model, model, -1);
120 }
121
122 public void fireModelChanged(Model model, Object object) {
123 fireModelChanged(model, object, -1);
124 }
125
126 public void fireModelChanged(Model model, Object object, int index) {
127 if (listeners != null) {
128 for (ModelListener listener : listeners) {
129 listener.modelChangedEvent(model, object, index);
130 }
131 }
132 }
133
134 public void addModelListener(ModelListener listener) {
135 if (listeners == null) {
136 listeners = new java.util.ArrayList<ModelListener>();
137 }
138 listeners.add(listener);
139 }
140
141 public void removeModelListener(ModelListener listener) {
142 if (listeners != null) {
143 listeners.remove(listener);
144 }
145 }
146
147 public void addModelRestoreListener(ModelListener listener) {
148 if (restoreListeners == null) {
149 restoreListeners = new java.util.ArrayList<ModelListener>();
150 }
151 restoreListeners.add(listener);
152 }
153
154 public void fireModelRestored(Model model) {
155 if (restoreListeners != null) {
156 for (ModelListener listener : restoreListeners ) {
157 listener.modelRestored(model);
158 }
159 }
160 }
161
162 public int getListenerCount() {
163 return listeners != null ? listeners.size() : 0;
164 }
165
166 private ArrayList<ModelListener> listeners = null;
167
168 private ArrayList<ModelListener> restoreListeners = null;
169 }
170
171
172 // set to store all created models
173 final static Set<Model> FULL_MODEL_SET = new HashSet<Model>();
174 final static Set<Model> CONNECTED_MODEL_SET = new HashSet<Model>();
175
176 }
177