Author: pkluegl
Date: Fri Jul 27 07:27:56 2018
New Revision: 1836780

URL: http://svn.apache.org/viewvc?rev=1836780&view=rev
Log:
UIMA-5809 - applied patch

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/action/MarkAction.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/ScoreCondition.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/SplitTest.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=1836780&r1=1836779&r2=1836780&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 Jul 27 07:27:56 2018
@@ -53,6 +53,7 @@ import org.apache.uima.cas.TypeSystem;
 import org.apache.uima.cas.impl.FSIteratorImplBase;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.fit.util.CasUtil;
 import org.apache.uima.fit.util.FSCollectionFactory;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.jcas.tcas.Annotation;
@@ -596,51 +597,17 @@ public class RutaStream extends FSIterat
     return result;
   }
 
-  private List<AnnotationFS> getAnnotationsInWindow2(AnnotationFS 
windowAnnotation, Type type) {
-    List<AnnotationFS> result = new ArrayList<AnnotationFS>();
-    windowAnnotation = cas.createAnnotation(type, windowAnnotation.getBegin(),
-            windowAnnotation.getEnd() + 1);
-    FSIterator<AnnotationFS> completeIt = 
getCas().getAnnotationIndex(type).iterator();
-    if (getDocumentAnnotation().getEnd() < windowAnnotation.getEnd()) {
-      completeIt.moveToLast();
-    } else {
-      completeIt.moveTo(windowAnnotation);
-    }
-    while (completeIt.isValid()
-            && ((Annotation) completeIt.get()).getBegin() >= 
windowAnnotation.getBegin()) {
-      completeIt.moveToPrevious();
-    }
-
-    if (completeIt.isValid()) {
-      completeIt.moveToNext();
-    } else {
-      completeIt.moveToFirst();
-    }
-
-    while (completeIt.isValid()
-            && ((Annotation) completeIt.get()).getBegin() < 
windowAnnotation.getBegin()) {
-      completeIt.moveToNext();
-    }
-
-    while (completeIt.isValid()
-            && ((Annotation) completeIt.get()).getBegin() >= 
windowAnnotation.getBegin()) {
-      Annotation annotation = (Annotation) completeIt.get();
-      if (getCas().getTypeSystem().subsumes(type, annotation.getType())
-              && annotation.getEnd() <= windowAnnotation.getEnd()) {
-        result.add(annotation);
-      }
-      completeIt.moveToNext();
-    }
-    return result;
-  }
-
   public List<AnnotationFS> getAnnotationsInWindow(AnnotationFS 
windowAnnotation, Type type) {
+    
     if (windowAnnotation == null || type == null) {
       return Collections.emptyList();
     }
+    TypeSystem typeSystem = this.getCas().getTypeSystem();
     List<AnnotationFS> result = new ArrayList<AnnotationFS>();
-    List<AnnotationFS> inWindow = getAnnotationsInWindow2(windowAnnotation, 
type);
-    result = inWindow;
+    if (typeSystem.subsumes(type, windowAnnotation.getType())) {
+      result.add(windowAnnotation);
+    }
+    result.addAll(CasUtil.selectCovered(this.cas, type, windowAnnotation));
     return result;
   }
 
@@ -759,21 +726,6 @@ public class RutaStream extends FSIterat
     return documentAnnotation;
   }
 
-  public RutaAnnotation getCorrectTMA(List<AnnotationFS> annotationsInWindow,
-          RutaAnnotation heuristicAnnotation) {
-    for (AnnotationFS annotation : annotationsInWindow) {
-      if (annotation instanceof RutaAnnotation) {
-        RutaAnnotation tma = (RutaAnnotation) annotation;
-        if (tma.getBegin() == heuristicAnnotation.getBegin()
-                && tma.getEnd() == heuristicAnnotation.getEnd() && 
tma.getAnnotation().getType()
-                        
.equals(heuristicAnnotation.getAnnotation().getType())) {
-          return tma;
-        }
-      }
-    }
-    return null;
-  }
-
   public void retainTypes(List<Type> list) {
     filter.retainTypes(list);
     currentIt = filter.createFilteredIterator(cas, basicType);
@@ -1378,4 +1330,24 @@ public class RutaStream extends FSIterat
 
     return cas.getAnnotationType();
   }
+  
+  public RutaAnnotation getRutaAnnotationFor(AnnotationFS annotation, boolean 
create,
+          RutaStream stream) {
+    Type heuristicType = 
this.cas.getTypeSystem().getType(RutaAnnotation.class.getName());
+    List<AnnotationFS> ras = CasUtil.selectAt(this.cas, heuristicType, 
annotation.getBegin(),
+            annotation.getEnd());
+    for (AnnotationFS each : ras) {
+      if (((RutaAnnotation) each).getAnnotation() == annotation) {
+        return (RutaAnnotation) each;
+      }
+    }
+    if (create) {
+      JCas jCas = stream.getJCas();
+      RutaAnnotation result = new RutaAnnotation(jCas, annotation.getBegin(), 
annotation.getEnd());
+      result.setAnnotation((Annotation) annotation);
+      result.addToIndexes();
+      return result;
+    }
+    return null;
+  }
 }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkAction.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkAction.java?rev=1836780&r1=1836779&r2=1836780&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkAction.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkAction.java
 Fri Jul 27 07:27:56 2018
@@ -20,9 +20,8 @@
 package org.apache.uima.ruta.action;
 
 import java.util.List;
-
-import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.util.CasUtil;
 import org.apache.uima.jcas.tcas.Annotation;
 import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.expression.number.INumberExpression;
@@ -68,35 +67,29 @@ public class MarkAction extends Abstract
 
   protected void updateHeuristicAnnotation(MatchContext context, RutaStream 
stream,
           AnnotationFS matchedAnnotation, double deltaScore) {
-    Type heuristicType = stream.getJCas().getCasType(RutaAnnotation.type);
-    RutaAnnotation heuristicAnnotation = (RutaAnnotation) 
stream.getCas().createAnnotation(
-            heuristicType, matchedAnnotation.getBegin(), 
matchedAnnotation.getEnd());
-    Annotation newAnnotation = (Annotation) stream.getCas().createAnnotation(
-            type.getType(context, stream), heuristicAnnotation.getBegin(),
-            heuristicAnnotation.getEnd());
-    heuristicAnnotation.setScore(deltaScore);
-    heuristicAnnotation.setAnnotation(newAnnotation);
-    List<AnnotationFS> annotationsInWindow = 
stream.getAnnotationsInWindow(heuristicAnnotation,
-            heuristicType);
-
-    if (annotationsInWindow.isEmpty()) {
-      heuristicAnnotation.addToIndexes();
-      newAnnotation.addToIndexes();
-      stream.addAnnotation(newAnnotation, context.getRuleMatch());
+    
+    Annotation targetAnnotation = null;
+
+    List<AnnotationFS> annotationsInSpan = CasUtil.selectAt(stream.getCas(),
+            this.type.getType(context, stream), matchedAnnotation.getBegin(),
+            matchedAnnotation.getEnd());
+    if (annotationsInSpan.isEmpty()) {
+      targetAnnotation = this.createAnnotation(matchedAnnotation, context, 
stream);
     } else {
-      RutaAnnotation tma = stream.getCorrectTMA(annotationsInWindow, 
heuristicAnnotation);
-      if (tma != null) {
-        tma.removeFromIndexes();
-        double newScore = tma.getScore() + deltaScore;
-        tma.setScore(newScore);
-        tma.addToIndexes();
-      } else {
-        heuristicAnnotation.addToIndexes();
-        newAnnotation.addToIndexes();
-        stream.addAnnotation(newAnnotation, context.getRuleMatch());
-      }
+      targetAnnotation = (Annotation) annotationsInSpan.get(0);
+      
+    }
+
+    if (targetAnnotation == null) {
+      return;
     }
 
+    RutaAnnotation rutaAnnotation = 
stream.getRutaAnnotationFor(targetAnnotation, true, stream);
+    stream.removeAnnotation(rutaAnnotation);
+    double newScore = rutaAnnotation.getScore() + deltaScore;
+    rutaAnnotation.setScore(newScore);
+    rutaAnnotation.addToIndexes();
+    stream.addAnnotation(rutaAnnotation, context.getRuleMatch());
   }
 
   public INumberExpression getScore() {

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/ScoreCondition.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/ScoreCondition.java?rev=1836780&r1=1836779&r2=1836780&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/ScoreCondition.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/ScoreCondition.java
 Fri Jul 27 07:27:56 2018
@@ -51,16 +51,10 @@ public class ScoreCondition extends Term
   public EvaluatedCondition eval(MatchContext context, RutaStream stream, 
InferenceCrowd crowd) {
     AnnotationFS annotation = context.getAnnotation();
     RuleElement element = context.getElement();
-    Type heuristicType = stream.getJCas().getCasType(RutaAnnotation.type);
-    List<AnnotationFS> annotationsInWindow = 
stream.getAnnotationsInWindow(annotation,
-            heuristicType);
     double score = 0;
-    if (!annotationsInWindow.isEmpty()) {
-      RutaAnnotation heuristicAnnotation = (RutaAnnotation) 
stream.getCas().createAnnotation(
-              heuristicType, annotation.getBegin(), annotation.getEnd());
-      heuristicAnnotation.setAnnotation((Annotation) annotation);
-      RutaAnnotation tma = stream.getCorrectTMA(annotationsInWindow, 
heuristicAnnotation);
-      score = tma.getScore();
+    RutaAnnotation rutaAnnotation =  stream.getRutaAnnotationFor(annotation, 
false, stream);
+    if(rutaAnnotation != null) {
+      score = rutaAnnotation.getScore();
     }
     if (var != null) {
       element.getParent().getEnvironment().setVariableValue(var, score);

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/SplitTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/SplitTest.java?rev=1836780&r1=1836779&r2=1836780&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/SplitTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/SplitTest.java
 Fri Jul 27 07:27:56 2018
@@ -341,8 +341,8 @@ public class SplitTest {
   @Test
   public void testBoundary() throws Exception {
     String document = "Some text. More text , with 1 , and more. even more 
text.";
-    String script = "PERIOD #{-> T1} PERIOD;";
-    script += "#{-> T1} PERIOD;";
+    String script = "PERIOD (# PERIOD){-> T1};";
+    script += "(# PERIOD){-> T1};";
     script += "T1{-> SPLIT(PERIOD, true, false, true)};";
 
     CAS cas = RutaTestUtils.getCAS(document);
@@ -366,9 +366,7 @@ public class SplitTest {
     next = iterator.next();
     assertEquals("even more text.", next.getCoveredText());
 
-    if (cas != null) {
-      cas.release();
-    }
+    cas.release();
 
   }
 


Reply via email to