Debian Codegraph @

src/dr/inferencexml/hmc/GradientWrapperParser.java

raw symbols SCIP
1 /*
2 * GradientWrapperParser.java
3 *
4 * Copyright (c) 2002-2017 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.hmc;
27
28 import dr.inference.distribution.DistributionLikelihood;
29 import dr.inference.distribution.MultivariateDistributionLikelihood;
30 import dr.inference.model.GradientProvider;
31 import dr.inference.hmc.GradientWrtParameterProvider;
32 import dr.inference.model.Parameter;
33 import dr.xml.*;
34
35 /**
36 * @author Marc A. Suchard
37 */
38
39 public class GradientWrapperParser extends AbstractXMLObjectParser {
40
41 public static final String NAME = "gradient";
42
43 @Override
44 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
45
46 Object obj = xo.getChild(0);
47
48 if (obj instanceof MultivariateDistributionLikelihood) {
49 final MultivariateDistributionLikelihood mdl = (MultivariateDistributionLikelihood)
50 xo.getChild(MultivariateDistributionLikelihood.class);
51
52 if (!(mdl.getDistribution() instanceof GradientProvider)) {
53 throw new XMLParseException("Not a gradient provider");
54 }
55
56 final GradientProvider provider = (GradientProvider) mdl.getDistribution();
57 final Parameter parameter = mdl.getDataParameter();
58
59 return new GradientWrtParameterProvider.ParameterWrapper(provider, parameter, mdl);
60 } else {
61 DistributionLikelihood dl = (DistributionLikelihood) obj;
62 if (!(dl.getDistribution() instanceof GradientProvider)) {
63 throw new XMLParseException("Not a gradient provider");
64 }
65
66 throw new RuntimeException("Not yet implemented");
67 }
68 }
69
70 @Override
71 public XMLSyntaxRule[] getSyntaxRules() {
72 return new XMLSyntaxRule[] {
73 new XORRule(
74 new ElementRule(DistributionLikelihood.class),
75 new ElementRule(MultivariateDistributionLikelihood.class)
76 )
77 };
78 }
79
80 @Override
81 public String getParserDescription() {
82 return null;
83 }
84
85 @Override
86 public Class getReturnType() {
87 return GradientWrtParameterProvider.class;
88 }
89
90 @Override
91 public String getParserName() {
92 return NAME;
93 }
94 }