You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by vm...@apache.org on 2019/04/15 16:48:14 UTC

svn commit: r1857598 - in /uima/ruta/trunk/ruta-core/src: main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java test/java/org/apache/uima/ruta/rule/WildCard2Test.java

Author: vmorari
Date: Mon Apr 15 16:48:14 2019
New Revision: 1857598

URL: http://svn.apache.org/viewvc?rev=1857598&view=rev
Log:
UIMA-5484: extended wildcard to not skip next optional rule element. added test.

Modified:
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/WildCard2Test.java

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java?rev=1857598&r1=1857597&r2=1857598&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java Mon Apr 15 16:48:14 2019
@@ -24,6 +24,8 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.CASException;
 import org.apache.uima.cas.FSIterator;
@@ -63,8 +65,17 @@ public class WildCardRuleElement extends
           RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
           RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
           InferenceCrowd crowd) {
+    Pair<RuleElement,Integer> next = getNextRuleElement(after, this);
+    RuleElement nextElement = next.getLeft();
+    int nextDepth = next.getRight().intValue();
+    List<RuleMatch> result = tryWithNextRuleElement(nextElement, after, annotation, ruleMatch,
+            ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
+    return result;
+  }
+
+  private Pair<RuleElement, Integer> getNextRuleElement(boolean after, RuleElement element) {
     RuleElement nextElement = null;
-    RuleElement current = this;
+    RuleElement current = element;
     int nextDepth = -1;
     while (nextElement == null && current != null && current.getContainer() != null) {
       RuleElementContainer container = current.getContainer();
@@ -79,9 +90,7 @@ public class WildCardRuleElement extends
     if (nextElement == null) {
       nextDepth = 0;
     }
-    List<RuleMatch> result = tryWithNextRuleElement(nextElement, after, annotation, ruleMatch,
-            ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
-    return result;
+    return new ImmutablePair<RuleElement, Integer>(nextElement, Integer.valueOf(nextDepth));
   }
 
   private List<RuleMatch> tryWithNextRuleElement(RuleElement nextElement, boolean after,
@@ -311,7 +320,10 @@ public class WildCardRuleElement extends
           // optional did not match -> match complete window/document
           // TODO refactor
 
-          AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, null, stream);
+          AnnotationFS nextAnchor = getNextAnchor(after, annotation, nextElement, ruleMatch,
+                  containerMatch, sideStepOrigin, stream, crowd);
+          
+          AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, nextAnchor, stream);
           doMatch(coveredByWildCard, ruleMatch, containerMatch, annotation == null, stream, crowd);
           if (ruleMatch.matched()) {
             ComposedRuleElementMatch nextContainerMatch = getContainerMatchOfNextElement(
@@ -330,8 +342,12 @@ public class WildCardRuleElement extends
           }
         } else {
 
-          result = cre.fallbackContinue(after, true, annotation, ruleMatch, ruleApply,
-                  containerMatch, sideStepOrigin, entryPoint, stream, crowd);
+          if(ruleApply == null && entryPoint != null && entryPoint.equals(nextElement)) {
+            return result;
+          } else {
+            result = cre.fallbackContinue(after, true, annotation, ruleMatch, ruleApply,
+                    containerMatch, sideStepOrigin, entryPoint, stream, crowd);
+          }
         }
       }
       return result;
@@ -379,6 +395,26 @@ public class WildCardRuleElement extends
     return result;
   }
 
+  private AnnotationFS getNextAnchor(boolean after, AnnotationFS annotation, RuleElement nextElement,
+          RuleMatch ruleMatch, ComposedRuleElementMatch containerMatch,
+          RutaRuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd) {
+    AnnotationFS nextAnchor = null;
+    Pair<RuleElement, Integer> nextNext = getNextRuleElement(after, nextElement);
+    if (nextNext != null && nextNext.getLeft() != null) {
+      List<RuleMatch> tryWithNextNextRuleElement = tryWithNextRuleElement(nextNext.getLeft(), after, annotation, ruleMatch, null, containerMatch, nextNext.getRight().intValue(), sideStepOrigin, nextNext.getLeft(), stream, crowd);
+      for (RuleMatch eachNextRuleMatch : tryWithNextNextRuleElement) {
+        if(eachNextRuleMatch.matched()) {
+          List<AnnotationFS> matchedAnnotationsOfElement = eachNextRuleMatch.getMatchedAnnotationsOfElement(nextNext.getLeft());
+          if (matchedAnnotationsOfElement != null && !matchedAnnotationsOfElement.isEmpty()) {
+            nextAnchor = after ? matchedAnnotationsOfElement.get(0) : matchedAnnotationsOfElement.get(matchedAnnotationsOfElement.size()-1);
+            break;
+          }
+        }
+      }
+    }
+    return nextAnchor;
+  }
+
   private boolean nextElementDidMatch(List<RuleMatch> result, RuleElement nextElement) {
     if (result == null || result.isEmpty()) {
       return false;

Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/WildCard2Test.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/WildCard2Test.java?rev=1857598&r1=1857597&r2=1857598&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/WildCard2Test.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/WildCard2Test.java Mon Apr 15 16:48:14 2019
@@ -29,6 +29,7 @@ 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.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class WildCard2Test {
@@ -76,6 +77,17 @@ public class WildCard2Test {
   }
 
   @Test
+  public void testOptional2() throws Exception {
+    String document = "Cw 1 2 3 test";
+    String script = "(CW # COLON?){-> T1} SW;";
+
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
+
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "Cw 1 2 3");
+  }
+  
+  @Test
   public void testLookaheadInGreedy() throws Exception {
     String document = "Some test. Some test. Some test.";
     String script = "((CW #){-> T1})+;";
@@ -207,6 +219,20 @@ public class WildCard2Test {
     RutaTestUtils.assertAnnotationsEquals(cas, 1, 0);
     RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "a a a");
   }
+  
+  @Test
+  public void testInlinedRulesAtWildcardWithOptional() throws Exception {
+    String document = "1 a a b / A 1";
+    String script = "NUM #{->T1} NUM;\n";
+    script += "T1{->T2}<-{# COLON? CW;} NUM;\n";
+    script += "T2 -> {(#<-{SW # NUM?;} COLON? SPECIAL){-> T3} CW;};\n";
+    
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
+    
+    RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "a a b / A");
+    RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "a a b /");
+  }
 
   @Test
   public void testWithFailingNextRuleElement() throws Exception {