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.TableFeature;
009    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
010    import org.maltparser.core.syntaxgraph.SyntaxGraphException;
011    import org.maltparser.core.syntaxgraph.node.DependencyNode;
012    
013    public class InputTableFeature extends TableFeature {
014            protected AddressFunction addressFunction;
015    
016            public InputTableFeature(DataFormatInstance dataFormatInstance) throws MaltChainedException {
017                    super();
018                    setTableHandler(dataFormatInstance.getSymbolTables());
019            }
020            
021            public void initialize(Object[] arguments) throws MaltChainedException {
022                    if (arguments.length != 2) {
023                            throw new SyntaxGraphException("Could not initialize InputTableFeature: number of arguments are not correct. ");
024                    }
025                    if (!(arguments[0] instanceof String)) {
026                            throw new SyntaxGraphException("Could not initialize InputTableFeature: the first argument is not a string. ");
027                    }
028                    if (!(arguments[1] instanceof AddressFunction)) {
029                            throw new SyntaxGraphException("Could not initialize InputTableFeature: the second argument is not an address function. ");
030                    }
031                    setTableName((String)arguments[0]);
032                    setSymbolTable(tableHandler.getSymbolTable(getTableName()));
033                    setAddressFunction((AddressFunction)arguments[1]);
034                    setType(ColumnDescription.STRING); // TODO Probably it could possible to vary the type
035            }
036            
037            public Class<?>[] getParameterTypes() {
038                    Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
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.setKnown(true);
054                            featureValue.setNullValue(true);                        
055                    } else {
056                            final DependencyNode node = (DependencyNode)a.getAddress();
057                            if (!node.isRoot()) {
058                                    if (getSymbolTable() != null && node.hasLabel(getSymbolTable())) {
059                                            featureValue.setIndexCode(node.getLabelCode(getSymbolTable()));
060                                            featureValue.setSymbol(getSymbolTable().getSymbolCodeToString(node.getLabelCode(getSymbolTable())));
061    //                                      featureValue.setKnown(getSymbolTable().getKnown(node.getLabelCode(getSymbolTable())));
062                                            featureValue.setNullValue(false);
063                                    } else {
064    //                                              featureValue.setCode(0);
065    //                                              featureValue.setSymbol("#null");
066                                            if (getSymbolTable() != null) {
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    //                                      else {
083    //                                              featureValue.setCode(0);
084    //                                              featureValue.setSymbol("#null");
085    //                                      }
086    //                                      featureValue.setCode(0);
087    //                                      featureValue.setSymbol("#null");
088    //                              featureValue.setKnown(true);
089                                    featureValue.setNullValue(true);
090                            }
091                    }
092                    featureValue.setValue(1);
093            }
094            
095            public AddressFunction getAddressFunction() {
096                    return addressFunction;
097            }
098    
099            public void setAddressFunction(AddressFunction addressFunction) {
100                    this.addressFunction = addressFunction;
101            }
102            
103            public boolean equals(Object obj) {
104                    if (this == obj)
105                            return true;
106                    if (obj == null)
107                            return false;
108                    if (getClass() != obj.getClass())
109                            return false;
110                    return obj.toString().equals(toString());
111            }
112            
113            public int hashCode() {
114                    return 217 + (null == toString() ? 0 : toString().hashCode());
115            }
116            
117            public String toString() {
118                    final StringBuilder sb = new StringBuilder();
119                    sb.append("InputTable(");
120                    sb.append(super.toString());
121                    sb.append(", ");
122                    sb.append(addressFunction.toString());
123                    sb.append(")");
124                    return sb.toString();
125            }
126    
127    }