UIMA-6414: Ruta: missing match for optional after sidestep out of composed

- set sideStepOrigin if the sequence can be continued on same level
diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java b/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
index 2207f17..f367d61 100644
--- a/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
+++ b/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
@@ -113,8 +113,12 @@
       } else {

         RuleElement nextRuleElement = getContainer().getNextElement(newDirection, this);

         if (nextRuleElement != null) {

+          RuleElement sideStepOrigin = null;

+          if (getContainer() instanceof RuleElement) {

+            sideStepOrigin = (RuleElement) getContainer();

+          }

           result = nextRuleElement.continueMatch(newDirection, annotation, ruleMatch, ruleApply,

-                  sideStepContainerMatch, null, null, stream, crowd);

+                  sideStepContainerMatch, sideStepOrigin, entryPoint, stream, crowd);

         } else if (getContainer() instanceof ComposedRuleElement) {

           ComposedRuleElement composed = (ComposedRuleElement) getContainer();

           result = composed.fallbackContinue(newDirection, false, annotation, ruleMatch, ruleApply,

@@ -127,7 +131,8 @@
 

   protected void doneMatching(RuleMatch ruleMatch, RuleApply ruleApply, RutaStream stream,

           InferenceCrowd crowd) {

-    // do not execute actions if they already have been or if this is just a lookahead (ruleApply==null)

+    // do not execute actions if they already have been or if this is just a lookahead

+    // (ruleApply==null)

     if (!ruleMatch.isApplied() && ruleApply != null) {

       ruleApply.add(ruleMatch, stream);

       if (ruleMatch.matchedCompletely()) {

diff --git a/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java b/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java
index 385478e..e7d4a6c 100644
--- a/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java
+++ b/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java
@@ -41,7 +41,7 @@
   }
 
   @Test
-  public void testComposedInSequence() throws Exception {
+  public void testAcrossComposedInSequence() throws Exception {
     String text = "bla CAP 1-2 bla";
 
     String script = "FOREACH(cap) CAP{}{";
@@ -53,4 +53,16 @@
 
     RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "1-2");
   }
+
+  @Test
+  public void testLeaveComposedInSequence() throws Exception {
+    String text = "bla w CAP w bla";
+    String script = "(W @CAP W) {->T1} ANY{-PARTOF(NUM)};";
+
+    CAS cas = RutaTestUtils.getCAS(text);
+    Ruta.apply(cas, script);
+
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "w CAP w");
+  }
+
 }