| 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.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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
abstract ParametricDistributionModel parseDistributionModel(Parameter[] parameters, double offset); |
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
} |