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