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.SearchResult.CandidateAnswer;
import cz.brmlab.yodaqa.model.SearchResult.ResultInfo;

/**
 * Take an input CAS and generate per-answer CAS instances.
 *
 * So far, this answer generator is super-primitive, just to showcase
 * a CAS multiplier behavior. Just generates two answers from the
 * input CAS sofa as two 10-character segments. */

public class AnswerGenerator extends JCasMultiplier_ImplBase {
	JCas src_jcas;
	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 {
		JCas resultsView;
		try {
			resultsView = jcas.getView("Result");
		} catch (Exception e) {
			throw new AnalysisEngineProcessException(e);
		}

		src_jcas = resultsView;
		ri = (ResultInfo) resultsView.getJFSIndexRepository().getAllIndexedFS(ResultInfo.type).next();

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

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

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

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

			ResultInfo ri2 = (ResultInfo) copier.copyFs(ri);
			ri2.addToIndexes();
		} catch (Exception e) {
			jcas.release();
			throw new AnalysisEngineProcessException(e);
		}
		i++;
		return jcas;
	}
}

@@ -474,16 +474,69 @@
 erInfo;%0A
+import cz.brmlab.yodaqa.model.Question.QuestionInfo;%0A
 import c
@@ -967,24 +967,58 @@
 %7B%0A%09JCas 
-src_jcas
+questionJcas, resultJcas;%0A%09QuestionInfo qi
 ;%0A%09Resul
@@ -1300,33 +1300,57 @@
 %7B%0A%09%09
-JCas resultsView;%0A%09%09try %7B
+try %7B%0A%09%09%09questionJcas = jcas.getView(%22Question%22);
 %0A%09%09%09
@@ -1355,21 +1355,20 @@
 %09%09result
-sView
+Jcas
  = jcas.
@@ -1471,30 +1471,106 @@
 %0A%0A%09%09
-src_jcas = resultsView
+qi = (QuestionInfo) questionJcas.getJFSIndexRepository().getAllIndexedFS(QuestionInfo.type).next()
 ;%0A%09%09
@@ -1589,29 +1589,28 @@
 Info) result
-sView
+Jcas
 .getJFSIndex
@@ -1686,13 +1686,12 @@
 sult
-sView
+Jcas
 .get
@@ -1970,17 +1970,18 @@
 sCopier 
-c
+qC
 opier = 
@@ -1994,21 +1994,98 @@
 sCopier(
-src_j
+questionJcas.getCas(), jcas.getCas());%0A%09%09CasCopier rCopier = new CasCopier(resultJ
 cas.getC
@@ -2258,13 +2258,15 @@
 age(
-src_j
+resultJ
 cas.
@@ -2469,16 +2469,96 @@
 xes();%0A%0A
+%09%09%09QuestionInfo qi2 = (QuestionInfo) qCopier.copyFs(qi);%0A%09%09%09qi2.addToIndexes();%0A
 %09%09%09Resul
@@ -2582,17 +2582,18 @@
 ltInfo) 
-c
+rC
 opier.co

Copy QuestionInfo to the AnswerCAS