### Question:
OnlyFirstBlock 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; } boolean stop = false; Type type = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher() .getType(getParent() == null ? this : getParent(), stream); RutaStream window = stream.getWindowStream(each, type); for (RutaStatement element : getElements()) { if (stop) break; if (element != null) { ScriptApply elementApply = element.apply(window, crowd); if (elementApply instanceof BlockApply) { BlockApply ba = (BlockApply) elementApply; if (ba.getRuleApply().getApplied() > 0) { stop = true; } } else if (elementApply instanceof RuleApply) { RuleApply ra = (RuleApply) elementApply; if (ra.getApplied() > 0) { stop = true; } } } } } } crowd.endVisit(this, result); return result; } OnlyFirstBlock(RutaBlock parent, String defaultNamespace); @Override ScriptApply apply(RutaStream stream, InferenceCrowd crowd);  }### Answer:
@Test public void test() { String document = "some text"; String script = "ONLYFIRST Document{}{\n"; script += "Document{-> T1};\n"; script += "Document{-> T2};\n"; script += "}"; Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(RutaEngine.PARAM_ADDITIONAL_EXTENSIONS, new String[] { OnlyFirstBlockExtension.class.getName() }); CAS cas = null; try { cas = RutaTestUtils.getCAS(document); Ruta.apply(cas, script, parameters); } catch (Exception e) { e.printStackTrace(); } Type t = null; AnnotationIndex<AnnotationFS> ai = null; FSIterator<AnnotationFS> iterator = null; t = RutaTestUtils.getTestType(cas, 1); ai = cas.getAnnotationIndex(t); assertEquals(1, ai.size()); iterator = ai.iterator(); assertEquals("some text", iterator.next().getCoveredText()); t = RutaTestUtils.getTestType(cas, 2); ai = cas.getAnnotationIndex(t); assertEquals(0, ai.size()); if (cas != null) { cas.release(); } }