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