| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.model; |
| 27 |
|
| 28 |
import dr.xml.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class VectorSliceParameter extends CompoundParameter { |
| 34 |
|
| 35 |
public final static String VECTOR_SLICE_PARAMETER = "vectorSlice"; |
| 36 |
public static final String SLICE_DIMENSION = "sliceDimension"; |
| 37 |
|
| 38 |
private final int sliceDimension; |
| 39 |
private Bounds<Double> bounds; |
| 40 |
|
| 41 |
public Bounds<Double> getBounds() { |
| 42 |
|
| 43 |
if (bounds == null) { |
| 44 |
bounds = new sliceBounds(); |
| 45 |
} |
| 46 |
return bounds; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public VectorSliceParameter(String name, int sliceDimension) { |
| 52 |
super(name); |
| 53 |
this.sliceDimension = sliceDimension; |
| 54 |
} |
| 55 |
|
| 56 |
public int getDimension() { |
| 57 |
return getParameterCount(); |
| 58 |
} |
| 59 |
|
| 60 |
public double getParameterValue(int dim) { |
| 61 |
Parameter parameter = getParameter(dim); |
| 62 |
return parameter.getParameterValue(sliceDimension); |
| 63 |
} |
| 64 |
|
| 65 |
public void addDimension(int index, double value) { |
| 66 |
throw new RuntimeException("Not yet implemented."); |
| 67 |
} |
| 68 |
|
| 69 |
public double removeDimension(int index) { |
| 70 |
throw new RuntimeException("Not yet implemented."); |
| 71 |
} |
| 72 |
|
| 73 |
public void setParameterValue(int dim, double value) { |
| 74 |
Parameter parameter = getParameter(dim); |
| 75 |
parameter.setParameterValue(sliceDimension, value); |
| 76 |
} |
| 77 |
|
| 78 |
public void setParameterValueQuietly(int dim, double value) { |
| 79 |
Parameter parameter = getParameter(dim); |
| 80 |
parameter.setParameterValueQuietly(sliceDimension, value); |
| 81 |
} |
| 82 |
|
| 83 |
public void setParameterValueNotifyChangedAll(int dim, double value){ |
| 84 |
Parameter parameter = getParameter(dim); |
| 85 |
parameter.setParameterValueNotifyChangedAll(sliceDimension, value); |
| 86 |
} |
| 87 |
|
| 88 |
public String getDimensionName(int dim) { |
| 89 |
|
| 90 |
return getParameter(dim).getVariableName() + Integer.toString(sliceDimension + 1); |
| 91 |
} |
| 92 |
|
| 93 |
private class sliceBounds implements Bounds<Double>{ |
| 94 |
|
| 95 |
|
| 96 |
@Override |
| 97 |
public Double getUpperLimit(int dimension) { |
| 98 |
return getParameter(dimension).getBounds().getUpperLimit(sliceDimension); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Double getLowerLimit(int dimension) { |
| 103 |
return getParameter(dimension).getBounds().getLowerLimit(sliceDimension); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public int getBoundsDimension() { |
| 108 |
return getDimension(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 113 |
|
| 114 |
public String getParserName() { |
| 115 |
return VECTOR_SLICE_PARAMETER; |
| 116 |
} |
| 117 |
|
| 118 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 119 |
|
| 120 |
|
| 121 |
int sliceDimension = xo.getIntegerAttribute(SLICE_DIMENSION); |
| 122 |
VectorSliceParameter vectorSlice = new VectorSliceParameter(xo.getId(), sliceDimension - 1); |
| 123 |
|
| 124 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 125 |
Parameter parameter = (Parameter) xo.getChild(i); |
| 126 |
if (parameter instanceof MatrixParameter) { |
| 127 |
MatrixParameter mp = (MatrixParameter) parameter; |
| 128 |
for (int j = 0; j < mp.getParameterCount(); ++j) { |
| 129 |
checkAndAdd(vectorSlice, mp.getParameter(j), sliceDimension); |
| 130 |
} |
| 131 |
} else { |
| 132 |
checkAndAdd(vectorSlice, parameter, sliceDimension); |
| 133 |
} |
| 134 |
} |
| 135 |
return vectorSlice; |
| 136 |
} |
| 137 |
|
| 138 |
private void checkAndAdd(final VectorSliceParameter vectorSlice, final Parameter parameter, |
| 139 |
final int sliceDimension) throws XMLParseException { |
| 140 |
vectorSlice.addParameter(parameter); |
| 141 |
if (sliceDimension < 1 || sliceDimension > parameter.getDimension()) { |
| 142 |
throw new XMLParseException("Slice dimension " + sliceDimension + " is invalid for a parameter" + |
| 143 |
" with dimension = " + parameter.getDimension()); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public String getParserDescription() { |
| 152 |
return "A vector parameter constructed from a slice of component parameters."; |
| 153 |
} |
| 154 |
|
| 155 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 156 |
return rules; |
| 157 |
} |
| 158 |
|
| 159 |
private final XMLSyntaxRule[] rules = { |
| 160 |
new ElementRule(Parameter.class, 1, Integer.MAX_VALUE), |
| 161 |
AttributeRule.newIntegerRule(SLICE_DIMENSION), |
| 162 |
}; |
| 163 |
|
| 164 |
public Class getReturnType() { |
| 165 |
return VectorSliceParameter.class; |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
} |