001    package org.maltparser.parser.transition;
002    
003    import java.util.HashMap;
004    import java.util.SortedMap;
005    import java.util.TreeMap;
006    
007    import org.maltparser.core.exception.MaltChainedException;
008    import org.maltparser.core.symbol.Table;
009    import org.maltparser.parser.history.container.DecisionPropertyTable;
010    /**
011    *
012    * @author Johan Hall
013    * @since 1.1
014    **/
015    public class TransitionTable implements Table, DecisionPropertyTable {
016            private String name;
017            private final SortedMap<Integer,Transition> code2transitionMap;
018            private final HashMap<String,Transition> symbol2transitionMap;
019            private final HashMap<Transition,TransitionTable> childrenTables;
020            
021            public TransitionTable(String tableName) {
022                    setName(name);
023                    code2transitionMap = new TreeMap<Integer,Transition>();
024                    symbol2transitionMap = new HashMap<String,Transition>();
025                    childrenTables = new HashMap<Transition,TransitionTable>();
026            }
027            
028            public void addTransition(int code, String symbol, boolean labeled, TransitionTable childrenTable) {
029                    final Transition transition = new Transition(code, symbol, labeled); 
030                    code2transitionMap.put(code,transition);
031                    symbol2transitionMap.put(symbol, transition);
032                    if (childrenTable != null) {
033                            childrenTables.put(transition, childrenTable);
034                    }
035            }
036            
037            public boolean continueWithNextDecision(int code) throws MaltChainedException {
038                    if (code2transitionMap.containsKey(code)) {
039                            return code2transitionMap.get(code).isLabeled();
040                    }
041                    return true;
042            }
043            
044            public boolean continueWithNextDecision(String symbol) throws MaltChainedException {
045                    if (symbol2transitionMap.containsKey(symbol)) {
046                            return symbol2transitionMap.get(symbol).isLabeled();
047                    }
048                    return true;
049            }
050            
051            public Table getTableForNextDecision(int code) throws MaltChainedException {
052                    if (code2transitionMap.containsKey(code)) {
053                            return childrenTables.get(code2transitionMap.get(code));
054                    }
055                    return null;
056            }
057            
058            public Table getTableForNextDecision(String symbol) throws MaltChainedException {
059                    if (symbol2transitionMap.containsKey(symbol)) {
060                            return childrenTables.get(symbol2transitionMap.get(symbol));
061                    }
062                    return null;
063            }
064            
065            public Transition getTransition(String symbol) {
066                    return symbol2transitionMap.get(symbol);
067            }
068            
069            public Transition getTransition(int code) {
070                    return code2transitionMap.get(code);
071            }
072            
073            public int addSymbol(String symbol) throws MaltChainedException {
074                    return -1;
075            }
076    
077            public String getName() {
078                    return name;
079            }
080    
081            
082            public String getSymbolCodeToString(int code) throws MaltChainedException {
083                    if (code < 0) {
084                            return null;
085                    }
086                    return code2transitionMap.get(code).getSymbol();
087            }
088    
089            public int getSymbolStringToCode(String symbol) throws MaltChainedException {
090                    if (symbol == null) {
091                            return -1;
092                    }
093                    return symbol2transitionMap.get(symbol).getCode();
094            }
095    
096            protected void setName(String name) {
097                    this.name = name;
098            }
099            
100            public int size() {
101                    return code2transitionMap.size();
102            }
103    
104    }