Search in sources :

Example 21 with Context

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

the class StellarProcessorUtils method run.

/**
 * This utility class is intended for use while unit testing Stellar operators.
 * It is included in the "main" code so third-party operators will not need
 * a test dependency on Stellar's test-jar.
 *
 * This class ensures the basic contract of a stellar expression is adhered to:
 * 1. Validate works on the expression
 * 2. The output can be serialized and deserialized properly
 *
 * @param rule
 * @param variables
 * @param context
 * @return ret
 */
public static Object run(String rule, Map<String, Object> variables, Context context) {
    StellarProcessor processor = new StellarProcessor();
    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
    Object ret = processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
    byte[] raw = SerDeUtils.toBytes(ret);
    Object actual = SerDeUtils.fromBytes(raw, Object.class);
    Assert.assertEquals(ret, actual);
    return ret;
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) VariableResolver(org.apache.metron.stellar.dsl.VariableResolver) Spliterators(java.util.Spliterators) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) IntConsumer(java.util.function.IntConsumer) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) MapVariableResolver(org.apache.metron.stellar.dsl.MapVariableResolver) StreamSupport(java.util.stream.StreamSupport) Assert(org.junit.Assert) StellarPredicateProcessor(org.apache.metron.stellar.common.StellarPredicateProcessor) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 22 with Context

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

the class StellarFilter method configure.

@Override
public void configure(Map<String, Object> config) {
    Object o = config.get(QUERY_STRING_CONF);
    if (o instanceof String) {
        query = o.toString();
    }
    Context stellarContext = (Context) config.get("stellarContext");
    if (stellarContext == null) {
        stellarContext = Context.EMPTY_CONTEXT();
    }
    processor.validate(query, true, stellarContext);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) JSONObject(org.json.simple.JSONObject)

Example 23 with Context

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

the class StellarClasspathFunctionResolver method create.

public static ClasspathFunctionResolver create(Properties config) {
    ClasspathFunctionResolver resolver = new ClasspathFunctionResolver();
    Context context = new Context.Builder().with(Context.Capabilities.STELLAR_CONFIG, () -> config).build();
    resolver.initialize(context);
    return resolver;
}
Also used : Context(org.apache.metron.stellar.dsl.Context) ClasspathFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.ClasspathFunctionResolver)

Example 24 with Context

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

the class BasicStellarTest method testContextActivityTypeReset.

@Test
public void testContextActivityTypeReset() {
    String query = "someVar";
    Context context = Context.EMPTY_CONTEXT();
    validate(query, context);
    Assert.assertNull(context.getActivityType());
    run(query, ImmutableMap.of("someVar", "someValue"), context);
    Assert.assertNull(context.getActivityType());
}
Also used : Context(org.apache.metron.stellar.dsl.Context) Test(org.junit.Test)

Example 25 with Context

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

the class ShellFunctionsTest method testListVarsWithVars.

@Test
public void testListVarsWithVars() {
    Map<String, VariableResult> variables = ImmutableMap.of("foo", VariableResult.withExpression(2.0, "1 + 1"));
    Context context = new Context.Builder().with(Context.Capabilities.SHELL_VARIABLES, () -> variables).build();
    Object out = run("SHELL_LIST_VARS()", new HashMap<>(), context);
    Assert.assertEquals(expectedListWithFoo, out);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) VariableResult(org.apache.metron.stellar.common.shell.VariableResult) SettingsBuilder(org.jboss.aesh.console.settings.SettingsBuilder) Test(org.junit.Test)

Aggregations

Context (org.apache.metron.stellar.dsl.Context)27 Test (org.junit.Test)15 HashMap (java.util.HashMap)14 Map (java.util.Map)12 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)9 List (java.util.List)8 ArrayList (java.util.ArrayList)6 DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 StellarFunctions (org.apache.metron.stellar.dsl.StellarFunctions)5 Assert (org.junit.Assert)5 ImmutableList (com.google.common.collect.ImmutableList)3 MapVariableResolver (org.apache.metron.stellar.dsl.MapVariableResolver)3 VariableResolver (org.apache.metron.stellar.dsl.VariableResolver)3 Before (org.junit.Before)3 Collectors (java.util.stream.Collectors)2 StellarPredicateProcessor (org.apache.metron.stellar.common.StellarPredicateProcessor)2 ConversionUtils (org.apache.metron.stellar.common.utils.ConversionUtils)2 ParseException (org.apache.metron.stellar.dsl.ParseException)2 FunctionResolver (org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver)2