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/08/25 00:43:35 UTC

svn commit: r1517209 - in /uima/sandbox/uimafit/trunk/uimafit-examples: ./ src/main/java/org/apache/uima/fit/examples/resource/

Author: rec
Date: Sat Aug 24 22:43:35 2013
New Revision: 1517209

URL: http://svn.apache.org/r1517209
Log:
[UIMA-3217] Review the examples
- Remove dependency on JUnit

Added:
    uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample2.java   (with props)
    uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample3.java   (with props)
Modified:
    uima/sandbox/uimafit/trunk/uimafit-examples/pom.xml
    uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample.java

Modified: uima/sandbox/uimafit/trunk/uimafit-examples/pom.xml
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-examples/pom.xml?rev=1517209&r1=1517208&r2=1517209&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-examples/pom.xml (original)
+++ uima/sandbox/uimafit/trunk/uimafit-examples/pom.xml Sat Aug 24 22:43:35 2013
@@ -46,6 +46,7 @@
 		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
+			<scope>test</scope>
 		</dependency>
 	</dependencies>
 	<licenses>

Modified: uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample.java?rev=1517209&r1=1517208&r2=1517209&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample.java Sat Aug 24 22:43:35 2013
@@ -21,23 +21,19 @@ package org.apache.uima.fit.examples.res
 
 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.ExternalResourceFactory.bindResource;
 import static org.apache.uima.fit.factory.ExternalResourceFactory.createExternalResourceDescription;
-import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 
 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.fit.component.Resource_ImplBase;
 import org.apache.uima.fit.descriptor.ExternalResource;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.resource.DataResource;
 import org.apache.uima.resource.ExternalResourceDescription;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.SharedResourceObject;
-import org.junit.Test;
 
 /**
  * Example for the use of external resources with uimaFIT.
@@ -66,14 +62,13 @@ public class ExternalResourceExample {
    * was properly initialized by uimaFIT
    */
   public static class Annotator extends org.apache.uima.fit.component.JCasAnnotator_ImplBase {
-    final static String MODEL_KEY = "model";
+    final static String RES_MODEL = "model";
 
-    @ExternalResource(key = MODEL_KEY)
+    @ExternalResource(key = RES_MODEL)
     private SharedModel model;
 
     @Override
     public void process(JCas aJCas) throws AnalysisEngineProcessException {
-      assertTrue(model.getUri().endsWith("somemodel.bin"));
       // Prints the instance ID to the console - this proves the same instance
       // of the SharedModel is used in both Annotator instances.
       System.out.println(getClass().getSimpleName() + ": " + model);
@@ -81,92 +76,21 @@ public class ExternalResourceExample {
   }
 
   /**
-   * JUnit test that illustrates how to configure the annotator with the shared model object
+   * Illustrate how to configure the annotator with the shared model object.
    */
-  @Test
-  public void configureAnnotatorsIndividuallyExample() throws Exception {
+  public static void main(String[] args) throws Exception {
     ExternalResourceDescription extDesc = createExternalResourceDescription(SharedModel.class,
             new File("somemodel.bin"));
 
     // Binding external resource to each Annotator individually
-    AnalysisEngineDescription aed1 = createEngineDescription(Annotator.class,
-            Annotator.MODEL_KEY, extDesc);
-    AnalysisEngineDescription aed2 = createEngineDescription(Annotator.class,
-            Annotator.MODEL_KEY, extDesc);
+    AnalysisEngineDescription aed1 = createEngineDescription(Annotator.class, Annotator.RES_MODEL,
+            extDesc);
+    AnalysisEngineDescription aed2 = createEngineDescription(Annotator.class, Annotator.RES_MODEL,
+            extDesc);
 
     // Check the external resource was injected
     AnalysisEngineDescription aaed = createEngineDescription(aed1, aed2);
     AnalysisEngine ae = createEngine(aaed);
     ae.process(ae.newJCas());
   }
-
-  /**
-   * JUnit test that illustrates how to configure the Annotator with the SharedModel. You should
-   * avoid this approach unless you are absolutely sure you need this. For this approach to work it
-   * must be guaranteed that no two components in the aggregate use the same resource key for
-   * different resoures, e.g. one "model" for different kinds of models.
-   */
-  @Test
-  public void configureAggregatedExample() throws Exception {
-    AnalysisEngineDescription aed1 = createEngineDescription(Annotator.class);
-    AnalysisEngineDescription aed2 = createEngineDescription(Annotator.class);
-
-    // Bind external resource to the aggregate
-    AnalysisEngineDescription aaed = createEngineDescription(aed1, aed2);
-    bindResource(aaed, Annotator.MODEL_KEY, SharedModel.class, new File("somemodel.bin").toURI()
-            .toURL().toString());
-
-    // Check the external resource was injected
-    AnalysisEngine ae = createEngine(aaed);
-    ae.process(ae.newJCas());
-  }
-
-  /**
-   * Simple example resource that can use another resource.
-   */
-  public static class ChainableResource extends Resource_ImplBase {
-    public final static String PARAM_CHAINED_RESOURCE = "chainedResource";
-
-    @ExternalResource(key = PARAM_CHAINED_RESOURCE, mandatory = false)
-    private ChainableResource chainedResource;
-
-    @Override
-    public void afterResourcesInitialized() {
-      // init logic that requires external resources
-      System.out.println(getClass().getSimpleName() + ": " + chainedResource);
-    }
-  }
-
-  /**
-   * Example annotator that uses the resource. In the process() we only test if the model was
-   * properly initialized by uimaFIT
-   */
-  public static class Annotator2 extends org.apache.uima.fit.component.JCasAnnotator_ImplBase {
-    final static String MODEL_KEY = "Model";
-
-    @ExternalResource(key = MODEL_KEY)
-    private ChainableResource model;
-
-    @Override
-    public void process(JCas aJCas) throws AnalysisEngineProcessException {
-      System.out.println(getClass().getSimpleName() + ": " + model);
-    }
-  }
-
-  /**
-   * JUnit test that illustrates how to configure the annotator with a chainable resource
-   */
-  @Test
-  public void configureAnnotatorWithChainedResource() throws Exception {
-    AnalysisEngineDescription aed = createEngineDescription(
-            Annotator2.class,
-            Annotator2.MODEL_KEY,
-            createExternalResourceDescription(ChainableResource.class,
-                    ChainableResource.PARAM_CHAINED_RESOURCE,
-                    createExternalResourceDescription(ChainableResource.class)));
-
-    // Check the external resource was injected
-    AnalysisEngine ae = createEngine(aed);
-    ae.process(ae.newJCas());
-  }
 }
\ No newline at end of file

Added: uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample2.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample2.java?rev=1517209&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample2.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample2.java Sat Aug 24 22:43:35 2013
@@ -0,0 +1,97 @@
+/*
+ * 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.examples.resource;
+
+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.ExternalResourceFactory.bindResource;
+
+import java.io.File;
+
+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.fit.descriptor.ExternalResource;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.DataResource;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.SharedResourceObject;
+import org.junit.Test;
+
+/**
+ * Example for the use of external resources with uimaFIT.
+ */
+public class ExternalResourceExample2 {
+  /**
+   * Simple model that only stores the URI it was loaded from. Normally data would be loaded from
+   * the URI instead and made accessible through methods in this class. This simple example only
+   * allows to access the URI.
+   */
+  public static final class SharedModel implements SharedResourceObject {
+    private String uri;
+
+    public void load(DataResource aData) throws ResourceInitializationException {
+      uri = aData.getUri().toString();
+    }
+
+    public String getUri() {
+      return uri;
+    }
+  }
+
+  /**
+   * Example annotator that uses the share model object. In the process() we only test if the model
+   * was properly initialized by uimaFIT
+   */
+  public static class Annotator extends org.apache.uima.fit.component.JCasAnnotator_ImplBase {
+    final static String RES_MODEL = "model";
+
+    @ExternalResource(key = RES_MODEL)
+    private SharedModel model;
+
+    @Override
+    public void process(JCas aJCas) throws AnalysisEngineProcessException {
+      // Prints the instance ID to the console - this proves the same instance
+      // of the SharedModel is used in both Annotator instances.
+      System.out.println(getClass().getSimpleName() + ": " + model);
+    }
+  }
+
+  /**
+   * Illustrate how to configure the Annotator with the SharedModel. You should avoid this approach
+   * unless you are absolutely sure you need this. For this approach to work it must be guaranteed
+   * that no two components in the aggregate use the same resource key for different resoures, e.g.
+   * one "model" for different kinds of models.
+   */
+  @Test
+  public void configureAggregatedExample() throws Exception {
+    AnalysisEngineDescription aed1 = createEngineDescription(Annotator.class);
+    AnalysisEngineDescription aed2 = createEngineDescription(Annotator.class);
+
+    // Bind external resource to the aggregate
+    AnalysisEngineDescription aaed = createEngineDescription(aed1, aed2);
+    bindResource(aaed, Annotator.RES_MODEL, SharedModel.class, new File("somemodel.bin").toURI()
+            .toURL().toString());
+
+    // Check the external resource was injected
+    AnalysisEngine ae = createEngine(aaed);
+    ae.process(ae.newJCas());
+  }
+}
\ No newline at end of file

Propchange: uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample3.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample3.java?rev=1517209&view=auto
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample3.java (added)
+++ uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample3.java Sat Aug 24 22:43:35 2013
@@ -0,0 +1,84 @@
+/*
+ * 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.examples.resource;
+
+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.ExternalResourceFactory.createExternalResourceDescription;
+
+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.fit.component.Resource_ImplBase;
+import org.apache.uima.fit.descriptor.ExternalResource;
+import org.apache.uima.jcas.JCas;
+
+/**
+ * Example for the use of external resources with uimaFIT.
+ */
+public class ExternalResourceExample3 {
+  /**
+   * Simple example resource that can use another resource.
+   */
+  public static class ChainableResource extends Resource_ImplBase {
+    public final static String RES_CHAINED_RESOURCE = "chainedResource";
+
+    @ExternalResource(key = RES_CHAINED_RESOURCE, mandatory = false)
+    private ChainableResource chainedResource;
+
+    @Override
+    public void afterResourcesInitialized() {
+      // init logic that requires external resources
+      System.out.println(getClass().getSimpleName() + ": " + chainedResource);
+    }
+  }
+
+  /**
+   * Example annotator that uses the resource. In the process() we only test if the model was
+   * properly initialized by uimaFIT
+   */
+  public static class Annotator2 extends org.apache.uima.fit.component.JCasAnnotator_ImplBase {
+    final static String RES_MODEL = "model";
+
+    @ExternalResource(key = RES_MODEL)
+    private ChainableResource model;
+
+    @Override
+    public void process(JCas aJCas) throws AnalysisEngineProcessException {
+      System.out.println(getClass().getSimpleName() + ": " + model);
+    }
+  }
+
+  /**
+   * Illustrate how to configure the annotator with a chainable resource
+   */
+  public static void main(String[] args) throws Exception {
+    AnalysisEngineDescription aed = createEngineDescription(
+            Annotator2.class,
+            Annotator2.RES_MODEL,
+            createExternalResourceDescription(ChainableResource.class,
+                    ChainableResource.RES_CHAINED_RESOURCE,
+                    createExternalResourceDescription(ChainableResource.class)));
+
+    // Check the external resource was injected
+    AnalysisEngine ae = createEngine(aed);
+    ae.process(ae.newJCas());
+  }
+}
\ No newline at end of file

Propchange: uima/sandbox/uimafit/trunk/uimafit-examples/src/main/java/org/apache/uima/fit/examples/resource/ExternalResourceExample3.java
------------------------------------------------------------------------------
    svn:eol-style = native