001package org.maltparser.core.syntaxgraph.feature;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.feature.function.AddressFunction;
005import org.maltparser.core.feature.value.AddressValue;
006import org.maltparser.core.io.dataformat.ColumnDescription;
007import org.maltparser.core.io.dataformat.DataFormatInstance;
008import org.maltparser.core.symbol.SymbolTableHandler;
009import org.maltparser.core.symbol.TableFeature;
010import org.maltparser.core.symbol.nullvalue.NullValues.NullValueId;
011import org.maltparser.core.syntaxgraph.SyntaxGraphException;
012import org.maltparser.core.syntaxgraph.node.DependencyNode;
013/**
014*
015*
016* @author Johan Hall
017*/
018public final class OutputTableFeature extends TableFeature {
019        public final static Class<?>[] paramTypes = { java.lang.String.class, org.maltparser.core.feature.function.AddressFunction.class };
020        private AddressFunction addressFunction;
021        
022        public OutputTableFeature(DataFormatInstance dataFormatInstance, SymbolTableHandler tableHandler) throws MaltChainedException {
023                super(tableHandler);
024        }
025        
026        public void initialize(Object[] arguments) throws MaltChainedException {
027                if (arguments.length != 2) {
028                        throw new SyntaxGraphException("Could not initialize OutputTableFeature: number of arguments are not correct. ");
029                }
030                if (!(arguments[0] instanceof String)) {
031                        throw new SyntaxGraphException("Could not initialize OutputTableFeature: the first argument is not a string. ");
032                }
033                if (!(arguments[1] instanceof AddressFunction)) {
034                        throw new SyntaxGraphException("Could not initialize OutputTableFeature: the second argument is not an address function. ");
035                }
036                setSymbolTable(tableHandler.getSymbolTable((String)arguments[0]));
037                setAddressFunction((AddressFunction)arguments[1]);
038                setType(ColumnDescription.STRING); // TODO Probably it could possible to vary the type
039        }
040        
041        public Class<?>[] getParameterTypes() {
042                return paramTypes; 
043        }
044
045        public void update()  throws MaltChainedException {
046                final AddressValue a = addressFunction.getAddressValue();
047                
048                if (a.getAddress() == null) {
049                        featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
050                        featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
051//                      featureValue.setKnown(true);
052                        featureValue.setNullValue(true);                        
053                } else {
054//                      try { 
055//                              a.getAddressClass().asSubclass(org.maltparser.core.syntaxgraph.node.DependencyNode.class);
056
057                                final DependencyNode node = (DependencyNode)a.getAddress();
058                                if (!node.isRoot()) {
059                                        if (node.hasHead()) {
060                                                featureValue.setIndexCode(node.getHeadEdge().getLabelCode(getSymbolTable()));
061                                                featureValue.setSymbol(getSymbolTable().getSymbolCodeToString(node.getHeadEdge().getLabelCode(getSymbolTable())));
062//                                              featureValue.setKnown(getSymbolTable().getKnown(node.getHeadEdge().getLabelCode(getSymbolTable())));
063                                                featureValue.setNullValue(false);
064                                        } else {
065                                                featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.NO_VALUE));
066                                                featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_VALUE));
067//                                              featureValue.setKnown(true);
068                                                featureValue.setNullValue(true);
069                                        }       
070                                } else {
071                                        featureValue.setIndexCode(getSymbolTable().getNullValueCode(NullValueId.ROOT_NODE));
072                                        featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.ROOT_NODE));
073//                                      featureValue.setKnown(true);
074                                        featureValue.setNullValue(true);
075                                }
076//                      } catch (ClassCastException e) {
077//                              featureValue.setCode(getSymbolTable().getNullValueCode(NullValueId.NO_NODE));
078//                              featureValue.setSymbol(getSymbolTable().getNullValueSymbol(NullValueId.NO_NODE));
079//                              featureValue.setKnown(true);
080//                              featureValue.setNullValue(true);
081//                      }
082                }
083                featureValue.setValue(1);
084        }
085        
086        public AddressFunction getAddressFunction() {
087                return addressFunction;
088        }
089
090        public void setAddressFunction(AddressFunction addressFunction) {
091                this.addressFunction = addressFunction;
092        }
093        
094        public SymbolTableHandler getTableHandler() {
095                return tableHandler;
096        }
097
098        public boolean equals(Object obj) {
099                if (this == obj)
100                        return true;
101                if (obj == null)
102                        return false;
103                if (getClass() != obj.getClass())
104                        return false;
105                return obj.toString().equals(toString());
106        }
107        
108        public int hashCode() {
109                return 217 + (null == toString() ? 0 : toString().hashCode());
110        }
111        
112        public String toString() {
113                final StringBuilder sb = new StringBuilder();
114                sb.append("OutputTable(");
115                sb.append(super.toString());
116                sb.append(", ");
117                sb.append(addressFunction.toString());
118                sb.append(")");
119                return sb.toString();
120        }
121}