001    package org.maltparser.core.syntaxgraph.feature;
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.core.syntaxgraph.SyntaxGraphException;
007    import org.maltparser.core.syntaxgraph.node.DependencyNode;
008    import org.maltparser.core.syntaxgraph.node.TokenNode;
009    /**
010    *
011    *
012    * @author Johan Hall
013    */
014    public class DGraphAddressFunction extends AddressFunction {
015            public enum DGraphSubFunction {
016                    HEAD, LDEP, RDEP, RDEP2, LSIB, RSIB, PRED, SUCC
017            };
018            private AddressFunction addressFunction;
019            private String subFunctionName;
020            private DGraphSubFunction subFunction;
021            
022            public DGraphAddressFunction(String subFunctionName) {
023                    super();
024                    setSubFunctionName(subFunctionName);
025            }
026            
027            public void initialize(Object[] arguments) throws MaltChainedException {
028                    if (arguments.length != 1) {
029                            throw new SyntaxGraphException("Could not initialize NodeAddressFunction: number of arguments are not correct. ");
030                    }
031                    if (!(arguments[0] instanceof AddressFunction)) {
032                            throw new SyntaxGraphException("Could not initialize NodeAddressFunction: the second argument is not an addres function. ");
033                    }
034                    setAddressFunction((AddressFunction)arguments[0]);
035            }
036            
037            public Class<?>[] getParameterTypes() {
038                    Class<?>[] paramTypes = { org.maltparser.core.feature.function.AddressFunction.class };
039                    return paramTypes; 
040            }
041            
042            public void update() throws MaltChainedException {
043                    final AddressValue a = addressFunction.getAddressValue();
044                    if (a.getAddress() == null) {
045                            address.setAddress(null);
046                    } else {
047    //                      try { 
048    //                              a.getAddressClass().asSubclass(org.maltparser.core.syntaxgraph.node.DependencyNode.class);
049                    
050                                    final DependencyNode node = (DependencyNode)a.getAddress();
051                                    if (subFunction == DGraphSubFunction.HEAD && !node.isRoot()) {
052                                            address.setAddress(node.getHead());
053                                    } else if (subFunction == DGraphSubFunction.LDEP) {
054                                            address.setAddress(node.getLeftmostDependent());
055                                    } else if (subFunction == DGraphSubFunction.RDEP) {
056                                            address.setAddress(node.getRightmostDependent());
057                                    } else if (subFunction == DGraphSubFunction.RDEP2) {
058                                            // To emulate the behavior of MaltParser 0.4 (bug)
059                                            if (!node.isRoot()) {
060                                                    address.setAddress(node.getRightmostDependent());
061                                            } else {
062                                                    address.setAddress(null);
063                                            }
064                                    } else if (subFunction == DGraphSubFunction.LSIB) {
065                                            address.setAddress(node.getSameSideLeftSibling());
066                                    } else if (subFunction == DGraphSubFunction.RSIB) {
067                                            address.setAddress(node.getSameSideRightSibling());
068                                    } else if (node instanceof TokenNode && subFunction == DGraphSubFunction.PRED) {
069                                            address.setAddress(((TokenNode)node).getPredecessor());
070                                    } else if (node instanceof TokenNode && subFunction == DGraphSubFunction.SUCC) {
071                                            address.setAddress(((TokenNode)node).getSuccessor());
072                                    } else {
073                                            address.setAddress(null);
074                                    }
075    //                      } catch (ClassCastException e) {
076    //                              address.setAddress(null);
077    //                      }
078                    }
079            }
080            
081            public AddressFunction getAddressFunction() {
082                    return addressFunction;
083            }
084    
085            public void setAddressFunction(AddressFunction addressFunction) {
086                    this.addressFunction = addressFunction;
087            }
088    
089            public String getSubFunctionName() {
090                    return subFunctionName;
091            }
092    
093            public void setSubFunctionName(String subFunctionName) {
094                    this.subFunctionName = subFunctionName;
095                    subFunction = DGraphSubFunction.valueOf(subFunctionName.toUpperCase());
096            }
097            
098            public DGraphSubFunction getSubFunction() {
099                    return subFunction;
100            }
101    
102            public boolean equals(Object obj) {
103                    if (this == obj)
104                            return true;
105                    if (obj == null)
106                            return false;
107                    if (getClass() != obj.getClass())
108                            return false;
109                    if (!addressFunction.equals(((DGraphAddressFunction)obj).getAddressFunction())) {
110                            return false;
111                    } else if (!subFunction.equals(((DGraphAddressFunction)obj).getSubFunction())) {
112                            return false;
113                    } 
114                    return true;
115            }
116    
117            public String toString() {
118                    return subFunctionName + "(" + addressFunction.toString() + ")";
119            }
120    }