| 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.model; |
| 27 |
|
| 28 |
import dr.inference.model.ProductStatistic; |
| 29 |
import dr.inference.model.Statistic; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class ProductStatisticParser extends AbstractXMLObjectParser { |
| 35 |
|
| 36 |
public static String PRODUCT_STATISTIC = "productStatistic"; |
| 37 |
public static String PRODUCT = "product"; |
| 38 |
public static String ELEMENT_WISE = "elementwise"; |
| 39 |
public static String CONSTANT = "constant"; |
| 40 |
|
| 41 |
public String[] getParserNames() { |
| 42 |
return new String[]{getParserName(), PRODUCT}; |
| 43 |
} |
| 44 |
|
| 45 |
public String getParserName() { |
| 46 |
return PRODUCT_STATISTIC; |
| 47 |
} |
| 48 |
|
| 49 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 50 |
|
| 51 |
|
| 52 |
boolean elementwise = xo.getAttribute(ELEMENT_WISE, false); |
| 53 |
|
| 54 |
String name = PRODUCT_STATISTIC; |
| 55 |
if (xo.hasAttribute(Statistic.NAME)) { |
| 56 |
name = xo.getAttribute(Statistic.NAME, xo.getId()); |
| 57 |
} else if (xo.hasAttribute(XMLParser.ID)) { |
| 58 |
name = xo.getAttribute(XMLParser.ID, xo.getId()); |
| 59 |
} |
| 60 |
|
| 61 |
double[] constants = null; |
| 62 |
if (xo.hasAttribute(CONSTANT)) { |
| 63 |
constants = xo.getDoubleArrayAttribute(CONSTANT); |
| 64 |
} |
| 65 |
|
| 66 |
if (constants != null && constants.length != 1 && elementwise) { |
| 67 |
throw new XMLParseException("The constant given to " + getParserName() + " should be a single value if element-wise is being used."); |
| 68 |
} |
| 69 |
|
| 70 |
ProductStatistic productStatistic = new ProductStatistic(name, elementwise, constants); |
| 71 |
|
| 72 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 73 |
Object child = xo.getChild(i); |
| 74 |
if (child instanceof Statistic) { |
| 75 |
try { |
| 76 |
Statistic statistic = (Statistic)child; |
| 77 |
|
| 78 |
if (constants != null && constants.length != 1 && constants.length != statistic.getDimension()) { |
| 79 |
throw new XMLParseException("The constants given to " + getParserName() + " is not of the same dimension as the statistics"); |
| 80 |
} |
| 81 |
|
| 82 |
productStatistic.addStatistic(statistic); |
| 83 |
} catch (IllegalArgumentException iae) { |
| 84 |
throw new XMLParseException("Statistic added to " + getParserName() + " element is not of the same dimension"); |
| 85 |
} |
| 86 |
} else { |
| 87 |
throw new XMLParseException("Unknown element found in " + getParserName() + " element:" + child); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return productStatistic; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public String getParserDescription() { |
| 99 |
return "This element returns a statistic that is the product of the child statistics."; |
| 100 |
} |
| 101 |
|
| 102 |
public Class getReturnType() { |
| 103 |
return ProductStatistic.class; |
| 104 |
} |
| 105 |
|
| 106 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 107 |
return rules; |
| 108 |
} |
| 109 |
|
| 110 |
private final XMLSyntaxRule[] rules = { |
| 111 |
AttributeRule.newBooleanRule(ELEMENT_WISE, true), |
| 112 |
AttributeRule.newStringRule(Statistic.NAME, true), |
| 113 |
AttributeRule.newDoubleArrayRule(CONSTANT, true, "A constant to be multiplied with the parameters. If element-wise then this can be a vector of constants."), |
| 114 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 115 |
}; |
| 116 |
} |