Debian Codegraph @

src/dr/inferencexml/loggers/MLLoggerParser.java

raw symbols SCIP
1 /*
2 * MLLoggerParser.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.inference.model.Likelihood;
30 import dr.util.Identifiable;
31 import dr.xml.*;
32
33 import java.io.PrintWriter;
34
35 /**
36 *
37 */
38 public class MLLoggerParser extends LoggerParser {
39
40 public static final String LOG_ML = "logML";
41 public static final String LIKELIHOOD = "ml";
42
43 public String getParserName() {
44 return LOG_ML;
45 }
46
47 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
48
49 Likelihood likelihood = (Likelihood) xo.getElementFirstChild(LIKELIHOOD);
50
51 // logEvery of zero only displays at the end
52 int logEvery = xo.getAttribute(LOG_EVERY, 0);
53
54 final PrintWriter pw = getLogFile(xo, getParserName());
55
56 LogFormatter formatter = new TabDelimitedFormatter(pw);
57
58 MLLogger logger = new MLLogger(likelihood, formatter, logEvery);
59
60 if (xo.hasAttribute(TITLE)) {
61 logger.setTitle(xo.getStringAttribute(TITLE));
62 }
63
64 for (int i = 0; i < xo.getChildCount(); i++) {
65 Object child = xo.getChild(i);
66
67 if (child instanceof Columns) {
68
69 logger.addColumns(((Columns) child).getColumns());
70
71 } else if (child instanceof Loggable) {
72
73 logger.add((Loggable) child);
74
75 } else if (child instanceof Identifiable) {
76
77 logger.addColumn(new LogColumn.Default(((Identifiable) child).getId(), child));
78
79 } else {
80
81 logger.addColumn(new LogColumn.Default(child.getClass().toString(), child));
82 }
83 }
84
85 return logger;
86 }
87
88 //************************************************************************
89 // AbstractXMLObjectParser implementation
90 //************************************************************************
91
92 public XMLSyntaxRule[] getSyntaxRules() {
93 return rules;
94 }
95
96 private final XMLSyntaxRule[] rules = {
97 AttributeRule.newIntegerRule(LOG_EVERY, true),
98 new ElementRule(LIKELIHOOD,
99 new XMLSyntaxRule[]{new ElementRule(Likelihood.class)}),
100 new OrRule(
101 new ElementRule(Columns.class, 1, Integer.MAX_VALUE),
102 new ElementRule(Loggable.class, 1, Integer.MAX_VALUE)
103 )
104 };
105
106 public String getParserDescription() {
107 return "Logs one or more items every time the given likelihood improves";
108 }
109
110 public Class getReturnType() {
111 return MLLogger.class;
112 }
113 }