Debian Codegraph @

src/dr/inferencexml/model/ProductStatisticParser.java

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