Debian Codegraph @

src/dr/inferencexml/distribution/NormalDistributionModelParser.java

raw symbols SCIP
1 /*
2 * NormalDistributionModelParser.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.NormalDistributionModel;
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 NormalDistributionModelParser extends AbstractXMLObjectParser {
36
37 public static final String NORMAL_DISTRIBUTION_MODEL = "normalDistributionModel";
38 public static final String MEAN = "mean";
39 public static final String MU = "mu";
40 public static final String SCALE = "scale";
41 public static final String STDEV = "stdev";
42 public static final String PREC = "precision";
43
44 public String getParserName() {
45 return NORMAL_DISTRIBUTION_MODEL;
46 }
47
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 Parameter meanParam;
51 Parameter scaleParam;
52 Parameter precParam;
53
54 XMLObject cxo = xo.getChild(MEAN);
55 if (cxo.getChild(0) instanceof Parameter) {
56 meanParam = (Parameter) cxo.getChild(Parameter.class);
57 } else {
58 meanParam = new Parameter.Default(cxo.getDoubleChild(0));
59 }
60
61 cxo = xo.getChild(STDEV);
62 if (cxo == null) {
63 cxo = xo.getChild(SCALE);
64 }
65
66 if (cxo != null) {
67 if (cxo.getChild(0) instanceof Parameter) {
68 scaleParam = (Parameter) cxo.getChild(Parameter.class);
69 } else {
70 scaleParam = new Parameter.Default(cxo.getDoubleChild(0));
71 }
72
73 return new NormalDistributionModel(meanParam, scaleParam);
74 } else {
75 cxo = xo.getChild(PREC);
76 if (cxo.getChild(0) instanceof Parameter) {
77 precParam = (Parameter) cxo.getChild(Parameter.class);
78 } else {
79 precParam = new Parameter.Default(cxo.getDoubleChild(0));
80 }
81 }
82 return new NormalDistributionModel(meanParam, precParam, true);
83 }
84
85 //************************************************************************
86 // AbstractXMLObjectParser implementation
87 //************************************************************************
88
89 public XMLSyntaxRule[] getSyntaxRules() {
90 return rules;
91 }
92
93 private final XMLSyntaxRule[] rules = {
94 new ElementRule(MEAN,
95 new XMLSyntaxRule[]{
96 new XORRule(
97 new ElementRule(Parameter.class),
98 new ElementRule(Double.class)
99 )}
100 ),
101 new XORRule(
102 new XORRule(
103 new ElementRule(SCALE,
104 new XMLSyntaxRule[]{
105 new XORRule(
106 new ElementRule(Parameter.class),
107 new ElementRule(Double.class)
108 )}
109 ),
110 new ElementRule(STDEV,
111 new XMLSyntaxRule[]{
112 new XORRule(
113 new ElementRule(Parameter.class),
114 new ElementRule(Double.class)
115 )}
116 )
117 ),
118 new ElementRule(PREC,
119 new XMLSyntaxRule[]{
120 new XORRule(
121 new ElementRule(Parameter.class),
122 new ElementRule(Double.class)
123 )}
124 )
125 )
126 };
127
128 public String getParserDescription() {
129 return "Describes a normal distribution with a given mean and standard deviation " +
130 "that can be used in a distributionLikelihood element";
131 }
132
133 public Class getReturnType() {
134 return NormalDistributionModel.class;
135 }
136
137 }