| 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.TestStatistic; |
| 30 |
import dr.util.Attribute; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class TestStatisticParser extends AbstractXMLObjectParser { |
| 36 |
|
| 37 |
public static String TEST_STATISTIC = "test"; |
| 38 |
|
| 39 |
private static final String SEQUALS = "equals"; |
| 40 |
private static final String SGREATER_THAN = "greaterThan"; |
| 41 |
private static final String SLESS_THAN = "lessThan"; |
| 42 |
private static final String SINSIDE = "inside"; |
| 43 |
private static final String SOUTSIDE = "outside"; |
| 44 |
|
| 45 |
public String getParserName() { |
| 46 |
return TEST_STATISTIC; |
| 47 |
} |
| 48 |
|
| 49 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 50 |
|
| 51 |
String name = xo.getAttribute(Statistic.NAME, xo.hasId() ? xo.getId() : ""); |
| 52 |
Attribute attr = (Attribute) xo.getChild(Attribute.class); |
| 53 |
double testValue1; |
| 54 |
|
| 55 |
TestStatistic statistic; |
| 56 |
|
| 57 |
if (xo.hasChildNamed(SEQUALS)) { |
| 58 |
Attribute attr2 = (Attribute) xo.getElementFirstChild(SEQUALS); |
| 59 |
statistic = new TestStatistic(name, attr, attr2, TestStatistic.EQUALS); |
| 60 |
} else if (xo.hasChildNamed(SGREATER_THAN)) { |
| 61 |
Attribute attr2 = (Attribute) xo.getElementFirstChild(SGREATER_THAN); |
| 62 |
statistic = new TestStatistic(name, attr, attr2, TestStatistic.GREATER_THAN); |
| 63 |
} else if (xo.hasChildNamed(SLESS_THAN)) { |
| 64 |
Attribute attr2 = (Attribute) xo.getElementFirstChild(SLESS_THAN); |
| 65 |
statistic = new TestStatistic(name, attr, attr2, TestStatistic.LESS_THAN); |
| 66 |
} else if (xo.hasAttribute(SEQUALS)) { |
| 67 |
testValue1 = xo.getDoubleAttribute(SEQUALS); |
| 68 |
statistic = new TestStatistic(name, attr, testValue1, TestStatistic.EQUALS); |
| 69 |
} else if (xo.hasAttribute(SGREATER_THAN)) { |
| 70 |
testValue1 = xo.getDoubleAttribute(SGREATER_THAN); |
| 71 |
statistic = new TestStatistic(name, attr, testValue1, TestStatistic.GREATER_THAN); |
| 72 |
} else if (xo.hasAttribute(SLESS_THAN)) { |
| 73 |
testValue1 = xo.getDoubleAttribute(SLESS_THAN); |
| 74 |
statistic = new TestStatistic(name, attr, testValue1, TestStatistic.LESS_THAN); |
| 75 |
} else if (xo.hasAttribute(SINSIDE)) { |
| 76 |
double[] values = xo.getDoubleArrayAttribute(SINSIDE); |
| 77 |
if (values.length != 2) |
| 78 |
throw new XMLParseException("inside attribute of test element requires two values"); |
| 79 |
statistic = new TestStatistic(name, attr, values[0], values[1], TestStatistic.INSIDE); |
| 80 |
} else if (xo.hasAttribute(SOUTSIDE)) { |
| 81 |
double[] values = xo.getDoubleArrayAttribute(SOUTSIDE); |
| 82 |
if (values.length != 2) |
| 83 |
throw new XMLParseException("outside attribute of test element requires two values"); |
| 84 |
statistic = new TestStatistic(name, attr, values[0], values[1], TestStatistic.OUTSIDE); |
| 85 |
} else throw new XMLParseException(); |
| 86 |
|
| 87 |
return statistic; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public String getParserDescription() { |
| 95 |
return "This element represents a boolean statistic that returns 1 " + |
| 96 |
"if the conditions are met and 0 otherwise."; |
| 97 |
} |
| 98 |
|
| 99 |
public Class getReturnType() { |
| 100 |
return TestStatistic.class; |
| 101 |
} |
| 102 |
|
| 103 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 104 |
return rules; |
| 105 |
} |
| 106 |
|
| 107 |
private final XMLSyntaxRule[] rules = { |
| 108 |
new StringAttributeRule("name", "A name for this statistic, for logging purposes", true), |
| 109 |
new ElementRule(Attribute.class), |
| 110 |
new XORRule( |
| 111 |
new XMLSyntaxRule[]{ |
| 112 |
new ElementRule(SEQUALS, Attribute.class), |
| 113 |
new ElementRule(SGREATER_THAN, Attribute.class), |
| 114 |
new ElementRule(SLESS_THAN, Attribute.class), |
| 115 |
AttributeRule.newDoubleRule(SEQUALS), |
| 116 |
AttributeRule.newDoubleRule(SGREATER_THAN), |
| 117 |
AttributeRule.newDoubleRule(SLESS_THAN), |
| 118 |
AttributeRule.newDoubleArrayRule(SINSIDE), |
| 119 |
AttributeRule.newDoubleArrayRule(SOUTSIDE) |
| 120 |
} |
| 121 |
) |
| 122 |
}; |
| 123 |
} |