001    package org.maltparser.parser;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.syntaxgraph.LabelSet;
005    import org.maltparser.parser.guide.OracleGuide;
006    import org.maltparser.parser.history.GuideUserHistory;
007    import org.maltparser.parser.history.action.GuideUserAction;
008    import org.maltparser.parser.history.container.ActionContainer;
009    /**
010     * @author Johan Hall
011     *
012     */
013    public abstract class Oracle implements OracleGuide {
014            private DependencyParserConfig manager;
015            private GuideUserHistory history;
016            private String name;
017            protected ActionContainer[] actionContainers;
018            protected ActionContainer transActionContainer;
019            protected ActionContainer[] arcLabelActionContainers;
020            
021            public Oracle(DependencyParserConfig manager, GuideUserHistory history) throws MaltChainedException {
022                    this.manager = manager;
023                    this.history = history;
024                    initActionContainers();
025            }
026            
027            public void setManager(DependencyParserConfig manager) {
028                    this.manager = manager;
029            }
030    
031            public GuideUserHistory getHistory() {
032                    return history;
033            }
034    
035            public void setHistory(GuideUserHistory history) {
036                    this.history = history;
037            }
038    
039            public DependencyParserConfig getConfiguration() {
040                    return manager;
041            }
042            
043            public String getGuideName() {
044                    return name;
045            }
046            
047            public void setGuideName(String guideName) {
048                    this.name = guideName;
049            }
050            
051            protected GuideUserAction updateActionContainers(int transition, LabelSet arcLabels) throws MaltChainedException {      
052                    transActionContainer.setAction(transition);
053    
054                    if (arcLabels == null) {
055                            for (int i = 0; i < arcLabelActionContainers.length; i++) {
056                                    arcLabelActionContainers[i].setAction(-1);      
057                            }
058                    } else {
059                            for (int i = 0; i < arcLabelActionContainers.length; i++) {
060                                    arcLabelActionContainers[i].setAction(arcLabels.get(arcLabelActionContainers[i].getTable()).shortValue());
061                            }               
062                    }
063                    GuideUserAction oracleAction = history.getEmptyGuideUserAction();
064                    oracleAction.addAction(actionContainers);
065                    return oracleAction;
066            }
067            
068            public void initActionContainers() throws MaltChainedException {
069                    this.actionContainers = history.getActionContainerArray();
070                    if (actionContainers.length < 1) {
071                            throw new ParsingException("Problem when initialize the history (sequence of actions). There are no action containers. ");
072                    }
073                    int nLabels = 0;
074                    for (int i = 0; i < actionContainers.length; i++) {
075                            if (actionContainers[i].getTableContainerName().startsWith("A.")) {
076                                    nLabels++;
077                            }
078                    }
079                    int j = 0;
080                    for (int i = 0; i < actionContainers.length; i++) {
081                            if (actionContainers[i].getTableContainerName().equals("T.TRANS")) {
082                                    transActionContainer = actionContainers[i];
083                            } else if (actionContainers[i].getTableContainerName().startsWith("A.")) {
084                                    if (arcLabelActionContainers == null) {
085                                            arcLabelActionContainers = new ActionContainer[nLabels];
086                                    }
087                                    arcLabelActionContainers[j++] = actionContainers[i];
088                            }
089                    }
090            }
091    }