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