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