Debian Codegraph @

src/dr/inferencexml/model/RPNcalculatorStatisticParser.java

raw symbols SCIP
1 /*
2 * RPNcalculatorStatisticParser.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.RPNcalculatorStatistic;
29 import dr.inference.model.Statistic;
30 import dr.xml.*;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38 */
39 public class RPNcalculatorStatisticParser extends AbstractXMLObjectParser {
40
41 public static String RPN_STATISTIC = "RPNcalculator";
42 public static String VARIABLE = "variable";
43 public static String EXPRESSION = "expression";
44 public String getParserName() { return RPN_STATISTIC; }
45
46 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
47 List<String> expressions = new ArrayList<String>();
48 List<String> expressionNames = new ArrayList<String>();
49
50 Map<String, Statistic> variables = new HashMap<String, Statistic>();
51 for(int i = 0; i < xo.getChildCount(); ++i) {
52 final XMLObject child = (XMLObject)xo.getChild(i);
53 if( child.getName().equals(EXPRESSION) ) {
54 expressions.add(child.getStringChild(0));
55
56 String name = child.hasAttribute(Statistic.NAME) ? child.getStringAttribute(Statistic.NAME) :
57 ("expression_" + expressionNames.size());
58
59 expressionNames.add(name);
60
61 } else if( child.getName().equals(VARIABLE) ) {
62 Statistic s = (Statistic)child.getChild(Statistic.class);
63
64 if( s.getDimension() != 1 ) {
65 throw new XMLParseException("Sorry, no support for multi-dimentional yet");
66 }
67 //assert s.getDimension() == 1; // for now
68
69 String name = child.hasAttribute(Statistic.NAME) ? child.getStringAttribute(Statistic.NAME) :
70 s.getDimensionName(0);
71 variables.put(name, s);
72 } else {
73 throw new XMLParseException("Unknown element found in " + getParserName() + " element:" + child);
74 }
75 }
76
77 final String name = xo.hasAttribute(Statistic.NAME) ? xo.getStringAttribute(Statistic.NAME) : RPN_STATISTIC;
78 final String[] e = expressions.toArray(new String[expressions.size()]);
79 final String[] enames = expressionNames.toArray(new String[expressionNames.size()]);
80 try {
81 return new RPNcalculatorStatistic(name, e, enames, variables);
82 } catch ( RuntimeException err) {
83 throw new XMLParseException(err.getMessage());
84 }
85 }
86
87 public String getParserDescription() {
88 return "This element returns a statistic evaluated from arbitrary expression.";
89 }
90
91 public Class getReturnType() { return RPNcalculatorStatistic.class; }
92
93 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
94
95 private final XMLSyntaxRule[] rules = {
96 new ElementRule(EXPRESSION,
97 new XMLSyntaxRule[] { new ElementRule(String.class) }, 1, Integer.MAX_VALUE),
98 new ElementRule(VARIABLE,
99 new XMLSyntaxRule[] { new ElementRule(Statistic.class) } , 1, Integer.MAX_VALUE)
100 };
101
102 }