Details
Description
When running my Ruta script as an analysis engine in a pipeline, it does not work correctly when creating a new CAS and processing the pipeline a second time with the new CAS.
While reusing the old cas with "cas.reset()" is working, creating a new CAS results in failing rules including "CONTAINS" in the ruta script.
The ruta script used in the example:
mystic.ruta
PACKAGE de.tudarmstadt.algo.vpino.ruta; DECLARE test; Document{CONTAINS(CW)->MARK(test)};
The following Java class can reproduce the error. It creates four xmi files. The last xmi file is missing the annotations created with rules including "CONTAINS".
MysticPipe.java
package org.uimafit.pipeline; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.uima.UIMAFramework; import org.apache.uima.analysis_engine.AnalysisEngine; import org.apache.uima.analysis_engine.AnalysisEngineDescription; import org.apache.uima.analysis_engine.AnalysisEngineProcessException; import org.apache.uima.cas.CAS; import org.apache.uima.cas.impl.XmiCasSerializer; import org.apache.uima.fit.factory.AnalysisEngineFactory; import org.apache.uima.fit.pipeline.SimplePipeline; import org.apache.uima.resource.ResourceInitializationException; import org.apache.uima.resource.metadata.ResourceMetaData; import org.apache.uima.util.CasCreationUtils; import org.apache.uima.util.InvalidXMLException; import org.apache.uima.util.XMLInputSource; import org.apache.uima.util.XMLSerializer; import org.xml.sax.SAXException; public class MysticPipe { public static void main(String[] args) throws Exception { working("This is a test.", initPipeline()); failing("This is a test.", initPipeline()); } private static AnalysisEngine initPipeline() throws ResourceInitializationException, IOException, InvalidXMLException { File specFile = new File("./descriptor/de/tudarmstadt/algo/vpino/ruta/mysticEngine.xml"); XMLInputSource in = new XMLInputSource(specFile); AnalysisEngineDescription ruta = (AnalysisEngineDescription) UIMAFramework.getXMLParser().parseResourceSpecifier(in); return AnalysisEngineFactory.createEngine(ruta); } private static void working(String input, AnalysisEngine theEngine) throws ResourceInitializationException, AnalysisEngineProcessException, IOException, SAXException { final List<ResourceMetaData> metaData = new ArrayList<ResourceMetaData>(); metaData.add(theEngine.getMetaData()); final CAS cas = CasCreationUtils.createCas(metaData); System.out.println("create a new cas..."); cas.setDocumentLanguage("de"); cas.setDocumentText(input); SimplePipeline.runPipeline(cas, theEngine); writeXmiFile(cas, "works_test1");//CHECK //THE DIFFERENCE cas.reset(); //END DIFFERENCE System.out.println("create a new cas..."); cas.setDocumentLanguage("de"); cas.setDocumentText(input); SimplePipeline.runPipeline(cas, theEngine); writeXmiFile(cas, "works_test2");//CHECK } private static void failing(String input, AnalysisEngine theEngine) throws ResourceInitializationException, AnalysisEngineProcessException, IOException, SAXException { final List<ResourceMetaData> metaData = new ArrayList<ResourceMetaData>(); metaData.add(theEngine.getMetaData()); final CAS cas = CasCreationUtils.createCas(metaData); System.out.println("create a new cas..."); cas.setDocumentLanguage("de"); cas.setDocumentText(input); SimplePipeline.runPipeline(cas, theEngine); writeXmiFile(cas, "works_test3"); // CHECK //THE DIFFERENCE final CAS cas2 = CasCreationUtils.createCas(metaData); //END DIFFERENCE System.out.println("create a new cas..."); cas2.setDocumentLanguage("de"); cas2.setDocumentText(input); SimplePipeline.runPipeline(cas2, theEngine); writeXmiFile(cas2, "fail_test4"); //FAIL return; } public static void writeXmiFile(CAS aCas, String Fname) throws IOException, SAXException { File outFile = new File("output", Fname + ".xmi"); OutputStream out = null; try { // out = new StringOutputStream(); out = new FileOutputStream(outFile); XmiCasSerializer ser = new XmiCasSerializer(aCas.getTypeSystem()); XMLSerializer xmlSer = new XMLSerializer(out, false); ser.serialize(aCas, xmlSer.getContentHandler()); } finally { if (out != null) { out.close(); } } } }