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//                                              featureValue.setCode(0);
063//                                              featureValue.setSymbol("#null");
064                                        if (getSymbolTable() != null) {
065                                                featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
066                                                featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
067//                                              featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_VALUE));
068//                                              featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_VALUE));
069                                        } 
070//                                              else {
071//                                                      featureValue.setCode(0);
072//                                                      featureValue.setSymbol("#null");
073//                                              }
074//                                      featureValue.setKnown(true);
075                                        featureValue.setNullValue(true);
076                                }       
077                        } else {
078                                if (getSymbolTable() != null) {
079                                        featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE));
080                                        featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE));
081                                } 
082
083                                featureValue.setNullValue(true);
084                        }
085                }
086                featureValue.setValue(1);
087        }
088        
089        public AddressFunction getAddressFunction() {
090                return addressFunction;
091        }
092
093        public void setAddressFunction(AddressFunction addressFunction) {
094                this.addressFunction = addressFunction;
095        }
096        
097        public boolean equals(Object obj) {
098                if (this == obj)
099                        return true;
100                if (obj == null)
101                        return false;
102                if (getClass() != obj.getClass())
103                        return false;
104                return obj.toString().equals(toString());
105        }
106        
107        public int hashCode() {
108                return 217 + (null == toString() ? 0 : toString().hashCode());
109        }
110        
111        public String toString() {
112                final StringBuilder sb = new StringBuilder();
113                sb.append("InputTable(");
114                sb.append(super.toString());
115                sb.append(", ");
116                sb.append(addressFunction.toString());
117                sb.append(")");
118                return sb.toString();
119        }
120
121}