001    package org.maltparser.core.syntaxgraph;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.flow.FlowChartInstance;
005    import org.maltparser.core.flow.item.ChartItem;
006    import org.maltparser.core.flow.spec.ChartItemSpecification;
007    import org.maltparser.core.symbol.SymbolTable;
008    import org.maltparser.core.syntaxgraph.node.DependencyNode;
009    /**
010    *
011    *
012    * @author Johan Hall
013    */
014    public class CopyChartItem extends ChartItem {
015            private String targetName;
016            private String sourceName;
017            private String taskName;
018            
019            private TokenStructure cachedSource = null;
020            private TokenStructure cachedTarget = null;
021            
022            public CopyChartItem() {}
023            
024            public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
025                    super.initialize(flowChartinstance, chartItemSpecification);
026                    for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
027                            if (key.equals("target")) {
028                                    targetName = chartItemSpecification.getChartItemAttributes().get(key);
029                            } else if (key.equals("source")) {
030                                    sourceName = chartItemSpecification.getChartItemAttributes().get(key);
031                            }  else if (key.equals("task")) {
032                                    taskName = chartItemSpecification.getChartItemAttributes().get(key);
033                            }
034                    }
035                    if (targetName == null) {
036                            targetName = getChartElement("copy").getAttributes().get("target").getDefaultValue();
037                    } else if (sourceName == null) {
038                            sourceName = getChartElement("copy").getAttributes().get("source").getDefaultValue();
039                    } else if (taskName == null) {
040                            taskName = getChartElement("copy").getAttributes().get("task").getDefaultValue();
041                    }
042            }
043            
044            public boolean preprocess() throws MaltChainedException {
045                    return true;
046            }
047            
048            public boolean process(boolean continueNextSentence) throws MaltChainedException {
049                    if (taskName.equals("terminals")) {
050                            if (cachedSource == null) {
051                                    cachedSource = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, sourceName);
052                            }
053                            if (cachedTarget == null) {
054                                    cachedTarget = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, targetName);
055                            }
056                            copyTerminalStructure(cachedSource, cachedTarget);
057                    }
058                    return continueNextSentence;
059            }
060            
061            public boolean postprocess() throws MaltChainedException {
062                    return true;
063            }
064    
065            
066            public void terminate() throws MaltChainedException {
067                    cachedSource = null;
068                    cachedTarget = null;
069            }
070            
071            public void copyTerminalStructure(TokenStructure sourceGraph, TokenStructure targetGraph) throws MaltChainedException {
072                    targetGraph.clear();
073                    for (int index : sourceGraph.getTokenIndices()) {
074                            DependencyNode gnode = sourceGraph.getTokenNode(index);
075                            DependencyNode pnode = targetGraph.addTokenNode(gnode.getIndex());
076                            for (SymbolTable table : gnode.getLabelTypes()) {
077                                    pnode.addLabel(table, gnode.getLabelSymbol(table));
078                            }
079                    }
080            }
081            
082            public boolean equals(Object obj) {
083                    if (this == obj)
084                            return true;
085                    if (obj == null)
086                            return false;
087                    if (getClass() != obj.getClass())
088                            return false;
089                    return obj.toString().equals(this.toString());
090            }
091            
092            public int hashCode() {
093                    return 217 + (null == toString() ? 0 : toString().hashCode());
094            }
095            
096            public String toString() {
097                    final StringBuilder sb = new StringBuilder();
098                    sb.append("    copy ");
099                    sb.append("task:");
100                    sb.append(taskName);
101                    sb.append(' ');
102                    sb.append("source:");
103                    sb.append(sourceName);
104                    sb.append(' ');
105                    sb.append("target:");
106                    sb.append(targetName);
107                    return sb.toString();
108            }
109    }