Author: pkluegl
Date: Thu Jan  3 13:09:12 2019
New Revision: 1850232

URL: http://svn.apache.org/viewvc?rev=1850232&view=rev
Log:
UIMA-5365: remove value of label if element did not match after all

Modified:
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.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/RutaOptionalRuleElement.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.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/expression/annotation/AnnotationLabelExpressionTest.java

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java?rev=1850232&r1=1850231&r2=1850232&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
 Thu Jan  3 13:09:12 2019
@@ -1142,10 +1142,12 @@ public class RutaEnvironment {
     } else if (AnnotationFS.class.equals(variableType)) {
       if (context.getDirection()) {
         AnnotationFS annotation = null;
-        if (context.getDirection()) {
-          annotation = annotations.get(annotations.size() - 1);
-        } else {
-          annotation = annotations.get(0);
+        if (annotations != null) {
+          if (context.getDirection()) {
+            annotation = annotations.get(annotations.size() - 1);
+          } else {
+            annotation = annotations.get(0);
+          }
         }
         setVariableValue(var, annotation);
       }

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=1850232&r1=1850231&r2=1850232&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 Jan  3 13:09:12 2019
@@ -32,6 +32,7 @@ import java.util.TreeMap;
 
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.ruta.RutaEnvironment;
 import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.action.AbstractRutaAction;
 import org.apache.uima.ruta.block.RutaBlock;
@@ -552,7 +553,8 @@ public class ComposedRuleElement extends
             begin, end);
 
     MatchContext context = new MatchContext(annotation, this, ruleMatch, 
after);
-    context.getParent().getEnvironment().addMatchToVariable(ruleMatch, this, 
context, stream);
+    RutaEnvironment environment = context.getParent().getEnvironment();
+    environment.addMatchToVariable(ruleMatch, this, context, stream);
 
     List<EvaluatedCondition> evaluatedConditions = new 
ArrayList<EvaluatedCondition>(
             conditions.size());
@@ -570,6 +572,9 @@ public class ComposedRuleElement extends
     if (match.matched()) {
       boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
       match.setInlinedRulesMatched(inlinedRulesMatched);
+    } else {
+      // update label for failed match after evaluating conditions
+      environment.addAnnotationsToVariable(null, getLabel(), context);
     }
   }
 

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java?rev=1850232&r1=1850231&r2=1850232&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java
 Thu Jan  3 13:09:12 2019
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.List;
 
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaEnvironment;
 import org.apache.uima.ruta.RutaProcessRuntimeException;
 import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.action.AbstractRutaAction;
@@ -72,7 +73,8 @@ public class RutaOptionalRuleElement ext
     }
     // already set the matched text and inform others
     result.setMatchInfo(base, textsMatched, stream);
-    context.getParent().getEnvironment().addMatchToVariable(ruleMatch, this, 
context, stream);
+    RutaEnvironment environment = context.getParent().getEnvironment();
+    environment.addMatchToVariable(ruleMatch, this, context, stream);
     if (base) {
       for (AbstractRutaCondition condition : conditions) {
         crowd.beginVisit(condition, null);
@@ -88,6 +90,9 @@ public class RutaOptionalRuleElement ext
     if (result.matched()) {
       boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
       result.setInlinedRulesMatched(inlinedRulesMatched);
+    } else {
+      // update label for failed match after evaluating conditions
+      environment.addAnnotationsToVariable(null, getLabel(), context);
     }
     ruleMatch.setMatched(ruleMatch.matched() && result.matched());
   }

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=1850232&r1=1850231&r2=1850232&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 Jan  3 13:09:12 2019
@@ -25,6 +25,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaEnvironment;
 import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.action.AbstractRutaAction;
 import org.apache.uima.ruta.block.RutaBlock;
@@ -388,7 +389,8 @@ public class RutaRuleElement extends Abs
     }
     // already set the matched text and inform others
     result.setMatchInfo(base, textsMatched, stream);
-    context.getParent().getEnvironment().addMatchToVariable(ruleMatch, this, 
context, stream);
+    RutaEnvironment environment = context.getParent().getEnvironment();
+    environment.addMatchToVariable(ruleMatch, this, context, stream);
     if (base) {
       for (AbstractRutaCondition condition : conditions) {
         crowd.beginVisit(condition, null);
@@ -404,6 +406,9 @@ public class RutaRuleElement extends Abs
     if (result.matched()) {
       boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
       result.setInlinedRulesMatched(inlinedRulesMatched);
+    } else {
+      // update label for failed match after evaluating conditions
+      environment.addAnnotationsToVariable(null, getLabel(), context);
     }
     ruleMatch.setMatched(ruleMatch.matched() && result.matched());
   }

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=1850232&r1=1850231&r2=1850232&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
 Thu Jan  3 13:09:12 2019
@@ -30,6 +30,7 @@ import org.apache.uima.cas.FSIterator;
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.ruta.RutaEnvironment;
 import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.action.AbstractRutaAction;
 import org.apache.uima.ruta.block.RutaBlock;
@@ -653,7 +654,8 @@ public class WildCardRuleElement extends
       textsMatched.add(annotation);
     }
     result.setMatchInfo(base, textsMatched, stream);
-    context.getParent().getEnvironment().addMatchToVariable(ruleMatch, this, 
context, stream);
+    RutaEnvironment environment = context.getParent().getEnvironment();
+    environment.addMatchToVariable(ruleMatch, this, context, stream);
     if (base) {
       for (AbstractRutaCondition condition : conditions) {
         crowd.beginVisit(condition, null);
@@ -669,6 +671,9 @@ public class WildCardRuleElement extends
     if (result.matched()) {
       boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
       result.setInlinedRulesMatched(inlinedRulesMatched);
+    } else {
+      // update label for failed match after evaluating conditions
+      environment.addAnnotationsToVariable(null, getLabel(), context);
     }
     ruleMatch.setMatched(ruleMatch.matched() && result.matched());
   }

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java?rev=1850232&r1=1850231&r2=1850232&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
 Thu Jan  3 13:09:12 2019
@@ -753,9 +753,11 @@ public class AnnotationLabelExpressionTe
 
     String document = "a b c d";
 
-    String script = "W.begin==0{-> T1};\n";
+    String script = "";
+    script += "W.begin==0{-> T1};\n";
     script += "T1 a:ANY{REGEXP(\"c\")}->{a{-> T2};};\n";
     script += "T1 a:ANY{REGEXP(\"c\")}<-{a{-> T3};};\n";
+    script += "d:Document{-> 
T4}<-{a:d{STARTSWITH(CW)};b:d{STARTSWITH(W)};}->{a{->T5};};\n";
 
     CAS cas = RutaTestUtils.getCAS(document);
     Ruta.apply(cas, script);
@@ -763,6 +765,8 @@ public class AnnotationLabelExpressionTe
     RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "a");
     RutaTestUtils.assertAnnotationsEquals(cas, 2, 0);
     RutaTestUtils.assertAnnotationsEquals(cas, 3, 0);
+    RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, document);
+    RutaTestUtils.assertAnnotationsEquals(cas, 5, 0);
 
   }
 


Reply via email to