| 1 | package dr.inferencexml.distribution; |
| 2 | |
| 3 | import dr.inference.distribution.IndependentNormalDistributionModel; |
| 4 | import dr.inference.model.Parameter; |
| 5 | import dr.xml.*; |
| 6 | |
| 7 | public class IndependentNormalDistributionModelParser extends AbstractXMLObjectParser { |
| 8 | public static String INDEPENDENT_NORMAL_DISTRIBUTION_MODEL = "independentNormalDistributionModel"; |
| 9 | public static String MEAN = "mean"; |
| 10 | public static String VARIANCE = "variance"; |
| 11 | public static String PRECISION = "precision"; |
| 12 | public static String DATA = "data"; |
| 13 | public static String ID = "id"; |
| 14 | |
| 15 | @Override |
| 16 | public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 17 | String id = xo.getStringAttribute(ID); |
| 18 | Parameter mean = (Parameter) xo.getChild(MEAN).getChild(Parameter.class); |
| 19 | Parameter precision = null; |
| 20 | if(xo.getChild(PRECISION) != null){ |
| 21 | precision = (Parameter) xo.getChild(PRECISION).getChild(Parameter.class); |
| 22 | } |
| 23 | Parameter variance = null; |
| 24 | if(xo.getChild(VARIANCE) != null){ |
| 25 | variance = (Parameter) xo.getChild(VARIANCE).getChild(Parameter.class); |
| 26 | } |
| 27 | Parameter data = (Parameter) xo.getChild(DATA).getChild(Parameter.class); |
| 28 | |
| 29 | return new IndependentNormalDistributionModel(id, mean, variance, precision, data); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public XMLSyntaxRule[] getSyntaxRules() { |
| 34 | return rules; |
| 35 | } |
| 36 | |
| 37 | XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 38 | new ElementRule(MEAN, new XMLSyntaxRule[]{ |
| 39 | new ElementRule(Parameter.class) |
| 40 | }), |
| 41 | new XORRule( |
| 42 | new ElementRule(VARIANCE, new XMLSyntaxRule[]{ |
| 43 | new ElementRule(Parameter.class) |
| 44 | }), |
| 45 | new ElementRule(PRECISION, new XMLSyntaxRule[]{ |
| 46 | new ElementRule(Parameter.class) |
| 47 | }) |
| 48 | ), |
| 49 | new ElementRule(DATA, new XMLSyntaxRule[]{ |
| 50 | new ElementRule(Parameter.class) |
| 51 | }) |
| 52 | }; |
| 53 | |
| 54 | @Override |
| 55 | public String getParserDescription() { |
| 56 | return "A series of independent normal distribution models"; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Class getReturnType() { |
| 61 | return IndependentNormalDistributionModel.class; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String getParserName() { |
| 66 | return INDEPENDENT_NORMAL_DISTRIBUTION_MODEL; |
| 67 | } |
| 68 | } |