Debian Codegraph @

src/dr/inferencexml/loggers/ColumnsParser.java

raw symbols SCIP
1 /*
2 * ColumnsParser.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.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 // AbstractXMLObjectParser implementation
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 // Anything goes???
148 new ElementRule(Object.class, 1, Integer.MAX_VALUE),
149 };
150
151 }