Debian Codegraph @

src/dr/app/beast/BeastParser.java

raw symbols SCIP
1 /*
2 * BeastParser.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.app.beast;
27
28 import dr.util.Citation;
29 import dr.util.Pair;
30 import dr.util.Version;
31 import dr.xml.PropertyParser;
32 import dr.xml.UserInput;
33 import dr.xml.XMLObjectParser;
34 import dr.xml.XMLParser;
35
36 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.lang.reflect.Field;
41 import java.util.*;
42 import java.util.logging.Logger;
43
44 /**
45 * @author Alexei Drummond
46 * @author Andrew Rambaut
47 * @author Walter Xie
48 * @version $Id: BeastParser.java,v 1.76 2006/08/30 16:01:59 rambaut Exp $
49 */
50 public class BeastParser extends XMLParser {
51
52 public static final String RELEASE ="release";
53 public static final String DEV = "development";
54 public static final String PARSER_PROPERTIES_SUFFIX ="_parsers.properties";
55 public String parsers;
56
57 public BeastParser(String[] args, List<String> additionalParsers, boolean verbose, boolean parserWarnings, boolean strictXML, Version version) {
58 super(verbose, parserWarnings, strictXML, version);
59
60 addCitable(BeastVersion.INSTANCE);
61
62 setup(args);
63
64 if (verbose) {
65 System.out.println("Built-in parsers:");
66 Iterator iterator = getParsers();
67 while (iterator.hasNext()) {
68 XMLObjectParser parser = (XMLObjectParser) iterator.next();
69 System.out.println(parser.getParserName());
70 }
71
72 }
73
74 // Try to find and load the additional 'core' parsers
75 try {
76 Properties properties = new Properties();
77 properties.load(this.getClass().getResourceAsStream("beast.properties"));
78
79 // get the parsers file prefix from the beast.properties file
80 parsers = properties.getProperty("parsers");
81
82 if (System.getProperty("parsers") != null) {
83 // If a system property has been set then allow this to override the default
84 // e.g. -Dparsers=development
85 parsers = System.getProperty("parsers");
86 }
87
88 if (parsers.equalsIgnoreCase(DEV)) {
89 this.parserWarnings = true; // if dev, then auto turn on, otherwise default to turn off
90 }
91
92 // always load release_parsers.properties !!!
93 loadProperties(this.getClass(), RELEASE + PARSER_PROPERTIES_SUFFIX, verbose, this.parserWarnings, false);
94
95 // suppose to load developement_parsers.properties
96 if (parsers != null && (!parsers.equalsIgnoreCase(RELEASE))) {
97 // load the development parsers
98 if (parsers.equalsIgnoreCase(DEV)) {
99 System.out.println("Loading additional development parsers from " + parsers + PARSER_PROPERTIES_SUFFIX
100 + ", which is additional set of parsers only available for development version ...");
101 }
102 loadProperties(this.getClass(), parsers + PARSER_PROPERTIES_SUFFIX, verbose, this.parserWarnings, true);
103 }
104 // load additional parsers
105 if (additionalParsers != null) {
106 for (String addParsers : additionalParsers) {
107 loadProperties(this.getClass(), addParsers + PARSER_PROPERTIES_SUFFIX, verbose, verbose, true);
108 }
109 }
110 } catch (IOException e) {
111 e.printStackTrace();
112 }
113
114 // Now search the package hierarchy for 'beast.properties' files.
115 // try {
116 // loadProperties(this.getClass(), verbose);
117 // } catch (IOException e) {
118 // e.printStackTrace();
119 // }
120 }
121
122 /**
123 * Load the parser for *.properties file
124 * @param c BeastParser
125 * @param parsersFile parser file name, (*.properties)
126 * @param verbose verbose
127 * @param parserWarning parserWarning
128 * @param canReplace can this new loaded parser to replace old one with the same name
129 * @throws IOException IOException
130 */
131 private void loadProperties(Class c, String parsersFile, boolean verbose, boolean parserWarning, boolean canReplace) throws IOException {
132
133 if (verbose) {
134 if (parsersFile.equalsIgnoreCase(RELEASE + PARSER_PROPERTIES_SUFFIX)) {
135 System.out.println("\nAlways loading " + parsersFile + ":");
136 } else {
137 System.out.println("\n\nLoading additional parsers (" + parsersFile + "):");
138 }
139 }
140 final InputStream stream = c.getResourceAsStream(parsersFile);
141 if (stream == null) {
142 throw new RuntimeException("Parsers file not found: " + parsersFile);
143 }
144 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
145 String line = reader.readLine();
146
147 while (line != null) {
148 if (verbose && line.trim().startsWith("#")) System.out.println(line);
149
150 if (line.trim().length() > 0 && !line.trim().startsWith("#")) {
151 try {
152 if (line.contains("Vector")) {
153 System.out.println("");
154 }
155 Class parser = Class.forName(line);
156 if (XMLObjectParser.class.isAssignableFrom(parser)) {
157 // if this class is an XMLObjectParser then create an instance
158 boolean replaced = addXMLObjectParser((XMLObjectParser) parser.newInstance(), canReplace);
159 if (verbose) {
160 System.out.println((replaced ? "Replaced" : "Loaded") + " parser: " + parser.getName());
161 } else if (parserWarning && replaced) {
162 System.out.println("WARNING: parser - " + parser.getName() + " in " + parsersFile +" is duplicated, "
163 + "which is REPLACING the same parser loaded previously.\n");
164 }
165 } else {
166 boolean parserFound = false;
167 // otherwise look for a static member which is an instance of XMLObjectParser
168 Field[] fields = parser.getDeclaredFields();
169 for (Field field : fields) {
170 if (XMLObjectParser.class.isAssignableFrom(field.getType())) {
171 try {
172 boolean replaced = addXMLObjectParser((XMLObjectParser) field.get(null), canReplace);
173 if (verbose) {
174 System.out.println((replaced ? "Replaced" : "Loaded") + " parser: "
175 + parser.getName() + "." + field.getName());
176 } else if (parserWarning && replaced) {
177 System.out.println("WARNING: parser - " + parser.getName() + " in " + parsersFile +" is duplicated, "
178 + "which is REPLACING the same parser loaded previously.\n");
179 }
180 } catch (IllegalArgumentException iae) {
181 System.err.println("Failed to install parser: " + iae.getMessage());
182 }
183 parserFound = true;
184 }
185 }
186
187 if (!parserFound) {
188 throw new IllegalArgumentException(parser.getName() + " is not of type XMLObjectParser " +
189 "and doesn't contain any static members of this type");
190 }
191 }
192
193 } catch (Exception e) {
194 System.err.println("\nFailed to load parser: " + e.getMessage());
195 System.err.println("line = " + line + "\n");
196 }
197 }
198 line = reader.readLine();
199 }
200
201 if (verbose) {
202 System.out.println("load " + parsersFile + " successfully.\n");
203 }
204 }
205
206 @Override
207 protected void executingRunnable() {
208 Logger.getLogger("dr.apps.beast").info("\nCitations for this analysis: ");
209
210 Map<String, Set<Pair<String, String>>> categoryMap = new LinkedHashMap<String, Set<Pair<String, String>>>();
211
212 // force the Framework category to be first...
213 categoryMap.put("Framework", new LinkedHashSet<Pair<String, String>>());
214
215 for (Pair<String, String> keyPair : getCitationStore().keySet()) {
216 Set<Pair<String, String>> pairSet = categoryMap.get(keyPair.fst);
217 if (pairSet == null) {
218 pairSet = new LinkedHashSet<Pair<String, String>>();
219 categoryMap.put(keyPair.fst, pairSet);
220 }
221 pairSet.add(keyPair);
222 }
223
224 for (String category : categoryMap.keySet()) {
225 Logger.getLogger("dr.apps.beast").info("\n"+category.toUpperCase());
226 Set<Pair<String, String>> pairSet = categoryMap.get(category);
227
228 for (Pair<String, String>keyPair : pairSet) {
229 Logger.getLogger("dr.apps.beast").info(keyPair.snd + ":");
230
231 for (Citation citation : getCitationStore().get(keyPair)) {
232 Logger.getLogger("dr.apps.beast").info("\t" + citation.toString());
233 }
234 }
235 }
236
237 // clear the citation store so all the same citations don't get cited again
238 getCitationStore().clear();
239
240 Logger.getLogger("dr.apps.beast").info("\n");
241
242 }
243
244 private void setup(String[] args) {
245
246 for (int i = 0; i < args.length; i++) {
247 storeObject(Integer.toString(i), args[i]);
248 }
249
250 // built-in parsers
251
252 addXMLObjectParser(new PropertyParser());
253 addXMLObjectParser(UserInput.STRING_PARSER);
254 addXMLObjectParser(UserInput.DOUBLE_PARSER);
255 addXMLObjectParser(UserInput.INTEGER_PARSER);
256
257 addXMLObjectParser(new dr.xml.AttributeParser());
258 addXMLObjectParser(new dr.xml.AttributesParser());
259
260 addXMLObjectParser(new dr.inference.model.StatisticParser());
261 addXMLObjectParser(new dr.inference.model.ParameterParser());
262
263 //**************** all other parsers are read at runtime from property lists *********************
264 }
265 }
266