001 package org.maltparser.core.syntaxgraph.feature;
002
003 import org.maltparser.core.exception.MaltChainedException;
004 import org.maltparser.core.feature.function.AddressFunction;
005 import org.maltparser.core.feature.value.AddressValue;
006 import org.maltparser.core.io.dataformat.ColumnDescription;
007 import org.maltparser.core.io.dataformat.DataFormatInstance;
008 import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
009 import org.maltparser.core.syntaxgraph.SyntaxGraphException;
010 import org.maltparser.core.syntaxgraph.node.DependencyNode;
011
012 /**
013 *
014 *
015 * @author Johan Hall
016 */
017 public final class InputColumnFeature extends ColumnFeature {
018 private final DataFormatInstance dataFormatInstance;
019 private AddressFunction addressFunction;
020
021 public InputColumnFeature(DataFormatInstance dataFormatInstance) throws MaltChainedException {
022 super();
023 this.dataFormatInstance = dataFormatInstance;
024 }
025
026 public void initialize(Object[] arguments) throws MaltChainedException {
027 if (arguments.length != 2) {
028 throw new SyntaxGraphException("Could not initialize InputColumnFeature: number of arguments are not correct. ");
029 }
030 if (!(arguments[0] instanceof String)) {
031 throw new SyntaxGraphException("Could not initialize InputColumnFeature: the first argument is not a string. ");
032 }
033 if (!(arguments[1] instanceof AddressFunction)) {
034 throw new SyntaxGraphException("Could not initialize InputColumnFeature: the second argument is not an address function. ");
035 }
036 ColumnDescription column = dataFormatInstance.getColumnDescriptionByName((String)arguments[0]);
037 if (column == null) {
038 throw new SyntaxGraphException("Could not initialize InputColumnFeature: the input column type '"+(String)arguments[0]+"' could not be found in the data format specification. ' ");
039 }
040 setColumn(column);
041 setAddressFunction((AddressFunction)arguments[1]);
042 }
043
044 public Class<?>[] getParameterTypes() {
045 Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
046 return paramTypes;
047 }
048
049 public void update() throws MaltChainedException {
050 final AddressValue a = addressFunction.getAddressValue();
051
052 if (a.getAddress() == null) {
053 featureValue.update(column.getSymbolTable().getNullValueCode(NullValueId.NO_NODE),
054 column.getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE), true, 1);
055 } else {
056 final DependencyNode node = (DependencyNode)a.getAddress();
057
058 if (!node.isRoot()) {
059 int indexCode = node.getLabelCode(column.getSymbolTable());
060 String symbol = column.getSymbolTable().getSymbolCodeToString(indexCode);
061 if (column.getType() == ColumnDescription.STRING) {
062 featureValue.update(indexCode, symbol, false, 1);
063 } else {
064 castFeatureValue(symbol);
065 }
066 } else {
067 featureValue.update(column.getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE),
068 column.getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE), true, 1);
069 }
070 }
071
072 }
073
074 public AddressFunction getAddressFunction() {
075 return addressFunction;
076 }
077
078 public void setAddressFunction(AddressFunction addressFunction) {
079 this.addressFunction = addressFunction;
080 }
081
082 public DataFormatInstance getDataFormatInstance() {
083 return dataFormatInstance;
084 }
085
086 public boolean equals(Object obj) {
087 if (this == obj)
088 return true;
089 if (obj == null)
090 return false;
091 if (getClass() != obj.getClass())
092 return false;
093 return obj.toString().equals(toString());
094 }
095
096 public int hashCode() {
097 return 217 + (null == toString() ? 0 : toString().hashCode());
098 }
099
100 public String toString() {
101 final StringBuilder sb = new StringBuilder();
102 sb.append("InputColumn(");
103 sb.append(super.toString());
104 sb.append(", ");
105 sb.append(addressFunction.toString());
106 sb.append(")");
107 return sb.toString();
108 }
109 }