Search in sources :

Example 1 with LogicalExpression

use of org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression in project graylog2-server by Graylog2.

the class PrecedenceTest method andVsEquality.

@Test
public void andVsEquality() {
    final Rule rule = parseRule("rule \"test\" when true == false && true then end");
    final LogicalExpression when = rule.when();
    assertThat(when).isInstanceOf(AndExpression.class);
    AndExpression andExpr = (AndExpression) when;
    assertThat(andExpr.left()).isInstanceOf(EqualityExpression.class);
    assertThat(andExpr.right()).isInstanceOf(BooleanExpression.class);
}
Also used : LogicalExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression) AndExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.AndExpression) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) BaseParserTest(org.graylog.plugins.pipelineprocessor.BaseParserTest) Test(org.junit.Test)

Example 2 with LogicalExpression

use of org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression in project graylog2-server by Graylog2.

the class PrecedenceTest method comparisonVsEqual.

@Test
public void comparisonVsEqual() {
    final Rule rule = parseRule("rule \"test\" when 1 > 2 == false then end");
    final LogicalExpression when = rule.when();
    assertThat(when).isInstanceOf(EqualityExpression.class);
    EqualityExpression topEqual = (EqualityExpression) when;
    assertThat(topEqual.left()).isInstanceOf(ComparisonExpression.class);
    assertThat(topEqual.right()).isInstanceOf(BooleanExpression.class);
}
Also used : LogicalExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) EqualityExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.EqualityExpression) BaseParserTest(org.graylog.plugins.pipelineprocessor.BaseParserTest) Test(org.junit.Test)

Example 3 with LogicalExpression

use of org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression in project graylog2-server by Graylog2.

the class PrecedenceTest method orVsEquality.

@Test
public void orVsEquality() {
    final Rule rule = parseRule("rule \"test\" when true == false || true then end");
    final LogicalExpression when = rule.when();
    assertThat(when).isInstanceOf(OrExpression.class);
    OrExpression orEprx = (OrExpression) when;
    assertThat(orEprx.left()).isInstanceOf(EqualityExpression.class);
    assertThat(orEprx.right()).isInstanceOf(BooleanExpression.class);
}
Also used : LogicalExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) OrExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.OrExpression) BaseParserTest(org.graylog.plugins.pipelineprocessor.BaseParserTest) Test(org.junit.Test)

Example 4 with LogicalExpression

use of org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression in project graylog2-server by Graylog2.

the class PipelineInterpreter method evaluateRuleCondition.

private boolean evaluateRuleCondition(Rule rule, Message message, String msgId, Pipeline pipeline, EvaluationContext context, List<Rule> rulesToRun, InterpreterListener interpreterListener) {
    interpreterListener.evaluateRule(rule, pipeline);
    final boolean matched;
    final LogicalExpression logicalExpression = rule.when();
    try {
        matched = logicalExpression.evaluateBool(context);
    } catch (Exception e) {
        context.onEvaluationException(e, logicalExpression);
        message.addProcessingError(new Message.ProcessingError(ProcessingFailureCause.RuleConditionEvaluationError, String.format(Locale.ENGLISH, "Error evaluating condition for rule <%s/%s> (pipeline <%s/%s>)", rule.name(), rule.id(), pipeline.name(), pipeline.id()), context.lastEvaluationError().toString()));
        interpreterListener.failEvaluateRule(rule, pipeline);
        return false;
    }
    if (matched) {
        rule.markMatch();
        interpreterListener.satisfyRule(rule, pipeline);
        log.debug("[{}] rule `{}` matches, scheduling to run", msgId, rule.name());
        rulesToRun.add(rule);
        return true;
    } else {
        rule.markNonMatch();
        interpreterListener.dissatisfyRule(rule, pipeline);
        log.debug("[{}] rule `{}` does not match", msgId, rule.name());
    }
    return false;
}
Also used : LogicalExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with LogicalExpression

use of org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression in project graylog2-server by Graylog2.

the class PrecedenceTest method parenGroup.

@Test
public void parenGroup() {
    final Rule rule = parseRule("rule \"test\" when true == (false == false) then end");
    final LogicalExpression when = rule.when();
    assertThat(when).isInstanceOf(EqualityExpression.class);
    EqualityExpression topEqual = (EqualityExpression) when;
    assertThat(topEqual.left()).isInstanceOf(BooleanExpression.class);
    assertThat(topEqual.right()).isInstanceOf(EqualityExpression.class);
    final BooleanExpression trueExpr = (BooleanExpression) topEqual.left();
    assertThat(trueExpr.evaluateBool(null)).isTrue();
    final EqualityExpression falseFalse = (EqualityExpression) topEqual.right();
    assertThat(falseFalse.evaluateBool(null)).isTrue();
}
Also used : LogicalExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression) BooleanExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.BooleanExpression) Rule(org.graylog.plugins.pipelineprocessor.ast.Rule) EqualityExpression(org.graylog.plugins.pipelineprocessor.ast.expressions.EqualityExpression) BaseParserTest(org.graylog.plugins.pipelineprocessor.BaseParserTest) Test(org.junit.Test)

Aggregations

LogicalExpression (org.graylog.plugins.pipelineprocessor.ast.expressions.LogicalExpression)6 BaseParserTest (org.graylog.plugins.pipelineprocessor.BaseParserTest)5 Rule (org.graylog.plugins.pipelineprocessor.ast.Rule)5 Test (org.junit.Test)5 AndExpression (org.graylog.plugins.pipelineprocessor.ast.expressions.AndExpression)2 EqualityExpression (org.graylog.plugins.pipelineprocessor.ast.expressions.EqualityExpression)2 ExecutionException (java.util.concurrent.ExecutionException)1 BooleanExpression (org.graylog.plugins.pipelineprocessor.ast.expressions.BooleanExpression)1 OrExpression (org.graylog.plugins.pipelineprocessor.ast.expressions.OrExpression)1