001    package org.maltparser.core.syntaxgraph.feature;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.feature.FeatureException;
005    import org.maltparser.core.feature.function.FeatureFunction;
006    import org.maltparser.core.feature.function.Modifiable;
007    import org.maltparser.core.feature.value.FeatureValue;
008    import org.maltparser.core.feature.value.SingleFeatureValue;
009    import org.maltparser.core.io.dataformat.ColumnDescription;
010    import org.maltparser.core.symbol.SymbolTable;
011    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
012    
013    /**
014    *
015    *
016    * @author Johan Hall
017    */
018    public abstract class ColumnFeature implements FeatureFunction, Modifiable {
019            protected ColumnDescription column;
020            protected final SingleFeatureValue featureValue;
021            
022            public ColumnFeature() throws MaltChainedException {
023                    featureValue = new SingleFeatureValue(this);
024            }
025            
026            public abstract void update() throws MaltChainedException;
027            public abstract void initialize(Object[] arguments) throws MaltChainedException;
028            public abstract Class<?>[] getParameterTypes();
029            
030            public String getSymbol(int value) throws MaltChainedException {
031                    return column.getSymbolTable().getSymbolCodeToString(value);
032            }
033            
034            public int getCode(String value) throws MaltChainedException {
035                    return column.getSymbolTable().getSymbolStringToCode(value);
036            }
037            
038            public ColumnDescription getColumn() {
039                    return column;
040            }
041            
042            protected void setColumn(ColumnDescription column) {
043                    this.column = column;
044            }
045            
046            public void setFeatureValue(int indexCode) throws MaltChainedException {
047                    final String symbol = column.getSymbolTable().getSymbolCodeToString(indexCode);
048                    
049                    if (symbol == null) {
050                            featureValue.update(indexCode, column.getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE), true, 1);
051                    } else {
052                            boolean nullValue = column.getSymbolTable().isNullValue(indexCode);
053                            if (column.getType() == ColumnDescription.STRING || nullValue) {
054                                    featureValue.update(indexCode, symbol, nullValue, 1);
055                            } else {
056                                    castFeatureValue(symbol);
057                            }
058                    }
059            }
060            
061            public void setFeatureValue(String symbol) throws MaltChainedException {
062                    final int indexCode = column.getSymbolTable().getSymbolStringToCode(symbol);
063                    if (indexCode < 0) {
064                            featureValue.update(column.getSymbolTable().getNullValueCode(NullValueId.NO_NODE), symbol, true, 1);
065                    } else {
066                            boolean nullValue = column.getSymbolTable().isNullValue(symbol);
067                            if (column.getType() == ColumnDescription.STRING || nullValue) {
068                                    featureValue.update(indexCode, symbol, nullValue, 1);
069                            } else {
070                                    castFeatureValue(symbol);
071                            }
072                    }
073            }
074            
075            protected void castFeatureValue(String symbol) throws MaltChainedException {
076                    if (column.getType() == ColumnDescription.INTEGER) {
077                            try {
078                                    final int dotIndex = symbol.indexOf('.');
079                                    if (dotIndex == -1) {
080                                            featureValue.setValue(Integer.parseInt(symbol));
081                                            featureValue.setSymbol(symbol);
082                                    } else {
083                                            featureValue.setValue(Integer.parseInt(symbol.substring(0,dotIndex)));
084                                            featureValue.setSymbol(symbol.substring(0,dotIndex));
085                                    }
086                            } catch (NumberFormatException e) {
087                                    throw new FeatureException("Could not cast the feature value '"+symbol+"' to integer value.", e);
088                            }
089                    } else if (column.getType() == ColumnDescription.BOOLEAN) {
090                            final int dotIndex = symbol.indexOf('.');
091                            if (symbol.equals("1") || symbol.equals("true") ||  symbol.equals("#true#") || (dotIndex != -1 && symbol.substring(0,dotIndex).equals("1"))) {
092                                    featureValue.setValue(1);
093                                    featureValue.setSymbol("true");
094                            } else if (symbol.equals("false") || symbol.equals("0") || (dotIndex != -1 && symbol.substring(0,dotIndex).equals("0"))) {
095                                    featureValue.setValue(0);
096                                    featureValue.setSymbol("false");
097                            } else {
098                                    throw new FeatureException("Could not cast the feature value '"+symbol+"' to boolean value.");
099                            }
100                    } else if (column.getType() == ColumnDescription.REAL) {
101                            try {
102                                    featureValue.setValue(Double.parseDouble(symbol));
103                                    featureValue.setSymbol(symbol);
104                            } catch (NumberFormatException e) {
105                                    throw new FeatureException("Could not cast the feature value '"+symbol+"' to real value.", e);
106                            }
107                    }
108                    if (column.getType() == ColumnDescription.INTEGER || column.getType() == ColumnDescription.BOOLEAN || column.getType() == ColumnDescription.REAL) {
109                            featureValue.setNullValue(false);
110                            featureValue.setIndexCode(1);
111                    }
112            }
113            
114            public FeatureValue getFeatureValue() {
115                    return featureValue;
116            }
117            
118            public boolean equals(Object obj) {
119                    if (this == obj)
120                            return true;
121                    if (obj == null)
122                            return false;
123                    if (getClass() != obj.getClass())
124                            return false;
125                    return obj.toString().equals(this.toString());
126            }
127    
128            public String getColumnName() {
129                    return column.getName();
130            }
131            
132            public SymbolTable getSymbolTable() {
133                    return column.getSymbolTable();
134            }
135            
136            public  int getType() {
137                    return column.getType();
138            }
139            
140            public String getMapIdentifier() {
141                    return getSymbolTable().getName();
142            }
143            
144            public String toString() {
145                    return column.getName();
146            }
147    }