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