Debian Codegraph @

src/dr/inferencexml/model/CompoundLikelihoodParser.java

raw symbols SCIP
1 /*
2 * CompoundLikelihoodParser.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.model;
27
28 import dr.inference.model.CompoundLikelihood;
29 import dr.inference.model.Likelihood;
30 import dr.xml.*;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.logging.Logger;
35
36 /**
37 */
38 public class CompoundLikelihoodParser extends AbstractXMLObjectParser {
39 public static final String COMPOUND_LIKELIHOOD = "compoundLikelihood";
40 public static final String THREADS = "threads";
41 @Deprecated public static final String POSTERIOR = "posterior"; // replaced with JOINT
42 public static final String JOINT = "joint";
43 public static final String PRIOR = "prior";
44 public static final String LIKELIHOOD = "likelihood";
45 public static final String PSEUDO_PRIOR = "pseudoPrior";
46 public static final String REFERENCE_PRIOR = "referencePrior";
47 public static final String WORKING_PRIOR = "workingPrior";
48
49 public String getParserName() {
50 return COMPOUND_LIKELIHOOD;
51 }
52
53 public String[] getParserNames() {
54 return new String[]{getParserName(), POSTERIOR, JOINT, PRIOR, LIKELIHOOD, PSEUDO_PRIOR, REFERENCE_PRIOR, WORKING_PRIOR};
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 // the default is -1 threads (automatic thread pool size) but an XML attribute can override it
60 int threads = xo.getAttribute(THREADS, -1);
61
62 // both the XML attribute and a system property can override it
63 if (System.getProperty("thread.count") != null) {
64
65 threads = Integer.parseInt(System.getProperty("thread.count"));
66 if (threads < -1 || threads > 1000) {
67 // put an upper limit here - may be unnecessary?
68 threads = -1;
69 }
70 }
71 // }
72
73 List<Likelihood> likelihoods = new ArrayList<Likelihood>();
74 for (int i = 0; i < xo.getChildCount(); i++) {
75 final Object child = xo.getChild(i);
76 if (child instanceof Likelihood) {
77
78 if (likelihoods.contains(child)) {
79 throw new XMLParseException("The likelihood element, '" + ((Likelihood) child).getId() + "', is already present in the likelihood or prior density.");
80 }
81 likelihoods.add((Likelihood) child);
82
83 // } else if (child instanceof BeagleBranchLikelihoods){
84 //
85 // //TODO
86 // likelihoods.addAll( ((BeagleBranchLikelihoods)child).getBranchLikelihoods());
87
88 } else {
89
90 throw new XMLParseException("An element (" + child + ") which is not a likelihood has been added to a "
91 + COMPOUND_LIKELIHOOD + " element");
92 }
93 }
94
95 CompoundLikelihood compoundLikelihood;
96
97 if (xo.getName().equalsIgnoreCase(LIKELIHOOD)) {
98 compoundLikelihood = new CompoundLikelihood(threads, likelihoods);
99 switch (threads) {
100 case -1:
101 Logger.getLogger("dr.evomodel").info("\nLikelihood computation is using an auto sizing thread pool.");
102 break;
103 case 0:
104 Logger.getLogger("dr.evomodel").info("\nLikelihood computation is using a single thread.");
105 break;
106 default:
107 Logger.getLogger("dr.evomodel").info("\nLikelihood computation is using a pool of " + threads + " threads.");
108 break;
109 }
110 } else {
111 compoundLikelihood = new CompoundLikelihood(likelihoods);
112 }
113
114
115 // TODO
116 // System.err.println("CompundLikelihood consists of " + compoundLikelihood.getLikelihoodCount() + " likelihood element(s)");
117
118 return compoundLikelihood;
119 }
120
121 //************************************************************************
122 // AbstractXMLObjectParser implementation
123 //************************************************************************
124
125 public String getParserDescription() {
126 return "A likelihood function which is simply the product of its component likelihood functions.";
127 }
128
129 public XMLSyntaxRule[] getSyntaxRules() {
130 return rules;
131 }
132
133 private final XMLSyntaxRule[] rules = {
134 AttributeRule.newIntegerRule(THREADS, true),
135 new ElementRule(Likelihood.class, -1, Integer.MAX_VALUE)
136 };
137
138 public Class getReturnType() {
139 return CompoundLikelihood.class;
140 }
141 }