Author: pkluegl
Date: Thu Dec 20 08:30:47 2018
New Revision: 1849363

URL: http://svn.apache.org/viewvc?rev=1849363&view=rev
Log:
UIMA-5414: added two params+impl, added test, added doc mention

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/engine/RutaEngine.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RegExpRule.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleApply.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
    uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml

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=1849363&r1=1849362&r2=1849363&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 
Thu Dec 20 08:30:47 2018
@@ -130,6 +130,10 @@ public class RutaStream extends FSIterat
 
   private boolean emptyIsInvisible;
 
+  private long maxRuleMatches;
+
+  private long maxRuleElementMatches;
+
   public RutaStream(CAS cas, Type basicType, FilterManager filter, boolean 
lowMemoryProfile,
           boolean simpleGreedyForComposed, boolean emptyIsInvisible, 
TypeUsageInformation typeUsage,
           InferenceCrowd crowd) {
@@ -484,6 +488,8 @@ public class RutaStream extends FSIterat
     stream.setDynamicAnchoring(dynamicAnchoring);
     stream.setGreedyRuleElement(greedyRuleElement);
     stream.setGreedyRule(greedyRule);
+    stream.setMaxRuleMatches(maxRuleMatches);
+    stream.setMaxRuleElementMatches(maxRuleElementMatches);
     return stream;
   }
 
@@ -494,6 +500,8 @@ public class RutaStream extends FSIterat
     stream.setDynamicAnchoring(dynamicAnchoring);
     stream.setGreedyRuleElement(greedyRuleElement);
     stream.setGreedyRule(greedyRule);
+    stream.setMaxRuleMatches(maxRuleMatches);
+    stream.setMaxRuleElementMatches(maxRuleElementMatches);
     return stream;
   }
 
@@ -1353,4 +1361,20 @@ public class RutaStream extends FSIterat
     }
     return null;
   }
+
+  public void setMaxRuleMatches(long maxRuleMatches) {
+    this.maxRuleMatches = maxRuleMatches;
+  }
+
+  public void setMaxRuleElementMatches(long maxRuleElementMatches) {
+    this.maxRuleElementMatches = maxRuleElementMatches;
+  }
+
+  public long getMaxRuleMatches() {
+    return this.maxRuleMatches;
+  }
+
+  public long getMaxRuleElementMatches() {
+    return this.maxRuleElementMatches;
+  }
 }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaEngine.java
 Thu Dec 20 08:30:47 2018
@@ -492,6 +492,24 @@ public class RutaEngine extends JCasAnno
   @ConfigurationParameter(name = PARAM_INFERENCE_VISITORS, mandatory = false, 
defaultValue = {})
   private String[] inferenceVisitors;
 
+  /**
+   * Maximum amount of allowed matches of a single rule.
+   */
+  public static final String PARAM_MAX_RULE_MATCHES = "maxRuleMatches";
+
+  @ConfigurationParameter(name = PARAM_MAX_RULE_MATCHES, mandatory = false, 
defaultValue = ""
+          + Integer.MAX_VALUE)
+  private int maxRuleMatches;
+
+  /**
+   * Maximum amount of allowed matches of a single rule element.
+   */
+  public static final String PARAM_MAX_RULE_ELEMENT_MATCHES = 
"maxRuleElementMatches";
+
+  @ConfigurationParameter(name = PARAM_MAX_RULE_ELEMENT_MATCHES, mandatory = 
false, defaultValue = ""
+          + Integer.MAX_VALUE)
+  private int maxRuleElementMatches;
+
   private UimaContext context;
 
   private RutaModule script;
@@ -578,6 +596,8 @@ public class RutaEngine extends JCasAnno
     stream.setDynamicAnchoring(dynamicAnchoring);
     stream.setGreedyRuleElement(greedyRuleElement);
     stream.setGreedyRule(greedyRule);
+    stream.setMaxRuleMatches(maxRuleMatches);
+    stream.setMaxRuleElementMatches(maxRuleElementMatches);
     try {
       script.apply(stream, crowd);
     } catch (Throwable e) {

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
 Thu Dec 20 08:30:47 2018
@@ -81,7 +81,7 @@ public abstract class AbstractRuleElemen
   protected void doneMatching(RuleMatch ruleMatch, RuleApply ruleApply, 
RutaStream stream,
           InferenceCrowd crowd) {
     if (!ruleMatch.isApplied()) {
-      ruleApply.add(ruleMatch);
+      ruleApply.add(ruleMatch, stream);
       if (ruleMatch.matchedCompletely()) {
         ruleMatch.getRule().getRoot().applyRuleElements(ruleMatch, stream, 
crowd);
       }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java
 Thu Dec 20 08:30:47 2018
@@ -29,6 +29,7 @@ import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaProcessRuntimeException;
 import org.apache.uima.ruta.RutaStream;
 
 public class ComposedRuleElementMatch extends RuleElementMatch {
@@ -79,6 +80,11 @@ public class ComposedRuleElementMatch ex
       innerMatches.put(ruleElement, list);
     }
     list.add(ruleElementMatch);
+
+    if (list.size() > stream.getMaxRuleElementMatches()) {
+      throw new RutaProcessRuntimeException("Rule element exceeded the allowed 
amount of matches ("
+              + stream.getMaxRuleElementMatches() + "): " + 
ruleElement.toString());
+    }
     evaluateInnerMatches(included, stream);
     enforceUpdate();
   }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java
 Thu Dec 20 08:30:47 2018
@@ -58,10 +58,10 @@ public class ConjunctRulesRuleElement ex
         }
       }
       allMatched &= oneMatched;
-      
+
       // only the matches that actually matched successfully
       for (RuleMatch eachRuleMatch : startMatch) {
-        if(eachRuleMatch.matched()) {
+        if (eachRuleMatch.matched()) {
           result.add(eachRuleMatch);
         }
       }
@@ -70,7 +70,7 @@ public class ConjunctRulesRuleElement ex
     for (RuleMatch each : result) {
       if (!each.isApplied()) {
         each.setMatched(allMatched);
-        ruleApply.add(each);
+        ruleApply.add(each, stream);
         if (each.matched() && allMatched) {
           each.getRule().getRoot().applyRuleElements(each, stream, crowd);
         }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RegExpRule.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RegExpRule.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RegExpRule.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RegExpRule.java
 Thu Dec 20 08:30:47 2018
@@ -100,7 +100,7 @@ public class RegExpRule extends Abstract
       }
       List<AnnotationFS> matchedAnnotationsOfRoot = 
ruleMatch.getMatchedAnnotationsOfRoot();
       if (matchedAnnotationsOfRoot != null && 
!matchedAnnotationsOfRoot.isEmpty()) {
-        ruleApply.add(ruleMatch);
+        ruleApply.add(ruleMatch, stream);
       }
     }
 

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleApply.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleApply.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleApply.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleApply.java
 Thu Dec 20 08:30:47 2018
@@ -22,7 +22,9 @@ package org.apache.uima.ruta.rule;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.uima.ruta.RutaProcessRuntimeException;
 import org.apache.uima.ruta.RutaStatement;
+import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.ScriptApply;
 
 public class RuleApply extends ScriptApply {
@@ -45,7 +47,7 @@ public class RuleApply extends ScriptApp
     return list;
   }
 
-  public void add(AbstractRuleMatch<? extends AbstractRule> match) {
+  public void add(AbstractRuleMatch<? extends AbstractRule> match, RutaStream 
stream) {
     if (match.matchedCompletely()) {
       applied++;
     }
@@ -53,6 +55,10 @@ public class RuleApply extends ScriptApp
     if (acceptMatches) {
       list.add(match);
     }
+    if (tried > stream.getMaxRuleMatches()) {
+      throw new RutaProcessRuntimeException("Rule exceeded the allowed amount 
of matches ("
+              + stream.getMaxRuleMatches() + "): " + 
match.getRule().toString());
+    }
   }
 
   public int getTried() {
@@ -71,9 +77,9 @@ public class RuleApply extends ScriptApp
     this.acceptMatches = acceptMatches;
   }
 
-  public void addAll(List<RuleMatch> matches) {
+  public void addAll(List<RuleMatch> matches, RutaStream stream) {
     for (RuleMatch ruleMatch : matches) {
-      add(ruleMatch);
+      add(ruleMatch, stream);
     }
   }
 

Modified: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/engine/RutaEngineTest.java
 Thu Dec 20 08:30:47 2018
@@ -31,6 +31,7 @@ import org.apache.uima.analysis_engine.A
 import org.apache.uima.cas.CAS;
 import org.apache.uima.fit.factory.AnalysisEngineFactory;
 import org.apache.uima.jcas.JCas;
+import org.apache.uima.ruta.RutaProcessRuntimeException;
 import org.apache.uima.ruta.TypeUsageInformation;
 import org.junit.Assert;
 import org.junit.Test;
@@ -104,4 +105,50 @@ public class RutaEngineTest {
     }
   }
 
+  @Test
+  public void testMaxRuleMatches() throws Throwable {
+
+    String document = "This is some text.";
+    String script = "W;";
+
+    AnalysisEngine ae = AnalysisEngineFactory.createEngine(RutaEngine.class, 
RutaEngine.PARAM_RULES,
+            script, RutaEngine.PARAM_MAX_RULE_MATCHES, Long.valueOf(2));
+
+    CAS cas = RutaTestUtils.getCAS(document);
+
+    try {
+      ae.process(cas);
+    } catch (AnalysisEngineProcessException e) {
+      Throwable cause = e.getCause();
+      Assert.assertTrue(cause instanceof RutaProcessRuntimeException);
+      String message = cause.getMessage();
+      Assert.assertTrue(message.startsWith("Rule exceeded the allowed amount 
of matches"));
+      return;
+    }
+    Assert.fail("expected RutaProcessRuntimeException");
+  }
+
+  @Test
+  public void testMaxRuleElementMatches() throws Throwable {
+
+    String document = "This is some text.";
+    String script = "W+;";
+
+    AnalysisEngine ae = AnalysisEngineFactory.createEngine(RutaEngine.class, 
RutaEngine.PARAM_RULES,
+            script, RutaEngine.PARAM_MAX_RULE_ELEMENT_MATCHES, 
Long.valueOf(2));
+
+    CAS cas = RutaTestUtils.getCAS(document);
+
+    try {
+      ae.process(cas);
+    } catch (AnalysisEngineProcessException e) {
+      Throwable cause = e.getCause();
+      Assert.assertTrue(cause instanceof RutaProcessRuntimeException);
+      String message = cause.getMessage();
+      Assert.assertTrue(message.startsWith("Rule element exceeded the allowed 
amount of matches"));
+      return;
+    }
+    Assert.fail("expected RutaProcessRuntimeException");
+  }
+
 }

Modified: uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml?rev=1849363&r1=1849362&r2=1849363&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml (original)
+++ uima/ruta/trunk/ruta-docbook/src/docbook/tools.ruta.overview.xml Thu Dec 20 
08:30:47 2018
@@ -972,6 +972,23 @@ Document{-> EXEC(MyAnalysisEngine, {MyTy
                   </entry>
                   <entry>Multi String</entry>
                 </row>
+                <row>
+                  <entry>
+                    <link 
linkend='ugr.tools.ruta.ae.basic.parameter.maxRuleMatches'>maxRuleMatches</link>
+                  </entry>
+                  <entry>Maximum amount of allowed matches of a single rule.
+                  </entry>
+                  <entry>Single Integer</entry>
+                </row>
+                <row>
+                  <entry>
+                    <link 
linkend='ugr.tools.ruta.ae.basic.parameter.maxRuleMatches'>maxRuleElementMatches</link>
+                  </entry>
+                  <entry>Maximum amount of allowed matches of a single rule 
element.
+                  </entry>
+                  <entry>Single Integer</entry>
+                </row>
+                
               </tbody>
             </tgroup>
           </table>
@@ -1266,6 +1283,18 @@ Document{-> EXEC(MyAnalysisEngine, {MyTy
                        applying the rules.
           </para>
         </section>
+        <section id="ugr.tools.ruta.ae.basic.parameter.maxRuleMatches">
+          <title>maxRuleMatches</title>
+          <para>
+             Maximum amount of allowed matches of a single rule.
+          </para>
+        </section>
+        <section id="ugr.tools.ruta.ae.basic.parameter.maxRuleElementMatches">
+          <title>maxRuleElementMatches</title>
+          <para>
+             Maximum amount of allowed matches of a single rule element.
+          </para>
+        </section>
       </section>
     </section>
     <section id="ugr.tools.ruta.ae.annotationwriter">


Reply via email to