| 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.loggers; |
| 27 |
|
| 28 |
import dr.inference.loggers.*; |
| 29 |
import dr.util.Identifiable; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ColumnsParser extends AbstractXMLObjectParser { |
| 38 |
public static final String COLUMN = "column"; |
| 39 |
public static final String LABEL = "label"; |
| 40 |
public static final String SIGNIFICANT_FIGURES = "sf"; |
| 41 |
public static final String DECIMAL_PLACES = "dp"; |
| 42 |
public static final String WIDTH = "width"; |
| 43 |
public static final String FORMAT = "format"; |
| 44 |
public static final String PERCENT = "percent"; |
| 45 |
public static final String BOOL = "boolean"; |
| 46 |
|
| 47 |
public String getParserName() { |
| 48 |
return COLUMN; |
| 49 |
} |
| 50 |
|
| 51 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 52 |
|
| 53 |
String label = xo.getAttribute(LABEL, ""); |
| 54 |
final int sf = xo.getAttribute(SIGNIFICANT_FIGURES, -1); |
| 55 |
final int dp = xo.getAttribute(DECIMAL_PLACES, -1); |
| 56 |
final int width = xo.getAttribute(WIDTH, -1); |
| 57 |
|
| 58 |
String format = xo.getAttribute(FORMAT, ""); |
| 59 |
|
| 60 |
ArrayList colList = new ArrayList(); |
| 61 |
|
| 62 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 63 |
|
| 64 |
Object child = xo.getChild(i); |
| 65 |
LogColumn[] cols; |
| 66 |
|
| 67 |
if (child instanceof Loggable) { |
| 68 |
cols = ((Loggable) child).getColumns(); |
| 69 |
} else if (child instanceof Identifiable) { |
| 70 |
cols = new LogColumn[]{new LogColumn.Default(((Identifiable) child).getId(), child)}; |
| 71 |
} else { |
| 72 |
cols = new LogColumn[]{new LogColumn.Default(child.getClass().toString(), child)}; |
| 73 |
} |
| 74 |
|
| 75 |
if (format.equals(PERCENT)) { |
| 76 |
for (int k = 0; k < cols.length; ++k) { |
| 77 |
if (cols[k] instanceof NumberColumn) { |
| 78 |
cols[k] = new PercentColumn((NumberColumn) cols[k]); |
| 79 |
} |
| 80 |
} |
| 81 |
} else if (format.equals(BOOL)) { |
| 82 |
for (int k = 0; k < cols.length; ++k) { |
| 83 |
if (cols[k] instanceof NumberColumn) { |
| 84 |
cols[k] = new BooleanColumn((NumberColumn) cols[k]); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
for (int j = 0; j < cols.length; j++) { |
| 90 |
|
| 91 |
if (!label.equals("")) { |
| 92 |
if (cols.length > 1) { |
| 93 |
cols[j].setLabel(label + Integer.toString(j + 1)); |
| 94 |
} else { |
| 95 |
cols[j].setLabel(label); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if (cols[j] instanceof NumberColumn) { |
| 100 |
if (sf != -1) { |
| 101 |
((NumberColumn) cols[j]).setSignificantFigures(sf); |
| 102 |
} |
| 103 |
if (dp != -1) { |
| 104 |
((NumberColumn) cols[j]).setDecimalPlaces(dp); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (width > 0) { |
| 109 |
cols[j].setMinimumWidth(width); |
| 110 |
} |
| 111 |
|
| 112 |
colList.add(cols[j]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
LogColumn[] columns = new LogColumn[colList.size()]; |
| 117 |
colList.toArray(columns); |
| 118 |
|
| 119 |
return new Columns(columns); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public String getParserDescription() { |
| 127 |
return "Specifies formating options for one or more columns in a log file."; |
| 128 |
} |
| 129 |
|
| 130 |
public Class getReturnType() { |
| 131 |
return Columns.class; |
| 132 |
} |
| 133 |
|
| 134 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 135 |
return rules; |
| 136 |
} |
| 137 |
|
| 138 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 139 |
new StringAttributeRule(LABEL, |
| 140 |
"The label of the column. " + |
| 141 |
"If this is specified and more than one statistic is in this column, " + |
| 142 |
"then the label will be appended by the index of the statistic to create individual column names", true), |
| 143 |
AttributeRule.newIntegerRule(SIGNIFICANT_FIGURES, true), |
| 144 |
AttributeRule.newIntegerRule(DECIMAL_PLACES, true), |
| 145 |
AttributeRule.newIntegerRule(WIDTH, true), |
| 146 |
AttributeRule.newStringRule(FORMAT, true), |
| 147 |
|
| 148 |
new ElementRule(Object.class, 1, Integer.MAX_VALUE), |
| 149 |
}; |
| 150 |
|
| 151 |
} |