Search in sources :

Example 1 with DefaultVariableResolver

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

the class BinFunctionsTest method run.

public static Object run(String rule, Map<String, Object> variables) {
    Context context = Context.EMPTY_CONTEXT();
    StellarProcessor processor = new StellarProcessor();
    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
    return processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) Test(org.junit.Test) Assert(org.junit.Assert) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 2 with DefaultVariableResolver

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

the class MedianAbsoluteDeviationTest method run.

public static Object run(String rule, Map<String, Object> variables) {
    Context context = Context.EMPTY_CONTEXT();
    StellarProcessor processor = new StellarProcessor();
    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
    return processor.parse(rule, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) TDistribution(org.apache.commons.math3.distribution.TDistribution) ImmutableMap(com.google.common.collect.ImmutableMap) SerDeUtils(org.apache.metron.common.utils.SerDeUtils) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) Test(org.junit.Test) GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) ArrayList(java.util.ArrayList) List(java.util.List) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Map(java.util.Map) StellarFunctions(org.apache.metron.stellar.dsl.StellarFunctions) Assert(org.junit.Assert) Context(org.apache.metron.stellar.dsl.Context) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 3 with DefaultVariableResolver

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

the class WindowLookbackTest method testSpecifyingOnlySelector.

@Test
public void testSpecifyingOnlySelector() {
    String stellarStatement = "PROFILE_WINDOW('1 hour')";
    Map<String, Object> variables = new HashMap<>();
    StellarProcessor stellar = new StellarProcessor();
    List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
    Assert.assertEquals(TimeUnit.HOURS.toMillis(1) / getDurationMs(), periods.size());
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) Test(org.junit.Test)

Example 4 with DefaultVariableResolver

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

the class WindowLookbackTest method test.

public State test(String windowSelector, Date now, Optional<Map<String, Object>> config, Assertions... assertions) {
    List<Range<Long>> windowIntervals = WindowProcessor.process(windowSelector).toIntervals(now.getTime());
    String stellarStatement = "PROFILE_WINDOW('" + windowSelector + "', now" + (config.isPresent() ? ", config" : "") + ")";
    Map<String, Object> variables = new HashMap<>();
    variables.put("now", now.getTime());
    if (config.isPresent()) {
        variables.put("config", config.get());
    }
    StellarProcessor stellar = new StellarProcessor();
    List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
    State state = new State(windowIntervals, periods);
    for (Assertions assertion : assertions) {
        Assert.assertTrue(assertion.name(), assertion.test(state));
    }
    return state;
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) Range(org.apache.commons.lang3.Range) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 5 with DefaultVariableResolver

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

the class KafkaFunctionsIntegrationTest method run.

/**
 * Runs a Stellar expression.
 * @param expr The expression to run.
 */
private Object run(String expr) {
    // make the global properties available to the function
    Context context = new Context.Builder().with(Context.Capabilities.GLOBAL_CONFIG, () -> global).build();
    FunctionResolver functionResolver = new SimpleFunctionResolver().withClass(KafkaFunctions.KafkaGet.class).withClass(KafkaFunctions.KafkaPut.class).withClass(KafkaFunctions.KafkaProps.class).withClass(KafkaFunctions.KafkaTail.class);
    StellarProcessor processor = new StellarProcessor();
    return processor.parse(expr, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), functionResolver, context);
}
Also used : Context(org.apache.metron.stellar.dsl.Context) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ComponentRunner(org.apache.metron.integration.ComponentRunner) FunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) BeforeClass(org.junit.BeforeClass) StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) Test(org.junit.Test) HashMap(java.util.HashMap) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) KafkaComponent(org.apache.metron.integration.components.KafkaComponent) ArrayList(java.util.ArrayList) List(java.util.List) BaseIntegrationTest(org.apache.metron.integration.BaseIntegrationTest) After(org.junit.After) Map(java.util.Map) ZKServerComponent(org.apache.metron.integration.components.ZKServerComponent) SimpleFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.SimpleFunctionResolver) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Context(org.apache.metron.stellar.dsl.Context) Before(org.junit.Before) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver) SimpleFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.SimpleFunctionResolver) FunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.FunctionResolver) SimpleFunctionResolver(org.apache.metron.stellar.dsl.functions.resolver.SimpleFunctionResolver)

Aggregations

DefaultVariableResolver (org.apache.metron.stellar.dsl.DefaultVariableResolver)21 Test (org.junit.Test)18 StellarProcessor (org.apache.metron.stellar.common.StellarProcessor)14 HashMap (java.util.HashMap)12 Context (org.apache.metron.stellar.dsl.Context)12 Assert (org.junit.Assert)11 Map (java.util.Map)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 StellarFunctions (org.apache.metron.stellar.dsl.StellarFunctions)8 ParseException (org.apache.metron.stellar.dsl.ParseException)7 StellarProcessorUtils.runPredicate (org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate)5 Joiner (com.google.common.base.Joiner)4 ImmutableList (com.google.common.collect.ImmutableList)4 java.util (java.util)4 List (java.util.List)4 Before (org.junit.Before)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 ArrayList (java.util.ArrayList)3 StringUtils (org.apache.commons.lang3.StringUtils)3 GaussianRandomGenerator (org.apache.commons.math3.random.GaussianRandomGenerator)2