001package org.maltparser.parser.algorithm.stack; 002 003import org.maltparser.core.exception.MaltChainedException; 004import org.maltparser.core.feature.function.AddressFunction; 005import org.maltparser.core.feature.value.AddressValue; 006import org.maltparser.parser.AlgoritmInterface; 007import org.maltparser.parser.ParsingException; 008/** 009 * @author Johan Hall 010 * 011 */ 012public final class StackAddressFunction extends AddressFunction { 013 public final static Class<?>[] paramTypes = { java.lang.Integer.class }; 014 public enum StackSubFunction { 015 STACK, INPUT, LOOKAHEAD 016 }; 017 private final String subFunctionName; 018 private final StackSubFunction subFunction; 019 private final AlgoritmInterface parsingAlgorithm; 020 private int index; 021 022 public StackAddressFunction(String _subFunctionName, AlgoritmInterface _parsingAlgorithm) { 023 super(); 024 this.subFunctionName = _subFunctionName; 025 this.subFunction = StackSubFunction.valueOf(subFunctionName.toUpperCase()); 026 this.parsingAlgorithm = _parsingAlgorithm; 027 } 028 029 public void initialize(Object[] arguments) throws MaltChainedException { 030 if (arguments.length != 1) { 031 throw new ParsingException("Could not initialize "+this.getClass().getName()+": number of arguments are not correct. "); 032 } 033 if (!(arguments[0] instanceof Integer)) { 034 throw new ParsingException("Could not initialize "+this.getClass().getName()+": the first argument is not an integer. "); 035 } 036 037 setIndex(((Integer)arguments[0]).intValue()); 038 } 039 040 public Class<?>[] getParameterTypes() { 041 return paramTypes; 042 } 043 044 public void update() throws MaltChainedException { 045 update((StackConfig)parsingAlgorithm.getCurrentParserConfiguration()); 046 } 047 048 public void update(Object[] arguments) throws MaltChainedException { 049 if (subFunction == StackSubFunction.STACK) { 050 address.setAddress(((StackConfig)arguments[0]).getStackNode(index)); 051 } else if (subFunction == StackSubFunction.LOOKAHEAD) { 052 address.setAddress(((StackConfig)arguments[0]).getLookaheadNode(index)); 053 } else if (subFunction == StackSubFunction.INPUT) { 054 address.setAddress(((StackConfig)arguments[0]).getInputNode(index)); 055 } else { 056 address.setAddress(null); 057 } 058 } 059 060 private void update(StackConfig config) throws MaltChainedException { 061 if (subFunction == StackSubFunction.STACK) { 062 address.setAddress(config.getStackNode(index)); 063 } else if (subFunction == StackSubFunction.LOOKAHEAD) { 064 address.setAddress(config.getLookaheadNode(index)); 065 } else if (subFunction == StackSubFunction.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 StackSubFunction getSubFunction() { 077 return subFunction; 078 } 079 080 public AddressValue getAddressValue() { 081 return address; 082 } 083 084 public int getIndex() { 085 return index; 086 } 087 088 public void setIndex(int index) { 089 this.index = index; 090 } 091 092 public boolean equals(Object obj) { 093 if (this == obj) 094 return true; 095 if (obj == null) 096 return false; 097 if (getClass() != obj.getClass()) 098 return false; 099 100 StackAddressFunction other = (StackAddressFunction) obj; 101 if (index != other.index) 102 return false; 103 if (parsingAlgorithm == null) { 104 if (other.parsingAlgorithm != null) 105 return false; 106 } else if (!parsingAlgorithm.equals(other.parsingAlgorithm)) 107 return false; 108 if (subFunction == null) { 109 if (other.subFunction != null) 110 return false; 111 } else if (!subFunction.equals(other.subFunction)) 112 return false; 113 return true; 114 } 115 116 public String toString() { 117 final StringBuilder sb = new StringBuilder(); 118 sb.append(subFunctionName); 119 sb.append('['); 120 sb.append(index); 121 sb.append(']'); 122 return sb.toString(); 123 } 124}