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.symbol.SymbolTable;
009    import org.maltparser.core.syntaxgraph.node.NonTerminalNode;
010    import org.maltparser.core.syntaxgraph.node.PhraseStructureNode;
011    /**
012    *
013    *
014    * @author Johan Hall
015    */
016    public class HeadRule extends ArrayList<PrioList> {
017            public static final long serialVersionUID = 8045568022124826323L;
018            protected HeadRules headRules;
019            protected SymbolTable table;
020            protected int symbolCode;
021            protected Direction defaultDirection;
022            
023            public HeadRule(HeadRules headRules, String ruleSpec) throws MaltChainedException {
024                    setHeadRules(headRules);
025                    init(ruleSpec);
026            }
027            
028            public void init(String ruleSpec) throws MaltChainedException {
029                    String spec = ruleSpec.trim();
030                    String[] items = spec.split("\t");
031                    if (items.length != 3) {
032                            throw new HeadRuleException("The specification of the head rule is not correct '"+ruleSpec+"'. ");
033                    }
034                    
035                    int index = items[0].indexOf(':');
036                    if (index != -1) {
037                            SymbolTable t = getDataFormatInstance().getSymbolTables().getSymbolTable(items[0].substring(0, index));
038                            if (t == null) {
039                                    throw new HeadRuleException("The specification of the head rule is not correct '"+ruleSpec+"'. ");
040                            }
041                            setTable(t);
042                            setSymbolCode(table.addSymbol(items[0].substring(index+1)));
043                    } else {
044                            throw new HeadRuleException("The specification of the head rule is not correct '"+ruleSpec+"'. ");
045                    }
046                    if (items[1].charAt(0) == 'r') {
047                            defaultDirection = Direction.RIGHT;     
048                    } else if (items[1].charAt(0) == 'l') {
049                            defaultDirection = Direction.LEFT;
050                    } else {
051                            throw new HeadRuleException("Could not determine the default direction of the head rule '"+ruleSpec+"'. ");
052                    }
053                    if (items[2].length() > 1) {
054                            if (items[2].indexOf(';') == -1) {
055                                    add(new PrioList(this, items[2]));
056                            } else {
057                                    String[] lists = items[2].split(";");
058                                    for (int i = 0; i < lists.length; i++) {
059                                            add(new PrioList(this, lists[i]));
060                                    }
061                            }
062                    }
063            }
064    
065            public PhraseStructureNode getHeadChild(NonTerminalNode nt) throws MaltChainedException {
066                    PhraseStructureNode headChild = null;
067                    for (int i = 0; i < size(); i++) {
068                            headChild = get(i).getHeadChild(nt);
069                            if (headChild != null) {
070                                    break;
071                            }
072                    }
073                    return headChild;
074            }
075            
076            public SymbolTable getTable() {
077                    return table;
078            }
079            
080            public void setTable(SymbolTable table) {
081                    this.table = table;
082            }
083            
084            public int getSymbolCode() {
085                    return symbolCode;
086            }
087            
088            public void setSymbolCode(int symbolCode) {
089                    this.symbolCode = symbolCode;
090            }
091            
092            public String getSymbolString() throws MaltChainedException {
093                    return table.getSymbolCodeToString(symbolCode);
094            }
095            
096            public Direction getDefaultDirection() {
097                    return defaultDirection;
098            }
099            
100            public void setDefaultDirection(Direction direction) {
101                    this.defaultDirection = direction;
102            }
103            
104            public Logger getLogger() {
105                    return headRules.getLogger();
106            }
107            
108            public void setHeadRules(HeadRules headRules) {
109                    this.headRules = headRules;
110            }
111            
112            public DataFormatInstance getDataFormatInstance() {
113                    return headRules.getDataFormatInstance();
114            }
115            
116            public String toString() {
117                    final StringBuilder sb = new StringBuilder();
118                    sb.append(table.getName());
119                    sb.append(':');
120                    try {
121                            sb.append(getSymbolString());
122                    } catch (MaltChainedException e) {
123                            if (getLogger().isDebugEnabled()) {
124                                    getLogger().debug("",e);
125                            } else {
126                                    getLogger().error(e.getMessageChain());
127                            }
128                    }
129                    sb.append('\t');
130                    if (defaultDirection == Direction.LEFT) {
131                            sb.append('l');
132                    } else if (defaultDirection == Direction.RIGHT) {
133                            sb.append('r');
134                    }
135                    sb.append('\t');
136                    if (size() == 0) {
137                            sb.append('*');
138                    } else {
139                            for (int i = 0; i < size(); i++) {
140                                    sb.append(get(i));
141                                    if (i < size()-1) {
142                                            sb.append(';');
143                                    }
144                            }
145                    }
146                    return sb.toString();
147            }
148    }