| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
} |