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 2013/07/31 17:55:26 UTC

svn commit: r1508946 - in /uima/sandbox/ruta/trunk/ruta-core/src: main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java test/java/org/apache/uima/ruta/InlinedRulesTest.java

Author: pkluegl
Date: Wed Jul 31 15:55:26 2013
New Revision: 1508946

URL: http://svn.apache.org/r1508946
Log:
UIMA-3115
- implemented "inline rules as block"
- added test

Added:
    uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java
Modified:
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java?rev=1508946&r1=1508945&r2=1508946&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java Wed Jul 31 15:55:26 2013
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.ruta.RutaBlock;
 import org.apache.uima.ruta.RutaElement;
 import org.apache.uima.ruta.RutaStatement;
@@ -53,7 +54,7 @@ public abstract class AbstractRuleElemen
   protected List<RutaStatement> inlinedRules;
 
   protected boolean inlineMode;
-  
+
   public AbstractRuleElement(RuleElementQuantifier quantifier,
           List<AbstractRutaCondition> conditions, List<AbstractRutaAction> actions,
           RuleElementContainer container, RutaBlock parent) {
@@ -85,12 +86,25 @@ public abstract class AbstractRuleElemen
     }
   }
 
+  protected void processInlinedRules(RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd) {
+    if (inlineMode) {
+      List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOf(this);
+      for (AnnotationFS annotationFS : matchedAnnotationsOf) {
+        RutaStream windowStream = stream.getWindowStream(annotationFS, annotationFS.getType());
+        for (RutaStatement each : inlinedRules) {
+          each.apply(windowStream, crowd);
+        }
+      }
+    }
+  }
+
   public void apply(RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd) {
     for (AbstractRutaAction action : actions) {
       crowd.beginVisit(action, null);
       action.execute(ruleMatch, this, stream, crowd);
       crowd.endVisit(action, null);
     }
+    processInlinedRules(ruleMatch, stream, crowd);
   }
 
   protected List<RuleElementMatch> getMatch(RuleMatch ruleMatch,
@@ -189,7 +203,7 @@ public abstract class AbstractRuleElemen
   public void setInlineMode(boolean blockMode) {
     this.inlineMode = blockMode;
   }
-  
+
   public List<RutaStatement> getInlinedRules() {
     return inlinedRules;
   }
@@ -197,5 +211,5 @@ public abstract class AbstractRuleElemen
   public boolean getInlineMode() {
     return inlineMode;
   }
-  
+
 }

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java?rev=1508946&r1=1508945&r2=1508946&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java Wed Jul 31 15:55:26 2013
@@ -295,7 +295,8 @@ public class WildCardRuleElement extends
     boolean doneHere = false;
     while (!doneHere && iterator.isValid() && stream.isVisible(iterator.get())) {
       AnnotationFS nextOne = iterator.get();
-      RutaBasic endAnchor = stream.getEndAnchor(nextOne.getBegin());
+      int pointer = after? nextOne.getBegin() : nextOne.getEnd();
+      RutaBasic anchor = stream.getAnchor(!after, pointer);
       ComposedRuleElementMatch extendedContainerMatch = containerMatch.copy();
       RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch);
 
@@ -305,12 +306,12 @@ public class WildCardRuleElement extends
       if (extendedMatch.matched()) {
         ComposedRuleElementMatch nextContainerMatch = getContainerMatchOfNextElement(
                 extendedContainerMatch, nextDepth);
-        if (endAnchor == null) {
+        if (anchor == null) {
           result = nextElement.startMatch(extendedMatch, ruleApply, nextContainerMatch,
                   nextElement, stream, crowd);
         } else {
           // TODO match and containermatch should be on the correct level!
-          result = nextElement.continueMatch(after, endAnchor, extendedMatch, ruleApply,
+          result = nextElement.continueMatch(after, anchor, extendedMatch, ruleApply,
                   nextContainerMatch, sideStepOrigin, nextElement, stream, crowd);
         }
         List<RuleElementMatch> nextList = nextContainerMatch.getInnerMatches().get(nextElement);

Added: uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java?rev=1508946&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java (added)
+++ uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java Wed Jul 31 15:55:26 2013
@@ -0,0 +1,114 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.ruta.engine.Ruta;
+import org.junit.Test;
+
+public class InlinedRulesTest {
+
+  @Test
+  public void test() {
+    String document = "The Ruta language is an imperative rule language extended with scripting elements. A Ruta rule defines a pattern of annotations with additional conditions. If this pattern applies, then the actions of the rule are performed  on the matched annotations. A rule is composed of a sequence of rule elements and a rule element essentially consists of four parts: A matching condition, an optional quantifier, a list of conditions and a list of actions. The matching condition is typically a type of an annotation by which the rule element matches on the covered text of one of those annotations. The quantifier specifies, whether it is necessary that the rule element successfully matches and how often the rule element may match. The list of conditions specifies additional constraints that the matched text or annotations need to fulfill. The list of actions defines the consequences of the rule and often creates new annotations or modifies existing annotations.";
+    String script = "";
+    script += "PERIOD #{-> T1} @PERIOD;\n";
+    script += "#{-> T1} PERIOD;\n";
+    // inlined as block
+    script += "T1{STARTSWITH(Document)}->{CW{->T2};};\n";
+    script += "(T1 PERIOD T1){CONTAINS(COLON)}->{COMMA #{->T3} COMMA; COLON ANY{-> T4};};\n";
+    script += "(COLON # COMMA)->{ANY{REGEXP(\"a.*\", true)-> T5};};";
+    // inlined as condition
+    script += "T1{->T6}<-{ANY COLON ANY{->T6};};\n";
+    script += "T1{->T7}<-{CW COLON CW{->T8};};\n";
+    CAS cas = null;
+    try {
+      cas = RutaTestUtils.getCAS(document);
+      Ruta.apply(cas, script);
+    } catch (Exception e) {
+    }
+
+    Type t = null;
+    AnnotationIndex<AnnotationFS> ai = null;
+    FSIterator<AnnotationFS> iterator = null;
+
+
+    
+    t = RutaTestUtils.getTestType(cas, 2);
+    ai = cas.getAnnotationIndex(t);
+    iterator = ai.iterator();
+    assertEquals(2, ai.size());
+    assertEquals("The", iterator.next().getCoveredText());
+    assertEquals("Ruta", iterator.next().getCoveredText());
+    
+    t = RutaTestUtils.getTestType(cas, 3);
+    ai = cas.getAnnotationIndex(t);
+    iterator = ai.iterator();
+    assertEquals(3, ai.size());
+    assertEquals("then the actions of the rule are performed  on the matched annotations. A rule is composed of a sequence of rule elements and a rule element essentially consists of four parts: A matching condition", iterator.next().getCoveredText());
+    assertEquals("an optional quantifier", iterator.next().getCoveredText());
+    assertEquals("an optional quantifier", iterator.next().getCoveredText());
+    
+    t = RutaTestUtils.getTestType(cas, 4);
+    ai = cas.getAnnotationIndex(t);
+    iterator = ai.iterator();
+    assertEquals(2, ai.size());
+    assertEquals("A", iterator.next().getCoveredText());
+    assertEquals("A", iterator.next().getCoveredText());
+    
+    t = RutaTestUtils.getTestType(cas, 5);
+    ai = cas.getAnnotationIndex(t);
+    iterator = ai.iterator();
+    assertEquals(1, ai.size());
+    assertEquals("A", iterator.next().getCoveredText());
+
+//    for (AnnotationFS a : ai) {
+//      System.out.println(a.getCoveredText());
+//    }
+//    t = RutaTestUtils.getTestType(cas, 6);
+//    ai = cas.getAnnotationIndex(t);
+//    iterator = ai.iterator();
+//    assertEquals(2, ai.size());
+//    assertEquals("A rule is composed of a sequence of rule elements and a rule element essentially consists of four parts: A matching condition, an optional quantifier, a list of conditions and a list of actions", iterator.next().getCoveredText());
+//    assertEquals("A", iterator.next().getCoveredText());
+//    
+//    t = RutaTestUtils.getTestType(cas, 7);
+//    ai = cas.getAnnotationIndex(t);
+//    iterator = ai.iterator();
+//    assertEquals(0, ai.size());
+//    
+//    t = RutaTestUtils.getTestType(cas, 8);
+//    ai = cas.getAnnotationIndex(t);
+//    iterator = ai.iterator();
+//    assertEquals(1, ai.size());
+//    assertEquals("A", iterator.next().getCoveredText());
+    
+    if (cas != null) {
+      cas.release();
+    }
+
+  }
+}