Author: pkluegl
Date: Mon Jan  7 12:37:00 2019
New Revision: 1850634

URL: http://svn.apache.org/viewvc?rev=1850634&view=rev
Log:
UIMA-5881: fixed rule element match lookahead

Modified:
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java
    
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/MarkInGreedyComposedTest.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/engine/RutaTestUtils.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java?rev=1850634&r1=1850633&r2=1850634&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java
 Mon Jan  7 12:37:00 2019
@@ -55,6 +55,7 @@ import org.apache.uima.util.CasIOUtils;
 import org.apache.uima.util.FileUtils;
 import org.apache.uima.util.InvalidXMLException;
 import org.apache.uima.util.XMLInputSource;
+import org.xml.sax.SAXException;
 
 public class RutaTestUtils {
 
@@ -174,8 +175,8 @@ public class RutaTestUtils {
     aed.getAnalysisEngineMetaData().setTypeSystem(mergeTypeSystems);
 
     AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
-    ae.setConfigParameterValue(RutaEngine.PARAM_SCRIPT_PATHS, new String[] { 
ruleFile
-            .getParentFile().getPath() });
+    ae.setConfigParameterValue(RutaEngine.PARAM_SCRIPT_PATHS,
+            new String[] { ruleFile.getParentFile().getPath() });
     String name = ruleFile.getName();
     if (name.endsWith(RutaEngine.SCRIPT_FILE_EXTENSION)) {
       name = name.substring(0, name.length() - 5);
@@ -208,8 +209,10 @@ public class RutaTestUtils {
   /**
    * Helper to get the test type, e.g. org.apache.uima.T1, org.apache.uima.T2, 
...
    * 
-   * @param cas - The CAS object containing the type system
-   * @param i - typeId, converted to {@link #TYPE} + i
+   * @param cas
+   *          - The CAS object containing the type system
+   * @param i
+   *          - typeId, converted to {@link #TYPE} + i
    * @return the test type object with the given counter
    */
   public static Type getTestType(CAS cas, int i) {
@@ -218,14 +221,20 @@ public class RutaTestUtils {
     return cas.getTypeSystem().getType(TYPE + i);
   }
 
-  public static CAS getCAS(String document) throws 
ResourceInitializationException, IOException,
-          InvalidXMLException {
+  public static CAS getCAS(String document)
+          throws ResourceInitializationException, IOException, 
InvalidXMLException {
     return getCAS(document, null, null);
   }
 
   public static CAS getCAS(String document, Map<String, String> complexTypes,
-          Map<String, List<TestFeature>> features) throws 
ResourceInitializationException,
-          IOException, InvalidXMLException {
+          Map<String, List<TestFeature>> features)
+          throws ResourceInitializationException, IOException, 
InvalidXMLException {
+    return getCAS(document, complexTypes, features, false);
+  }
+
+  public static CAS getCAS(String document, Map<String, String> complexTypes,
+          Map<String, List<TestFeature>> features, boolean storeTypeSystem)
+          throws ResourceInitializationException, IOException, 
InvalidXMLException {
     URL url = RutaEngine.class.getClassLoader().getResource("BasicEngine.xml");
     if (url == null) {
       url = RutaTestUtils.class.getClassLoader()
@@ -259,6 +268,16 @@ public class RutaTestUtils {
     tsds.add(basicTypeSystem);
     TypeSystemDescription mergeTypeSystems = 
CasCreationUtils.mergeTypeSystems(tsds);
 
+    if (storeTypeSystem) {
+      try (OutputStream os = new FileOutputStream("TypeSystem.xml")) {
+        try {
+          mergeTypeSystems.toXML(os);
+        } catch (SAXException e) {
+          e.printStackTrace();
+        }
+      }
+    }
+
     aed.getAnalysisEngineMetaData().setTypeSystem(mergeTypeSystems);
     AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
     CAS cas = ae.newCAS();
@@ -273,14 +292,18 @@ public class RutaTestUtils {
       System.out.println(annotationFS.getCoveredText());
     }
   }
-  
+
   /**
    * Helper for common assertion in JUnit tests
    * 
-   * @param cas - The CAS object containing the type system
-   * @param typeId - typeId, converted to {@link #TYPE} + i
-   * @param expectedCnt - the expected amount of annotations
-   * @param expecteds -  the exprected covered texts
+   * @param cas
+   *          - The CAS object containing the type system
+   * @param typeId
+   *          - typeId, converted to {@link #TYPE} + i
+   * @param expectedCnt
+   *          - the expected amount of annotations
+   * @param expecteds
+   *          - the exprected covered texts
    */
   public static void assertAnnotationsEquals(CAS cas, int typeId, int 
expectedCnt,
           String... expecteds) {
@@ -295,8 +318,8 @@ public class RutaTestUtils {
       for (String expected : expecteds) {
         String actual = iterator.next().getCoveredText();
         if (!actual.equals(expected)) {
-          throw new AssertionError("expected text (" + expected + ") does not 
match with actual ("
-                  + actual + ").");
+          throw new AssertionError(
+                  "expected text (" + expected + ") does not match with actual 
(" + actual + ").");
         }
       }
     }
@@ -305,7 +328,8 @@ public class RutaTestUtils {
   /**
    * Helper to run Ruta on a tests script
    * 
-   * @param testClass - the class of the unit test
+   * @param testClass
+   *          - the class of the unit test
    * @return the annotated {@link CAS}
    */
   public static CAS processTestScript(Class<?> testClass) {
@@ -322,15 +346,14 @@ public class RutaTestUtils {
     }
     return cas;
   }
-  
-  
+
   public static void storeCas(CAS cas, String name) {
     File file = new File("input/" + name + ".xmi");
     file.getParentFile().mkdirs();
     OutputStream fos = null;
     try {
       fos = new FileOutputStream(file);
-      CasIOUtils.save(cas, fos , SerialFormat.XMI);
+      CasIOUtils.save(cas, fos, SerialFormat.XMI);
     } catch (IOException e) {
       throw new IllegalArgumentException(e);
     } finally {

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=1850634&r1=1850633&r2=1850634&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 Jan  7 12:37:00 2019
@@ -361,7 +361,11 @@ public class WildCardRuleElement extends
         result = nextElement.continueMatch(after, anchor, extendedMatch, 
ruleApply,
                 nextContainerMatch, sideStepOrigin, nextElement, stream, 
crowd);
 //        List<RuleElementMatch> nextList = 
nextContainerMatch.getInnerMatches().get(nextElement);
-        if (result == null || result.isEmpty() || !result.get(result.size() - 
1).matched()) {
+
+        // cannot use container match since there could be new alternatives in 
the depth search
+        boolean nextElementDidMatch = nextElementDidMatch(result, nextElement);
+
+        if (!nextElementDidMatch) {
           moveOn(after, iterator, stream);
         } else {
           doneHere = true;
@@ -375,6 +379,32 @@ public class WildCardRuleElement extends
     return result;
   }
 
+  private boolean nextElementDidMatch(List<RuleMatch> result, RuleElement 
nextElement) {
+    if (result == null || result.isEmpty()) {
+      return false;
+    }
+
+    for (RuleMatch ruleMatch : result) {
+      if (ruleMatch.matched()) {
+        return true;
+      }
+    }
+    for (RuleMatch ruleMatch : result) {
+      List<List<RuleElementMatch>> matchInfo = 
ruleMatch.getMatchInfo(nextElement);
+      List<RuleElementMatch> matches = matchInfo.get(matchInfo.size() - 1);
+      if (matches != null) {
+        for (RuleElementMatch ruleElementMatch : matches) {
+          if (ruleElementMatch != null && ruleElementMatch.matched()) {
+            return true;
+          }
+        }
+      }
+
+    }
+
+    return false;
+  }
+
   private ComposedRuleElementMatch getContainerMatchOfNextElement(
           ComposedRuleElementMatch extendedContainerMatch, int nextDepth) {
     ComposedRuleElementMatch result = extendedContainerMatch;

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/MarkInGreedyComposedTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/MarkInGreedyComposedTest.java?rev=1850634&r1=1850633&r2=1850634&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/MarkInGreedyComposedTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/MarkInGreedyComposedTest.java
 Mon Jan  7 12:37:00 2019
@@ -27,112 +27,76 @@ import org.junit.Test;
 public class MarkInGreedyComposedTest {
 
   @Test
-  public void testWildCardFollowedByComposedReversed() {
+  public void testWildCardFollowedByComposedReversed() throws Exception {
     String document = "1 x f B e d B x c A b a A 1";
     String script = "";
     script += "( ( (SW{REGEXP(\"x\")} SW ) #) {-> T1} )+ @NUM;";
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document);
-      Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
 
-    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "x f B e d B", "x c A b a 
A");
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
 
-    cas.release();
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "x f B e d B", "x c A b a 
A");
   }
 
   @Test
-  public void testSimpleFollowedByComposedReversed() {
+  public void testSimpleFollowedByComposedReversed() throws Exception {
     String document = "1 x f B x c A 1";
     String script = "";
     script += "( ( (SW{REGEXP(\"x\")} SW ) CW) {-> T1} )+ @NUM;";
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document);
-      Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
 
-    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "x f B", "x c A");
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
 
-    cas.release();
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "x f B", "x c A");
   }
 
   @Test
-  public void testWildCardFollowedByComposed() {
+  public void testWildCardFollowedByComposed() throws Exception {
     String document = "A c b A c x A c b A c x";
     String script = "";
     script += "( ( # (SW SW{REGEXP(\"x\")}) ) {-> T1} )+;";
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document);
-      Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
 
-    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "A c b A c x", "A c b A c 
x");
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
 
-    cas.release();
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "A c b A c x", "A c b A c 
x");
   }
 
   @Test
-  public void testWildCardFollowedByCondition() {
+  public void testWildCardFollowedByCondition() throws Exception {
     String document = "A A b A c X X b X c";
     String script = "";
     script += "((# SW{REGEXP(\"c\")}){-> T1})+; ";
-    script += "((# SW{REGEXP(\"c\")}){-> MARKONCE(T2)})+; ";
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document);
-      Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
+    script += "((# SW{REGEXP(\"c\")}){-> MARKONCE(T2)})+;";
+
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
 
     RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "A A b A c", "X X b X c");
     RutaTestUtils.assertAnnotationsEquals(cas, 2, 2, "A A b A c", "X X b X c");
-
-    cas.release();
   }
 
   @Test
-  public void testWithWildCard() {
+  public void testWithWildCard() throws Exception {
     String document = "1 . 2 .";
     String script = "";
     script += "((# PERIOD){-> T1})+; ";
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document);
-      Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
 
-    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "1 .", "2 .");
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
 
-    cas.release();
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "1 .", "2 .");
   }
 
   @Test
-  public void test() {
+  public void test() throws Exception {
     String document = "1 . 2 .";
     String script = "";
     script += "((NUM PERIOD){-> MARKONCE(T1)})+; ";
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document);
-      Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
 
-    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "1 .", "2 .");
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
 
-    cas.release();
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "1 .", "2 .");
   }
 }

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=1850634&r1=1850633&r2=1850634&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 Jan  7 12:37:00 2019
@@ -208,4 +208,21 @@ public class WildCard2Test {
     RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "a a a");
   }
 
+  @Test
+  public void testWithFailingNextRuleElement() throws Exception {
+    String document = "a b c 1 d e f 2 g h i 3 / 4 m n o";
+    String script = "\"a\" -> T1;\n";
+    script += "INT a;\n";
+    script += "INT t;\n";
+    script += "(T1 # NUM{PARSE(a), a<100} ANY{REGEXP(\"[/]\")} 
NUM{PARSE(t),t<200}){ -> T2};\n";
+
+    CAS cas = RutaTestUtils.getCAS(document, null, null, true);
+    Ruta.apply(cas, script);
+
+    RutaTestUtils.storeCas(cas, 
"testFailingConditionWithFailingNextRuleElement");
+
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "a");
+    RutaTestUtils.assertAnnotationsEquals(cas, 2, 0);
+  }
+
 }


Reply via email to