| 1 | |
|
| 2 | |
|
| 3 | |
package org.galagosearch.core.parse; |
| 4 | |
|
| 5 | |
import org.galagosearch.tupleflow.ExNihiloSource; |
| 6 | |
import org.galagosearch.tupleflow.IncompatibleProcessorException; |
| 7 | |
import org.galagosearch.tupleflow.Linkage; |
| 8 | |
import org.galagosearch.tupleflow.OutputClass; |
| 9 | |
import org.galagosearch.tupleflow.TupleFlowParameters; |
| 10 | |
import org.galagosearch.tupleflow.Processor; |
| 11 | |
import org.galagosearch.tupleflow.Step; |
| 12 | |
import org.galagosearch.tupleflow.TypeReader; |
| 13 | |
import org.galagosearch.tupleflow.execution.ErrorHandler; |
| 14 | |
import org.galagosearch.tupleflow.execution.Verification; |
| 15 | |
import org.galagosearch.core.types.ExtractedLink; |
| 16 | |
import org.galagosearch.core.types.IdentifiedLink; |
| 17 | |
import java.io.IOException; |
| 18 | |
import org.galagosearch.core.types.DocumentData; |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
@OutputClass(className = "org.galagosearch.core.parse.DocumentLinkData") |
| 25 | |
public class LinkCombiner implements ExNihiloSource<IdentifiedLink>, IdentifiedLink.Source { |
| 26 | |
TypeReader<ExtractedLink> extractedLinks; |
| 27 | |
TypeReader<DocumentData> documentDatas; |
| 28 | |
DocumentLinkData linkData; |
| 29 | |
public Processor<DocumentLinkData> processor; |
| 30 | |
|
| 31 | |
@SuppressWarnings("unchecked") |
| 32 | 0 | public LinkCombiner(TupleFlowParameters parameters) throws IOException { |
| 33 | 0 | String extractedLinksName = parameters.getXML().get("extractedLinks"); |
| 34 | 0 | String documentDatasName = parameters.getXML().get("documentDatas"); |
| 35 | |
|
| 36 | 0 | extractedLinks = parameters.getTypeReader(extractedLinksName); |
| 37 | 0 | documentDatas = parameters.getTypeReader(documentDatasName); |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public void setProcessor(Step processor) throws IncompatibleProcessorException { |
| 41 | 0 | Linkage.link(this, processor); |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
void match(DocumentData docData, ExtractedLink link) { |
| 45 | 0 | if (linkData == null) { |
| 46 | 0 | linkData = new DocumentLinkData(); |
| 47 | 0 | linkData.identifier = docData.identifier; |
| 48 | 0 | linkData.url = docData.url; |
| 49 | 0 | linkData.textLength = docData.textLength; |
| 50 | |
} |
| 51 | |
|
| 52 | 0 | linkData.links.add(link); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
void flush() throws IOException { |
| 56 | 0 | if (linkData != null) { |
| 57 | 0 | processor.process(linkData); |
| 58 | |
} |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
public void run() throws IOException { |
| 62 | 0 | ExtractedLink link = extractedLinks.read(); |
| 63 | 0 | DocumentData docData = documentDatas.read(); |
| 64 | |
|
| 65 | 0 | while (docData != null && link != null) { |
| 66 | 0 | int result = link.destUrl.compareTo(docData.url); |
| 67 | 0 | if (result == 0) { |
| 68 | 0 | match(docData, link); |
| 69 | 0 | link = extractedLinks.read(); |
| 70 | |
} else { |
| 71 | 0 | if (result < 0) { |
| 72 | 0 | link = extractedLinks.read(); |
| 73 | |
} else { |
| 74 | 0 | docData = documentDatas.read(); |
| 75 | |
} |
| 76 | |
} |
| 77 | 0 | } |
| 78 | |
|
| 79 | 0 | processor.close(); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
public Class<IdentifiedLink> getOutputClass() { |
| 83 | 0 | return IdentifiedLink.class; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public static void verify(TupleFlowParameters parameters, ErrorHandler handler) { |
| 87 | 12 | if (!Verification.requireParameters(new String[] { "extractedLinks", "documentDatas" }, |
| 88 | |
parameters.getXML(), handler)) { |
| 89 | 0 | return; |
| 90 | |
} |
| 91 | |
|
| 92 | 12 | String extractedLinksName = parameters.getXML().get("extractedLinks"); |
| 93 | 12 | String documentDatasName = parameters.getXML().get("documentDatas"); |
| 94 | |
|
| 95 | 12 | Verification.verifyTypeReader(extractedLinksName, ExtractedLink.class, parameters, handler); |
| 96 | 12 | Verification.verifyTypeReader(documentDatasName, DocumentData.class, parameters, handler); |
| 97 | 12 | } |
| 98 | |
} |