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 2019/11/13 15:35:46 UTC

svn commit: r1869746 - in /uima/ruta/trunk: ruta-core/src/main/java/org/apache/uima/ruta/rule/ ruta-core/src/main/java/org/apache/uima/ruta/visitor/ ruta-core/src/test/java/org/apache/uima/ruta/visitor/ ruta-typesystem/src/main/resources/org/apache/uim...

Author: pkluegl
Date: Wed Nov 13 15:35:46 2019
New Revision: 1869746

URL: http://svn.apache.org/viewvc?rev=1869746&view=rev
Log:
UIMA-3139: create better debug annotations for inlined rules

Added:
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/visitor/
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/visitor/InlinedRulesExplanationTest.java
Modified:
    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/ComposedRuleElement.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.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/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
    uima/ruta/trunk/ruta-typesystem/src/main/resources/org/apache/uima/ruta/engine/RutaInternalTypeSystem.xml

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=1869746&r1=1869745&r2=1869746&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 Wed Nov 13 15:35:46 2019
@@ -140,18 +140,36 @@ public abstract class AbstractRuleElemen
     }
   }
 
+  protected void processInlinedActionRules(RuleMatch ruleMatch, RutaStream stream,
+          InferenceCrowd crowd) {
+    List<List<RuleElementMatch>> matchInfo = ruleMatch.getMatchInfo(this);
+    // TODO: which rule element match should be used? all? should context matter?
+    if (matchInfo == null || matchInfo.isEmpty() || matchInfo.get(0) == null
+            || matchInfo.get(0).isEmpty()) {
+      return;
+    }
+    RuleElementMatch ruleElementMatch = matchInfo.get(0).get(0);
+    processInlinedActionRules(ruleMatch, ruleElementMatch, stream, crowd);
+  }
+
   protected List<List<ScriptApply>> processInlinedActionRules(RuleMatch ruleMatch,
-          RutaStream stream, InferenceCrowd crowd) {
+          RuleElementMatch ruleElementMatch, RutaStream stream, InferenceCrowd crowd) {
     if (inlinedActionRuleBlocks != null && !inlinedActionRuleBlocks.isEmpty()) {
-      return processInlinedRules(inlinedActionRuleBlocks, ruleMatch, stream, crowd);
+      List<List<ScriptApply>> inlinedBlocksApplies = processInlinedRules(inlinedActionRuleBlocks,
+              ruleMatch, stream, crowd);
+      ruleElementMatch.setInlinedActionRules(inlinedBlocksApplies);
+      return inlinedBlocksApplies;
     }
     return null;
   }
 
   protected List<List<ScriptApply>> processInlinedConditionRules(RuleMatch ruleMatch,
-          RutaStream stream, InferenceCrowd crowd) {
+          RuleElementMatch ruleElementMatch, RutaStream stream, InferenceCrowd crowd) {
     if (inlinedConditionRuleBlocks != null && !inlinedConditionRuleBlocks.isEmpty()) {
-      return processInlinedRules(inlinedConditionRuleBlocks, ruleMatch, stream, crowd);
+      List<List<ScriptApply>> inlinedBlocksApplies = processInlinedRules(inlinedConditionRuleBlocks,
+              ruleMatch, stream, crowd);
+      ruleElementMatch.setInlinedConditionRules(inlinedBlocksApplies);
+      return inlinedBlocksApplies;
     }
     return null;
   }
@@ -159,20 +177,14 @@ public abstract class AbstractRuleElemen
   protected List<List<ScriptApply>> processInlinedRules(List<List<RutaStatement>> inlinedRuleBlocks,
           RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd) {
     List<List<ScriptApply>> result = new ArrayList<>();
+
     List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOfElement(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 (List<RutaStatement> inlinedRules : inlinedRuleBlocks) {
         List<ScriptApply> blockResult = new ArrayList<>();
         for (RutaStatement each : inlinedRules) {
           ScriptApply apply = each.apply(windowStream, crowd);
-          // blockApply.add(apply);
-          ruleMatch.addDelegateApply(this, apply);
           blockResult.add(apply);
         }
         result.add(blockResult);
@@ -188,12 +200,15 @@ public abstract class AbstractRuleElemen
       action.execute(new MatchContext(this, ruleMatch), stream, crowd);
       crowd.endVisit(action, null);
     }
+
     processInlinedActionRules(ruleMatch, stream, crowd);
   }
 
-  protected boolean matchInnerRules(RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd) {
+  protected boolean matchInlinedRules(RuleMatch ruleMatch, RuleElementMatch ruleElementMatch,
+          RutaStream stream, InferenceCrowd crowd) {
 
-    List<List<ScriptApply>> blockResults = processInlinedConditionRules(ruleMatch, stream, crowd);
+    List<List<ScriptApply>> blockResults = processInlinedConditionRules(ruleMatch, ruleElementMatch,
+            stream, crowd);
     if (blockResults == null) {
       return true;
     }

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=1869746&r1=1869745&r2=1869746&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 Wed Nov 13 15:35:46 2019
@@ -577,7 +577,7 @@ public class ComposedRuleElement extends
     containerMatch.setConditionInfo(evaluatedConditions);
     containerMatch.evaluateInnerMatches(true, stream);
     if (containerMatch.matched()) {
-      boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+      boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, containerMatch, stream, crowd);
       containerMatch.setInlinedRulesMatched(inlinedRulesMatched);
     } else {
       // update label for failed match after evaluating conditions

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java?rev=1869746&r1=1869745&r2=1869746&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java Wed Nov 13 15:35:46 2019
@@ -24,6 +24,7 @@ import java.util.List;
 
 import org.apache.uima.cas.text.AnnotationFS;
 import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.ScriptApply;
 
 public class RuleElementMatch {
 
@@ -43,6 +44,10 @@ public class RuleElementMatch {
 
   private boolean ruleAnchor = false;
 
+  private List<List<ScriptApply>> inlinedActionRules;
+
+  private List<List<ScriptApply>> inlinedConditionRules;
+
   public RuleElementMatch(RuleElement ruleElement, ComposedRuleElementMatch containerMatch) {
     super();
     this.ruleElement = ruleElement;
@@ -54,8 +59,7 @@ public class RuleElementMatch {
     return ruleElement.getLabel();
   }
 
-  public void setMatchInfo(boolean baseCondition, List<AnnotationFS> texts,
-         RutaStream stream) {
+  public void setMatchInfo(boolean baseCondition, List<AnnotationFS> texts, RutaStream stream) {
     baseConditionMatched = baseCondition;
     textsMatched = texts;
     conditionsMatched = baseConditionMatched;
@@ -63,7 +67,7 @@ public class RuleElementMatch {
       containerMatch.addInnerMatch(ruleElement, this, stream);
     }
   }
-  
+
   public void setConditionInfo(boolean baseCondition, List<EvaluatedCondition> conditionList) {
     baseConditionMatched = baseCondition;
     conditions = conditionList;
@@ -74,7 +78,6 @@ public class RuleElementMatch {
       }
     }
   }
-  
 
   public boolean matched() {
     return baseConditionMatched && conditionsMatched && inlinedRulesMatched;
@@ -159,4 +162,22 @@ public class RuleElementMatch {
   public void setInlinedRulesMatched(boolean inlinedRulesMatched) {
     this.inlinedRulesMatched = inlinedRulesMatched;
   }
+
+  public void setInlinedActionRules(List<List<ScriptApply>> inlinedBlocksApplies) {
+    this.inlinedActionRules = inlinedBlocksApplies;
+  }
+
+  public void setInlinedConditionRules(List<List<ScriptApply>> inlinedBlocksApplies) {
+    this.inlinedConditionRules = inlinedBlocksApplies;
+
+  }
+
+  public List<List<ScriptApply>> getInlinedActionRules() {
+    return this.inlinedActionRules;
+  }
+
+  public List<List<ScriptApply>> getInlinedConditionRules() {
+    return this.inlinedConditionRules;
+
+  }
 }

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=1869746&r1=1869745&r2=1869746&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 Wed Nov 13 15:35:46 2019
@@ -88,7 +88,7 @@ public class RutaOptionalRuleElement ext
     }
     result.setConditionInfo(base, evaluatedConditions);
     if (result.matched()) {
-      boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+      boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, result, stream, crowd);
       result.setInlinedRulesMatched(inlinedRulesMatched);
     } else {
       // update label for failed match after evaluating conditions

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=1869746&r1=1869745&r2=1869746&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 Wed Nov 13 15:35:46 2019
@@ -359,7 +359,7 @@ public class RutaRuleElement extends Abs
     }
     result.setConditionInfo(base, evaluatedConditions);
     if (result.matched()) {
-      boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+      boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, result, stream, crowd);
       result.setInlinedRulesMatched(inlinedRulesMatched);
     } else {
       // update label for failed match after evaluating conditions

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=1869746&r1=1869745&r2=1869746&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 Wed Nov 13 15:35:46 2019
@@ -771,7 +771,7 @@ public class WildCardRuleElement extends
     }
     result.setConditionInfo(base, evaluatedConditions);
     if (result.matched()) {
-      boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+      boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, result, stream, crowd);
       result.setInlinedRulesMatched(inlinedRulesMatched);
     } else {
       // update label for failed match after evaluating conditions

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java?rev=1869746&r1=1869745&r2=1869746&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java Wed Nov 13 15:35:46 2019
@@ -79,7 +79,7 @@ public class DebugInfoCollectorVisitor i
   }
 
   public boolean createDebugInfo(RutaRule rule) {
-    return createDebugInfo || ids.contains("" + rule.getId());
+    return createDebugInfo || ids.contains(String.valueOf(rule.getId()));
   }
 
   @Override

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java?rev=1869746&r1=1869745&r2=1869746&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java Wed Nov 13 15:35:46 2019
@@ -26,6 +26,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.util.FSCollectionFactory;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.jcas.cas.FSArray;
 import org.apache.uima.ruta.RutaElement;
@@ -223,7 +224,7 @@ public class DebugInfoFactory {
     drm.setMatched(match.matchedCompletely());
     if (match instanceof RuleMatch) {
       ComposedRuleElementMatch rootMatch = ((RuleMatch) match).getRootMatch();
-      setInnerMatches(stream, addToIndex, cas, drm, rootMatch);
+      setInnerMatches(stream, addToIndex, withMatches, timeInfo, drm, rootMatch);
       // if (match.matched()) {
       List<DebugScriptApply> delegates = new ArrayList<DebugScriptApply>();
       for (ScriptApply rem : ((RuleMatch) match).getDelegateApply().values()) {
@@ -288,46 +289,57 @@ public class DebugInfoFactory {
     return drm;
   }
 
-  private void setInnerMatches(RutaStream stream, boolean addToIndex, JCas cas, DebugRuleMatch drm,
-          ComposedRuleElementMatch rootMatch) {
+  private void setInnerMatches(RutaStream stream, boolean addToIndex, boolean withMatches,
+          Map<RutaElement, Long> timeInfo, DebugRuleMatch drm, ComposedRuleElementMatch rootMatch) {
     Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = rootMatch.getInnerMatches()
             .entrySet();
     List<DebugRuleElementMatches> ruleElementMatches = new ArrayList<DebugRuleElementMatches>();
     for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
       RuleElement re = entry.getKey();
       List<RuleElementMatch> rems = entry.getValue();
-      ruleElementMatches.add(createDebugRuleElementMatches(re, rems, stream, addToIndex));
+      ruleElementMatches.add(
+              createDebugRuleElementMatches(re, rems, stream, addToIndex, withMatches, timeInfo));
     }
 
-    drm.setElements(UIMAUtils.toFSArray(cas, ruleElementMatches));
+    drm.setElements(UIMAUtils.toFSArray(stream.getJCas(), ruleElementMatches));
   }
 
-  private void setInnerMatches(RutaStream stream, boolean addToIndex, JCas cas,
-          DebugRuleElementMatch drm, ComposedRuleElementMatch rootMatch) {
+  private void setInnerMatches(RutaStream stream, boolean addToIndex, boolean withMatches,
+          Map<RutaElement, Long> timeInfo, DebugRuleElementMatch drm,
+          ComposedRuleElementMatch rootMatch) {
     Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = rootMatch.getInnerMatches()
             .entrySet();
     List<DebugRuleElementMatches> ruleElementMatches = new ArrayList<DebugRuleElementMatches>();
     for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
       RuleElement re = entry.getKey();
       List<RuleElementMatch> rems = entry.getValue();
-      ruleElementMatches.add(createDebugRuleElementMatches(re, rems, stream, addToIndex));
+      ruleElementMatches.add(
+              createDebugRuleElementMatches(re, rems, stream, addToIndex, withMatches, timeInfo));
     }
-    drm.setElements(UIMAUtils.toFSArray(cas, ruleElementMatches));
+    drm.setElements(UIMAUtils.toFSArray(stream.getJCas(), ruleElementMatches));
   }
 
   public DebugRuleElementMatches createDebugRuleElementMatches(RuleElement re,
-          List<RuleElementMatch> rems, RutaStream stream, boolean addToIndex) {
+          List<RuleElementMatch> rems, RutaStream stream, boolean addToIndex, boolean withMatches,
+          Map<RutaElement, Long> timeInfo) {
     JCas cas = stream.getJCas();
     DebugRuleElementMatches drems = new DebugRuleElementMatches(cas);
     drems.setElement(verbalizer.verbalize(re));
     List<DebugRuleElementMatch> remList = new ArrayList<DebugRuleElementMatch>();
     if (rems != null) {
       for (RuleElementMatch each : rems) {
+        DebugRuleElementMatch rem = null;
         if (each instanceof ComposedRuleElementMatch) {
-          remList.add(createDebugComposedRuleElementMatch((ComposedRuleElementMatch) each, stream,
-                  addToIndex));
+          rem = createDebugComposedRuleElementMatch((ComposedRuleElementMatch) each, stream,
+                  addToIndex, withMatches, timeInfo);
         } else {
-          remList.add(createDebugRuleElementMatch(each, stream, addToIndex));
+          rem = createDebugRuleElementMatch(each, stream, addToIndex);
+        }
+        FSArray inlinedConditionRules = createInlinedRules(each.getInlinedConditionRules(), stream,
+                addToIndex, withMatches, timeInfo);
+        rem.setInlinedConditionRules(inlinedConditionRules);
+        if (rem != null) {
+          remList.add(rem);
         }
       }
     }
@@ -335,20 +347,56 @@ public class DebugInfoFactory {
       drems.setRuleAnchor(rems.get(0).isRuleAnchor());
     }
     drems.setMatches(UIMAUtils.toFSArray(cas, remList));
+
+    FSArray inlinedActionRules = createInlinedActionRules(rems, stream, addToIndex, withMatches,
+            timeInfo);
+    drems.setInlinedActionRules(inlinedActionRules);
+
     if (addToIndex)
       drems.addToIndexes();
     return drems;
   }
 
+  private FSArray createInlinedRules(List<List<ScriptApply>> blocks, RutaStream stream,
+          boolean addToIndex, boolean withMatches, Map<RutaElement, Long> timeInfo) {
+    JCas jcas = stream.getJCas();
+    if (blocks == null || blocks.isEmpty()) {
+      return null;
+    }
+
+    List<FSArray> resultList = new ArrayList<>();
+    for (List<ScriptApply> block : blocks) {
+      List<DebugScriptApply> list = new ArrayList<>();
+      for (ScriptApply ruleApply : block) {
+        DebugScriptApply debugScriptApply = createDebugScriptApply(ruleApply, stream, addToIndex,
+                withMatches, timeInfo);
+        list.add(debugScriptApply);
+      }
+      resultList.add(FSCollectionFactory.createFSArray(jcas, list));
+    }
+    return FSCollectionFactory.createFSArray(jcas, resultList);
+  }
+
+  private FSArray createInlinedActionRules(List<RuleElementMatch> rems, RutaStream stream,
+          boolean addToIndex, boolean withMatches, Map<RutaElement, Long> timeInfo) {
+    if (rems == null || rems.isEmpty()) {
+      return null;
+    }
+
+    return createInlinedRules(rems.get(0).getInlinedActionRules(), stream, addToIndex, withMatches,
+            timeInfo);
+  }
+
   public DebugRuleElementMatch createDebugComposedRuleElementMatch(ComposedRuleElementMatch rem,
-          RutaStream stream, boolean addToIndex) {
+          RutaStream stream, boolean addToIndex, boolean withMatches,
+          Map<RutaElement, Long> timeInfo) {
     JCas cas = stream.getJCas();
     DebugRuleElementMatch drem = new DebugRuleElementMatch(cas);
 
     DebugEvaluatedCondition base = new DebugEvaluatedCondition(cas);
     base.setValue(rem.isBaseConditionMatched());
 
-    setInnerMatches(stream, addToIndex, cas, drem, rem);
+    setInnerMatches(stream, addToIndex, withMatches, timeInfo, drem, rem);
 
     String baseString = verbalizer.verbalize(rem.getRuleElement());
     base.setElement(baseString);
@@ -395,6 +443,7 @@ public class DebugInfoFactory {
         drem.setEnd(end);
       }
     }
+
     if (addToIndex)
       drem.addToIndexes();
     return drem;

Added: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/visitor/InlinedRulesExplanationTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/visitor/InlinedRulesExplanationTest.java?rev=1869746&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/visitor/InlinedRulesExplanationTest.java (added)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/visitor/InlinedRulesExplanationTest.java Wed Nov 13 15:35:46 2019
@@ -0,0 +1,105 @@
+/*
+ * 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.visitor;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.jcas.JCas;
+import org.apache.uima.jcas.cas.FSArray;
+import org.apache.uima.ruta.engine.Ruta;
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.ruta.engine.RutaTestUtils;
+import org.apache.uima.ruta.type.DebugBlockApply;
+import org.apache.uima.ruta.type.DebugMatchedRuleMatch;
+import org.apache.uima.ruta.type.DebugRuleApply;
+import org.apache.uima.ruta.type.DebugRuleElementMatch;
+import org.apache.uima.ruta.type.DebugRuleElementMatches;
+import org.apache.uima.ruta.type.DebugScriptApply;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class InlinedRulesExplanationTest {
+
+  @Test
+  public void test() throws Exception {
+
+    String document = "This is a test.";
+    String script = "";
+    script += "CW<-{ANY;W;}<-{ANY;CW;}->{Document;} SW<-{ANY; SW;}->{Document; ANY;}->{ANY; NUM;};";
+
+    CAS cas = RutaTestUtils.getCAS(document);
+    Map<String, Object> parameters = new HashMap<>();
+    parameters.put(RutaEngine.PARAM_DEBUG, Boolean.TRUE);
+    parameters.put(RutaEngine.PARAM_DEBUG_WITH_MATCHES, Boolean.TRUE);
+
+    Ruta.apply(cas, script, parameters);
+
+    JCas jcas = cas.getJCas();
+
+    Collection<DebugScriptApply> debugScriptApplies = JCasUtil.select(jcas, DebugScriptApply.class);
+    Assert.assertEquals(1, debugScriptApplies.size());
+    DebugScriptApply debugScriptApply = debugScriptApplies.iterator().next();
+    Assert.assertTrue(debugScriptApply instanceof DebugBlockApply);
+    DebugBlockApply debugBlockApply = (DebugBlockApply) debugScriptApply;
+
+    FSArray innerApply = debugBlockApply.getInnerApply();
+    Assert.assertEquals(1, innerApply.size());
+    FeatureStructure innerApplyFS = innerApply.get(0);
+    Assert.assertTrue(debugScriptApply instanceof DebugRuleApply);
+    DebugRuleApply debugRuleApply = (DebugRuleApply) innerApplyFS;
+
+    FSArray rules = debugRuleApply.getRules();
+    Assert.assertEquals(1, rules.size());
+    FeatureStructure ruleMatchFS = rules.get(0);
+    Assert.assertTrue(ruleMatchFS instanceof DebugMatchedRuleMatch);
+    DebugMatchedRuleMatch debugMatchedRuleMatch = (DebugMatchedRuleMatch) ruleMatchFS;
+    Assert.assertEquals(0, debugMatchedRuleMatch.getDelegates().size());
+
+    FSArray reMatchesArray = debugMatchedRuleMatch.getElements();
+    Assert.assertEquals(2, reMatchesArray.size());
+    FeatureStructure reMatches1FS = reMatchesArray.get(0);
+    FeatureStructure reMatches2FS = reMatchesArray.get(1);
+    Assert.assertTrue(reMatches1FS instanceof DebugRuleElementMatches);
+    Assert.assertTrue(reMatches2FS instanceof DebugRuleElementMatches);
+    DebugRuleElementMatches re1Matches = (DebugRuleElementMatches) reMatches1FS;
+    DebugRuleElementMatches re2Matches = (DebugRuleElementMatches) reMatches2FS;
+
+    FSArray inlinedActionRules1 = re1Matches.getInlinedActionRules();
+    FSArray inlinedActionRules2 = re2Matches.getInlinedActionRules();
+
+    Assert.assertEquals(1, inlinedActionRules1.size());
+    Assert.assertEquals(2, inlinedActionRules2.size());
+
+    DebugRuleElementMatch re1Match = (DebugRuleElementMatch) re1Matches.getMatches().get(0);
+    DebugRuleElementMatch re2Match = (DebugRuleElementMatch) re2Matches.getMatches().get(0);
+
+    FSArray inlinedConditionRules1 = re1Match.getInlinedConditionRules();
+    FSArray inlinedConditionRules2 = re2Match.getInlinedConditionRules();
+
+    Assert.assertEquals(2, inlinedConditionRules1.size());
+    Assert.assertEquals(1, inlinedConditionRules2.size());
+
+  }
+}

Modified: uima/ruta/trunk/ruta-typesystem/src/main/resources/org/apache/uima/ruta/engine/RutaInternalTypeSystem.xml
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-typesystem/src/main/resources/org/apache/uima/ruta/engine/RutaInternalTypeSystem.xml?rev=1869746&r1=1869745&r2=1869746&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-typesystem/src/main/resources/org/apache/uima/ruta/engine/RutaInternalTypeSystem.xml (original)
+++ uima/ruta/trunk/ruta-typesystem/src/main/resources/org/apache/uima/ruta/engine/RutaInternalTypeSystem.xml Wed Nov 13 15:35:46 2019
@@ -17,7 +17,6 @@
   specific language governing permissions and limitations
   under the License.
 -->
-
 <typeSystemDescription xmlns="http://uima.apache.org/resourceSpecifier">
   <name>org.apache.uima.ruta.engine.RutaInternalTypeSystem</name>
   <description/>
@@ -156,6 +155,13 @@
           <elementType>org.apache.uima.ruta.type.DebugRuleElementMatches</elementType>
           <multipleReferencesAllowed>false</multipleReferencesAllowed>
         </featureDescription>
+        <featureDescription>
+          <name>inlinedConditionRules</name>
+          <description/>
+          <rangeTypeName>uima.cas.FSArray</rangeTypeName>
+          <elementType>org.apache.uima.ruta.type.DebugScriptApply</elementType>
+          <multipleReferencesAllowed>false</multipleReferencesAllowed>
+        </featureDescription>
       </features>
     </typeDescription>
     <typeDescription>
@@ -179,6 +185,13 @@
           <description/>
           <rangeTypeName>uima.cas.Boolean</rangeTypeName>
         </featureDescription>
+        <featureDescription>
+          <name>inlinedActionRules</name>
+          <description/>
+          <rangeTypeName>uima.cas.FSArray</rangeTypeName>
+          <elementType>org.apache.uima.ruta.type.DebugScriptApply</elementType>
+          <multipleReferencesAllowed>false</multipleReferencesAllowed>
+        </featureDescription>
       </features>
     </typeDescription>
     <typeDescription>