001    package org.maltparser.parser.algorithm;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.feature.FeatureException;
005    import org.maltparser.core.feature.function.AddressFunction;
006    import org.maltparser.core.feature.function.FeatureFunction;
007    import org.maltparser.core.feature.value.AddressValue;
008    import org.maltparser.core.feature.value.FeatureValue;
009    import org.maltparser.core.feature.value.SingleFeatureValue;
010    import org.maltparser.core.io.dataformat.ColumnDescription;
011    import org.maltparser.core.io.dataformat.DataFormatInstance;
012    import org.maltparser.core.symbol.SymbolTable;
013    import org.maltparser.core.symbol.SymbolTableHandler;
014    import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
015    import org.maltparser.core.syntaxgraph.node.DependencyNode;
016    /**
017    *
018    * @author Johan Hall
019    * @since 1.1
020    **/
021    public class InputArcDirFeature implements FeatureFunction {
022            protected ColumnDescription column;
023            protected DataFormatInstance dataFormatInstance;
024            protected SymbolTableHandler tableHandler;
025            protected SymbolTable table;
026            protected SingleFeatureValue featureValue;
027            protected ParsingAlgorithm parsingAlgorithm;
028            protected AddressFunction addressFunction;
029            
030            public InputArcDirFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler, ParsingAlgorithm parsingAlgorithm) throws MaltChainedException {
031                    super();
032                    setDataFormatInstance(dataFormatInstance);
033                    setTableHandler(tableHandler);
034                    setFeatureValue(new SingleFeatureValue(this));
035                    setParsingAlgorithm(parsingAlgorithm);
036            }
037            
038            public void initialize(Object[] arguments) throws MaltChainedException {
039                    if (arguments.length != 2) {
040                            throw new FeatureException("Could not initialize InputArcDirFeature: number of arguments are not correct. ");
041                    }
042                    if (!(arguments[0] instanceof String)) {
043                            throw new FeatureException("Could not initialize InputArcDirFeature: the first argument is not a string. ");
044                    }
045                    if (!(arguments[1] instanceof AddressFunction)) {
046                            throw new FeatureException("Could not initialize InputArcDirFeature: the second argument is not an address function. ");
047                    }
048                    setColumn(dataFormatInstance.getColumnDescriptionByName((String)arguments[0]));
049                    setSymbolTable(tableHandler.addSymbolTable("ARCDIR_"+column.getName(),ColumnDescription.INPUT, "one"));
050                    table.addSymbol("LEFT");
051                    table.addSymbol("RIGHT");
052                    table.addSymbol("ROOT");
053                    setAddressFunction((AddressFunction)arguments[1]);
054            }
055            
056            public Class<?>[] getParameterTypes() {
057                    Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
058                    return paramTypes;
059            }
060            
061            public int getCode(String symbol) throws MaltChainedException {
062                    return table.getSymbolStringToCode(symbol);
063            }
064            
065            public String getSymbol(int code) throws MaltChainedException {
066                    return table.getSymbolCodeToString(code);
067            }
068            
069            public FeatureValue getFeatureValue() {
070                    return featureValue;
071            }
072    
073            public void updateCardinality() throws MaltChainedException {
074                    featureValue.setCardinality(table.getValueCounter());
075            }
076    
077            public void update() throws MaltChainedException {
078                    AddressValue a = addressFunction.getAddressValue();
079                    if (a.getAddress() != null && a.getAddressClass() == org.maltparser.core.syntaxgraph.node.DependencyNode.class) {
080                            DependencyNode node = (DependencyNode)a.getAddress();
081                            try {
082                                    int index = Integer.parseInt(node.getLabelSymbol(column.getSymbolTable()));
083                                    if (node.isRoot()) {
084                                            featureValue.setCode(table.getNullValueCode(NullValueId.ROOT_NODE));
085                                            featureValue.setSymbol(table.getNullValueSymbol(NullValueId.ROOT_NODE));
086                                            featureValue.setKnown(true);
087                                            featureValue.setNullValue(true);
088                                    } else if (index == 0) {
089                                            featureValue.setCode(table.getSymbolStringToCode("ROOT"));
090                                            featureValue.setSymbol("ROOT");
091                                            featureValue.setKnown(true);
092                                            featureValue.setNullValue(false);
093                                    } else if (index < node.getIndex()) {
094                                            featureValue.setCode(table.getSymbolStringToCode("LEFT"));
095                                            featureValue.setSymbol("LEFT");
096                                            featureValue.setKnown(true);
097                                            featureValue.setNullValue(false);
098                                    } else if (index > node.getIndex()) {
099                                            featureValue.setCode(table.getSymbolStringToCode("RIGHT"));
100                                            featureValue.setSymbol("RIGHT");
101                                            featureValue.setKnown(true);
102                                            featureValue.setNullValue(false);
103                                    }
104                            } catch (NumberFormatException e) {
105                                    throw new FeatureException("The index of the feature must be an integer value. ", e);
106                            }
107                    } else {
108                            featureValue.setCode(table.getNullValueCode(NullValueId.NO_NODE));
109                            featureValue.setSymbol(table.getNullValueSymbol(NullValueId.NO_NODE));
110                            featureValue.setKnown(true);
111                            featureValue.setNullValue(true);
112                    }
113            }
114    
115            public AddressFunction getAddressFunction() {
116                    return addressFunction;
117            }
118    
119            public void setAddressFunction(AddressFunction addressFunction) {
120                    this.addressFunction = addressFunction;
121            }
122            
123            public ParsingAlgorithm getParsingAlgorithm() {
124                    return parsingAlgorithm;
125            }
126    
127            public void setParsingAlgorithm(ParsingAlgorithm parsingAlgorithm) {
128                    this.parsingAlgorithm = parsingAlgorithm;
129            }
130    
131            public ColumnDescription getColumn() {
132                    return column;
133            }
134    
135            public void setColumn(ColumnDescription column) throws MaltChainedException {
136                    if (column.getType() != ColumnDescription.INTEGER) {
137                            throw new FeatureException("InputArc feature column must be of type integer. ");
138                    }
139                    this.column = column;
140            }
141    
142            public DataFormatInstance getDataFormatInstance() {
143                    return dataFormatInstance;
144            }
145    
146            public void setDataFormatInstance(DataFormatInstance dataFormatInstance) {
147                    this.dataFormatInstance = dataFormatInstance;
148            }
149    
150            public void setFeatureValue(SingleFeatureValue featureValue) {
151                    this.featureValue = featureValue;
152            }
153            
154            public SymbolTable getSymbolTable() {
155                    return table;
156            }
157    
158            public void setSymbolTable(SymbolTable table) {
159                    this.table = table;
160            }
161            
162            public SymbolTableHandler getTableHandler() {
163                    return tableHandler;
164            }
165    
166            public void setTableHandler(SymbolTableHandler tableHandler) {
167                    this.tableHandler = tableHandler;
168            }
169            
170            public boolean equals(Object obj) {
171                    if (!(obj instanceof InputArcDirFeature)) {
172                            return false;
173                    }
174                    if (!obj.toString().equals(this.toString())) {
175                            return false;
176                    }
177                    return true;
178            }
179            
180            public String toString() {
181                    return "InputArcDir(" + column.getName() + ", " + addressFunction.toString() + ")";
182            }
183    }