* @param parent the instance node
* @return the configured Instance
* @throws Exception if generation fails, e.g., due to unknown attribute type
*/
protected Instance createInstance(Instances header, Element parent) throws Exception {
Instance result;
Element node;
Element child;
boolean sparse;
int i;
int index;
Vector list;
Vector subList;
double[] values;
String content;
double weight;
Instances data;
result = null;
// sparse?
sparse = (parent.getAttribute(ATT_TYPE).equals(VAL_SPARSE));
values = new double[header.numAttributes()];
// weight
if (parent.getAttribute(ATT_WEIGHT).length() != 0) {
weight = Double.parseDouble(parent.getAttribute(ATT_WEIGHT));
} else {
weight = 1.0;
}
list = getChildTags(parent, TAG_VALUE);
for (i = 0; i < list.size(); i++) {
node = (Element) list.get(i);
// determine index
if (sparse) {
index = Integer.parseInt(node.getAttribute(ATT_INDEX)) - 1;
} else {
index = i;
}
// set value
if (node.getAttribute(ATT_MISSING).equals(VAL_YES)) {
values[index] = Instance.missingValue();
} else {
content = getContent(node);
switch (header.attribute(index).type()) {
case Attribute.NUMERIC:
values[index] = Double.parseDouble(content);
break;
case Attribute.DATE:
values[index] = header.attribute(index).parseDate(content);
break;
case Attribute.NOMINAL:
values[index] = header.attribute(index).indexOfValue(content);
break;
case Attribute.STRING:
values[index] = header.attribute(index).addStringValue(
content);
break;
case Attribute.RELATIONAL:
subList = getChildTags(node, TAG_INSTANCES);
child = (Element) subList.get(0);
data = createInstances(header.attribute(index).relation(),
child);
values[index] = header.attribute(index).addRelation(data);
break;
default:
throw new Exception(
"Attribute type " + header.attribute(index).type()
+ " is not supported!");
}
}
}
// create instance
if (sparse) {
result = new SparseInstance(weight, values);
} else {
result = new Instance(weight, values);
}
return result;
}