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