Debian Codegraph @

src/dr/inferencexml/model/SumStatisticParser.java

raw symbols SCIP
1 /*
2 * SumStatisticParser.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.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 // AbstractXMLObjectParser implementation
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 }