Search in sources :

Example 6 with ParseException

use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.

the class StringFunctionsTest method testCountMatches.

/**
 * COUNT_MATCHES StringFunction
 */
@Test
public void testCountMatches() throws Exception {
    Assert.assertEquals(0, (int) run("COUNT_MATCHES(null, '*')", new HashedMap()));
    Assert.assertEquals(2, (int) run("COUNT_MATCHES('apachemetron', 'e')", new HashedMap()));
    Assert.assertEquals(2, (int) run("COUNT_MATCHES('anand', 'an')", new HashedMap()));
    Assert.assertEquals(0, (int) run("COUNT_MATCHES('abcd', null)", new HashedMap()));
    // No input
    boolean thrown = false;
    try {
        run("COUNT_MATCHES()", Collections.emptyMap());
    } catch (ParseException pe) {
        thrown = true;
        Assert.assertTrue(pe.getMessage().contains("incorrect arguments"));
    }
    Assert.assertTrue(thrown);
    thrown = false;
    // Incorrect number of arguments - 1
    try {
        run("COUNT_MATCHES('abc')", Collections.emptyMap());
    } catch (ParseException pe) {
        thrown = true;
        Assert.assertTrue(pe.getMessage().contains("incorrect arguments"));
    }
    Assert.assertTrue(thrown);
    thrown = false;
    // Integer input
    try {
        run("COUNT_MATCHES(123, 456)", Collections.emptyMap());
    } catch (ParseException pe) {
        thrown = true;
        Assert.assertTrue(pe.getMessage().contains("cannot be cast"));
    }
    Assert.assertTrue(thrown);
}
Also used : ParseException(org.apache.metron.stellar.dsl.ParseException) HashedMap(org.apache.commons.collections4.map.HashedMap) Test(org.junit.Test)

Example 7 with ParseException

use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.

the class WindowProcessor method process.

/**
 * Create a reusable Window object (parameterized by time) from a statement specifying the window intervals
 * conforming to the Window grammar.
 *
 * @param statement
 * @return Window returns a Window object (parameterized by time) from a statement specifying the window
 * intervals conforming to the Window grammar.
 * @throws ParseException
 */
public static Window process(String statement) throws ParseException {
    TokenStream tokens = createTokenStream(statement);
    if (tokens == null) {
        return null;
    }
    WindowProcessor treeBuilder = new WindowProcessor();
    WindowParser parser = createParser(tokens, Optional.of(treeBuilder));
    parser.window();
    if (treeBuilder.throwable != null) {
        throw new ParseException(treeBuilder.throwable.getMessage(), treeBuilder.throwable);
    }
    return treeBuilder.getWindow();
}
Also used : WindowParser(org.apache.metron.profiler.client.window.generated.WindowParser) ParseException(org.apache.metron.stellar.dsl.ParseException)

Example 8 with ParseException

use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.

the class DefaultProfileBuilder method assign.

/**
 * Executes a set of expressions whose results need to be assigned to a variable.
 *
 * @param expressions Maps the name of a variable to the expression whose result should be assigned to it.
 * @param transientState Additional transient state provided to the expression.
 * @param expressionType The type of expression; init, update, result.  Provides additional context if expression execution fails.
 */
private void assign(Map<String, String> expressions, Map<String, Object> transientState, String expressionType) {
    // for each expression...
    for (Map.Entry<String, String> entry : MapUtils.emptyIfNull(expressions).entrySet()) {
        String var = entry.getKey();
        String expr = entry.getValue();
        try {
            // assign the result of the expression to the variable
            executor.assign(var, expr, transientState);
        } catch (Throwable e) {
            // in-scope variables = persistent state maintained by the profiler + the transient state
            Set<String> variablesInScope = new HashSet<>();
            variablesInScope.addAll(transientState.keySet());
            variablesInScope.addAll(executor.getState().keySet());
            String msg = format("Bad '%s' expression: error='%s', expr='%s', profile='%s', entity='%s', variables-available='%s'", expressionType, e.getMessage(), expr, profileName, entity, variablesInScope);
            LOG.error(msg, e);
            throw new ParseException(msg, e);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ParseException(org.apache.metron.stellar.dsl.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with ParseException

use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.

the class StellarCompiler method exitVariable.

@Override
public void exitVariable(StellarParser.VariableContext ctx) {
    final FrameContext.Context context = getArgContext();
    expression.tokenDeque.push(new Token<>((tokenDeque, state) -> {
        String varName = ctx.getText();
        if (state.context.getActivityType().equals(ActivityType.PARSE_ACTIVITY) && !state.variableResolver.exists(varName)) {
            // when parsing, missing variables are an error!
            throw new ParseException(String.format("variable: %s is not defined", varName));
        }
        tokenDeque.push(new Token<>(state.variableResolver.resolve(varName), Object.class, context));
    }, DeferredFunction.class, context));
    expression.variablesUsed.add(ctx.getText());
}
Also used : ArithmeticEvaluator(org.apache.metron.stellar.common.evaluators.ArithmeticEvaluator) HashMap(java.util.HashMap) FunctionMarker(org.apache.metron.stellar.dsl.FunctionMarker) Deque(java.util.Deque) StellarParser(org.apache.metron.stellar.common.generated.StellarParser) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) LinkedList(java.util.LinkedList) Context(org.apache.metron.stellar.dsl.Context) FunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) Iterator(java.util.Iterator) Collection(java.util.Collection) ActivityType(org.apache.metron.stellar.dsl.Context.ActivityType) Set(java.util.Set) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) ComparisonExpressionWithOperatorEvaluator(org.apache.metron.stellar.common.evaluators.ComparisonExpressionWithOperatorEvaluator) StellarBaseListener(org.apache.metron.stellar.common.generated.StellarBaseListener) String.format(java.lang.String.format) Serializable(java.io.Serializable) List(java.util.List) NumberLiteralEvaluator(org.apache.metron.stellar.common.evaluators.NumberLiteralEvaluator) ConversionUtils(org.apache.metron.stellar.common.utils.ConversionUtils) Token(org.apache.metron.stellar.dsl.Token) ArrayDeque(java.util.ArrayDeque) ParseException(org.apache.metron.stellar.dsl.ParseException) Joiner(com.google.common.base.Joiner) Token(org.apache.metron.stellar.dsl.Token) ParseException(org.apache.metron.stellar.dsl.ParseException)

Example 10 with ParseException

use of org.apache.metron.stellar.dsl.ParseException in project metron by apache.

the class BasicStellarTest method testShortCircuit_boolean.

@Test
public void testShortCircuit_boolean() throws Exception {
    Assert.assertTrue(runPredicate("'metron' in ['metron', 'metronicus', 'mortron'] or (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("true or (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("true or (false or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("TO_UPPER('foo') == 'FOO' or (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("false and (true or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("true or false or false or true", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("false or (false and THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("'casey' == 'casey' or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("TO_UPPER('casey') == 'CASEY' or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("NOT(TO_UPPER('casey') != 'CASEY') or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("(TO_UPPER('casey') == 'CASEY') or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("NOT(NOT(TO_UPPER('casey') != 'CASEY') or THROW('exception'))", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertFalse(runPredicate("NOT(NOT(TO_UPPER('casey') != 'CASEY')) and THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    Assert.assertTrue(runPredicate("RET_TRUE('foo') or THROW('exception')", new DefaultVariableResolver(x -> null, x -> false)));
    boolean thrown = false;
    try {
        runPredicate("NOT(foo == null or THROW('exception')) and THROW('and exception')", new DefaultVariableResolver(x -> null, x -> false));
    } catch (ParseException pe) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
    thrown = false;
    try {
        runPredicate("(foo == null or THROW('exception') ) or THROW('and exception')", new DefaultVariableResolver(x -> null, x -> false));
    } catch (ParseException pe) {
        thrown = true;
    }
    Assert.assertTrue(thrown);
    Assert.assertTrue(runPredicate("( RET_TRUE('foo', true, false) or ( foo == null or THROW('exception') ) or THROW('and exception')) or THROW('or exception')", new DefaultVariableResolver(x -> null, x -> false)));
}
Also used : StellarProcessorUtils.run(org.apache.metron.stellar.common.utils.StellarProcessorUtils.run) java.util(java.util) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) StellarProcessorUtils.runPredicate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) StellarProcessorUtils.validate(org.apache.metron.stellar.common.utils.StellarProcessorUtils.validate) Test(org.junit.Test) StringUtils(org.apache.commons.lang3.StringUtils) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Stellar(org.apache.metron.stellar.dsl.Stellar) ClasspathFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.ClasspathFunctionResolver) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Assert(org.junit.Assert) ParseException(org.apache.metron.stellar.dsl.ParseException) ExpectedException(org.junit.rules.ExpectedException) Joiner(com.google.common.base.Joiner) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) ParseException(org.apache.metron.stellar.dsl.ParseException) Test(org.junit.Test)

Aggregations

ParseException (org.apache.metron.stellar.dsl.ParseException)12 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 HashedMap (org.apache.commons.collections4.map.HashedMap)4 Joiner (com.google.common.base.Joiner)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)3 StellarProcessorUtils.runPredicate (org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate)3 Context (org.apache.metron.stellar.dsl.Context)3 DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)3 StellarFunction (org.apache.metron.stellar.dsl.StellarFunction)3 Assert (org.junit.Assert)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 StringUtils (org.apache.commons.lang3.StringUtils)2 StellarProcessorUtils.run (org.apache.metron.stellar.common.utils.StellarProcessorUtils.run)2