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.SymbolTableHandler;
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    *
014    *
015    * @author Johan Hall
016    */
017    public class OutputTableFeature extends TableFeature {
018            protected AddressFunction addressFunction;
019            protected SymbolTableHandler tableHandler;
020            
021            public OutputTableFeature(DataFormatInstance dataFormatInstance) throws MaltChainedException {
022                    super();
023                    setTableHandler(dataFormatInstance.getSymbolTables());
024            }
025            
026            public void initialize(Object[] arguments) throws MaltChainedException {
027                    if (arguments.length != 2) {
028                            throw new SyntaxGraphException("Could not initialize OutputTableFeature: number of arguments are not correct. ");
029                    }
030                    if (!(arguments[0] instanceof String)) {
031                            throw new SyntaxGraphException("Could not initialize OutputTableFeature: the first argument is not a string. ");
032                    }
033                    if (!(arguments[1] instanceof AddressFunction)) {
034                            throw new SyntaxGraphException("Could not initialize OutputTableFeature: the second argument is not an address function. ");
035                    }
036                    setSymbolTable(tableHandler.getSymbolTable((String)arguments[0]));
037                    setAddressFunction((AddressFunction)arguments[1]);
038            }
039            
040            public Class<?>[] getParameterTypes() {
041                    Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
042                    return paramTypes; 
043            }
044    
045            public void update()  throws MaltChainedException {
046                    final AddressValue a = addressFunction.getAddressValue();
047                    
048                    if (a.getAddress() == null) {
049                            featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
050                            featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
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 (node.hasHead()) {
060                                                    featureValue.setCode(node.getHeadEdge().getLabelCode(getSymbolTable()));
061                                                    featureValue.setSymbol(getSymbolTable().getSymbolCodeToString(node.getHeadEdge().getLabelCode(getSymbolTable())));
062                                                    featureValue.setKnown(getSymbolTable().getKnown(node.getHeadEdge().getLabelCode(getSymbolTable())));
063                                                    featureValue.setNullValue(false);
064                                            } else {
065                                                    featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_VALUE));
066                                                    featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_VALUE));
067                                                    featureValue.setKnown(true);
068                                                    featureValue.setNullValue(true);
069                                            }       
070                                    } else {
071                                            featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE));
072                                            featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE));
073                                            featureValue.setKnown(true);
074                                            featureValue.setNullValue(true);
075                                    }
076    //                      } catch (ClassCastException e) {
077    //                              featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
078    //                              featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
079    //                              featureValue.setKnown(true);
080    //                              featureValue.setNullValue(true);
081    //                      }
082                    }
083            }
084            
085            public AddressFunction getAddressFunction() {
086                    return addressFunction;
087            }
088    
089            public void setAddressFunction(AddressFunction addressFunction) {
090                    this.addressFunction = addressFunction;
091            }
092            
093            public SymbolTableHandler getTableHandler() {
094                    return tableHandler;
095            }
096    
097            public void setTableHandler(SymbolTableHandler tableHandler) {
098                    this.tableHandler = tableHandler;
099            }
100    
101            
102            public boolean equals(Object obj) {
103                    if (this == obj)
104                            return true;
105                    if (obj == null)
106                            return false;
107                    if (getClass() != obj.getClass())
108                            return false;
109                    return obj.toString().equals(toString());
110            }
111            
112            public int hashCode() {
113                    return 217 + (null == toString() ? 0 : toString().hashCode());
114            }
115            
116            public String toString() {
117                    final StringBuilder sb = new StringBuilder();
118                    sb.append("OutputTable(");
119                    sb.append(super.toString());
120                    sb.append(", ");
121                    sb.append(addressFunction.toString());
122                    sb.append(")");
123                    return sb.toString();
124            }
125    }