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