001 package org.maltparser.parser.algorithm.nivre;
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.parser.Algorithm;
007 import org.maltparser.parser.ParsingException;
008
009 /**
010 *
011 * @author Johan Hall
012 **/
013 public class NivreAddressFunction extends AddressFunction {
014 public enum NivreSubFunction {
015 STACK, INPUT
016 };
017 private String subFunctionName;
018 private NivreSubFunction subFunction;
019 private Algorithm parsingAlgorithm;
020 private int index;
021
022 public NivreAddressFunction(String subFunctionName, Algorithm parsingAlgorithm) {
023 super();
024 setSubFunctionName(subFunctionName);
025 setAlgorithm(parsingAlgorithm);
026 }
027
028 public void initialize(Object[] arguments) throws MaltChainedException {
029 if (arguments.length != 1) {
030 throw new ParsingException("Could not initialize "+this.getClass().getName()+": number of arguments are not correct. ");
031 }
032 if (!(arguments[0] instanceof Integer)) {
033 throw new ParsingException("Could not initialize "+this.getClass().getName()+": the first argument is not an integer. ");
034 }
035
036 setIndex(((Integer)arguments[0]).intValue());
037 }
038
039 public Class<?>[] getParameterTypes() {
040 Class<?>[] paramTypes = { java.lang.Integer.class };
041 return paramTypes;
042 }
043
044 public void update() throws MaltChainedException {
045 update((NivreConfig)parsingAlgorithm.getCurrentParserConfiguration());
046 }
047
048 public void update(Object[] arguments) throws MaltChainedException {
049 // if (arguments.length != 1 || !(arguments[0] instanceof NivreConfig)) {
050 // throw new ParsingException("Arguments to the Nivre address function is not correct. ");
051 // }
052 // update((NivreConfig)arguments[0]);
053 if (subFunction == NivreSubFunction.STACK) {
054 address.setAddress(((NivreConfig)arguments[0]).getStackNode(index));
055 } else if (subFunction == NivreSubFunction.INPUT) {
056 address.setAddress(((NivreConfig)arguments[0]).getInputNode(index));
057 } else {
058 address.setAddress(null);
059 }
060 }
061
062 private void update(NivreConfig config) throws MaltChainedException {
063 if (subFunction == NivreSubFunction.STACK) {
064 address.setAddress(config.getStackNode(index));
065 } else if (subFunction == NivreSubFunction.INPUT) {
066 address.setAddress(config.getInputNode(index));
067 } else {
068 address.setAddress(null);
069 }
070 }
071
072 public String getSubFunctionName() {
073 return subFunctionName;
074 }
075
076 public void setSubFunctionName(String subFunctionName) {
077 this.subFunctionName = subFunctionName;
078 subFunction = NivreSubFunction.valueOf(subFunctionName.toUpperCase());
079 }
080
081 public NivreSubFunction getSubFunction() {
082 return subFunction;
083 }
084
085 public AddressValue getAddressValue() {
086 return address;
087 }
088
089 public Algorithm getParsingAlgorithm() {
090 return parsingAlgorithm;
091 }
092
093 public void setAlgorithm(Algorithm parsingAlgorithm) {
094 this.parsingAlgorithm = parsingAlgorithm;
095 }
096
097 public int getIndex() {
098 return index;
099 }
100
101 public void setIndex(int index) {
102 this.index = index;
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
113 NivreAddressFunction other = (NivreAddressFunction) obj;
114 if (index != other.index)
115 return false;
116 if (parsingAlgorithm == null) {
117 if (other.parsingAlgorithm != null)
118 return false;
119 } else if (!parsingAlgorithm.equals(other.parsingAlgorithm))
120 return false;
121 if (subFunction == null) {
122 if (other.subFunction != null)
123 return false;
124 } else if (!subFunction.equals(other.subFunction))
125 return false;
126 return true;
127 }
128
129 public String toString() {
130 final StringBuilder sb = new StringBuilder();
131 sb.append(subFunctionName);
132 sb.append('[');
133 sb.append(index);
134 sb.append(']');
135 return sb.toString();
136 }
137 }