@Test public void testConditionMacro() throws Exception { String script = "CONDITION isSmall() = REGEXP(\".\");\n"; script += "FOREACH(num) NUM{} {\n"; script += "num{isSmall()-> T1};"; script += "}"; CAS cas = RutaTestUtils.getCAS("1 22 333"); Ruta.apply(cas, script); RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "1"); }
@Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { BlockApply result = new BlockApply(this); getEnvironment().ensureMaterializedInitialValues(new MatchContext(this), stream); crowd.beginVisit(this, result); setRuleElementAnchor(); boolean leftToRight = true; if (direction != null) { leftToRight = direction.getBooleanValue(new MatchContext(getParent()), stream); } RuleApply apply = rule.apply(stream, crowd, true); List<AbstractRuleMatch<? extends AbstractRule>> list = apply.getList(); if (!leftToRight) { Collections.reverse(list); } for (AbstractRuleMatch<? extends AbstractRule> eachMatch : list) { if (eachMatch.matched()) { List<AnnotationFS> matchedAnnotations = ((RuleMatch) eachMatch).getMatchedAnnotations(null, null); if (matchedAnnotations == null || matchedAnnotations.isEmpty()) { continue; } for (AnnotationFS eachMatchedAnnotation : matchedAnnotations) { environment.addVariable(name, RutaConstants.RUTA_VARIABLE_ANNOTATION); environment.setVariableValue(name, eachMatchedAnnotation); for (RutaStatement element : elements) { if (element != null) { element.apply(stream, crowd); } } } } } crowd.endVisit(this, result); return result; }
ForEachBlock 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); setRuleElementAnchor(); boolean leftToRight = true; if (direction != null) { leftToRight = direction.getBooleanValue(new MatchContext(getParent()), stream); } RuleApply apply = rule.apply(stream, crowd, true); List<AbstractRuleMatch<? extends AbstractRule>> list = apply.getList(); if (!leftToRight) { Collections.reverse(list); } for (AbstractRuleMatch<? extends AbstractRule> eachMatch : list) { if (eachMatch.matched()) { List<AnnotationFS> matchedAnnotations = ((RuleMatch) eachMatch).getMatchedAnnotations(null, null); if (matchedAnnotations == null || matchedAnnotations.isEmpty()) { continue; } for (AnnotationFS eachMatchedAnnotation : matchedAnnotations) { environment.addVariable(name, RutaConstants.RUTA_VARIABLE_ANNOTATION); environment.setVariableValue(name, eachMatchedAnnotation); for (RutaStatement element : elements) { if (element != null) { element.apply(stream, crowd); } } } } } crowd.endVisit(this, result); return result; } }
ForEachBlock 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); setRuleElementAnchor(); boolean leftToRight = true; if (direction != null) { leftToRight = direction.getBooleanValue(new MatchContext(getParent()), stream); } RuleApply apply = rule.apply(stream, crowd, true); List<AbstractRuleMatch<? extends AbstractRule>> list = apply.getList(); if (!leftToRight) { Collections.reverse(list); } for (AbstractRuleMatch<? extends AbstractRule> eachMatch : list) { if (eachMatch.matched()) { List<AnnotationFS> matchedAnnotations = ((RuleMatch) eachMatch).getMatchedAnnotations(null, null); if (matchedAnnotations == null || matchedAnnotations.isEmpty()) { continue; } for (AnnotationFS eachMatchedAnnotation : matchedAnnotations) { environment.addVariable(name, RutaConstants.RUTA_VARIABLE_ANNOTATION); environment.setVariableValue(name, eachMatchedAnnotation); for (RutaStatement element : elements) { if (element != null) { element.apply(stream, crowd); } } } } } crowd.endVisit(this, result); return result; } ForEachBlock(String varName, IBooleanExpression direction, RutaRule rule,
          List<RutaStatement> elements, RutaBlock parent, String defaultNamespace); }
ForEachBlock 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); setRuleElementAnchor(); boolean leftToRight = true; if (direction != null) { leftToRight = direction.getBooleanValue(new MatchContext(getParent()), stream); } RuleApply apply = rule.apply(stream, crowd, true); List<AbstractRuleMatch<? extends AbstractRule>> list = apply.getList(); if (!leftToRight) { Collections.reverse(list); } for (AbstractRuleMatch<? extends AbstractRule> eachMatch : list) { if (eachMatch.matched()) { List<AnnotationFS> matchedAnnotations = ((RuleMatch) eachMatch).getMatchedAnnotations(null, null); if (matchedAnnotations == null || matchedAnnotations.isEmpty()) { continue; } for (AnnotationFS eachMatchedAnnotation : matchedAnnotations) { environment.addVariable(name, RutaConstants.RUTA_VARIABLE_ANNOTATION); environment.setVariableValue(name, eachMatchedAnnotation); for (RutaStatement element : elements) { if (element != null) { element.apply(stream, crowd); } } } } } crowd.endVisit(this, result); return result; } ForEachBlock(String varName, IBooleanExpression direction, RutaRule rule,
          List<RutaStatement> elements, RutaBlock parent, String defaultNamespace); @Override ScriptApply apply(RutaStream stream, InferenceCrowd crowd); @Override String toString(); IBooleanExpression getDirection(); }
ForEachBlock 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); setRuleElementAnchor(); boolean leftToRight = true; if (direction != null) { leftToRight = direction.getBooleanValue(new MatchContext(getParent()), stream); } RuleApply apply = rule.apply(stream, crowd, true); List<AbstractRuleMatch<? extends AbstractRule>> list = apply.getList(); if (!leftToRight) { Collections.reverse(list); } for (AbstractRuleMatch<? extends AbstractRule> eachMatch : list) { if (eachMatch.matched()) { List<AnnotationFS> matchedAnnotations = ((RuleMatch) eachMatch).getMatchedAnnotations(null, null); if (matchedAnnotations == null || matchedAnnotations.isEmpty()) { continue; } for (AnnotationFS eachMatchedAnnotation : matchedAnnotations) { environment.addVariable(name, RutaConstants.RUTA_VARIABLE_ANNOTATION); environment.setVariableValue(name, eachMatchedAnnotation); for (RutaStatement element : elements) { if (element != null) { element.apply(stream, crowd); } } } } } crowd.endVisit(this, result); return result; } ForEachBlock(String varName, IBooleanExpression direction, RutaRule rule,
          List<RutaStatement> elements, RutaBlock parent, String defaultNamespace); @Override ScriptApply apply(RutaStream stream, InferenceCrowd crowd); @Override String toString(); IBooleanExpression getDirection();  }