| 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.evoxml; |
| 27 |
|
| 28 |
import dr.evolution.datatype.DataType; |
| 29 |
import dr.evolution.datatype.GeneralDataType; |
| 30 |
import dr.util.Identifiable; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class CompositeDataTypeParser extends AbstractXMLObjectParser { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static final String COMPOSITE_DATA_TYPE = "compositeDataType"; |
| 48 |
|
| 49 |
public static final String COMPOSITE_STATE_SEPARATOR = "-"; |
| 50 |
|
| 51 |
|
| 52 |
public String getParserName() { return COMPOSITE_DATA_TYPE; } |
| 53 |
|
| 54 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 55 |
|
| 56 |
List<String> states = new ArrayList<String>(); |
| 57 |
|
| 58 |
List<DataType> dataTypes = new ArrayList<DataType>(); |
| 59 |
|
| 60 |
for (int i =0; i < xo.getChildCount(); i++) { |
| 61 |
if (xo.getChild(i) instanceof DataType) { |
| 62 |
dataTypes.add((DataType)xo.getChild(i)); |
| 63 |
} else { |
| 64 |
throw new XMLParseException("illegal element in " + getParserName() + " element"); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if (dataTypes.size() != 2) { |
| 69 |
throw new XMLParseException("CompositeDataType requires exactly 2 component DataTypes"); |
| 70 |
} |
| 71 |
|
| 72 |
DataType dt1 = dataTypes.get(0); |
| 73 |
DataType dt2 = dataTypes.get(1); |
| 74 |
|
| 75 |
List<String> jointStates = new ArrayList<String>(); |
| 76 |
|
| 77 |
for (int state1 = 0; state1 < dt1.getStateCount(); state1++) { |
| 78 |
String code1 = dt1.getCode(state1); |
| 79 |
for (int state2 = 0; state2 < dt2.getStateCount(); state2++) { |
| 80 |
String code2 = dt2.getCode(state2); |
| 81 |
jointStates.add(code1 + COMPOSITE_STATE_SEPARATOR + code2); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (states.size() == 0) { |
| 86 |
throw new XMLParseException("No state elements defined in " + getParserName() + " element"); |
| 87 |
} else if (states.size() < 2 ) { |
| 88 |
throw new XMLParseException("Fewer than two state elements defined in " + getParserName() + " element"); |
| 89 |
} |
| 90 |
|
| 91 |
GeneralDataType dataType = new GeneralDataType(states); |
| 92 |
|
| 93 |
return dataType; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public String getParserDescription() { |
| 101 |
return "Defines a composite DataType consisting of multiple data types"; |
| 102 |
} |
| 103 |
|
| 104 |
public Class getReturnType() { return DataType.class; } |
| 105 |
|
| 106 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 107 |
|
| 108 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 109 |
new ElementRule(DataType.class, 2, 2), |
| 110 |
}; |
| 111 |
} |