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);
}
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();
}
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);
}
}
}
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());
}
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)));
}
Aggregations