Debian Codegraph @

src/dr/inferencexml/model/TestStatisticParser.java

raw symbols SCIP
1 /*
2 * TestStatisticParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 // AbstractXMLObjectParser implementation
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 }