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