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.DataFormatInstance;
007    import org.maltparser.core.symbol.TableFeature;
008    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
009    import org.maltparser.core.syntaxgraph.SyntaxGraphException;
010    import org.maltparser.core.syntaxgraph.node.DependencyNode;
011    
012    public class InputTableFeature extends TableFeature {
013            protected AddressFunction addressFunction;
014    
015            public InputTableFeature(DataFormatInstance dataFormatInstance) throws MaltChainedException {
016                    super();
017                    setTableHandler(dataFormatInstance.getSymbolTables());
018            }
019            
020            public void initialize(Object[] arguments) throws MaltChainedException {
021                    if (arguments.length != 2) {
022                            throw new SyntaxGraphException("Could not initialize InputTableFeature: number of arguments are not correct. ");
023                    }
024                    if (!(arguments[0] instanceof String)) {
025                            throw new SyntaxGraphException("Could not initialize InputTableFeature: the first argument is not a string. ");
026                    }
027                    if (!(arguments[1] instanceof AddressFunction)) {
028                            throw new SyntaxGraphException("Could not initialize InputTableFeature: the second argument is not an address function. ");
029                    }
030                    setTableName((String)arguments[0]);
031                    setSymbolTable(tableHandler.getSymbolTable(getTableName()));
032                    setAddressFunction((AddressFunction)arguments[1]);
033            }
034            
035            public Class<?>[] getParameterTypes() {
036                    Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
037                    return paramTypes; 
038            }
039    
040            public void update()  throws MaltChainedException {
041                    final AddressValue a = addressFunction.getAddressValue();
042                    
043                    if (a.getAddress() == null) {
044                            if (getSymbolTable() != null) {
045                                    featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
046                                    featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
047                            } else {
048                                    featureValue.setCode(0);
049                                    featureValue.setSymbol("#null");
050                            }
051                            featureValue.setKnown(true);
052                            featureValue.setNullValue(true);                        
053                    } else {
054    //                      try { 
055    //                              a.getAddressClass().asSubclass(org.maltparser.core.syntaxgraph.node.DependencyNode.class);
056    
057                                    final DependencyNode node = (DependencyNode)a.getAddress();
058                                    if (!node.isRoot()) {
059                                            if (getSymbolTable() != null && node.hasLabel(getSymbolTable())) {
060                                                    featureValue.setCode(node.getLabelCode(getSymbolTable()));
061                                                    featureValue.setSymbol(getSymbolTable().getSymbolCodeToString(node.getLabelCode(getSymbolTable())));
062                                                    featureValue.setKnown(getSymbolTable().getKnown(node.getLabelCode(getSymbolTable())));
063                                                    featureValue.setNullValue(false);
064                                            } else {
065                                                    featureValue.setCode(0);
066                                                    featureValue.setSymbol("#null");
067    //                                              if (getSymbolTable() != null) {
068    //                                                      featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_VALUE));
069    //                                                      featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_VALUE));
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.setCode(getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE));
080    //                                              featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE));
081    //                                      } else {
082    //                                              featureValue.setCode(0);
083    //                                              featureValue.setSymbol("#null");
084    //                                      }
085                                            featureValue.setCode(0);
086                                            featureValue.setSymbol("#null");
087                                            featureValue.setKnown(true);
088                                            featureValue.setNullValue(true);
089                                    }
090    //                      } catch (ClassCastException e) {
091    //                              featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
092    //                              featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
093    //                              featureValue.setKnown(true);
094    //                              featureValue.setNullValue(true);
095    //                      }
096                    }
097            }
098            
099            public AddressFunction getAddressFunction() {
100                    return addressFunction;
101            }
102    
103            public void setAddressFunction(AddressFunction addressFunction) {
104                    this.addressFunction = addressFunction;
105            }
106            
107            public boolean equals(Object obj) {
108                    if (this == obj)
109                            return true;
110                    if (obj == null)
111                            return false;
112                    if (getClass() != obj.getClass())
113                            return false;
114                    return obj.toString().equals(toString());
115            }
116            
117            public int hashCode() {
118                    return 217 + (null == toString() ? 0 : toString().hashCode());
119            }
120            
121            public String toString() {
122                    final StringBuilder sb = new StringBuilder();
123                    sb.append("InputTable(");
124                    sb.append(super.toString());
125                    sb.append(", ");
126                    sb.append(addressFunction.toString());
127                    sb.append(")");
128                    return sb.toString();
129            }
130    
131    }