001package org.maltparser.parser.algorithm.planar;
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 PlanarAddressFunction extends AddressFunction {
014        public final static Class<?>[] paramTypes = { java.lang.Integer.class };
015        public enum PlanarSubFunction {
016                STACK, INPUT
017        };
018        private final String subFunctionName;
019        private final PlanarSubFunction subFunction;
020        private final AlgoritmInterface parsingAlgorithm;
021        private int index;
022        
023        public PlanarAddressFunction(String _subFunctionName, AlgoritmInterface _parsingAlgorithm) {
024                super();
025                this.subFunctionName = _subFunctionName;
026                this.subFunction = PlanarSubFunction.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((PlanarConfig)parsingAlgorithm.getCurrentParserConfiguration());
047        }
048        
049        public void update(Object[] arguments) throws MaltChainedException {
050                if (arguments.length != 1 || !(arguments[0] instanceof PlanarConfig)) {
051                        throw new ParsingException("Arguments to the planar address function is not correct. ");
052                }
053                update((PlanarConfig)arguments[0]);
054        }
055        
056        private void update(PlanarConfig config) throws MaltChainedException {
057                if (subFunction == PlanarSubFunction.STACK) {
058                        address.setAddress(config.getStackNode(index));
059                } else if (subFunction == PlanarSubFunction.INPUT) {
060                        address.setAddress(config.getInputNode(index));
061                } else {
062                        address.setAddress(null);
063                }
064        }
065        
066        public String getSubFunctionName() {
067                return subFunctionName;
068        }
069        
070        public PlanarSubFunction getSubFunction() {
071                return subFunction;
072        }
073        
074        public AddressValue getAddressValue() {
075                return address;
076        }
077
078        public int getIndex() {
079                return index;
080        }
081
082        public void setIndex(int index) {
083                this.index = index;
084        }
085        
086        public boolean equals(Object obj) {
087                if (this == obj)
088                        return true;
089                if (obj == null)
090                        return false;
091                if (getClass() != obj.getClass())
092                        return false;
093                
094                PlanarAddressFunction other = (PlanarAddressFunction) obj;
095                if (index != other.index)
096                        return false;
097                if (parsingAlgorithm == null) {
098                        if (other.parsingAlgorithm != null)
099                                return false;
100                } else if (!parsingAlgorithm.equals(other.parsingAlgorithm))
101                        return false;
102                if (subFunction == null) {
103                        if (other.subFunction != null)
104                                return false;
105                } else if (!subFunction.equals(other.subFunction))
106                        return false;
107                return true;
108        }
109        
110        public String toString() {
111                StringBuilder sb = new StringBuilder();
112                sb.append(subFunctionName);
113                sb.append('[');
114                sb.append(index);
115                sb.append(']');
116                return sb.toString();
117        }
118}