| 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.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 |
|
| 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 |
} |