| 1 | /* |
| 2 | * CompoundModel.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 java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * An interface that describes a model of some data. |
| 33 | * |
| 34 | * @version $Id: CompoundModel.java,v 1.8 2005/05/24 20:25:59 rambaut Exp $ |
| 35 | * |
| 36 | * @author Alexei Drummond |
| 37 | * @author Andrew Rambaut |
| 38 | */ |
| 39 | |
| 40 | public class CompoundModel implements Model { |
| 41 | |
| 42 | public static final String COMPOUND_MODEL = "compoundModel"; |
| 43 | |
| 44 | public CompoundModel(String name) { this.name = name; } |
| 45 | |
| 46 | |
| 47 | public void addModel(Model model) { |
| 48 | |
| 49 | if ( !models.contains(model) ) { |
| 50 | models.add(model); |
| 51 | // add all listeners to this model |
| 52 | for( ModelListener ml : listeners ) { |
| 53 | model.addModelListener(ml); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public int getModelCount() { |
| 59 | return models.size(); |
| 60 | } |
| 61 | |
| 62 | public final Model getModel(int i) { |
| 63 | return models.get(i); |
| 64 | } |
| 65 | |
| 66 | public boolean isUsed() { |
| 67 | return listeners.size() > 0; |
| 68 | } |
| 69 | |
| 70 | public void addModelListener(ModelListener listener) { |
| 71 | // add listener to all models comprizing this compund model - a change in any one of them |
| 72 | // means the compund model changed |
| 73 | |
| 74 | listeners.add(listener); |
| 75 | for( Model m : models ) { |
| 76 | m.addModelListener(listener); |
| 77 | } |
| 78 | |
| 79 | //throw new IllegalArgumentException("Compound models don't have listeners"); |
| 80 | } |
| 81 | |
| 82 | public void removeModelListener(ModelListener listener) { |
| 83 | for( Model m : models ) { |
| 84 | m.removeModelListener(listener); |
| 85 | } |
| 86 | listeners.remove(listener); |
| 87 | // throw new IllegalArgumentException("Compound models don't have listeners"); |
| 88 | } |
| 89 | |
| 90 | public void storeModelState() { |
| 91 | for (Model model : models) { |
| 92 | model.storeModelState(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public void restoreModelState() { |
| 97 | for (Model model : models) { |
| 98 | model.restoreModelState(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public void acceptModelState() { |
| 103 | for (Model model : models) { |
| 104 | model.acceptModelState(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public boolean isValidState() { |
| 109 | |
| 110 | for (int i = 0; i < models.size(); i++) { |
| 111 | if (!getModel(i).isValidState()) { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | public int getVariableCount() { return 0; } |
| 120 | |
| 121 | public Variable getVariable(int index) { |
| 122 | throw new IllegalArgumentException("Compound models don't have parameters"); |
| 123 | } |
| 124 | |
| 125 | // ************************************************************** |
| 126 | // Identifiable IMPLEMENTATION |
| 127 | // ************************************************************** |
| 128 | |
| 129 | private String id = null; |
| 130 | |
| 131 | public void setId(String id) { this.id = id; } |
| 132 | |
| 133 | public String getId() { return id; } |
| 134 | |
| 135 | /** |
| 136 | * @return the name of this model |
| 137 | */ |
| 138 | public String getModelName() { return name; } |
| 139 | |
| 140 | /* AER - do we need a parser? |
| 141 | public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 142 | |
| 143 | public String getParserName() { return COMPOUND_MODEL; } |
| 144 | |
| 145 | public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 146 | |
| 147 | CompoundModel compoundModel = new CompoundModel("model"); |
| 148 | |
| 149 | int childCount = xo.getChildCount(); |
| 150 | for (int i = 0; i < childCount; i++) { |
| 151 | Object xoc = xo.getChild(i); |
| 152 | if (xoc instanceof Model) { |
| 153 | compoundModel.addModel((Model)xoc); |
| 154 | } |
| 155 | } |
| 156 | return compoundModel; |
| 157 | } |
| 158 | |
| 159 | //************************************************************************ |
| 160 | // AbstractXMLObjectParser implementation |
| 161 | //************************************************************************ |
| 162 | |
| 163 | public String getParserDescription() { |
| 164 | return "This element represents a combination of models."; |
| 165 | } |
| 166 | |
| 167 | public Class getReturnType() { return CompoundModel.class; } |
| 168 | |
| 169 | public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 170 | |
| 171 | private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 172 | new OneOrMoreRule(Model.class) |
| 173 | }; |
| 174 | };*/ |
| 175 | |
| 176 | private String name = null; |
| 177 | private final ArrayList<Model> models = new ArrayList<Model>(); |
| 178 | private final List<ModelListener> listeners = new ArrayList<ModelListener>(); |
| 179 | } |
| 180 |