Debian Codegraph @

src/dr/inferencexml/distribution/DistributionModelParser.java

raw symbols SCIP
1 /*
2 * DistributionModelParser.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.inferencexml.distribution;
27
28 import dr.inference.distribution.ParametricDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.xml.*;
31
32 /**
33 */
34 public abstract class DistributionModelParser extends AbstractXMLObjectParser {
35
36 public static final String OFFSET = "offset";
37 public static final String MEAN = "mean";
38 public static final String SHAPE = "shape";
39 public static final String SCALE = "scale";
40 public static final String RATE = "rate";
41
42 /**
43 * @param parameters an array of the parsed parameters, in order of the getParameterNames() array.
44 * @param offset the parsed offset
45 * @return a distribution model constructed from provided parameters and offset
46 */
47 abstract ParametricDistributionModel parseDistributionModel(Parameter[] parameters, double offset);
48
49 /**
50 * @return a list of xml element names for parameters of this distribution.
51 */
52 abstract String[] getParameterNames();
53
54 abstract boolean allowOffset();
55
56 public final Object parseXMLObject(XMLObject xo) throws XMLParseException {
57
58 double offset = xo.getAttribute(OFFSET, 0.0);
59
60 String[] names = getParameterNames();
61 Parameter[] parameters = new Parameter[names.length];
62 for (int i = 0; i < names.length; i++) {
63 parameters[i] = getParameter(xo, names[i]);
64 }
65
66 return parseDistributionModel(parameters, offset);
67 }
68
69 private Parameter getParameter(XMLObject xo, String parameterName) throws XMLParseException {
70 final XMLObject cxo = xo.getChild(parameterName);
71 return cxo.getChild(0) instanceof Parameter ?
72 (Parameter) cxo.getChild(Parameter.class) : new Parameter.Default(cxo.getDoubleChild(0));
73 }
74
75 public XMLSyntaxRule[] getSyntaxRules() {
76
77 String[] names = getParameterNames();
78
79 XMLSyntaxRule[] rules = new XMLSyntaxRule[names.length + (allowOffset() ? 1 : 0)];
80 for (int i = 0; i < names.length; i++) {
81 rules[i] = new XORRule(
82 new ElementRule(names[i], Double.class),
83 new ElementRule(names[i], Parameter.class)
84 );
85 }
86 if (allowOffset()) {
87 rules[rules.length - 1] = AttributeRule.newDoubleRule(OFFSET, true);
88 }
89
90 return rules;
91 }
92
93 }