Debian Codegraph @

src/dr/inferencexml/operators/BitSwapOperatorParser.java

raw symbols SCIP
1 /*
2 * BitSwapOperatorParser.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.operators;
27
28 import dr.inference.model.Parameter;
29 import dr.inference.model.Statistic;
30 import dr.inference.operators.BitSwapOperator;
31 import dr.inference.operators.MCMCOperator;
32 import dr.inferencexml.distribution.MixedDistributionLikelihoodParser;
33 import dr.xml.*;
34
35 /**
36 *
37 */
38 public class BitSwapOperatorParser extends AbstractXMLObjectParser {
39
40 public static final String BIT_SWAP_OPERATOR = "bitSwapOperator";
41 public static final String RADIUS = "radius";
42
43 private static final String DATA = MixedDistributionLikelihoodParser.DATA;
44 private static final String INDICATORS = MixedDistributionLikelihoodParser.INDICATORS;
45
46 public String getParserName() {
47 return BIT_SWAP_OPERATOR;
48 }
49
50 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
51
52 final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
53 Parameter data = (Parameter) ((XMLObject) xo.getChild(DATA)).getChild(Parameter.class);
54 Parameter indicators = (Parameter) ((XMLObject) xo.getChild(INDICATORS)).getChild(Parameter.class);
55 int radius = -1;
56
57 if (xo.hasAttribute(RADIUS)) {
58 double rd = xo.getDoubleAttribute(RADIUS);
59
60 if (rd > 0) {
61 if (rd < 1) {
62 rd = Math.round(rd * indicators.getDimension());
63 }
64 radius = (int) Math.round(rd);
65 if (!(radius >= 1 && radius < indicators.getDimension() - 1)) {
66 radius = -1;
67 }
68 }
69 if (radius < 1) {
70 throw new XMLParseException("invalid radius " + rd);
71 }
72 }
73
74 return new BitSwapOperator(data, indicators, radius, weight);
75 }
76
77 //************************************************************************
78 // AbstractXMLObjectParser implementation
79 //************************************************************************
80
81 public String getParserDescription() {
82 return "This element returns a bit-swap operator on a given parameter and data.";
83 }
84
85 public Class getReturnType() {
86 return BitSwapOperator.class;
87 }
88
89 public XMLSyntaxRule[] getSyntaxRules() {
90 return rules;
91 }
92
93 private final XMLSyntaxRule[] rules = {
94 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
95 AttributeRule.newDoubleRule(RADIUS, true),
96 new ElementRule(DATA, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}),
97 new ElementRule(INDICATORS, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}),
98 };
99 }