| 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.GammaDistributionModel; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
public class GammaDistributionModelParser extends AbstractXMLObjectParser { |
| 33 |
|
| 34 |
public static final String MEAN = "mean"; |
| 35 |
public static final String SHAPE = "shape"; |
| 36 |
public static final String SCALE = "scale"; |
| 37 |
public static final String RATE = "rate"; |
| 38 |
public static final String OFFSET = "offset"; |
| 39 |
|
| 40 |
public String getParserName() { |
| 41 |
return GammaDistributionModel.GAMMA_DISTRIBUTION_MODEL; |
| 42 |
} |
| 43 |
|
| 44 |
private Parameter getParameterOrValue(String name, XMLObject xo) throws XMLParseException { |
| 45 |
Parameter parameter; |
| 46 |
if (xo.hasChildNamed(name)) { |
| 47 |
XMLObject cxo = xo.getChild(name); |
| 48 |
|
| 49 |
parameter = (Parameter)cxo.getChild(Parameter.class); |
| 50 |
if (parameter == null) { |
| 51 |
if (cxo.getChildCount() < 1) { |
| 52 |
throw new XMLParseException("Distribution parameter, " + name + ", is missing a value or parameter element"); |
| 53 |
} |
| 54 |
try { |
| 55 |
double value = cxo.getDoubleChild(0); |
| 56 |
parameter = new Parameter.Default(value); |
| 57 |
} catch (XMLParseException xpe) { |
| 58 |
throw new XMLParseException("Distribution parameter, " + name + ", has bad value: " + xpe.getMessage()); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return parameter; |
| 63 |
} else { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 70 |
|
| 71 |
double offset = xo.getAttribute(OFFSET, 0.0); |
| 72 |
|
| 73 |
Parameter shapeParameter = getParameterOrValue(SHAPE, xo); |
| 74 |
|
| 75 |
Parameter parameter2; |
| 76 |
GammaDistributionModel.GammaParameterizationType parameterization; |
| 77 |
|
| 78 |
if (xo.hasChildNamed(SCALE)) { |
| 79 |
parameter2 = getParameterOrValue(SCALE, xo); |
| 80 |
parameterization = GammaDistributionModel.GammaParameterizationType.ShapeScale; |
| 81 |
} else if (xo.hasChildNamed(RATE)) { |
| 82 |
parameter2 = getParameterOrValue(RATE, xo); |
| 83 |
parameterization = GammaDistributionModel.GammaParameterizationType.ShapeRate; |
| 84 |
} else if (xo.hasChildNamed(MEAN)) { |
| 85 |
parameter2 = getParameterOrValue(MEAN, xo); |
| 86 |
parameterization = GammaDistributionModel.GammaParameterizationType.ShapeMean; |
| 87 |
} else { |
| 88 |
parameter2 = null; |
| 89 |
parameterization = GammaDistributionModel.GammaParameterizationType.OneParameter; |
| 90 |
} |
| 91 |
|
| 92 |
return new GammaDistributionModel(parameterization, shapeParameter, parameter2, offset); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 100 |
return rules; |
| 101 |
} |
| 102 |
|
| 103 |
private final XMLSyntaxRule[] rules = { |
| 104 |
new ElementRule(SHAPE, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Shape parameter"), |
| 105 |
new ElementRule(SCALE, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Scale parameter", true), |
| 106 |
new ElementRule(RATE, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Rate parameter", true), |
| 107 |
new ElementRule(MEAN, new XMLSyntaxRule[]{new ElementRule(Parameter.class, true)}, "Mean parameter", true), |
| 108 |
AttributeRule.newDoubleRule(OFFSET, true) |
| 109 |
}; |
| 110 |
|
| 111 |
public String getParserDescription() { |
| 112 |
return "The gamma probability distribution."; |
| 113 |
} |
| 114 |
|
| 115 |
public Class getReturnType() { |
| 116 |
return GammaDistributionModel.class; |
| 117 |
} |
| 118 |
} |