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 class OutputColumnFeature 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        
025        public OutputColumnFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException {
026                super();
027                this.dataFormatInstance = dataFormatInstance;
028                this.tableHandler = tableHandler;
029        }
030        
031        public void initialize(Object[] arguments) throws MaltChainedException {
032                if (arguments.length != 2) {
033                        throw new SyntaxGraphException("Could not initialize OutputColumnFeature: number of arguments are not correct. ");
034                }
035                if (!(arguments[0] instanceof String)) {
036                        throw new SyntaxGraphException("Could not initialize OutputColumnFeature: the first argument is not a string. ");
037                }
038                if (!(arguments[1] instanceof AddressFunction)) {
039                        throw new SyntaxGraphException("Could not initialize OutputColumnFeature: the second argument is not an address function. ");
040                }
041                ColumnDescription column = dataFormatInstance.getColumnDescriptionByName((String)arguments[0]);
042                if (column == null) {
043                        throw new SyntaxGraphException("Could not initialize OutputColumnFeature: the output column type '"+(String)arguments[0]+"' could not be found in the data format specification. ' ");
044                }
045                setColumn(column);
046                setSymbolTable(tableHandler.getSymbolTable(column.getName()));
047                setAddressFunction((AddressFunction)arguments[1]);
048        }
049        
050        public Class<?>[] getParameterTypes() {
051                return paramTypes; 
052        }
053
054        public void update()  throws MaltChainedException {
055                final AddressValue a = addressFunction.getAddressValue();
056                
057                if (a.getAddress() == null) {
058                        featureValue.update(symbolTable.getNullValueCode(NullValueId.NO_NODE), 
059                                        symbolTable.getNullValueSymbol(NullValueId.NO_NODE), true, 1);
060                } else {
061                        final DependencyNode node = (DependencyNode)a.getAddress();
062                        if (!node.isRoot()) {
063                                if (node.hasHead()) {
064                                        int indexCode = node.getHeadEdge().getLabelCode(symbolTable);
065                                        String symbol = symbolTable.getSymbolCodeToString(indexCode);
066                                        if (column.getType() == ColumnDescription.STRING) {
067                                                featureValue.update(indexCode, symbol, false, 1);
068                                        } else {
069                                                castFeatureValue(symbol);
070                                        }
071                                } else {
072                                        featureValue.update(symbolTable.getNullValueCode(NullValueId.NO_VALUE), 
073                                                        symbolTable.getNullValueSymbol(NullValueId.NO_VALUE), true, 1);
074                                }       
075                        } else {
076                                featureValue.update(symbolTable.getNullValueCode(NullValueId.ROOT_NODE), 
077                                                symbolTable.getNullValueSymbol(NullValueId.ROOT_NODE), true, 1);
078                        }
079                }
080        }
081        
082        public AddressFunction getAddressFunction() {
083                return addressFunction;
084        }
085
086        public void setAddressFunction(AddressFunction addressFunction) {
087                this.addressFunction = addressFunction;
088        }
089        
090        public DataFormatInstance getDataFormatInstance() {
091                return dataFormatInstance;
092        }
093
094        public boolean equals(Object obj) {
095                if (this == obj)
096                        return true;
097                if (obj == null)
098                        return false;
099                if (getClass() != obj.getClass())
100                        return false;
101                return obj.toString().equals(toString());
102        }
103        
104        public int hashCode() {
105                return 217 + (null == toString() ? 0 : toString().hashCode());
106        }
107        
108        public String toString() {
109                final StringBuilder sb = new StringBuilder();
110                sb.append("OutputColumn(");
111                sb.append(super.toString());
112                sb.append(", ");
113                sb.append(addressFunction.toString());
114                sb.append(")");
115                return sb.toString();
116        }
117}