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 2017/07/06 15:11:38 UTC

svn commit: r1801069 - in /uima/ruta/trunk/ruta-core/src: main/java/org/apache/uima/ruta/engine/ main/java/org/apache/uima/ruta/rule/ test/java/org/apache/uima/ruta/action/ test/java/org/apache/uima/ruta/rule/quantifier/

Author: pkluegl
Date: Thu Jul  6 15:11:38 2017
New Revision: 1801069

URL: http://svn.apache.org/viewvc?rev=1801069&view=rev
Log:
UIMA-5450
- check on failed
- added util method for storing CAS for debugging
- added test

Added:
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/quantifier/MinMaxQuantifierTest.java   (with props)
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/ComposedRuleElement.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/GatherTest.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=1801069&r1=1801068&r2=1801069&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 Thu Jul  6 15:11:38 2017
@@ -20,7 +20,9 @@
 package org.apache.uima.ruta.engine;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -32,11 +34,13 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.analysis_engine.AnalysisEngine;
 import org.apache.uima.analysis_engine.AnalysisEngineDescription;
 import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
 import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.SerialFormat;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.cas.text.AnnotationIndex;
@@ -47,6 +51,7 @@ import org.apache.uima.resource.Resource
 import org.apache.uima.resource.metadata.TypeDescription;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.util.CasCreationUtils;
+import org.apache.uima.util.CasIOUtils;
 import org.apache.uima.util.FileUtils;
 import org.apache.uima.util.InvalidXMLException;
 import org.apache.uima.util.XMLInputSource;
@@ -315,4 +320,19 @@ 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);
+    } catch (IOException e) {
+      throw new IllegalArgumentException(e);
+    } finally {
+      IOUtils.closeQuietly(fos);
+    }
+  }
 }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java?rev=1801069&r1=1801068&r2=1801069&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java Thu Jul  6 15:11:38 2017
@@ -83,8 +83,8 @@ public class ComposedRuleElement extends
         RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch, true);
         ComposedRuleElementMatch composedMatch = createComposedMatch(extendedMatch,
                 extendedContainerMatch, stream);
-        List<RuleMatch> startRuleMatches = each.startMatch(extendedMatch, null, composedMatch,
-                this, stream, crowd);
+        List<RuleMatch> startRuleMatches = each.startMatch(extendedMatch, null, composedMatch, this,
+                stream, crowd);
         for (RuleMatch startRuleMatch : startRuleMatches) {
           ComposedRuleElementMatch startElementMatch = (ComposedRuleElementMatch) startRuleMatch
                   .getLastMatch(this, true);
@@ -223,8 +223,8 @@ public class ComposedRuleElement extends
         AnnotationFS lastAnnotation = eachRuleMatch.getLastMatchedAnnotation(context, stream);
         boolean failed = !eachComposedMatch.matched();
         List<AnnotationFS> textsMatched = eachRuleMatch.getMatchedAnnotationsOfRoot();
-        if ((!stream.isGreedyAnchoring() && !stream.isOnlyOnce())
-                || (!textsMatched.isEmpty() && !earlyExit(textsMatched.get(0), ruleApply, stream))) {
+        if ((!stream.isGreedyAnchoring() && !stream.isOnlyOnce()) || (!textsMatched.isEmpty()
+                && !earlyExit(textsMatched.get(0), ruleApply, stream))) {
           List<RuleMatch> fallbackContinue = fallbackContinue(after, failed, lastAnnotation,
                   eachRuleMatch, ruleApply, eachComposedMatch, sideStepOrigin, entryPoint, stream,
                   crowd);
@@ -269,8 +269,8 @@ public class ComposedRuleElement extends
         boolean failed = !eachComposedMatch.matched();
 
         List<AnnotationFS> textsMatched = eachRuleMatch.getMatchedAnnotationsOfRoot();
-        if ((!stream.isGreedyAnchoring() && !stream.isOnlyOnce())
-                || (!textsMatched.isEmpty() && !earlyExit(textsMatched.get(0), ruleApply, stream))) {
+        if ((!stream.isGreedyAnchoring() && !stream.isOnlyOnce()) || (!textsMatched.isEmpty()
+                && !earlyExit(textsMatched.get(0), ruleApply, stream))) {
           List<RuleMatch> fallbackContinue = fallbackContinue(after, failed, lastAnnotation,
                   eachRuleMatch, ruleApply, eachComposedMatch, sideStepOrigin, entryPoint, stream,
                   crowd);
@@ -317,7 +317,8 @@ public class ComposedRuleElement extends
   }
 
   private Map<RuleMatch, ComposedRuleElementMatch> mergeDisjunctiveRuleMatches(
-          Map<RuleMatch, ComposedRuleElementMatch> ruleMatches, boolean direction, RutaStream stream) {
+          Map<RuleMatch, ComposedRuleElementMatch> ruleMatches, boolean direction,
+          RutaStream stream) {
     // TODO hotfix: this needs a correct implementation
     Map<RuleMatch, ComposedRuleElementMatch> result = new TreeMap<RuleMatch, ComposedRuleElementMatch>(
             ruleMatchComparator);
@@ -336,10 +337,8 @@ public class ComposedRuleElement extends
           largestEntry = entry;
           largestAnnotation = lastMatchedAnnotation;
         } else {
-          if (lastMatchedAnnotation != null
-                  && largestAnnotation != null
-                  && lastMatchedAnnotation.getCoveredText().length() > largestAnnotation
-                          .getCoveredText().length()) {
+          if (lastMatchedAnnotation != null && largestAnnotation != null && lastMatchedAnnotation
+                  .getCoveredText().length() > largestAnnotation.getCoveredText().length()) {
             largestEntry = entry;
             largestAnnotation = lastMatchedAnnotation;
           }
@@ -448,8 +447,8 @@ public class ComposedRuleElement extends
             result = nextElement.continueMatch(after, backtrackedAnnotation, ruleMatch, ruleApply,
                     parentContainerMatch, sideStepOrigin, entryPoint, stream, crowd);
           } else {
-            result = fallback(after, failed, annotation, ruleMatch, ruleApply,
-                    parentContainerMatch, sideStepOrigin, entryPoint, stream, crowd);
+            result = fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+                    sideStepOrigin, entryPoint, stream, crowd);
           }
         } else {
           if (this.equals(entryPoint)) {
@@ -521,6 +520,8 @@ public class ComposedRuleElement extends
       result = sideStepOrigin.continueSideStep(after, ruleMatch, ruleApply, containerMatch,
               entryPoint, stream, crowd);
     } else {
+      // take care that failed matches wont be applied
+      ruleMatch.setMatched(ruleMatch.matched && !failed);
       result.add(ruleMatch);
       doneMatching(ruleMatch, ruleApply, stream, crowd);
     }
@@ -540,7 +541,8 @@ public class ComposedRuleElement extends
           RutaStream stream, InferenceCrowd crowd) {
     List<AnnotationFS> textsMatched = match.getTextsMatched();
     if (textsMatched == null || textsMatched.isEmpty()) {
-      getParent().getEnvironment().addMatchToVariable(ruleMatch, this, new MatchContext(getParent()), stream);
+      getParent().getEnvironment().addMatchToVariable(ruleMatch, this,
+              new MatchContext(getParent()), stream);
       match.evaluateInnerMatches(true, stream);
       return;
     }
@@ -551,7 +553,7 @@ public class ComposedRuleElement extends
 
     MatchContext context = new MatchContext(annotation, this, ruleMatch, after);
     context.getParent().getEnvironment().addMatchToVariable(ruleMatch, this, context, stream);
-    
+
     List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>(
             conditions.size());
     for (AbstractRutaCondition condition : conditions) {
@@ -559,7 +561,7 @@ public class ComposedRuleElement extends
       EvaluatedCondition eval = condition.eval(context, stream, crowd);
       crowd.endVisit(condition, null);
       evaluatedConditions.add(eval);
-      if(!eval.isValue()) {
+      if (!eval.isValue()) {
         break;
       }
     }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java?rev=1801069&r1=1801068&r2=1801069&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java Thu Jul  6 15:11:38 2017
@@ -276,7 +276,7 @@ public class RutaRuleElement extends Abs
     if (matchInfo == null) {
       context.getParent().getEnvironment().addMatchToVariable(ruleMatch, this, context, stream);
       if (quantifier.isOptional(context, stream)) {
-        result = continueMatchSomewhereElse(after, true, annotation, ruleMatch, ruleApply,
+        result = continueMatchSomewhereElse(after, false, annotation, ruleMatch, ruleApply,
                 containerMatch, sideStepOrigin, entryPoint, stream, crowd);
       } else if (getContainer() instanceof ComposedRuleElement) {
         ComposedRuleElement cre = (ComposedRuleElement) getContainer();

Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/GatherTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/GatherTest.java?rev=1801069&r1=1801068&r2=1801069&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/GatherTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/action/GatherTest.java Thu Jul  6 15:11:38 2017
@@ -88,7 +88,7 @@ public class GatherTest {
   }
 
   @Test
-  public void testOptionalMatch() {
+  public void testOptionalMatch() throws Exception {
     String document = "A X C";
     String script = "";
     script += "W{REGEXP(\"A\")->MARK(T1)};";
@@ -105,13 +105,8 @@ public class GatherTest {
     list.add(new TestFeature(fn1, "", "uima.tcas.Annotation"));
     String fn2 = "b";
     list.add(new TestFeature(fn2, "", "uima.tcas.Annotation"));
-    CAS cas = null;
-    try {
-      cas = RutaTestUtils.getCAS(document, complexTypes, features);
+    CAS cas  = RutaTestUtils.getCAS(document, complexTypes, features);
       Ruta.apply(cas, script);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
 
     Type t = null;
     AnnotationIndex<AnnotationFS> ai = null;

Added: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/quantifier/MinMaxQuantifierTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/quantifier/MinMaxQuantifierTest.java?rev=1801069&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/quantifier/MinMaxQuantifierTest.java (added)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/quantifier/MinMaxQuantifierTest.java Thu Jul  6 15:11:38 2017
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.ruta.rule.quantifier;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.ruta.engine.Ruta;
+import org.apache.uima.ruta.engine.RutaTestUtils;
+import org.junit.Test;
+
+public class MinMaxQuantifierTest {
+
+  @Test
+  public void test() throws Exception {
+    String document = "A, A. B, B, B";
+    String script = "";
+    script += "(CW{-PARTOF(TruePositive)} (COMMA CW)[2,5]){-> TruePositive};\n";
+    script += "TruePositive{-> T1};\n";
+
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
+
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "B, B, B");
+    cas.release();
+  }
+}

Propchange: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/quantifier/MinMaxQuantifierTest.java
------------------------------------------------------------------------------
    svn:eol-style = native