Author: pkluegl
Date: Wed May 22 10:38:43 2019
New Revision: 1859714

URL: http://svn.apache.org/viewvc?rev=1859714&view=rev
Log:
UIMA-6051: fixing complete name in types declared in anonymous scripts

Modified:
    
uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/descriptor/GenerateDescriptorTest.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java

Modified: 
uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g?rev=1859714&r1=1859713&r2=1859714&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
 Wed May 22 10:38:43 2019
@@ -242,44 +242,57 @@ public void setExternalFactory(RutaExter
        
        public void addType(RutaBlock parent, Token nameToken, Token 
parentTypeToken, List featureTypes,
           List featureNames) {
-          String name = nameToken.getText();
-          String parentType = "uima.tcas.Annotation";
-          if(parentTypeToken != null) {
-               parentType = parentTypeToken.getText();
+          
+      
+          
+      String name = nameToken.getText();
+      String parentType = "uima.tcas.Annotation";
+      if(parentTypeToken != null) {
+        parentType = parentTypeToken.getText();
          }
+         
          String resolvedType = name;
+         
          if (!name.contains(".")) {
-           if(StringUtils.isBlank(moduleName)) {
-             resolvedType = namespace + "." + name;
+           List<String> typeNameParts = new ArrayList<>();
+           
+           if(parent != null && !StringUtils.isBlank(parent.getNamespace())) {
+             typeNameParts.add(parent.getNamespace());
            } else {
-             resolvedType = namespace + "." + moduleName + "." + name;
-           }
+             if(!StringUtils.isBlank(namespace)) {
+            typeNameParts.add(namespace);
+             }
+             if(!StringUtils.isBlank(moduleName)) {
+            typeNameParts.add(moduleName);
+          }
+        }
+           typeNameParts.add(name);
+           resolvedType = StringUtils.join(typeNameParts, ".");
          }
-          parent.getEnvironment().declareType(resolvedType);
-         if(descInfo != null) {
-                 name = parent.getNamespace() + "." + name.trim();
-                 String descriptionString = null;
-                 if(StringUtils.isBlank(namespace)) {
-                       if(StringUtils.isBlank(moduleName)) {
-                               descriptionString = "Type defined in unknown 
script.";
-                       } else {
-                               descriptionString = "Type defined in " + 
moduleName;
-                       }
-                         
-                         } else {
-                         descriptionString = "Type defined in " + 
parent.getNamespace();
-                 }
-                 descInfo.addType(name, descriptionString, parentType);
-                 if(featureTypes != null && featureNames != null) {
-                         for (int i = 0; i < featureTypes.size(); i++) {
-                                 Object object = featureTypes.get(i);
-                                 String ftype = (String) featureTypes.get(i);
-                                 String fname = (String) featureNames.get(i);
-                                 descInfo.addFeature(name, fname, fname, 
ftype);
-                         }
-                 }
+      parent.getEnvironment().declareType(resolvedType);
+      
+      if(descInfo != null) {
+        String descriptionString = null;
+        if(StringUtils.isBlank(namespace)) {
+          if(StringUtils.isBlank(moduleName)) {
+            descriptionString = "Type defined in unknown script.";
+             } else {
+            descriptionString = "Type defined in " + moduleName;
+          }
+               } else {
+          descriptionString = "Type defined in " + parent.getNamespace();
+        }
+        descInfo.addType(resolvedType, descriptionString, parentType);
+        if(featureTypes != null && featureNames != null) {
+          for (int i = 0; i < featureTypes.size(); i++) {
+            Object object = featureTypes.get(i);
+            String ftype = (String) featureTypes.get(i);
+            String fname = (String) featureNames.get(i);
+            descInfo.addFeature(resolvedType, fname, fname, ftype);
+          }
+               }
          }
-        }
+       }
        
        public boolean isType(RutaBlock parent, String type) {
                return parent.getEnvironment().getType(type) != null || 
type.equals("Document");

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java?rev=1859714&r1=1859713&r2=1859714&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java
 Wed May 22 10:38:43 2019
@@ -399,8 +399,6 @@ public class RutaDescriptorBuilder {
     if (name.indexOf(".") == -1) {
       if (types.containsKey(name)) {
         name = types.get(name);
-      } else {
-        name = packageName + "." + name;
       }
     }
     int lastIndexOf = name.lastIndexOf(".");

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/descriptor/GenerateDescriptorTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/descriptor/GenerateDescriptorTest.java?rev=1859714&r1=1859713&r2=1859714&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/descriptor/GenerateDescriptorTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/descriptor/GenerateDescriptorTest.java
 Wed May 22 10:38:43 2019
@@ -180,7 +180,7 @@ public class GenerateDescriptorTest {
   }
 
   @Test
-  public void testRuleScriptName() throws Exception {
+  public void testTypeWithRuleScriptNameWithPackage() throws Exception {
 
     String script = "";
     script += "PACKAGE test.package;\n";
@@ -191,7 +191,7 @@ public class GenerateDescriptorTest {
     RutaBuildOptions options = new RutaBuildOptions();
     RutaDescriptorInformation descriptorInformation = 
rdf.parseDescriptorInformation(script, null,
             options);
-    String typeSystemOutput = "target/temp/testRuleScriptName_TypeSystem.xml";
+    String typeSystemOutput = 
"target/temp/testTypeWithRuleScriptNameWithPackage_TypeSystem.xml";
     ClassLoader classLoader = GenerateDescriptorTest.class.getClassLoader();
     TypeSystemDescription tsd = 
rdf.createTypeSystemDescription(typeSystemOutput,
             descriptorInformation, options, classLoader);
@@ -202,6 +202,29 @@ public class GenerateDescriptorTest {
     Assert.assertNotNull(tagType);
 
   }
+
+  @Test
+  public void testTypeWithRuleScriptNameWithoutPackage() throws Exception {
+
+    String script = "";
+    script += "DECLARE SimpleType;\n";
+
+    RutaDescriptorFactory rdf = new 
RutaDescriptorFactory(GenerateDescriptorTest.basicTSUrl,
+            GenerateDescriptorTest.basicAEUrl);
+    RutaBuildOptions options = new RutaBuildOptions();
+    RutaDescriptorInformation descriptorInformation = 
rdf.parseDescriptorInformation(script, null,
+            options);
+    String typeSystemOutput = 
"target/temp/testTypeWithRuleScriptNameWithoutPackage_TypeSystem.xml";
+    ClassLoader classLoader = GenerateDescriptorTest.class.getClassLoader();
+    TypeSystemDescription tsd = 
rdf.createTypeSystemDescription(typeSystemOutput,
+            descriptorInformation, options, classLoader);
+    ResourceManager rm = new ResourceManager_impl(classLoader);
+    tsd.resolveImports(rm);
+
+    TypeDescription tagType = tsd.getType("SimpleType");
+    Assert.assertNotNull(tagType);
+
+  }
 
   @Test
   public void testScriptOnly() throws Exception {

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=1859714&r1=1859713&r2=1859714&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
 Wed May 22 10:38:43 2019
@@ -26,17 +26,27 @@ import java.util.List;
 
 import org.apache.commons.lang3.NotImplementedException;
 import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.commons.lang3.tuple.Pair;
+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.Type;
+import org.apache.uima.cas.text.AnnotationFS;
 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.fit.util.CasUtil;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.ruta.RutaProcessRuntimeException;
 import org.apache.uima.ruta.TypeUsageInformation;
+import org.apache.uima.ruta.descriptor.RutaBuildOptions;
+import org.apache.uima.ruta.descriptor.RutaDescriptorFactory;
+import org.apache.uima.ruta.descriptor.RutaDescriptorInformation;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -179,4 +189,56 @@ public class RutaEngineTest {
     ae.process(cas);
   }
 
+  @Test
+  public void testProcessWithRulesDeclareWithoutPackage() throws Throwable {
+
+    String rules = "DECLARE MyType; SW{-> MyType};";
+    String text = "This is a test";
+
+    RutaDescriptorFactory rdf = new RutaDescriptorFactory();
+    RutaBuildOptions buildOptions = new RutaBuildOptions();
+    RutaDescriptorInformation descriptorInformation = 
rdf.parseDescriptorInformation(rules, "",
+            buildOptions);
+    Pair<AnalysisEngineDescription, TypeSystemDescription> descriptions = rdf
+            .createDescriptions(null, null, descriptorInformation, 
buildOptions, null, null, null);
+
+    AnalysisEngine ae = 
UIMAFramework.produceAnalysisEngine(descriptions.getLeft());
+    CAS cas = ae.newCAS();
+    cas.setDocumentText(text);
+
+    ae.process(cas);
+
+    Type type = cas.getTypeSystem().getType("MyType");
+    Assert.assertNotNull(type);
+    Collection<AnnotationFS> select = CasUtil.select(cas, type);
+    Assert.assertEquals(3, select.size());
+
+  }
+
+  @Test
+  public void testProcessWithRulesDeclareWithPackage() throws Throwable {
+
+    String rules = "PACKAGE pack; DECLARE MyType; SW{-> MyType};";
+    String text = "This is a test";
+
+    RutaDescriptorFactory rdf = new RutaDescriptorFactory();
+    RutaBuildOptions buildOptions = new RutaBuildOptions();
+    RutaDescriptorInformation descriptorInformation = 
rdf.parseDescriptorInformation(rules, "",
+            buildOptions);
+    Pair<AnalysisEngineDescription, TypeSystemDescription> descriptions = rdf
+            .createDescriptions(null, null, descriptorInformation, 
buildOptions, null, null, null);
+
+    AnalysisEngine ae = 
UIMAFramework.produceAnalysisEngine(descriptions.getLeft());
+    CAS cas = ae.newCAS();
+    cas.setDocumentText(text);
+
+    ae.process(cas);
+
+    Type type = cas.getTypeSystem().getType("pack.MyType");
+    Assert.assertNotNull(type);
+    Collection<AnnotationFS> select = CasUtil.select(cas, type);
+    Assert.assertEquals(3, select.size());
+
+  }
+
 }


Reply via email to