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 protected String subFunctionName; 018 protected NivreSubFunction subFunction; 019 protected Algorithm parsingAlgorithm; 020 protected 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 } 054 055 private void update(NivreConfig config) throws MaltChainedException { 056 if (subFunction == NivreSubFunction.STACK) { 057 address.setAddress(config.getStackNode(index)); 058 } else if (subFunction == NivreSubFunction.INPUT) { 059 address.setAddress(config.getInputNode(index)); 060 } else { 061 address.setAddress(null); 062 } 063 } 064 065 public String getSubFunctionName() { 066 return subFunctionName; 067 } 068 069 public void setSubFunctionName(String subFunctionName) { 070 this.subFunctionName = subFunctionName; 071 subFunction = NivreSubFunction.valueOf(subFunctionName.toUpperCase()); 072 } 073 074 public NivreSubFunction getSubFunction() { 075 return subFunction; 076 } 077 078 public AddressValue getAddressValue() { 079 return address; 080 } 081 082 public Algorithm getParsingAlgorithm() { 083 return parsingAlgorithm; 084 } 085 086 public void setAlgorithm(Algorithm parsingAlgorithm) { 087 this.parsingAlgorithm = parsingAlgorithm; 088 } 089 090 public int getIndex() { 091 return index; 092 } 093 094 public void setIndex(int index) { 095 this.index = index; 096 } 097 098 public boolean equals(Object obj) { 099 if (this == obj) 100 return true; 101 if (obj == null) 102 return false; 103 if (getClass() != obj.getClass()) 104 return false; 105 106 NivreAddressFunction other = (NivreAddressFunction) obj; 107 if (index != other.index) 108 return false; 109 if (parsingAlgorithm == null) { 110 if (other.parsingAlgorithm != null) 111 return false; 112 } else if (!parsingAlgorithm.equals(other.parsingAlgorithm)) 113 return false; 114 if (subFunction == null) { 115 if (other.subFunction != null) 116 return false; 117 } else if (!subFunction.equals(other.subFunction)) 118 return false; 119 return true; 120 } 121 122 public String toString() { 123 return subFunctionName + "[" + index + "]"; 124 } 125 }