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            protected AddressFunction addressFunction;
019            protected DataFormatInstance dataFormatInstance;
020            
021            public InputColumnFeature(DataFormatInstance dataFormatInstance) throws MaltChainedException {
022                    super();
023                    setDataFormatInstance(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 void setDataFormatInstance(DataFormatInstance dataFormatInstance) {
087                    this.dataFormatInstance = dataFormatInstance;
088            }
089            
090            public boolean equals(Object obj) {
091                    if (this == obj)
092                            return true;
093                    if (obj == null)
094                            return false;
095                    if (getClass() != obj.getClass())
096                            return false;
097                    return obj.toString().equals(toString());
098            }
099            
100            public int hashCode() {
101                    return 217 + (null == toString() ? 0 : toString().hashCode());
102            }
103            
104            public String toString() {
105                    final StringBuilder sb = new StringBuilder();
106                    sb.append("InputColumn(");
107                    sb.append(super.toString());
108                    sb.append(", ");
109                    sb.append(addressFunction.toString());
110                    sb.append(")");
111                    return sb.toString();
112            }
113    }