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.ParsingAlgorithm;
007    import org.maltparser.parser.algorithm.ParsingException;
008    
009    /**
010    *
011    * @author Johan Hall
012    * @since 1.1
013    **/
014    public class NivreAddressFunction extends AddressFunction {
015            public enum NivreSubFunction {
016                    STACK, INPUT
017            };
018            protected String subFunctionName;
019            protected NivreSubFunction subFunction;
020            protected Nivre parsingAlgorithm;
021            protected int index;
022            
023            public NivreAddressFunction(String subFunctionName, ParsingAlgorithm parsingAlgorithm) {
024                    super();
025                    setSubFunctionName(subFunctionName);
026                    setParsingAlgorithm((Nivre)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                    Class<?>[] paramTypes = { java.lang.Integer.class };
042                    return paramTypes; 
043            }
044            
045            public void update() throws MaltChainedException {
046                    if (subFunction == NivreSubFunction.STACK) {
047                            address.setAddress(parsingAlgorithm.getStackNode(index));
048                    } else if (subFunction == NivreSubFunction.INPUT) {
049                            address.setAddress(parsingAlgorithm.getInputNode(index));
050                    } else {
051                            address.setAddress(null);
052                    }
053            }
054            
055            public String getSubFunctionName() {
056                    return subFunctionName;
057            }
058    
059            public void setSubFunctionName(String subFunctionName) {
060                    this.subFunctionName = subFunctionName;
061                    subFunction = NivreSubFunction.valueOf(subFunctionName.toUpperCase());
062            }
063            
064            public NivreSubFunction getSubFunction() {
065                    return subFunction;
066            }
067            
068            public AddressValue getAddressValue() {
069                    return address;
070            }
071            
072            public Nivre getParsingAlgorithm() {
073                    return parsingAlgorithm;
074            }
075    
076            public void setParsingAlgorithm(Nivre parsingAlgorithm) {
077                    this.parsingAlgorithm = parsingAlgorithm;
078            }
079    
080            public int getIndex() {
081                    return index;
082            }
083    
084            public void setIndex(int index) {
085                    this.index = index;
086            }
087            
088            public boolean equals(Object obj) {
089                    if (this == obj)
090                            return true;
091                    if (obj == null)
092                            return false;
093                    if (getClass() != obj.getClass())
094                            return false;
095                    
096                    NivreAddressFunction other = (NivreAddressFunction) obj;
097                    if (index != other.index)
098                            return false;
099                    if (parsingAlgorithm == null) {
100                            if (other.parsingAlgorithm != null)
101                                    return false;
102                    } else if (!parsingAlgorithm.equals(other.parsingAlgorithm))
103                            return false;
104                    if (subFunction == null) {
105                            if (other.subFunction != null)
106                                    return false;
107                    } else if (!subFunction.equals(other.subFunction))
108                            return false;
109                    return true;
110            }
111            
112            public String toString() {
113                    return subFunctionName + "[" + index + "]";
114            }
115    }