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