You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2016/01/04 15:20:47 UTC

svn commit: r1722880 - in /uima/ruta/trunk/ruta-core/src: main/java/org/apache/uima/ruta/RutaStream.java test/java/org/apache/uima/ruta/expression/annotation/AnnotationAddressExpressionTest.java

Author: pkluegl
Date: Mon Jan  4 14:20:47 2016
New Revision: 1722880

URL: http://svn.apache.org/viewvc?rev=1722880&view=rev
Log:
UIMA-4683
- assign feature values by address
- added test

Modified:
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationAddressExpressionTest.java

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java?rev=1722880&r1=1722879&r2=1722880&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java Mon Jan  4 14:20:47 2016
@@ -48,6 +48,7 @@ import org.apache.uima.jcas.tcas.Annotat
 import org.apache.uima.ruta.engine.RutaEngine;
 import org.apache.uima.ruta.expression.AnnotationTypeExpression;
 import org.apache.uima.ruta.expression.IRutaExpression;
+import org.apache.uima.ruta.expression.annotation.IAnnotationExpression;
 import org.apache.uima.ruta.expression.bool.IBooleanExpression;
 import org.apache.uima.ruta.expression.feature.FeatureExpression;
 import org.apache.uima.ruta.expression.feature.GenericFeatureExpression;
@@ -970,6 +971,19 @@ public class RutaStream extends FSIterat
         Type t = ate.getType(context, this);
         assignAnnotationByTypeInWindow(annotation, feature, context, t);
       }
+    } else if (value instanceof IAnnotationExpression && !feature.getRange().isPrimitive()) {
+      IAnnotationExpression ae = (IAnnotationExpression) value;
+      AnnotationFS a = ae.getAnnotation(context, this);
+      if (a != null) {
+        // TODO support annotation list expressions
+        if (feature.getRange().isArray()) {
+          List<AnnotationFS> c = new ArrayList<AnnotationFS>();
+          c.add(a);
+          annotation.setFeatureValue(feature, UIMAUtils.toFSArray(this.getJCas(), c));
+        } else {
+          annotation.setFeatureValue(feature, a);
+        }
+      }
     } else if (value instanceof ITypeExpression && !feature.getRange().isPrimitive()) {
       ITypeExpression typeExpr = (ITypeExpression) value;
       Type t = typeExpr.getType(context, this);

Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationAddressExpressionTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationAddressExpressionTest.java?rev=1722880&r1=1722879&r2=1722880&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationAddressExpressionTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationAddressExpressionTest.java Mon Jan  4 14:20:47 2016
@@ -19,18 +19,30 @@
 
 package org.apache.uima.ruta.expression.annotation;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
 import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Feature;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.impl.AnnotationImpl;
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
 import org.apache.uima.ruta.engine.Ruta;
 import org.apache.uima.ruta.engine.RutaTestUtils;
+import org.apache.uima.ruta.engine.RutaTestUtils.TestFeature;
 import org.junit.Test;
 
 public class AnnotationAddressExpressionTest {
 
   @Test
-  public void test() {
+  public void testMatching() {
     String document = "Some text.";
 
     CAS cas = null;
@@ -66,4 +78,64 @@ public class AnnotationAddressExpression
 
   }
 
+  @Test
+  public void testFeatureAssignment() {
+    String document = "Some text.";
+
+    Map<String, String> typeMap = new TreeMap<String, String>();
+    String typeName = "Struct";
+    typeMap.put(typeName, "uima.tcas.Annotation");
+
+    Map<String, List<TestFeature>> featureMap = new TreeMap<String, List<TestFeature>>();
+    List<TestFeature> list = new ArrayList<RutaTestUtils.TestFeature>();
+    featureMap.put(typeName, list);
+    String fn = "a";
+    list.add(new TestFeature(fn, "", "uima.tcas.Annotation"));
+    
+    
+    CAS cas = null;
+    try {
+      cas = RutaTestUtils.getCAS(document, typeMap, featureMap);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    Type t1 = RutaTestUtils.getTestType(cas, 1);
+    AnnotationFS a1 = cas.createAnnotation(t1, 5, 9);
+    cas.addFsToIndexes(a1);
+    int ref = 0;
+    if (a1 instanceof AnnotationImpl) {
+      AnnotationImpl aImpl = (AnnotationImpl) a1;
+      ref = aImpl.getAddress();
+    }
+
+    String script = "";
+    script += "CREATE(Struct, \"a\" = $"+ref+");";
+    try {
+      Ruta.apply(cas, script);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+
+    Type t = null;
+    AnnotationIndex<AnnotationFS> ai = null;
+    FSIterator<AnnotationFS> iterator = null;
+
+    t = cas.getTypeSystem().getType(typeName);
+    Feature f1 = t.getFeatureByBaseName(fn);
+    ai = cas.getAnnotationIndex(t);
+
+    assertEquals(1, ai.size());
+    iterator = ai.iterator();
+    AnnotationFS next = iterator.next();
+    assertEquals("Some text.", next.getCoveredText());
+    AnnotationFS a = (AnnotationFS) next.getFeatureValue(f1);
+    assertNotNull("Feature value is null!", a);
+    assertEquals("text", a.getCoveredText());
+
+    if (cas != null) {
+      cas.release();
+    }
+
+  }
+  
 }