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