001    package org.maltparser.core.syntaxgraph.headrules;
002    
003    import java.util.ArrayList;
004    
005    import org.apache.log4j.Logger;
006    import org.maltparser.core.exception.MaltChainedException;
007    import org.maltparser.core.io.dataformat.DataFormatInstance;
008    import org.maltparser.core.syntaxgraph.node.NonTerminalNode;
009    import org.maltparser.core.syntaxgraph.node.PhraseStructureNode;
010    /**
011    *
012    *
013    * @author Johan Hall
014    */
015    public class PrioList extends ArrayList<PrioSet> {
016            public static final long serialVersionUID = 8045568022124816323L;
017            protected HeadRule headRule;
018            protected Direction direction;
019    
020            public PrioList(HeadRule headRule, String listSpec) throws MaltChainedException {
021                    setHeadRule(headRule);
022                    init(listSpec);
023            }
024            
025            public void init(String listSpec) throws MaltChainedException {
026                    String spec = listSpec.trim();
027                    if (spec.length() < 8) {
028                            throw new HeadRuleException("The specification of the priority list is not correct '"+listSpec+"'. ");
029                    }
030                    if (spec.charAt(0) == 'r') {
031                            direction = Direction.RIGHT;    
032                    } else if (spec.charAt(0) == 'l') {
033                            direction = Direction.LEFT;
034                    } else {
035                            throw new HeadRuleException("Could not determine the direction of the priority list '"+listSpec+"'. ");
036                    }
037                    if (spec.charAt(1) == '[' && spec.charAt(spec.length()-1) == ']') {
038                            String[] items = spec.substring(2,spec.length()-1).split(" ");
039                            for (int i=0; i<items.length; i++) {
040                                    add(new PrioSet(this, items[i]));
041                            }
042                    } else {
043                            throw new HeadRuleException("The specification of the priority list is not correct '"+listSpec+"'. ");
044                    }
045            }
046            
047            public PhraseStructureNode getHeadChild(NonTerminalNode nt) throws MaltChainedException {
048                    PhraseStructureNode headChild = null;
049                    for (int i = 0, n = size(); i < n; i++) {
050                            headChild = get(i).getHeadChild(nt, direction);
051                            if (headChild != null) {
052                                    break;
053                            }
054                    }
055                    return headChild; 
056            }
057            
058            public Logger getLogger() {
059                    return headRule.getLogger();
060            }
061            
062            public DataFormatInstance getDataFormatInstance() {
063                    return headRule.getDataFormatInstance();
064            }
065            
066            public HeadRule getHeadRule() {
067                    return headRule;
068            }
069    
070            public void setHeadRule(HeadRule headRule) {
071                    this.headRule = headRule;
072            }
073            
074            public boolean equals(Object obj) {
075                    if (this == obj)
076                            return true;
077                    if (obj == null)
078                            return false;
079                    if (getClass() != obj.getClass())
080                            return false;
081                    return super.equals(obj);
082            }
083            
084            public int hashCode() {
085                    return super.hashCode();
086            }
087            
088            public String toString() {
089                    final StringBuilder sb = new StringBuilder();
090                    if (direction == Direction.LEFT) {
091                            sb.append("l[");
092                    } else if (direction == Direction.RIGHT) {
093                            sb.append("r[");
094                    }
095                    for (PrioSet set : this) {
096                            sb.append(set);
097                            sb.append(' ');
098                    }
099                    if (sb.length() != 0) {
100                            sb.setLength(sb.length()-1);
101                    }
102                    sb.append("]");
103                    return sb.toString();
104            }
105    
106    
107    }