Author: pkluegl
Date: Fri Dec 21 15:06:50 2018
New Revision: 1849484

URL: http://svn.apache.org/viewvc?rev=1849484&view=rev
Log:
UIMA-5892: fallback rather on uimaFIT in order to allow the creator on 
framework level

Added:
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/DummyAnnotator.java
Modified:
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ExecAction.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java?rev=1849484&r1=1849483&r2=1849484&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
 Fri Dec 21 15:06:50 2018
@@ -46,7 +46,9 @@ import org.apache.uima.cas.Type;
 import org.apache.uima.cas.TypeSystem;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.fit.factory.TypeSystemDescriptionFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory;
 import org.apache.uima.resource.ResourceAccessException;
+import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
 import org.apache.uima.resource.metadata.TypeDescription;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
@@ -1021,6 +1023,13 @@ public class RutaEnvironment {
       }
     }
     // at least return default resource manager
+    try {
+      resourceManager = ResourceManagerFactory.newResourceManager();
+      return resourceManager;
+    } catch (ResourceInitializationException e) {
+      UIMAFramework.getLogger(getClass()).log(SEVERE,
+              "Failed to initialize fallback ResourceManager.", e);
+    }
     return UIMAFramework.newDefaultResourceManager();
   }
 

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ExecAction.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ExecAction.java?rev=1849484&r1=1849483&r2=1849484&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ExecAction.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ExecAction.java
 Fri Dec 21 15:06:50 2018
@@ -19,6 +19,8 @@
 
 package org.apache.uima.ruta.action;
 
+import static org.apache.uima.util.Level.SEVERE;
+
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
@@ -67,9 +69,9 @@ public class ExecAction extends CallActi
   }
 
   @Override
-  protected void callEngine(MatchContext context, InferenceCrowd crowd,
-          AnalysisEngine targetEngine, RutaStream stream) throws 
ResourceInitializationException,
-          AnalysisEngineProcessException {
+  protected void callEngine(MatchContext context, InferenceCrowd crowd, 
AnalysisEngine targetEngine,
+          RutaStream stream)
+          throws ResourceInitializationException, 
AnalysisEngineProcessException {
     CAS cas = stream.getCas();
     if (view != null) {
       String viewName = view.getStringValue(context, stream);
@@ -79,13 +81,14 @@ public class ExecAction extends CallActi
         try {
           String sourceUrlString = metaData.getSourceUrlString();
           if (sourceUrlString != null) {
-            AnalysisEngineDescription aed = (AnalysisEngineDescription) 
UIMAFramework
-                    .getXMLParser().parseResourceSpecifier(new 
XMLInputSource(sourceUrlString));
+            AnalysisEngineDescription aed = (AnalysisEngineDescription) 
UIMAFramework.getXMLParser()
+                    .parseResourceSpecifier(new 
XMLInputSource(sourceUrlString));
             AnalysisEngine createEngine = 
AnalysisEngineFactory.createEngine(aed, viewName);
             targetEngine = createEngine;
           }
         } catch (Exception e) {
-          e.printStackTrace();
+          UIMAFramework.getLogger(getClass()).log(SEVERE, "Failed to 
initialize AnalysisEngine.",
+                  e);
         }
       }
     }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java?rev=1849484&r1=1849483&r2=1849484&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java 
(original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java 
Fri Dec 21 15:06:50 2018
@@ -68,11 +68,16 @@ public class Ruta {
   /**
    * This applies the given rule on the given JCas object
    * 
-   * @param jcas - the current document
-   * @param rule - String containing one or more rules in valid ruta syntax
-   * @param configurationData - additional configuration parameter pairs
-   * @throws AnalysisEngineProcessException - problem while processing
-   * @throws ResourceInitializationException - problem while initializing
+   * @param jcas
+   *          - the current document
+   * @param rule
+   *          - String containing one or more rules in valid ruta syntax
+   * @param configurationData
+   *          - additional configuration parameter pairs
+   * @throws AnalysisEngineProcessException
+   *           - problem while processing
+   * @throws ResourceInitializationException
+   *           - problem while initializing
    */
   public static void applyRule(JCas jcas, String rule, Object... 
configurationData)
           throws ResourceInitializationException, 
AnalysisEngineProcessException {
@@ -97,8 +102,8 @@ public class Ruta {
     File scriptFile = File.createTempFile("Ruta", 
RutaEngine.SCRIPT_FILE_EXTENSION);
     scriptFile.deleteOnExit();
     FileUtils.saveString2File(script, scriptFile, "UTF-8");
-    ae.setConfigParameterValue(RutaEngine.PARAM_SCRIPT_PATHS, new String[] { 
scriptFile
-            .getParentFile().getAbsolutePath() });
+    ae.setConfigParameterValue(RutaEngine.PARAM_SCRIPT_PATHS,
+            new String[] { scriptFile.getParentFile().getAbsolutePath() });
     String name = scriptFile.getName().substring(0, 
scriptFile.getName().length() - 5);
     ae.setConfigParameterValue(RutaEngine.PARAM_MAIN_SCRIPT, name);
     if (parameters != null) {
@@ -112,9 +117,9 @@ public class Ruta {
     ae.destroy();
   }
 
-  public static void apply(CAS cas, String script) throws IOException, 
InvalidXMLException,
-          ResourceInitializationException, ResourceConfigurationException,
-          AnalysisEngineProcessException, URISyntaxException {
+  public static void apply(CAS cas, String script)
+          throws IOException, InvalidXMLException, 
ResourceInitializationException,
+          ResourceConfigurationException, AnalysisEngineProcessException, 
URISyntaxException {
     apply(cas, script, null);
   }
 
@@ -126,7 +131,7 @@ public class Ruta {
     }
     return result;
   }
-  
+
   private static int countRuleApplies(JCas jcas) {
     int result = 0;
     Collection<DebugBlockApply> blockApplies = JCasUtil.select(jcas, 
DebugBlockApply.class);
@@ -172,11 +177,12 @@ public class Ruta {
       result.add(dmrm);
     }
   }
-  
+
   /**
    * Removes all debug annotations from the index.
    * 
-   * @param jcas - the current document
+   * @param jcas
+   *          - the current document
    */
   public static void removeDebugInformation(JCas jcas) {
     jcas.removeAllIncludingSubtypes(DebugBlockApply.type);
@@ -192,17 +198,17 @@ public class Ruta {
     StringBuilder sb = new StringBuilder();
     int start = 0;
     int counter = 0;
-    while(matcher.find(start)) {
+    while (matcher.find(start)) {
       String group = matcher.group();
       sb.append(script.substring(start, matcher.start()));
-      if(counter < addresses.length) {
+      if (counter < addresses.length) {
         sb.append("$" + addresses[0]);
       } else {
         sb.append("$" + group);
       }
       start = matcher.end();
     }
-    if(start <script.length()) {
+    if (start < script.length()) {
       sb.append(script.substring(start, script.length()));
     }
     return sb.toString();
@@ -211,14 +217,12 @@ public class Ruta {
   public static String inject(String script, FeatureStructureImpl... 
annotations) {
     return inject(script, "$", getAddresses(annotations));
   }
-  
-
 
   private static int[] getAddresses(FeatureStructureImpl[] annotations) {
     int[] result = new int[annotations.length];
     for (int i = 0; i < annotations.length; i++) {
       result[i] = annotations[i].getAddress();
-      
+
     }
     return result;
   }
@@ -226,12 +230,17 @@ public class Ruta {
   /**
    * This method returns the spans of successful rule applies.
    * 
-   * @param jcas - the current document
-   * @param rule - String containing one or more rules in valid ruta syntax
-   * @param configurationData - additional configuration parameter pairs
+   * @param jcas
+   *          - the current document
+   * @param rule
+   *          - String containing one or more rules in valid ruta syntax
+   * @param configurationData
+   *          - additional configuration parameter pairs
    * @return list of successful rule matches
-   * @throws AnalysisEngineProcessException - problem while processing
-   * @throws ResourceInitializationException - problem while initializing
+   * @throws AnalysisEngineProcessException
+   *           - problem while processing
+   * @throws ResourceInitializationException
+   *           - problem while initializing
    */
   public static List<Annotation> select(JCas jcas, String rule, Object... 
configurationData)
           throws AnalysisEngineProcessException, 
ResourceInitializationException {
@@ -243,16 +252,20 @@ public class Ruta {
     return ruleMatches;
   }
 
-
   /**
    * This method returns true if the rule (or one of the rules) was able to 
match.
    * 
-   * @param jcas - the current document
-   * @param rule - String containing one or more rules in valid ruta syntax
-   * @param configurationData - additional configuration parameter pairs
+   * @param jcas
+   *          - the current document
+   * @param rule
+   *          - String containing one or more rules in valid ruta syntax
+   * @param configurationData
+   *          - additional configuration parameter pairs
    * @return true if matched, false otherwise
-   * @throws AnalysisEngineProcessException - problem while processing
-   * @throws ResourceInitializationException - problem while initializing
+   * @throws AnalysisEngineProcessException
+   *           - problem while processing
+   * @throws ResourceInitializationException
+   *           - problem while initializing
    */
   public static boolean matches(JCas jcas, String rule, Object... 
configurationData)
           throws AnalysisEngineProcessException, 
ResourceInitializationException {
@@ -262,17 +275,18 @@ public class Ruta {
     removeDebugInformation(jcas);
     return applies > 0;
   }
-  
 
-  public static AnalysisEngine wrapAnalysisEngine(URL descriptorUrl, String 
viewName, ResourceManager resourceManager)
-          throws ResourceInitializationException, 
ResourceConfigurationException,
-          InvalidXMLException, IOException, URISyntaxException {
-    return wrapAnalysisEngine(descriptorUrl, viewName, false, 
Charset.defaultCharset().name(), resourceManager);
-  }
-  
   public static AnalysisEngine wrapAnalysisEngine(URL descriptorUrl, String 
viewName,
-          boolean rutaEngine, String encoding, ResourceManager 
resourceManager) throws ResourceInitializationException,
+          ResourceManager resourceManager) throws 
ResourceInitializationException,
           ResourceConfigurationException, InvalidXMLException, IOException, 
URISyntaxException {
+    return wrapAnalysisEngine(descriptorUrl, viewName, false, 
Charset.defaultCharset().name(),
+            resourceManager);
+  }
+
+  public static AnalysisEngine wrapAnalysisEngine(URL descriptorUrl, String 
viewName,
+          boolean rutaEngine, String encoding, ResourceManager resourceManager)
+          throws ResourceInitializationException, 
ResourceConfigurationException,
+          InvalidXMLException, IOException, URISyntaxException {
     if (viewName.equals(CAS.NAME_DEFAULT_SOFA)) {
       XMLInputSource in = new XMLInputSource(descriptorUrl);
       ResourceSpecifier specifier = 
UIMAFramework.getXMLParser().parseResourceSpecifier(in);
@@ -293,7 +307,7 @@ public class Ruta {
       FileUtils.saveString2File(aaedString, tempFile);
       XMLInputSource in = new XMLInputSource(tempFile);
       ResourceSpecifier specifier = 
UIMAFramework.getXMLParser().parseResourceSpecifier(in);
-      AnalysisEngine ae = 
UIMAFramework.produceAnalysisEngine(specifier,resourceManager, null);
+      AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier, 
resourceManager, null);
       tempFile.delete();
       return ae;
     }
@@ -307,8 +321,8 @@ public class Ruta {
 
   @Deprecated
   public static AnalysisEngineDescription 
createAnalysisEngineDescription(String script,
-          TypeSystemDescription... tsds) throws IOException, 
InvalidXMLException,
-          ResourceInitializationException {
+          TypeSystemDescription... tsds)
+          throws IOException, InvalidXMLException, 
ResourceInitializationException {
     File scriptFile = File.createTempFile("Ruta", 
RutaEngine.SCRIPT_FILE_EXTENSION);
     scriptFile.deleteOnExit();
     if (!script.startsWith("PACKAGE")) {
@@ -321,8 +335,8 @@ public class Ruta {
             .parseResourceSpecifier(in);
     AnalysisEngineMetaData metaData = aed.getAnalysisEngineMetaData();
     ConfigurationParameterSettings settings = 
metaData.getConfigurationParameterSettings();
-    settings.setParameterValue(RutaEngine.PARAM_SCRIPT_PATHS, new String[] { 
scriptFile
-            .getParentFile().getAbsolutePath() });
+    settings.setParameterValue(RutaEngine.PARAM_SCRIPT_PATHS,
+            new String[] { scriptFile.getParentFile().getAbsolutePath() });
     String name = scriptFile.getName().substring(0, 
scriptFile.getName().length() - 5);
     settings.setParameterValue(RutaEngine.PARAM_MAIN_SCRIPT, name);
     if (tsds != null) {

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java?rev=1849484&r1=1849483&r2=1849484&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
 Fri Dec 21 15:06:50 2018
@@ -56,6 +56,7 @@ import org.apache.uima.cas.text.Annotati
 import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
 import org.apache.uima.fit.descriptor.ConfigurationParameter;
 import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
@@ -623,7 +624,7 @@ public class RutaEngine extends JCasAnno
     }
   }
 
-  private void initializeResourceManager() {
+  private void initializeResourceManager() throws 
ResourceInitializationException {
     if (context instanceof UimaContextAdmin) {
       UimaContextAdmin uca = (UimaContextAdmin) context;
       ResourceManager rm = uca.getResourceManager();
@@ -632,7 +633,7 @@ public class RutaEngine extends JCasAnno
       }
     }
     if (resourceManager == null) {
-      resourceManager = UIMAFramework.newDefaultResourceManager();
+      resourceManager = ResourceManagerFactory.newResourceManager();
     }
   }
 

Added: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/DummyAnnotator.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/DummyAnnotator.java?rev=1849484&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/DummyAnnotator.java
 (added)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/DummyAnnotator.java
 Fri Dec 21 15:06:50 2018
@@ -0,0 +1,32 @@
+/*
+ * 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.ruta.engine;
+
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
+import org.apache.uima.jcas.JCas;
+
+public class DummyAnnotator extends JCasAnnotator_ImplBase {
+
+  @Override
+  public void process(JCas jcas) throws AnalysisEngineProcessException {
+
+  }
+
+}

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java?rev=1849484&r1=1849483&r2=1849484&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
 Fri Dec 21 15:06:50 2018
@@ -30,7 +30,11 @@ import org.apache.uima.analysis_engine.A
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.internal.ResourceManagerFactory;
+import 
org.apache.uima.fit.internal.ResourceManagerFactory.ResourceManagerCreator;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.ResourceManager;
 import org.apache.uima.ruta.RutaProcessRuntimeException;
 import org.apache.uima.ruta.TypeUsageInformation;
 import org.junit.Assert;
@@ -151,4 +155,28 @@ public class RutaEngineTest {
     Assert.fail("expected RutaProcessRuntimeException");
   }
 
+  @Test
+  public void testResourceManagerWithUimaFITInEXEC() throws Throwable {
+
+    final ResourceManager resourceManager = 
ResourceManagerFactory.newResourceManager();
+    ResourceManagerFactory.setResourceManagerCreator(new 
ResourceManagerCreator() {
+
+      @Override
+      public ResourceManager newResourceManager() throws 
ResourceInitializationException {
+        return resourceManager;
+      }
+    });
+
+    String document = "This is some text.";
+    String script = "UIMAFIT org.apache.uima.ruta.engine.DummyAnnotator;";
+    script += "EXEC(DummyAnnotator);";
+
+    AnalysisEngine ae = AnalysisEngineFactory.createEngine(RutaEngine.class, 
RutaEngine.PARAM_RULES,
+            script, RutaEngine.PARAM_ADDITIONAL_UIMAFIT_ENGINES,
+            new String[] { DummyAnnotator.class.getName() });
+
+    CAS cas = RutaTestUtils.getCAS(document);
+    ae.process(cas);
+  }
+
 }


Reply via email to