### Question:
RutaScriptBlock extends RutaBlock { @Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { BlockApply result = new BlockApply(this); getEnvironment().ensureMaterializedInitialValues(new MatchContext(this), stream); crowd.beginVisit(this, result); RuleApply apply = rule.apply(stream, crowd, true); for (AbstractRuleMatch<? extends AbstractRule> eachMatch : apply.getList()) { if (eachMatch.matched()) { List<AnnotationFS> matchedAnnotations = ((RuleMatch) eachMatch).getMatchedAnnotations(null, null); if (matchedAnnotations == null || matchedAnnotations.isEmpty()) { continue; } AnnotationFS each = matchedAnnotations.get(0); if (each == null) { continue; } RutaMatcher matcher = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher(); if (matcher == null) { continue; } Type type = matcher.getType(getParent() == null ? this : getParent(), stream); RutaStream window = stream.getWindowStream(each, type); for (RutaStatement element : getElements()) { if (element != null) { element.apply(window, crowd); } } } } crowd.endVisit(this, result); return result; } RutaScriptBlock(String id, RutaRule rule, List<RutaStatement> elements, RutaBlock parent,
          String defaultNamespace); @Override ScriptApply apply(RutaStream stream, InferenceCrowd crowd); @Override String toString();  }### Answer:
@Test public void testInnerDocumentMatch() throws Exception { String document = "Some text"; String script = ""; script += "CW{ -> CREATE(RutaAnnotation, \"score\"=1)};"; script += "CW{ -> CREATE(RutaAnnotation, \"score\"=2)};"; script += "BLOCK(forEach) RutaAnnotation.score==1{}{"; script += "Document{-> T1};"; script += "MARK(T2);"; script += "RutaAnnotation{-> T3};"; script += "}"; CAS cas = RutaTestUtils.getCAS(document); Ruta.apply(cas, script); RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "Some"); RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "Some"); RutaTestUtils.assertAnnotationsEquals(cas, 3, 2, "Some", "Some"); }
@Test public void testConditionMacro() throws Exception { String script = "CONDITION isSmall() = REGEXP(\".\");\n"; script += "BLOCK(num) NUM{} {\n"; script += "Document{isSmall()-> T1};"; script += "}"; CAS cas = RutaTestUtils.getCAS("1 22 333"); Ruta.apply(cas, script); RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "1"); }
@Test public void testWindow() throws Exception { String script = "NUM{->T1};"; script += "BLOCK(num) NUM{} {\n"; script += "NUM{-> T2};"; script += "}"; CAS cas = RutaTestUtils.getCAS("1 22 333"); Ruta.apply(cas, script); RutaTestUtils.assertAnnotationsEquals(cas, 2, 3, "1", "22", "333"); }