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/08/02 15:29:14 UTC

svn commit: r1509697 - in /uima/sandbox/ruta/trunk/ruta-core/src: main/java/org/apache/uima/ruta/ main/java/org/apache/uima/ruta/rule/ main/java/org/apache/uima/ruta/verbalize/ main/java/org/apache/uima/ruta/visitor/ test/java/org/apache/uima/ruta/

Author: pkluegl
Date: Fri Aug  2 13:29:14 2013
New Revision: 1509697

URL: http://svn.apache.org/r1509697
Log:
UIMA-3115
- added simple explanation for inlined rule elements, which is actually not enough yet since it only adds the rule applies as delegates. There is no distinction between the rule elements and the matched texts on which the inlined rules are applied

Modified:
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/BlockApply.java
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/ScriptApply.java
    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/RuleMatch.java
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
    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/BlockApply.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/BlockApply.java?rev=1509697&r1=1509696&r2=1509697&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/BlockApply.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/BlockApply.java Fri Aug  2 13:29:14 2013
@@ -23,11 +23,12 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.uima.ruta.rule.RuleApply;
+import org.apache.uima.ruta.rule.RuleElement;
 
 public class BlockApply extends ScriptApply {
 
-  public BlockApply(RutaStatement tme) {
-    super(tme);
+  public BlockApply(RutaElement element) {
+    super(element);
   }
 
   private List<ScriptApply> innerApplies = new ArrayList<ScriptApply>();

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/ScriptApply.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/ScriptApply.java?rev=1509697&r1=1509696&r2=1509697&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/ScriptApply.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/ScriptApply.java Fri Aug  2 13:29:14 2013
@@ -23,15 +23,15 @@ public abstract class ScriptApply {
 
   public static int count = 0;
 
-  private final RutaStatement statement;
+  private final RutaElement element;
 
-  public ScriptApply(RutaStatement statement) {
+  public ScriptApply(RutaElement element) {
     super();
-    this.statement = statement;
+    this.element = element;
   }
 
-  public RutaStatement getElement() {
-    return statement;
+  public RutaElement getElement() {
+    return element;
   }
 
 }

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=1509697&r1=1509696&r2=1509697&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 Fri Aug  2 13:29:14 2013
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.BlockApply;
 import org.apache.uima.ruta.RutaBlock;
 import org.apache.uima.ruta.RutaElement;
 import org.apache.uima.ruta.RutaStatement;
@@ -99,16 +100,31 @@ public abstract class AbstractRuleElemen
           InferenceCrowd crowd) {
     List<ScriptApply> result = new ArrayList<ScriptApply>();
     List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOf(this);
+    // TODO where to implement the explanation of inlined rules?
+//    BlockApply blockApply = new BlockApply(this);
+//    RuleApply dummyRuleApply = getDummyRuleApply(ruleMatch);
+//    blockApply.setRuleApply(dummyRuleApply);
+//    ruleMatch.addDelegateApply(this, blockApply);
     for (AnnotationFS annotationFS : matchedAnnotationsOf) {
       RutaStream windowStream = stream.getWindowStream(annotationFS, annotationFS.getType());
       for (RutaStatement each : inlinedRules) {
         ScriptApply apply = each.apply(windowStream, crowd);
+//        blockApply.add(apply);
+        ruleMatch.addDelegateApply(this, apply);
         result.add(apply);
       }
     }
     return result;
   }
 
+  private RuleApply getDummyRuleApply(RuleMatch ruleMatch) {
+    List<RuleElement> list = new ArrayList<RuleElement>(1);
+    list.add(this);
+    RutaRule dummyRule = new RutaRule(list, parent, -1);
+    RuleApply result = new RuleApply(dummyRule, true);
+    return result;
+  }
+
   protected List<ScriptApply> processInlinedConditionRules(RuleMatch ruleMatch, RutaStream stream,
           InferenceCrowd crowd) {
     if (!inlineMode && inlinedRules != null && !inlinedRules.isEmpty()) {

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java?rev=1509697&r1=1509696&r2=1509697&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java Fri Aug  2 13:29:14 2013
@@ -29,6 +29,7 @@ import java.util.Set;
 
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaElement;
 import org.apache.uima.ruta.ScriptApply;
 import org.apache.uima.ruta.action.AbstractRutaAction;
 import org.apache.uima.ruta.engine.RutaEngine;
@@ -47,7 +48,7 @@ public class RuleMatch extends AbstractR
 
   private boolean applied = false;
 
-  private Map<AbstractRutaAction, ScriptApply> delegateApply;
+  private Map<RutaElement, ScriptApply> delegateApply;
 
   // private Map<RuleElement, List<RuleElementMatch>> map;
 
@@ -57,7 +58,7 @@ public class RuleMatch extends AbstractR
     super(rule);
     // map = new TreeMap<RuleElement, List<RuleElementMatch>>(
     // new RuleElementComparator(rule.getRoot()));
-    delegateApply = new HashMap<AbstractRutaAction, ScriptApply>(0);
+    delegateApply = new HashMap<RutaElement, ScriptApply>(0);
   }
 
   public boolean matchedCompletely() {
@@ -221,12 +222,12 @@ public class RuleMatch extends AbstractR
     return result.toString();
   }
 
-  public Map<AbstractRutaAction, ScriptApply> getDelegateApply() {
+  public Map<RutaElement, ScriptApply> getDelegateApply() {
     return delegateApply;
   }
 
-  public void addDelegateApply(AbstractRutaAction action, ScriptApply scriptApply) {
-    delegateApply.put(action, scriptApply);
+  public void addDelegateApply(RutaElement element, ScriptApply scriptApply) {
+    delegateApply.put(element, scriptApply);
   }
 
   public void setMatched(boolean matched) {
@@ -242,7 +243,7 @@ public class RuleMatch extends AbstractR
       copy.setRootMatch(rootMatch.copy(extendedContainerMatch));
     }
 
-    Map<AbstractRutaAction, ScriptApply> newDelegateApply = new HashMap<AbstractRutaAction, ScriptApply>(
+    Map<RutaElement, ScriptApply> newDelegateApply = new HashMap<RutaElement, ScriptApply>(
             delegateApply);
     copy.setDelegateApply(newDelegateApply);
     return copy;
@@ -252,7 +253,7 @@ public class RuleMatch extends AbstractR
     RuleMatch copy = new RuleMatch(rule);
     copy.setMatched(matched);
     copy.setRootMatch(rootMatch.copy());
-    Map<AbstractRutaAction, ScriptApply> newDelegateApply = new HashMap<AbstractRutaAction, ScriptApply>(
+    Map<RutaElement, ScriptApply> newDelegateApply = new HashMap<RutaElement, ScriptApply>(
             delegateApply);
     copy.setDelegateApply(newDelegateApply);
     return copy;
@@ -266,7 +267,7 @@ public class RuleMatch extends AbstractR
     }
   }
 
-  public void setDelegateApply(Map<AbstractRutaAction, ScriptApply> delegateApply) {
+  public void setDelegateApply(Map<RutaElement, ScriptApply> delegateApply) {
     this.delegateApply = delegateApply;
   }
 

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java?rev=1509697&r1=1509696&r2=1509697&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java Fri Aug  2 13:29:14 2013
@@ -33,6 +33,7 @@ import org.apache.uima.ruta.expression.R
 import org.apache.uima.ruta.expression.number.NumberExpression;
 import org.apache.uima.ruta.expression.string.StringExpression;
 import org.apache.uima.ruta.expression.type.TypeExpression;
+import org.apache.uima.ruta.rule.AbstractRuleElement;
 import org.apache.uima.ruta.rule.ComposedRuleElement;
 import org.apache.uima.ruta.rule.ConjunctRulesRuleElement;
 import org.apache.uima.ruta.rule.RegExpRule;
@@ -54,8 +55,14 @@ import org.apache.uima.ruta.rule.quantif
 
 public class ScriptVerbalizer {
 
+  private static final String CBCLOSE = "}";
+
+  private static final String CBOPEN = "{";
+
   private static final String THEN = " -> ";
 
+  private static final String THEN2 = " <- ";
+  
   private RutaVerbalizer verbalizer;
 
   public ScriptVerbalizer(RutaVerbalizer verbalizer) {
@@ -118,7 +125,7 @@ public class ScriptVerbalizer {
     result.append(verbalizeQuantifier(quantifier));
 
     if (!conditions.isEmpty() || !actions.isEmpty()) {
-      result.append("{");
+      result.append(CBOPEN);
       Iterator<AbstractRutaCondition> cit = conditions.iterator();
       while (cit.hasNext()) {
         AbstractRutaCondition each = cit.next();
@@ -138,7 +145,26 @@ public class ScriptVerbalizer {
           }
         }
       }
-      result.append("}");
+      result.append(CBCLOSE);
+    }
+    if(re instanceof AbstractRuleElement) {
+      AbstractRuleElement are = (AbstractRuleElement) re;
+      boolean blockMode = are.getInlineMode();
+      List<RutaStatement> inlinedRules = are.getInlinedRules();
+      if(inlinedRules != null && !inlinedRules.isEmpty()) {
+        if(blockMode) {
+          result.append(THEN);
+        } else {
+          result.append(THEN2);
+        }
+        result.append(CBOPEN);
+        for (RutaStatement rutaStatement : inlinedRules) {
+          result.append(verbalize(rutaStatement));
+          result.append(";");
+//          result.append("\n");
+        }
+        result.append(CBCLOSE);
+      }
     }
     return result.toString();
   }

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java?rev=1509697&r1=1509696&r2=1509697&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java Fri Aug  2 13:29:14 2013
@@ -31,9 +31,11 @@ import org.apache.uima.jcas.cas.FSArray;
 import org.apache.uima.ruta.BlockApply;
 import org.apache.uima.ruta.RutaBlock;
 import org.apache.uima.ruta.RutaElement;
+import org.apache.uima.ruta.RutaStatement;
 import org.apache.uima.ruta.RutaStream;
 import org.apache.uima.ruta.ScriptApply;
 import org.apache.uima.ruta.rule.AbstractRule;
+import org.apache.uima.ruta.rule.AbstractRuleElement;
 import org.apache.uima.ruta.rule.AbstractRuleMatch;
 import org.apache.uima.ruta.rule.ComposedRuleElement;
 import org.apache.uima.ruta.rule.ComposedRuleElementMatch;
@@ -86,6 +88,13 @@ public class DebugInfoFactory {
     List<DebugScriptApply> innerApply = new ArrayList<DebugScriptApply>();
     // TODO refactor and remove counting hotfix
     int applied = blockApply.getRuleApply().getApplied();
+    RutaElement element = blockApply.getElement();
+    String verbalize = "";
+    if(element instanceof RutaBlock) {
+      verbalize = verbalizer.verbalize((RutaBlock) element, false);
+    } else {
+      verbalize = verbalizer.verbalize(element);
+    }
     if (applied > 1) {
       List<ScriptApply> innerApplies = blockApply.getInnerApplies();
       List<List<ScriptApply>> loops = new ArrayList<List<ScriptApply>>();
@@ -123,7 +132,7 @@ public class DebugInfoFactory {
         counter++;
       }
       dba.setInnerApply(UIMAUtils.toFSArray(cas, innerApply));
-      dba.setElement(verbalizer.verbalize((RutaBlock) blockApply.getElement(), false));
+      dba.setElement(verbalize);
       DebugRuleApply ruleApply = createDebugRuleApply(blockApply.getRuleApply(), stream,
               addToIndex, withMatches, timeInfo);
       dba.setApplied(ruleApply.getApplied());
@@ -132,7 +141,7 @@ public class DebugInfoFactory {
       dba.setBegin(ruleApply.getBegin());
       dba.setEnd(ruleApply.getEnd());
       if (timeInfo != null) {
-        long time = timeInfo.get(blockApply.getElement());
+        long time = timeInfo.get(element);
         dba.setTime(time);
       }
       if (addToIndex)
@@ -144,7 +153,7 @@ public class DebugInfoFactory {
         innerApply.add(createDebugScriptApply(each, stream, addToIndex, withMatches, timeInfo));
       }
       dba.setInnerApply(UIMAUtils.toFSArray(cas, innerApply));
-      dba.setElement(verbalizer.verbalize((RutaBlock) blockApply.getElement(), false));
+      dba.setElement(verbalize);
       DebugRuleApply ruleApply = createDebugRuleApply(blockApply.getRuleApply(), stream,
               addToIndex, withMatches, timeInfo);
       dba.setApplied(ruleApply.getApplied());
@@ -153,8 +162,10 @@ public class DebugInfoFactory {
       dba.setBegin(ruleApply.getBegin());
       dba.setEnd(ruleApply.getEnd());
       if (timeInfo != null) {
-        long time = timeInfo.get(blockApply.getElement());
-        dba.setTime(time);
+        Long time = timeInfo.get(element);
+        if(time != null) {
+          dba.setTime(time);
+        }
       }
       if (addToIndex)
         dba.addToIndexes();
@@ -182,16 +193,27 @@ public class DebugInfoFactory {
       begin = end;
     }
     dra.setRules(UIMAUtils.toFSArray(cas, ruleMatches));
-    dra.setElement(verbalizer.verbalize(ruleApply.getElement()));
+    RutaElement element = ruleApply.getElement();
+    String namespace = "";
+    if(element instanceof RutaStatement) {
+      RutaStatement rs = (RutaStatement) element;
+      namespace = rs.getParent().getScript().getRootBlock().getNamespace();
+    } else if(element instanceof AbstractRuleElement) {
+      AbstractRuleElement are = (AbstractRuleElement) element;
+      are.getRule().getParent().getScript().getRootBlock().getNamespace();
+    }
+    dra.setElement(verbalizer.verbalize(element));
     dra.setApplied(ruleApply.getApplied());
     dra.setTried(ruleApply.getTried());
-    dra.setId(((AbstractRule) ruleApply.getElement()).getId());
-    dra.setScript(ruleApply.getElement().getParent().getScript().getRootBlock().getNamespace());
+    dra.setId(((AbstractRule) element).getId());
+    dra.setScript(namespace);
     dra.setBegin(begin);
     dra.setEnd(end);
     if (timeInfo != null) {
-      long time = timeInfo.get(ruleApply.getElement());
-      dra.setTime(time);
+      Long time = timeInfo.get(element);
+      if(time != null) {
+        dra.setTime(time);
+      }
     }
     if (addToIndex)
       dra.addToIndexes();
@@ -212,13 +234,13 @@ public class DebugInfoFactory {
     if (match instanceof RuleMatch) {
       ComposedRuleElementMatch rootMatch = ((RuleMatch) match).getRootMatch();
       setInnerMatches(stream, addToIndex, cas, drm, rootMatch);
-      if (match.matched()) {
+//      if (match.matched()) {
         List<DebugScriptApply> delegates = new ArrayList<DebugScriptApply>();
         for (ScriptApply rem : ((RuleMatch) match).getDelegateApply().values()) {
           delegates.add(createDebugScriptApply(rem, stream, addToIndex, withMatches, timeInfo));
         }
         drm.setDelegates(UIMAUtils.toFSArray(cas, delegates));
-      }
+//      }
     } else if (match instanceof RegExpRuleMatch) {
       RegExpRuleMatch rerm = (RegExpRuleMatch) match;
       Map<Integer, List<AnnotationFS>> map = rerm.getMap();

Modified: 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=1509697&r1=1509696&r2=1509697&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/InlinedRulesTest.java Fri Aug  2 13:29:14 2013
@@ -46,6 +46,7 @@ public class InlinedRulesTest {
     script += "T1{->T7}<-{CW COLON CW{->T7};};\n";
     script += "(T1 PERIOD{ -> T8} T1){CONTAINS(COLON)}<-{CW COLON CW{->T9}; COLON ANY{-> T10};};\n";
     script += "(T1 PERIOD{ -> T11} T1){CONTAINS(COLON)}<-{CW COLON CW{->T11};};\n";
+    script += "(T1 PERIOD T1{-> T12}){CONTAINS(COLON)}<-{W COLON (W W)<-{ANY{REGEXP(\"match.*\")};};};\n";
     CAS cas = null;
     try {
       cas = RutaTestUtils.getCAS(document);
@@ -123,6 +124,14 @@ public class InlinedRulesTest {
     iterator = ai.iterator();
     assertEquals(0, ai.size());
     
+    t = RutaTestUtils.getTestType(cas, 12);
+    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("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", iterator.next().getCoveredText());
+    
+    
     if (cas != null) {
       cas.release();
     }