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