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.TableFeature;
010import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
011import org.maltparser.core.syntaxgraph.SyntaxGraphException;
012import org.maltparser.core.syntaxgraph.node.DependencyNode;
013
014public final class InputTableFeature extends TableFeature {
015        public final static Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
016        private AddressFunction addressFunction;
017
018        public InputTableFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException {
019                super(tableHandler);
020        }
021        
022        public void initialize(Object[] arguments) throws MaltChainedException {
023                if (arguments.length != 2) {
024                        throw new SyntaxGraphException("Could not initialize InputTableFeature: number of arguments are not correct. ");
025                }
026                if (!(arguments[0] instanceof String)) {
027                        throw new SyntaxGraphException("Could not initialize InputTableFeature: the first argument is not a string. ");
028                }
029                if (!(arguments[1] instanceof AddressFunction)) {
030                        throw new SyntaxGraphException("Could not initialize InputTableFeature: the second argument is not an address function. ");
031                }
032                setTableName((String)arguments[0]);
033                setSymbolTable(tableHandler.getSymbolTable(getTableName()));
034                setAddressFunction((AddressFunction)arguments[1]);
035                setType(ColumnDescription.STRING); // TODO Probably it could possible to vary the type
036        }
037        
038        public Class<?>[] getParameterTypes() {
039                return paramTypes; 
040        }
041
042        public void update()  throws MaltChainedException {
043                final AddressValue a = addressFunction.getAddressValue();
044                
045                if (a.getAddress() == null) {
046                        if (getSymbolTable() != null) {
047                                featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
048                                featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
049                        } else {
050                                featureValue.setIndexCode(0);
051                                featureValue.setSymbol("#null");
052                        }
053                        featureValue.setNullValue(true);                        
054                } else {
055                        final DependencyNode node = (DependencyNode)a.getAddress();
056                        if (!node.isRoot()) {
057                                if (getSymbolTable() != null && node.hasLabel(getSymbolTable())) {
058                                        featureValue.setIndexCode(node.getLabelCode(getSymbolTable()));
059                                        featureValue.setSymbol(getSymbolTable().getSymbolCodeToString(node.getLabelCode(getSymbolTable())));
060                                        featureValue.setNullValue(false);
061                                } else {
062                                        if (getSymbolTable() != null) {
063                                                featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
064                                                featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
065
066                                        } 
067                                        featureValue.setNullValue(true);
068                                }       
069                        } else {
070                                if (getSymbolTable() != null) {
071                                        featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE));
072                                        featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE));
073                                } 
074
075                                featureValue.setNullValue(true);
076                        }
077                }
078                featureValue.setValue(1);
079        }
080        
081        public AddressFunction getAddressFunction() {
082                return addressFunction;
083        }
084
085        public void setAddressFunction(AddressFunction addressFunction) {
086                this.addressFunction = addressFunction;
087        }
088        
089        public boolean equals(Object obj) {
090                if (this == obj)
091                        return true;
092                if (obj == null)
093                        return false;
094                if (getClass() != obj.getClass())
095                        return false;
096                return obj.toString().equals(toString());
097        }
098        
099        public int hashCode() {
100                return 217 + (null == toString() ? 0 : toString().hashCode());
101        }
102        
103        public String toString() {
104                final StringBuilder sb = new StringBuilder();
105                sb.append("InputTable(");
106                sb.append(super.toString());
107                sb.append(", ");
108                sb.append(addressFunction.toString());
109                sb.append(")");
110                return sb.toString();
111        }
112
113}