    001    package org.maltparser.core.syntaxgraph.feature;
    002
    003    import org.maltparser.core.exception.MaltChainedException;
    004    import org.maltparser.core.feature.function.FeatureFunction;
    005    import org.maltparser.core.feature.function.Modifiable;
    006    import org.maltparser.core.feature.value.FeatureValue;
    007    import org.maltparser.core.feature.value.SingleFeatureValue;
    008    import org.maltparser.core.io.dataformat.ColumnDescription;
    009    import org.maltparser.core.symbol.SymbolTable;
    010    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
    011
    012    /**
    013    *
    014    *
    015    * @author Johan Hall
    016    */
    017    public abstract class ColumnFeature implements FeatureFunction, Modifiable {
    018            protected ColumnDescription column;
    019            protected SingleFeatureValue featureValue;
    020
    021            public ColumnFeature() throws MaltChainedException {
    022                    featureValue = new SingleFeatureValue(this);
    023            }
    024
    025            public abstract void update() throws MaltChainedException;
    026            public abstract void initialize(Object[] arguments) throws MaltChainedException;
    027            public abstract Class<?>[] getParameterTypes();
    028
    029            public String getSymbol(int value) throws MaltChainedException {
    030                    return column.getSymbolTable().getSymbolCodeToString(value);
    031            }
    032
    033            public int getCode(String value) throws MaltChainedException {
    034                    return column.getSymbolTable().getSymbolStringToCode(value);
    035            }
    036
    037            public ColumnDescription getColumn() {
    038                    return column;
    039            }
    040
    041            protected void setColumn(ColumnDescription column) {
    042                    this.column = column;
    043            }
    044
    045            public void updateCardinality() {
    046                    featureValue.setCardinality(column.getSymbolTable().getValueCounter());
    047            }
    048
    049            public void setFeatureValue(int value) throws MaltChainedException {
    050                    if (column.getSymbolTable().getSymbolCodeToString(value) == null) {
    051                            featureValue.setCode(value);
    052                            featureValue.setKnown(column.getSymbolTable().getKnown(value));
    053                            featureValue.setSymbol(column.getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
    054                            featureValue.setNullValue(true);
    055                    } else {
    056                            featureValue.setCode(value);
    057                            featureValue.setKnown(column.getSymbolTable().getKnown(value));
    058                            featureValue.setSymbol(column.getSymbolTable().getSymbolCodeToString(value));
    059                            featureValue.setNullValue(column.getSymbolTable().isNullValue(value));
    060                    }
    061            }
    062
    063            public void setFeatureValue(String value) throws MaltChainedException {
    064                    if (column.getSymbolTable().getSymbolStringToCode(value) < 0) {
    065                            featureValue.setCode(column.getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
    066                            featureValue.setKnown(column.getSymbolTable().getKnown(value));
    067                            featureValue.setSymbol(value);
    068                            featureValue.setNullValue(true);
    069                    } else {
    070                            featureValue.setCode(column.getSymbolTable().getSymbolStringToCode(value));
    071                            featureValue.setKnown(column.getSymbolTable().getKnown(value));
    072                            featureValue.setSymbol(value);
    073                            featureValue.setNullValue(column.getSymbolTable().isNullValue(value));
    074                    }
    075            }
    076
    077            public FeatureValue getFeatureValue() {
    078                    return featureValue;
    079            }
    080
    081            public boolean equals(Object obj) {
    082                    if (this == obj)
    083                            return true;
    084                    if (obj == null)
    085                            return false;
    086                    if (getClass() != obj.getClass())
    087                            return false;
    088                    return obj.toString().equals(this.toString());
    089            }
    090
    091            public String getColumnName() {
    092                    return column.getName();
    093            }
    094
    095            public SymbolTable getSymbolTable() {
    096                    return column.getSymbolTable();
    097            }
    098
    099            public String toString() {
    100                    return column.getName();
    101            }
    102    }