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 2020/02/03 07:27:01 UTC

[uima-uimafit] 01/01: [UIMA-6186] Factory methods for resource instances

This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch feature/UIMA-6186-Factory-methods-for-resource-instances
in repository https://gitbox.apache.org/repos/asf/uima-uimafit.git

commit d4de3a08b9fd5567aa5e1ecb6a0bd004d62e52c0
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Mon Feb 3 08:26:47 2020 +0100

    [UIMA-6186] Factory methods for resource instances
    
    - Added new createExternalResource methods to ExternalResourceFactory
    - Added unit tests for these methods
    - A bit of formatting and JavaDoc improvements.
---
 .../uima/fit/factory/ExternalResourceFactory.java  | 59 ++++++++++++++++++----
 .../fit/factory/ResourceManagerFactoryTest.java    | 55 ++++++++++++++++++++
 2 files changed, 104 insertions(+), 10 deletions(-)

diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java b/uimafit-core/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java
index 842e042..46abee8 100644
--- a/uimafit-core/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java
+++ b/uimafit-core/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java
@@ -20,6 +20,8 @@
 package org.apache.uima.fit.factory;
 
 import static java.util.Arrays.asList;
+import static java.util.Collections.emptyMap;
+import static org.apache.uima.UIMAFramework.produceResource;
 import static org.apache.uima.fit.factory.ConfigurationParameterFactory.canParameterBeSet;
 import static org.apache.uima.fit.factory.ConfigurationParameterFactory.createConfigurationData;
 
@@ -107,8 +109,7 @@ public final class ExternalResourceFactory {
   }
 
   /**
-   * Create an external resource description for a custom resource. This is intended to be used
-   * together with ....
+   * Create an external resource description for a custom resource.
    * 
    * @param aInterface
    *          the interface the resource should implement.
@@ -1235,8 +1236,7 @@ public final class ExternalResourceFactory {
    * uimaFIT internal use. This method is required by the ConfigurationParameterFactory, so it is
    * package private instead of private.
    */
-  static ResourceValueType getExternalResourceParameterType(Object aValue)
-  {
+  static ResourceValueType getExternalResourceParameterType(Object aValue) {
     if (aValue == null) {
       return ResourceValueType.NO_RESOURCE;
     }
@@ -1250,19 +1250,58 @@ public final class ExternalResourceFactory {
             .iterator().next() instanceof ExternalResourceDescription);
     if (isResourcePrimitive) {
       return ResourceValueType.PRIMITIVE;
-    }
-    else if (isResourceArray) {
+    } else if (isResourceArray) {
       return ResourceValueType.ARRAY;
-    }
-    else if (isResourceCollection) {
+    } else if (isResourceCollection) {
       return ResourceValueType.COLLECTION;
-    }
-    else {
+    } else {
       return ResourceValueType.NO_RESOURCE;
     }
   }
   
   /**
+   * Create an instance of a UIMA shared/external resource class.
+   * 
+   * @param <R>
+   *          the resource type.
+   * @param resourceClass
+   *          the class implementing the resource.
+   * @param params
+   *          parameters passed to the resource when it is created. Each parameter consists of two
+   *          arguments, the first being the name and the second being the parameter value
+   * @return the resource instance.
+   * @throws ResourceInitializationException
+   *           if there was a problem instantiating the resource.
+   */
+  public static <R> R createExternalResource(Class<? extends Resource> resourceClass,
+          Object... params) throws ResourceInitializationException {
+    return createExternalResource(resourceClass, null, params);
+  }
+
+  /**
+   * Create an instance of a UIMA shared/external resource class.
+   * 
+   * @param <R>
+   *          the resource type.
+   * @param resourceClass
+   *          the class implementing the resource.
+   * @param resMgr
+   *          a resource manager (optional).
+   * @param params
+   *          parameters passed to the resource when it is created. Each parameter consists of two
+   *          arguments, the first being the name and the second being the parameter value
+   * @return the resource instance.
+   * @throws ResourceInitializationException
+   *           if there was a problem instantiating the resource.
+   */
+  @SuppressWarnings("unchecked")
+  public static <R> R createExternalResource(Class<? extends Resource> resourceClass,
+          ResourceManager resMgr, Object... params) throws ResourceInitializationException {
+    ExternalResourceDescription res = createExternalResourceDescription(resourceClass, params);
+    return (R) produceResource(resourceClass, res.getResourceSpecifier(), resMgr, emptyMap());
+  }
+  
+  /**
    * Types of external resource values.
    */
   static enum ResourceValueType {
diff --git a/uimafit-core/src/test/java/org/apache/uima/fit/factory/ResourceManagerFactoryTest.java b/uimafit-core/src/test/java/org/apache/uima/fit/factory/ResourceManagerFactoryTest.java
new file mode 100644
index 0000000..efd65a6
--- /dev/null
+++ b/uimafit-core/src/test/java/org/apache/uima/fit/factory/ResourceManagerFactoryTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.factory;
+
+import static org.apache.uima.fit.factory.ExternalResourceFactory.createExternalResource;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.apache.uima.fit.component.Resource_ImplBase;
+import org.apache.uima.fit.descriptor.ConfigurationParameter;
+import org.junit.Test;
+
+public class ResourceManagerFactoryTest {
+  public static class SimpleResource extends Resource_ImplBase {
+    // Nothing to do
+  }
+  
+  @Test
+  public void thatResourceCanBeCreated() throws Exception {
+    SimpleResource sut = createExternalResource(SimpleResource.class);
+    
+    assertThat(sut).isInstanceOf(SimpleResource.class);
+  }
+
+  public static class ResourceWithParameters extends Resource_ImplBase {
+    public @ConfigurationParameter int intValue;
+    public @ConfigurationParameter String stringValue;
+  }
+  
+  @Test
+  public void thatResourceCanBeParametrized() throws Exception {
+    ResourceWithParameters sut = createExternalResource(ResourceWithParameters.class,
+            "intValue", "1",
+            "stringValue", "test");
+    
+    assertThat(sut).isInstanceOf(ResourceWithParameters.class);
+    assertThat(sut.intValue).isEqualTo(1);
+    assertThat(sut.stringValue).isEqualTo("test");
+  }
+}