    001    package org.maltparser.parser.algorithm.twoplanar;
    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 Carlos Gomez Rodriguez
    012    **/
    013    public class TwoPlanarAddressFunction extends AddressFunction {
    014            public enum TwoPlanarSubFunction {
    015                    ACTIVESTACK, INACTIVESTACK , INPUT
    016            };
    017            protected String subFunctionName;
    018            protected TwoPlanarSubFunction subFunction;
    019            protected Algorithm parsingAlgorithm;
    020            protected int index;
    021
    022            public TwoPlanarAddressFunction(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((TwoPlanarConfig)parsingAlgorithm.getCurrentParserConfiguration());
    046            }
    047
    048            public void update(Object[] arguments) throws MaltChainedException {
    049                    if (arguments.length != 1 || !(arguments[0] instanceof TwoPlanarConfig)) {
    050                            throw new ParsingException("Arguments to the two-planar address function are not correct. ");
    051                    }
    052                    update((TwoPlanarConfig)arguments[0]);
    053            }
    054
    055            private void update(TwoPlanarConfig config) throws MaltChainedException {
    056                    if (subFunction == TwoPlanarSubFunction.ACTIVESTACK) {
    057                            address.setAddress(config.getActiveStackNode(index));
    058                    } else if ( subFunction == TwoPlanarSubFunction.INACTIVESTACK ) {
    059                            address.setAddress(config.getInactiveStackNode(index));
    060                    } else if (subFunction == TwoPlanarSubFunction.INPUT) {
    061                            address.setAddress(config.getInputNode(index));
    062                    } else {
    063                            address.setAddress(null);
    064                    }
    065            }
    066
    067            public String getSubFunctionName() {
    068                    return subFunctionName;
    069            }
    070
    071            public void setSubFunctionName(String subFunctionName) {
    072                    this.subFunctionName = subFunctionName;
    073                    subFunction = TwoPlanarSubFunction.valueOf(subFunctionName.toUpperCase());
    074            }
    075
    076            public TwoPlanarSubFunction getSubFunction() {
    077                    return subFunction;
    078            }
    079
    080            public AddressValue getAddressValue() {
    081                    return address;
    082            }
    083
    084            public Algorithm getParsingAlgorithm() {
    085                    return parsingAlgorithm;
    086            }
    087
    088            public void setAlgorithm(Algorithm parsingAlgorithm) {
    089                    this.parsingAlgorithm = parsingAlgorithm;
    090            }
    091
    092            public int getIndex() {
    093                    return index;
    094            }
    095
    096            public void setIndex(int index) {
    097                    this.index = index;
    098            }
    099
    100            public boolean equals(Object obj) {
    101                    if (this == obj)
    102                            return true;
    103                    if (obj == null)
    104                            return false;
    105                    if (getClass() != obj.getClass())
    106                            return false;
    107
    108                    TwoPlanarAddressFunction other = (TwoPlanarAddressFunction) obj;
    109                    if (index != other.index)
    110                            return false;
    111                    if (parsingAlgorithm == null) {
    112                            if (other.parsingAlgorithm != null)
    113                                    return false;
    114                    } else if (!parsingAlgorithm.equals(other.parsingAlgorithm))
    115                            return false;
    116                    if (subFunction == null) {
    117                            if (other.subFunction != null)
    118                                    return false;
    119                    } else if (!subFunction.equals(other.subFunction))
    120                            return false;
    121                    return true;
    122            }
    123
    124            public String toString() {
    125                    return subFunctionName + "[" + index + "]";
    126            }
    127    }