You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2013/07/20 21:13:22 UTC
svn commit: r1505191 - in /uima/sandbox/uimafit/trunk/uimafit/src:
main/java/org/apache/uima/fit/pipeline/ main/java/org/apache/uima/fit/util/
test/java/org/apache/uima/fit/pipeline/
Author: rec
Date: Sat Jul 20 19:13:21 2013
New Revision: 1505191
URL: http://svn.apache.org/r1505191
Log:
[UIMA-3041] JCasIterable should have signature accepting descriptors
Added:
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/LifeCycleUtil.java (with props)
uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/pipeline/JCasIterableTest.java (with props)
Modified:
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/JCasIterable.java
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/SimplePipeline.java
Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/JCasIterable.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/JCasIterable.java?rev=1505191&r1=1505190&r2=1505191&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/JCasIterable.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/JCasIterable.java Sat Jul 20 19:13:21 2013
@@ -18,6 +18,10 @@
*/
package org.apache.uima.fit.pipeline;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
+import static org.apache.uima.fit.factory.CollectionReaderFactory.createCollectionReader;
+
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
@@ -25,11 +29,14 @@ import java.util.List;
import org.apache.uima.UIMAException;
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.collection.CollectionException;
import org.apache.uima.collection.CollectionReader;
+import org.apache.uima.collection.CollectionReaderDescription;
import org.apache.uima.fit.component.NoOpAnnotator;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.util.LifeCycleUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.metadata.ResourceMetaData;
import org.apache.uima.resource.metadata.TypeSystemDescription;
@@ -48,10 +55,15 @@ public class JCasIterable implements Ite
private AnalysisEngine[] analysisEngines;
private JCas jCas;
+
+ private boolean selfComplete = false;
+
+ private boolean selfDestroy = false;
/**
* Iterate over the documents loaded by the CollectionReader. (Uses an JCasAnnotatorAdapter to
- * create the document JCas.)
+ * create the document JCas.) By default, components get no lifecycle events, such as
+ * collectionProcessComplete or destroy when this constructor is used.
*
* @param aReader
* The CollectionReader for loading documents.
@@ -60,13 +72,13 @@ public class JCasIterable implements Ite
*/
public JCasIterable(final CollectionReader aReader,
final TypeSystemDescription aTypeSystemDescription) throws UIMAException, IOException {
- this(aReader, AnalysisEngineFactory
- .createEngine(NoOpAnnotator.class, aTypeSystemDescription));
+ this(aReader, AnalysisEngineFactory.createEngine(NoOpAnnotator.class, aTypeSystemDescription));
}
/**
* Iterate over the documents loaded by the CollectionReader, running the AnalysisEngine on each
- * one before yielding them.
+ * one before yielding them. By default, components get no lifecycle events, such as
+ * collectionProcessComplete or destroy when this constructor is used.
*
* @param aReader
* The CollectionReader for loading documents.
@@ -75,14 +87,38 @@ public class JCasIterable implements Ite
*/
public JCasIterable(final CollectionReader aReader, final AnalysisEngine... aEngines)
throws UIMAException, IOException {
- this.collectionReader = aReader;
- this.analysisEngines = aEngines;
+ collectionReader = aReader;
+ analysisEngines = aEngines;
+ jCas = CasCreationUtils.createCas(collectMetaData()).getJCas();
+ }
+
+ /**
+ * Iterate over the documents loaded by the CollectionReader, running the AnalysisEngine on each
+ * one before yielding them. When created with this constructor, analysis engines by default
+ * receive a collectionProcessComplete call when all documents have been read from the reader
+ * and all components get destroyed.
+ *
+ * @param aReader
+ * The CollectionReader for loading documents.
+ * @param aEngines
+ * The AnalysisEngines for processing documents.
+ */
+ public JCasIterable(final CollectionReaderDescription aReader,
+ final AnalysisEngineDescription... aEngines) throws UIMAException, IOException {
+ setSelfComplete(true);
+ setSelfDestroy(true);
+ collectionReader = createCollectionReader(aReader);
+ analysisEngines = new AnalysisEngine[] { createEngine(createEngineDescription(aEngines)) };
+ jCas = CasCreationUtils.createCas(collectMetaData()).getJCas();
+ }
+
+ private List<ResourceMetaData> collectMetaData() {
final List<ResourceMetaData> metaData = new ArrayList<ResourceMetaData>();
- metaData.add(aReader.getMetaData());
- for (AnalysisEngine engine : aEngines) {
+ metaData.add(collectionReader.getMetaData());
+ for (AnalysisEngine engine : analysisEngines) {
metaData.add(engine.getMetaData());
}
- this.jCas = CasCreationUtils.createCas(metaData).getJCas();
+ return metaData;
}
public Iterator<JCas> iterator() {
@@ -91,21 +127,39 @@ public class JCasIterable implements Ite
public boolean hasNext() {
try {
- return this.collectionReader.hasNext();
+ return collectionReader.hasNext();
} catch (CollectionException e) {
throw new IllegalStateException(e);
} catch (IOException e) {
throw new IllegalStateException(e);
}
+ finally {
+ if (selfDestroy) {
+ destroy();
+ }
+ }
}
public JCas next() {
- this.jCas.reset();
+ jCas.reset();
+ boolean error = true;
+ boolean destroyed = false;
try {
- this.collectionReader.getNext(this.jCas.getCas());
- for (AnalysisEngine engine : this.analysisEngines) {
- engine.process(this.jCas);
+ collectionReader.getNext(jCas.getCas());
+ for (AnalysisEngine engine : analysisEngines) {
+ engine.process(jCas);
+ }
+
+ if (!hasNext() && selfComplete) {
+ collectionProcessComplete();
+ }
+
+ if (!hasNext() && selfDestroy) {
+ destroy();
+ destroyed = true;
}
+
+ error = false;
} catch (CollectionException e) {
throw new IllegalStateException(e);
} catch (IOException e) {
@@ -113,10 +167,51 @@ public class JCasIterable implements Ite
} catch (AnalysisEngineProcessException e) {
throw new IllegalStateException(e);
}
- return this.jCas;
+ finally {
+ if (error && selfDestroy && !destroyed) {
+ destroy();
+ }
+ }
+
+ return jCas;
}
public void remove() {
throw new UnsupportedOperationException();
}
+
+ public void collectionProcessComplete() throws AnalysisEngineProcessException
+ {
+ LifeCycleUtil.collectionProcessComplete(analysisEngines);
+ }
+
+ public void destroy() {
+ LifeCycleUtil.close(collectionReader);
+ LifeCycleUtil.destroy(collectionReader);
+ LifeCycleUtil.destroy(analysisEngines);
+ }
+
+ public boolean isSelfComplete() {
+ return selfComplete;
+ }
+
+ /**
+ * Send a collectionProcessComplete call to analysis engines when the reader has no further
+ * CASes to produce.
+ */
+ public void setSelfComplete(boolean aSelfComplete) {
+ selfComplete = aSelfComplete;
+ }
+
+ public boolean isSelfDestroy() {
+ return selfDestroy;
+ }
+
+ /**
+ * Send a destroy call to analysis engines when the reader has no further CASes to produce or
+ * if an error occurs.
+ */
+ public void setSelfDestroy(boolean aSelfDestroy) {
+ selfDestroy = aSelfDestroy;
+ }
}
Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/SimplePipeline.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/SimplePipeline.java?rev=1505191&r1=1505190&r2=1505191&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/SimplePipeline.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/pipeline/SimplePipeline.java Sat Jul 20 19:13:21 2013
@@ -19,9 +19,12 @@
package org.apache.uima.fit.pipeline;
import static java.util.Arrays.asList;
-import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine;
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
import static org.apache.uima.fit.factory.CollectionReaderFactory.createCollectionReader;
+import static org.apache.uima.fit.util.LifeCycleUtil.close;
+import static org.apache.uima.fit.util.LifeCycleUtil.collectionProcessComplete;
+import static org.apache.uima.fit.util.LifeCycleUtil.destroy;
import java.io.IOException;
import java.util.ArrayList;
@@ -30,11 +33,9 @@ import java.util.List;
import org.apache.uima.UIMAException;
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.collection.CollectionReader;
import org.apache.uima.collection.CollectionReaderDescription;
-import org.apache.uima.collection.base_cpm.BaseCollectionReader;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.Resource;
import org.apache.uima.resource.metadata.ResourceMetaData;
@@ -221,38 +222,4 @@ public final class SimplePipeline {
engine.process(cas);
}
}
-
- /**
- * Notify a set of {@link AnalysisEngine analysis engines} that the collection process is
- * complete.
- */
- private static void collectionProcessComplete(final AnalysisEngine... engines)
- throws AnalysisEngineProcessException {
- for (AnalysisEngine e : engines) {
- e.collectionProcessComplete();
- }
- }
-
- /**
- * Destroy a set of {@link Resource resources}.
- */
- private static void destroy(final Resource... resources) {
- for (Resource r : resources) {
- if (r != null) {
- r.destroy();
- }
- }
- }
-
- private static void close(final BaseCollectionReader aReader) {
- if (aReader == null) {
- return;
- }
-
- try {
- aReader.close();
- } catch (IOException e) {
- // Ignore.
- }
- }
}
Added: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/LifeCycleUtil.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/LifeCycleUtil.java?rev=1505191&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/LifeCycleUtil.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/LifeCycleUtil.java Sat Jul 20 19:13:21 2013
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.fit.util;
+
+import java.io.IOException;
+
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.collection.base_cpm.BaseCollectionReader;
+import org.apache.uima.resource.Resource;
+
+/**
+ * Helper methods to handle the life cycle of UIMA components.
+ */
+public final class LifeCycleUtil {
+
+ private LifeCycleUtil() {
+ // No instances
+ }
+
+ /**
+ * Notify a set of {@link AnalysisEngine analysis engines} that the collection process is
+ * complete.
+ */
+ public static void collectionProcessComplete(final AnalysisEngine... engines)
+ throws AnalysisEngineProcessException {
+ for (AnalysisEngine e : engines) {
+ e.collectionProcessComplete();
+ }
+ }
+
+ /**
+ * Destroy a set of {@link Resource resources}.
+ */
+ public static void destroy(final Resource... resources) {
+ for (Resource r : resources) {
+ if (r != null) {
+ r.destroy();
+ }
+ }
+ }
+
+ /**
+ * Close a reader.
+ */
+ public static void close(final BaseCollectionReader aReader) {
+ if (aReader == null) {
+ return;
+ }
+
+ try {
+ aReader.close();
+ } catch (IOException e) {
+ // Ignore.
+ }
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/util/LifeCycleUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/pipeline/JCasIterableTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/pipeline/JCasIterableTest.java?rev=1505191&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/pipeline/JCasIterableTest.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/pipeline/JCasIterableTest.java Sat Jul 20 19:13:21 2013
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.uima.fit.pipeline;
+
+import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription;
+import static org.apache.uima.fit.factory.CollectionReaderFactory.createDescription;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+
+import org.apache.uima.UimaContext;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.collection.CollectionException;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.fit.component.JCasCollectionReader_ImplBase;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.util.Progress;
+import org.apache.uima.util.ProgressImpl;
+import org.junit.Test;
+
+public class JCasIterableTest {
+
+ @Test
+ public void test() throws Exception {
+ for (JCas jcas : new JCasIterable(createDescription(ThreeDocsReader.class),
+ createEngineDescription(GetTextAE.class))) {
+ System.out.println(jcas.getDocumentText());
+ }
+
+ assertEquals("Document 3", GetTextAE.lastText);
+ assertTrue(GetTextAE.complete);
+ assertTrue(GetTextAE.destroyed);
+ }
+
+ public static final class ThreeDocsReader extends JCasCollectionReader_ImplBase {
+ private final int N = 3;
+ private int n = 0;
+
+ public Progress[] getProgress() {
+ return new Progress[] { new ProgressImpl(n, N, "document") };
+ }
+
+ public boolean hasNext() throws IOException, CollectionException {
+ return n < N;
+ }
+
+ @Override
+ public void getNext(JCas aJCas) throws IOException, CollectionException {
+ n++;
+ aJCas.setDocumentText("Document " + n);
+ }
+ }
+
+ public static final class GetTextAE extends JCasAnnotator_ImplBase {
+ public static String lastText = null;
+
+ public static boolean complete = false;
+
+ public static boolean destroyed = false;
+
+ @Override
+ public void initialize(UimaContext aContext) throws ResourceInitializationException {
+ super.initialize(aContext);
+ lastText = null;
+ complete = false;
+ destroyed = false;
+ }
+
+ @Override
+ public void process(JCas aArg0) throws AnalysisEngineProcessException {
+ lastText = aArg0.getDocumentText();
+ }
+
+ @Override
+ public void collectionProcessComplete() throws AnalysisEngineProcessException {
+ complete = true;
+ }
+
+ @Override
+ public void destroy() {
+ destroyed = true;
+ }
+ }
+}
Propchange: uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/pipeline/JCasIterableTest.java
------------------------------------------------------------------------------
svn:eol-style = native