package cz.brmlab.yodaqa.pipeline;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.AbstractCas;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.fit.component.JCasMultiplier_ImplBase;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.CasCopier;

import cz.brmlab.yodaqa.model.CandidateAnswer.AnswerInfo;
import cz.brmlab.yodaqa.model.Question.QuestionInfo;
import cz.brmlab.yodaqa.model.SearchResult.CandidateAnswer;
import cz.brmlab.yodaqa.model.SearchResult.ResultInfo;

/**
 * Take an input ResultCAS and generate per-answer CandidateAnswerCAS
 * instances.
 *
 * We are a simple CAS multiplier that creates a dedicated CAS for
 * each to-be-analyzed candidate answer. */

public class AnswerGenerator extends JCasMultiplier_ImplBase {
	JCas questionJcas, resultJcas, pickedPassagesJcas;
	QuestionInfo qi;
	ResultInfo ri;

	/* Prepared list of answers to return. */
	FSIterator answers;
	int i;

	public void initialize(UimaContext aContext) throws ResourceInitializationException {
		super.initialize(aContext);
	}

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		try {
			questionJcas = jcas.getView("Question");
			resultJcas = jcas.getView("Result");
			pickedPassagesJcas = jcas.getView("PickedPassages");
		} catch (Exception e) {
			throw new AnalysisEngineProcessException(e);
		}

		qi = (QuestionInfo) questionJcas.getJFSIndexRepository().getAllIndexedFS(QuestionInfo.type).next();
		ri = (ResultInfo) resultJcas.getJFSIndexRepository().getAllIndexedFS(ResultInfo.type).next();

		answers = pickedPassagesJcas.getJFSIndexRepository().getAllIndexedFS(CandidateAnswer.type);
		i = 0;
	}

	public boolean hasNext() throws AnalysisEngineProcessException {
		return answers.hasNext();
	}

	public AbstractCas next() throws AnalysisEngineProcessException {
		JCas jcas = getEmptyJCas();
		CasCopier qCopier = new CasCopier(questionJcas.getCas(), jcas.getCas());
		CasCopier rCopier = new CasCopier(resultJcas.getCas(), jcas.getCas());
		try {
			CandidateAnswer answer = (CandidateAnswer) answers.next();
			jcas.setDocumentText(answer.getCoveredText());
			jcas.setDocumentLanguage(resultJcas.getDocumentLanguage());

			AnswerInfo ai = new AnswerInfo(jcas);
			ai.setConfidence(answer.getConfidence() * answer.getPassage().getScore());
			ai.setIsLast(!answers.hasNext());
			ai.addToIndexes();

			QuestionInfo qi2 = (QuestionInfo) qCopier.copyFs(qi);
			qi2.addToIndexes();
			ResultInfo ri2 = (ResultInfo) rCopier.copyFs(ri);
			ri2.addToIndexes();
		} catch (Exception e) {
			jcas.release();
			throw new AnalysisEngineProcessException(e);
		}
		i++;
		return jcas;
	}
}

@@ -304,17 +304,104 @@
 ima.
-jcas.JCas
+fit.util.JCasUtil;%0Aimport org.apache.uima.jcas.JCas;%0Aimport org.apache.uima.jcas.tcas.Annotation
 ;%0Aim
@@ -2225,24 +2225,105 @@
 Cas());%0A
+%09%09CasCopier pCopier = new CasCopier(pickedPassagesJcas.getCas(), jcas.getCas());%0A
 %09%09try %7B%0A
 %09%09%09Candi
@@ -2314,16 +2314,16 @@
 %09%09try %7B%0A
-
 %09%09%09Candi
@@ -2669,16 +2669,612 @@
 xes();%0A%0A
+%09%09%09/* Copy in-answer annotations */%0A%09%09%09int ofs = answer.getBegin();%0A%09%09%09for (Annotation a : JCasUtil.selectCovered(Annotation.class, answer)) %7B%0A%09%09%09%09if (a instanceof CandidateAnswer)%0A%09%09%09%09%09continue;%0A%09%09%09%09/* If we already deep-copied the annotation,%0A%09%09%09%09 * here we get its reference to fix things up.%0A%09%09%09%09 * XXX: We don't get a chance to remove e.g.%0A%09%09%09%09 * outside-reaching dependency annotations;%0A%09%09%09%09 * casCopier is a kind of silly interface. */%0A%09%09%09%09Annotation a2 = (Annotation) pCopier.copyFs(a);%0A%09%09%09%09a2.setBegin(a2.getBegin() - ofs);%0A%09%09%09%09a2.setEnd(a2.getEnd() - ofs);%0A%09%09%09%09a2.addToIndexes();%0A%09%09%09%7D%0A%0A
 %09%09%09Quest
@@ -3398,16 +3398,16 @@
 Fs(ri);%0A
-
 %09%09%09ri2.a
@@ -3421,16 +3421,17 @@
 exes();%0A
+%0A
 %09%09%7D catc

Copy over all relevant annotations to the CandidateAnswerCAS