Author: pkluegl
Date: Fri Jun 17 09:43:55 2016
New Revision: 1748825

URL: http://svn.apache.org/viewvc?rev=1748825&view=rev
Log:
UIMA-4983
- fixed modification of offsets
- added test

Modified:
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ShiftAction.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/ImplicitActionTest.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=1748825&r1=1748824&r2=1748825&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 
Fri Jun 17 09:43:55 2016
@@ -63,7 +63,6 @@ import org.apache.uima.ruta.expression.a
 import org.apache.uima.ruta.expression.bool.IBooleanExpression;
 import org.apache.uima.ruta.expression.bool.IBooleanListExpression;
 import org.apache.uima.ruta.expression.feature.FeatureExpression;
-import org.apache.uima.ruta.expression.feature.FeatureMatchExpression;
 import org.apache.uima.ruta.expression.feature.GenericFeatureExpression;
 import org.apache.uima.ruta.expression.feature.SimpleFeatureExpression;
 import org.apache.uima.ruta.expression.number.INumberExpression;
@@ -182,25 +181,25 @@ public class RutaStream extends FSIterat
 
   public void initalizeBasics(String[] reindexOnly) {
     AnnotationIndex<AnnotationFS> basicIndex = 
cas.getAnnotationIndex(basicType);
-    
+
     final List<AnnotationFS> allAnnotations = new LinkedList<AnnotationFS>();
     for (String eachTypeName : reindexOnly) {
-        Type type = cas.getTypeSystem().getType(eachTypeName);
-        if(type != null) {
-          AnnotationIndex<AnnotationFS> annotationIndex = null;
-          if(StringUtils.equals(eachTypeName, CAS.TYPE_NAME_ANNOTATION)) {
-            annotationIndex = cas.getAnnotationIndex();
-          } else {
-            annotationIndex = cas.getAnnotationIndex(type);
-          }
-          for (AnnotationFS a : annotationIndex) {
-            if (a.getBegin() != a.getEnd()) {
-              allAnnotations.add(a);
-            }
+      Type type = cas.getTypeSystem().getType(eachTypeName);
+      if (type != null) {
+        AnnotationIndex<AnnotationFS> annotationIndex = null;
+        if (StringUtils.equals(eachTypeName, CAS.TYPE_NAME_ANNOTATION)) {
+          annotationIndex = cas.getAnnotationIndex();
+        } else {
+          annotationIndex = cas.getAnnotationIndex(type);
+        }
+        for (AnnotationFS a : annotationIndex) {
+          if (a.getBegin() != a.getEnd()) {
+            allAnnotations.add(a);
           }
         }
+      }
     }
-    
+
     if (basicIndex.size() == 0) {
       TreeSet<Integer> anchors = new TreeSet<Integer>();
       for (AnnotationFS a : allAnnotations) {
@@ -590,8 +589,8 @@ public class RutaStream extends FSIterat
             && windowAnnotation.getBegin() == 0) {
       subSet = beginAnchors.values();
     } else {
-      subSet = beginAnchors.subMap(windowAnnotation.getBegin(), true, 
windowAnnotation.getEnd(),
-              false).values();
+      subSet = beginAnchors
+              .subMap(windowAnnotation.getBegin(), true, 
windowAnnotation.getEnd(), false).values();
     }
     return subSet;
   }
@@ -648,8 +647,8 @@ public class RutaStream extends FSIterat
       return result;
     }
     FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
-    FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(cas
-            .getAnnotationIndex(basicType).subiterator(windowAnnotation), 
defaultConstraint);
+    FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(
+            cas.getAnnotationIndex(basicType).subiterator(windowAnnotation), 
defaultConstraint);
 
     while (iterator.isValid()) {
       result.add((RutaBasic) iterator.get());
@@ -662,7 +661,8 @@ public class RutaStream extends FSIterat
     return getFirstBasicInWindow(windowAnnotation, currentIt);
   }
 
-  public RutaBasic getFirstBasicInWindow(AnnotationFS windowAnnotation, 
FSIterator<AnnotationFS> it) {
+  public RutaBasic getFirstBasicInWindow(AnnotationFS windowAnnotation,
+          FSIterator<AnnotationFS> it) {
     if (windowAnnotation instanceof RutaBasic) {
       return (RutaBasic) windowAnnotation;
     }
@@ -695,8 +695,7 @@ public class RutaStream extends FSIterat
       if (annotation instanceof RutaAnnotation) {
         RutaAnnotation tma = (RutaAnnotation) annotation;
         if (tma.getBegin() == heuristicAnnotation.getBegin()
-                && tma.getEnd() == heuristicAnnotation.getEnd()
-                && tma.getAnnotation().getType()
+                && tma.getEnd() == heuristicAnnotation.getEnd() && 
tma.getAnnotation().getType()
                         
.equals(heuristicAnnotation.getAnnotation().getType())) {
           return tma;
         }
@@ -758,8 +757,8 @@ public class RutaStream extends FSIterat
   }
 
   public RutaBasic getNextBasic2(AnnotationFS previous) {
-    AnnotationFS pointer = cas
-            .createAnnotation(basicType, previous.getEnd() - 1, 
previous.getEnd());
+    AnnotationFS pointer = cas.createAnnotation(basicType, previous.getEnd() - 
1,
+            previous.getEnd());
     currentIt.moveTo(pointer);
     if (currentIt.isValid()) {
       RutaBasic basic = (RutaBasic) currentIt.get();
@@ -853,10 +852,9 @@ public class RutaStream extends FSIterat
     }
 
     AnnotationFS windowAnnotation = filter.getWindowAnnotation();
-    if (!ignoreWindow
-            && windowAnnotation != null
-            && (annotationFS.getBegin() < windowAnnotation.getBegin() || 
annotationFS.getEnd() > windowAnnotation
-                    .getEnd())) {
+    if (!ignoreWindow && windowAnnotation != null
+            && (annotationFS.getBegin() < windowAnnotation.getBegin()
+                    || annotationFS.getEnd() > windowAnnotation.getEnd())) {
       return false;
     }
     int begin = annotationFS.getBegin();
@@ -903,8 +901,8 @@ public class RutaStream extends FSIterat
     Collection<AnnotationFS> result = new LinkedList<AnnotationFS>();
     AnnotationFS windowAnnotation = filter.getWindowAnnotation();
     if (windowAnnotation != null
-            && (windowAnnotation.getBegin() != 
cas.getDocumentAnnotation().getBegin() || windowAnnotation
-                    .getEnd() != cas.getDocumentAnnotation().getEnd())) {
+            && (windowAnnotation.getBegin() != 
cas.getDocumentAnnotation().getBegin()
+                    || windowAnnotation.getEnd() != 
cas.getDocumentAnnotation().getEnd())) {
       AnnotationFS frame = 
cas.createAnnotation(cas.getTypeSystem().getType(RutaEngine.FRAME_TYPE),
               windowAnnotation.getBegin(), windowAnnotation.getEnd());
       FSIterator<AnnotationFS> subiterator = 
cas.getAnnotationIndex(type).subiterator(frame);
@@ -979,7 +977,13 @@ public class RutaStream extends FSIterat
       if (value instanceof INumberExpression) {
         INumberExpression numberExpr = (INumberExpression) value;
         int v = numberExpr.getIntegerValue(context, this);
-        annotation.setIntValue(feature, v);
+        if (StringUtils.equals(feature.getShortName(), 
CAS.FEATURE_BASE_NAME_BEGIN)) {
+          changeBegin(annotation, v, context.getRuleMatch());
+        } else if(StringUtils.equals(feature.getShortName(), 
CAS.FEATURE_BASE_NAME_END)) {
+          changeEnd(annotation, v, context.getRuleMatch());
+        } else {
+          annotation.setIntValue(feature, v);
+        }
       }
     } else if (range.equals(UIMAConstants.TYPE_INTARRAY)) {
       if (value instanceof INumberExpression) {
@@ -1061,14 +1065,14 @@ public class RutaStream extends FSIterat
     } else if (value instanceof IAnnotationExpression && 
!feature.getRange().isPrimitive()) {
       IAnnotationExpression ae = (IAnnotationExpression) value;
       AnnotationFS a = ae.getAnnotation(context, this);
-        // 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);
-        }
+      // 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 IAnnotationListExpression && 
!feature.getRange().isPrimitive()) {
       IAnnotationListExpression ale = (IAnnotationListExpression) value;
       List<AnnotationFS> annotations = ale.getAnnotationList(context, this);
@@ -1097,8 +1101,8 @@ public class RutaStream extends FSIterat
         SimpleFeatureExpression sfe = (SimpleFeatureExpression) fe;
         List<AnnotationFS> featureAnnotations = inWindow;
         if (fe.getFeatures(context, this) != null) {
-          featureAnnotations = new 
ArrayList<AnnotationFS>(sfe.getFeatureAnnotations(inWindow,
-                  this, context, false));
+          featureAnnotations = new ArrayList<AnnotationFS>(
+                  sfe.getFeatureAnnotations(inWindow, this, context, false));
         }
         if (feature.getRange().isArray()) {
           annotation.setFeatureValue(feature,
@@ -1190,15 +1194,36 @@ public class RutaStream extends FSIterat
     } else {
       Collection<AnnotationFS> beginAnchors = getBeginAnchor(window.getBegin())
               .getBeginAnchors(type);
-      Collection<AnnotationFS> endAnchors = 
getEndAnchor(window.getEnd()).getEndAnchors(
-              type);
+      Collection<AnnotationFS> endAnchors = 
getEndAnchor(window.getEnd()).getEndAnchors(type);
       @SuppressWarnings("unchecked")
-      Collection<AnnotationFS> intersection = CollectionUtils
-              .intersection(beginAnchors, endAnchors);
+      Collection<AnnotationFS> intersection = 
CollectionUtils.intersection(beginAnchors,
+              endAnchors);
       result.addAll(intersection);
     }
     return result;
   }
+
+  public void changeOffsets(AnnotationFS annotation, int begin, int end, 
AbstractRuleMatch<? extends AbstractRule> modifikator) {
+    if(!(annotation instanceof Annotation)) {
+      return;
+    }
+    Annotation a = (Annotation) annotation;
+    if(annotation.getBegin() == begin && annotation.getEnd() == end) {
+      return;
+    }
+    // TODO implement incremental reindexing
+    removeAnnotation(a);
+    a.setBegin(begin);
+    a.setEnd(end);
+    addAnnotation(a, true, modifikator);
+  }
   
+  public void changeBegin(AnnotationFS annotation, int begin, 
AbstractRuleMatch<? extends AbstractRule> modifikator) {
+    changeOffsets(annotation, begin, annotation.getEnd(), modifikator);
+  }
+  
+  public void changeEnd(AnnotationFS annotation, int end, AbstractRuleMatch<? 
extends AbstractRule> modifikator) {
+    changeOffsets(annotation, annotation.getBegin(), end, modifikator);
+  }
 
 }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java?rev=1748825&r1=1748824&r2=1748825&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java 
(original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java 
Fri Jun 17 09:43:55 2016
@@ -55,5 +55,4 @@ public class UIMAConstants {
 
   public static final String FEATURE_COVERED_TEXT_SHORT = "ct";
   
-
 }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ShiftAction.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ShiftAction.java?rev=1748825&r1=1748824&r2=1748825&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ShiftAction.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ShiftAction.java
 Fri Jun 17 09:43:55 2016
@@ -87,10 +87,7 @@ public class ShiftAction extends MarkAct
       for (AnnotationFS eachAnchored : allAnchoredAnnotations) {
         if (eachAnchored.getBegin() >= windowBegin && eachAnchored.getEnd() <= 
windowEnd) {
           Annotation a = (Annotation) eachAnchored;
-          stream.removeAnnotation(a);
-          a.setBegin(eachDestination.getBegin());
-          a.setEnd(eachDestination.getEnd());
-          stream.addAnnotation(a, true, match);
+          stream.changeOffsets(a, eachDestination.getBegin(), 
eachDestination.getEnd(), match);
         }
       }
     }

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/ImplicitActionTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/ImplicitActionTest.java?rev=1748825&r1=1748824&r2=1748825&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/ImplicitActionTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/ImplicitActionTest.java
 Fri Jun 17 09:43:55 2016
@@ -19,17 +19,24 @@
 
 package org.apache.uima.ruta.action;
 
+import java.io.IOException;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
 import org.apache.uima.cas.CAS;
+import org.apache.uima.resource.ResourceConfigurationException;
+import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.ruta.FeatureMatch1Test;
+import org.apache.uima.ruta.engine.Ruta;
 import org.apache.uima.ruta.engine.RutaEngine;
 import org.apache.uima.ruta.engine.RutaTestUtils;
 import org.apache.uima.ruta.engine.RutaTestUtils.TestFeature;
+import org.apache.uima.util.InvalidXMLException;
 import org.junit.Test;
 
 public class ImplicitActionTest {
@@ -85,4 +92,21 @@ public class ImplicitActionTest {
     
     cas.release();
   }
+  
+  
+  @Test
+  public void testChangeOffsets() throws ResourceInitializationException, 
InvalidXMLException, IOException, AnalysisEngineProcessException, 
ResourceConfigurationException, URISyntaxException {
+    String text = "text 2 3 x 4 1";
+    String script = "";
+    script += "NUM{->T1};";
+    script += "first:T1{-> UNMARK(T1)} T1{-> T1.begin = first.begin};";
+    script += "T1{-> UNMARK(T1)} SW T1{->T2};";
+    CAS cas = RutaTestUtils.getCAS(text);
+    Ruta.apply(cas, script);
+    RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "4 1");
+    cas.release();
+  }
+  
+  
+  
 }


Reply via email to