| 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.app.beast.BeastVersion; |
| 29 |
import dr.inference.loggers.*; |
| 30 |
import dr.math.MathUtils; |
| 31 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 32 |
import dr.util.FileHelpers; |
| 33 |
import dr.util.Identifiable; |
| 34 |
import dr.util.Property; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.io.File; |
| 38 |
import java.io.PrintWriter; |
| 39 |
import java.util.Date; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class LoggerParser extends AbstractXMLObjectParser { |
| 46 |
|
| 47 |
public static final String LOG = "log"; |
| 48 |
public static final String ECHO = "echo"; |
| 49 |
public static final String ECHO_EVERY = "echoEvery"; |
| 50 |
public static final String TITLE = "title"; |
| 51 |
public static final String HEADER = "header"; |
| 52 |
public static final String FILE_NAME = FileHelpers.FILE_NAME; |
| 53 |
public static final String FORMAT = "format"; |
| 54 |
public static final String TAB = "tab"; |
| 55 |
public static final String HTML = "html"; |
| 56 |
public static final String PRETTY = "pretty"; |
| 57 |
public static final String LOG_EVERY = "logEvery"; |
| 58 |
public static final String ALLOW_OVERWRITE_LOG = "overwrite"; |
| 59 |
|
| 60 |
public static final String COLUMNS = "columns"; |
| 61 |
public static final String COLUMN = "column"; |
| 62 |
public static final String LABEL = "label"; |
| 63 |
public static final String SIGNIFICANT_FIGURES = "sf"; |
| 64 |
public static final String DECIMAL_PLACES = "dp"; |
| 65 |
public static final String WIDTH = "width"; |
| 66 |
|
| 67 |
public String getParserName() { |
| 68 |
return LOG; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 75 |
|
| 76 |
|
| 77 |
final int logEvery = xo.getIntegerAttribute(LOG_EVERY); |
| 78 |
|
| 79 |
final PrintWriter pw = getLogFile(xo, getParserName()); |
| 80 |
|
| 81 |
final LogFormatter formatter = new TabDelimitedFormatter(pw); |
| 82 |
|
| 83 |
boolean performanceReport = false; |
| 84 |
|
| 85 |
if (!xo.hasAttribute(FILE_NAME)) { |
| 86 |
|
| 87 |
performanceReport = true; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
final MCLogger logger = new MCLogger(formatter, logEvery, performanceReport, 10000); |
| 92 |
|
| 93 |
String title = null; |
| 94 |
if (xo.hasAttribute(TITLE)) { |
| 95 |
title = xo.getStringAttribute(TITLE); |
| 96 |
} |
| 97 |
|
| 98 |
String header = null; |
| 99 |
if (xo.hasAttribute(HEADER)) { |
| 100 |
header = xo.getStringAttribute(HEADER); |
| 101 |
} |
| 102 |
|
| 103 |
if (title == null) { |
| 104 |
final BeastVersion version = new BeastVersion(); |
| 105 |
|
| 106 |
title = "BEAST " + version.getVersionString() + "\n" + |
| 107 |
(header != null ? header + "\n" : "") + |
| 108 |
"Generated " + (new Date()).toString() + " [seed=" + MathUtils.getSeed() + "]\n" + |
| 109 |
System.getProperty("command_line", ""); |
| 110 |
} else { |
| 111 |
if (header != null) { |
| 112 |
title += "\n" + header; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
logger.setTitle(title); |
| 117 |
|
| 118 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 119 |
|
| 120 |
final Object child = xo.getChild(i); |
| 121 |
|
| 122 |
if (child instanceof Columns) { |
| 123 |
|
| 124 |
logger.addColumns(((Columns) child).getColumns()); |
| 125 |
|
| 126 |
} else if (child instanceof Loggable) { |
| 127 |
|
| 128 |
logger.add((Loggable) child); |
| 129 |
|
| 130 |
} else if (child instanceof Identifiable) { |
| 131 |
|
| 132 |
logger.addColumn(new LogColumn.Default(((Identifiable) child).getId(), child)); |
| 133 |
|
| 134 |
} else if (child instanceof Property) { |
| 135 |
logger.addColumn(new LogColumn.Default(((Property) child).getAttributeName(), child)); |
| 136 |
} else { |
| 137 |
|
| 138 |
logger.addColumn(new LogColumn.Default(child.getClass().toString(), child)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return logger; |
| 143 |
} |
| 144 |
|
| 145 |
public static PrintWriter getLogFile(XMLObject xo, String parserName) throws XMLParseException { |
| 146 |
return XMLParser.getFilePrintWriter(xo, parserName); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 154 |
return rules; |
| 155 |
} |
| 156 |
|
| 157 |
private final XMLSyntaxRule[] rules = { |
| 158 |
AttributeRule.newIntegerRule(LOG_EVERY), |
| 159 |
AttributeRule.newBooleanRule(ALLOW_OVERWRITE_LOG, true), |
| 160 |
new StringAttributeRule(FILE_NAME, |
| 161 |
"The name of the file to send log output to. " + |
| 162 |
"If no file name is specified then log is sent to standard output", true), |
| 163 |
new StringAttributeRule(TITLE, |
| 164 |
"The title of the log", true), |
| 165 |
new StringAttributeRule(HEADER, |
| 166 |
"The subtitle of the log", true), |
| 167 |
new OrRule( |
| 168 |
new XMLSyntaxRule[]{ |
| 169 |
new ElementRule(Columns.class, 1, Integer.MAX_VALUE), |
| 170 |
new ElementRule(Loggable.class, 1, Integer.MAX_VALUE), |
| 171 |
new ElementRule(Object.class, 1, Integer.MAX_VALUE) |
| 172 |
} |
| 173 |
) |
| 174 |
}; |
| 175 |
|
| 176 |
public String getParserDescription() { |
| 177 |
return "Logs one or more items at a given frequency to the screen or to a file"; |
| 178 |
} |
| 179 |
|
| 180 |
public Class getReturnType() { |
| 181 |
return MLLogger.class; |
| 182 |
} |
| 183 |
} |
| 184 |
|