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