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.setIndexCode(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.setIndexCode(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.setIndexCode(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.setIndexCode(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                    featureValue.setValue(1);
084            }
085            
086            public AddressFunction getAddressFunction() {
087                    return addressFunction;
088            }
089    
090            public void setAddressFunction(AddressFunction addressFunction) {
091                    this.addressFunction = addressFunction;
092            }
093            
094            public SymbolTableHandler getTableHandler() {
095                    return tableHandler;
096            }
097    
098            public void setTableHandler(SymbolTableHandler tableHandler) {
099                    this.tableHandler = tableHandler;
100            }
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("OutputTable(");
120                    sb.append(super.toString());
121                    sb.append(", ");
122                    sb.append(addressFunction.toString());
123                    sb.append(")");
124                    return sb.toString();
125            }
126    }