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