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