Debian Codegraph @

src/dr/inferencexml/distribution/TruncatedNormalDistributionModelParser.java

raw symbols SCIP
1 /*
2 * TruncatedNormalDistributionModelParser.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.TruncatedNormalDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.xml.*;
31
32 /**
33 * Reads a normal distribution model from a DOM Document element.
34 */
35 public class TruncatedNormalDistributionModelParser extends AbstractXMLObjectParser {
36
37 public static final String TRUNCATED_NORMAL_DISTRIBUTION_MODEL = "truncatedNormalDistributionModel";
38 public static final String MEAN = "mean";
39 public static final String STDEV = "stdev";
40 public static final String MINIMUM = "minimum";
41 public static final String MAXIMUM = "maximum";
42 public static final String PREC = "precision";
43
44 public String getParserName() {
45 return TRUNCATED_NORMAL_DISTRIBUTION_MODEL;
46 }
47
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 Parameter meanParam;
51 Parameter stdevParam;
52 Parameter minParam;
53 Parameter maxParam;
54 Parameter precParam;
55
56 XMLObject cxo = xo.getChild(MEAN);
57 if (cxo.getChild(0) instanceof Parameter) {
58 meanParam = (Parameter) cxo.getChild(Parameter.class);
59 } else {
60 meanParam = new Parameter.Default(cxo.getDoubleChild(0));
61 }
62
63 if(xo.hasChildNamed(MINIMUM)){
64 cxo = xo.getChild(MINIMUM);
65 if(cxo.getChild(0) instanceof Parameter) {
66 minParam = (Parameter) cxo.getChild(Parameter.class);
67 } else {
68 minParam = new Parameter.Default(cxo.getDoubleChild(0));
69 }
70 } else {
71 minParam = new Parameter.Default(Double.NEGATIVE_INFINITY);
72 }
73
74 if(xo.hasChildNamed(MAXIMUM)){
75 cxo = xo.getChild(MAXIMUM);
76 if(cxo.getChild(0) instanceof Parameter) {
77 maxParam = (Parameter) cxo.getChild(Parameter.class);
78 } else {
79 maxParam = new Parameter.Default(cxo.getDoubleChild(0));
80 }
81 } else {
82 maxParam = new Parameter.Default(Double.POSITIVE_INFINITY);
83 }
84
85 if (xo.getChild(STDEV) != null) {
86
87 cxo = xo.getChild(STDEV);
88 if (cxo.getChild(0) instanceof Parameter) {
89 stdevParam = (Parameter) cxo.getChild(Parameter.class);
90 } else {
91 stdevParam = new Parameter.Default(cxo.getDoubleChild(0));
92 }
93
94 return new TruncatedNormalDistributionModel(meanParam, stdevParam, minParam, maxParam);
95 }
96
97 cxo = xo.getChild(PREC);
98 if (cxo.getChild(0) instanceof Parameter) {
99 precParam = (Parameter) cxo.getChild(Parameter.class);
100 } else {
101 precParam = new Parameter.Default(cxo.getDoubleChild(0));
102 }
103 return new TruncatedNormalDistributionModel(meanParam, precParam, minParam, maxParam, true);
104 }
105
106 //************************************************************************
107 // AbstractXMLObjectParser implementation
108 //************************************************************************
109
110 public XMLSyntaxRule[] getSyntaxRules() {
111 return rules;
112 }
113
114 private final XMLSyntaxRule[] rules = {
115 new ElementRule(MEAN,
116 new XMLSyntaxRule[]{
117 new XORRule(
118 new ElementRule(Parameter.class),
119 new ElementRule(Double.class)
120 )}
121 ),
122 new XORRule(
123 new ElementRule(STDEV,
124 new XMLSyntaxRule[]{
125 new XORRule(
126 new ElementRule(Parameter.class),
127 new ElementRule(Double.class)
128 )}
129 ),
130 new ElementRule(PREC,
131 new XMLSyntaxRule[]{
132 new XORRule(
133 new ElementRule(Parameter.class),
134 new ElementRule(Double.class)
135 )}
136 )
137 ),
138 new OrRule(
139 new ElementRule(MINIMUM,
140 new XMLSyntaxRule[]{
141 new XORRule(
142 new ElementRule(Parameter.class),
143 new ElementRule(Double.class)
144 )}
145 ),
146 new ElementRule(MAXIMUM,
147 new XMLSyntaxRule[]{
148 new XORRule(
149 new ElementRule(Parameter.class),
150 new ElementRule(Double.class)
151 )
152 }
153 )
154 )
155 };
156
157 public String getParserDescription() {
158 return "Describes a truncated normal distribution with a given mean, standard deviation and minimum or" +
159 "maximum (or both) values that can be used in a distributionLikelihood element";
160 }
161
162 public Class getReturnType() {
163 return TruncatedNormalDistributionModel.class;
164 }
165
166 }