001package org.maltparser.parser.algorithm.twoplanar;
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 Carlos Gomez Rodriguez
012**/
013public final class TwoPlanarAddressFunction extends AddressFunction {
014        public final static Class<?>[] paramTypes = { java.lang.Integer.class };
015        public enum TwoPlanarSubFunction {
016                ACTIVESTACK, INACTIVESTACK , INPUT
017        };
018        private final String subFunctionName;
019        private final TwoPlanarSubFunction subFunction;
020        private final AlgoritmInterface parsingAlgorithm;
021        private int index;
022        
023        public TwoPlanarAddressFunction(String _subFunctionName, AlgoritmInterface _parsingAlgorithm) {
024                super();
025                this.subFunctionName = _subFunctionName;
026                this.subFunction = TwoPlanarSubFunction.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((TwoPlanarConfig)parsingAlgorithm.getCurrentParserConfiguration());
047        }
048        
049        public void update(Object[] arguments) throws MaltChainedException {
050                if (arguments.length != 1 || !(arguments[0] instanceof TwoPlanarConfig)) {
051                        throw new ParsingException("Arguments to the two-planar address function are not correct. ");
052                }
053                update((TwoPlanarConfig)arguments[0]);
054        }
055        
056        private void update(TwoPlanarConfig config) throws MaltChainedException {
057                if (subFunction == TwoPlanarSubFunction.ACTIVESTACK) {
058                        address.setAddress(config.getActiveStackNode(index));
059                } else if ( subFunction == TwoPlanarSubFunction.INACTIVESTACK ) {
060                        address.setAddress(config.getInactiveStackNode(index));
061                } else if (subFunction == TwoPlanarSubFunction.INPUT) {
062                        address.setAddress(config.getInputNode(index));
063                } else {
064                        address.setAddress(null);
065                }
066        }
067        
068        public String getSubFunctionName() {
069                return subFunctionName;
070        }
071        
072        public TwoPlanarSubFunction getSubFunction() {
073                return subFunction;
074        }
075        
076        public AddressValue getAddressValue() {
077                return address;
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                TwoPlanarAddressFunction other = (TwoPlanarAddressFunction) 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}