use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.
the class BasicStellarTest method testLogicalFunctions.
@Test
public void testLogicalFunctions() throws Exception {
final Map<String, String> variableMap = new HashMap<String, String>() {
{
put("foo", "casey");
put("ip", "192.168.0.1");
put("ip_src_addr", "192.168.0.1");
put("ip_dst_addr", "10.0.0.1");
put("other_ip", "10.168.0.1");
put("empty", "");
put("spaced", "metron is great");
}
};
Assert.assertTrue(runPredicate("IN_SUBNET(ip, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("IN_SUBNET(ip, '192.168.0.0/24', '11.0.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("IN_SUBNET(ip, '192.168.0.0/24', '11.0.0.0/24') in [true]", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("true in IN_SUBNET(ip, '192.168.0.0/24', '11.0.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("IN_SUBNET(ip_dst_addr, '192.168.0.0/24', '11.0.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("IN_SUBNET(other_ip, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
boolean thrown = false;
try {
runPredicate("IN_SUBNET(blah, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v)));
} catch (ParseException pe) {
thrown = true;
}
Assert.assertTrue(thrown);
Assert.assertTrue(runPredicate("true and STARTS_WITH(foo, 'ca')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("true and STARTS_WITH(TO_UPPER(foo), 'CA')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("(true and STARTS_WITH(TO_UPPER(foo), 'CA')) || true", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("true and ENDS_WITH(foo, 'sey')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_src_addr, '192.168.0.0/24') and IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("IN_SUBNET(ip_src_addr, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("not(IN_SUBNET(ip_src_addr, '192.168.0.0/24'))", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertFalse(runPredicate("IN_SUBNET(ip_dst_addr, '192.168.0.0/24')", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
Assert.assertTrue(runPredicate("not(IN_SUBNET(ip_dst_addr, '192.168.0.0/24'))", new DefaultVariableResolver(v -> variableMap.get(v), v -> variableMap.containsKey(v))));
}
use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.
the class DateFunctionsTest method run.
/**
* Runs a Stellar expression.
* @param expr The expression to run.
*/
private Object run(String expr) {
StellarProcessor processor = new StellarProcessor();
assertTrue(processor.validate(expr));
return processor.parse(expr, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), Context.EMPTY_CONTEXT());
}
use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.
the class MathFunctionsTest 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(v -> variables.get(v), v -> variables.containsKey(v)), StellarFunctions.FUNCTION_RESOLVER(), context);
}
use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.
the class LambdaExpression method apply.
public Object apply(List<Object> variableArgs) {
Map<String, Object> lambdaVariables = new HashMap<>();
int i = 0;
for (; i < Math.min(variables.size(), variableArgs.size()); ++i) {
lambdaVariables.put(variables.get(i), variableArgs.get(i));
}
for (; i < variables.size(); ++i) {
lambdaVariables.put(variables.get(i), null);
}
VariableResolver variableResolver = new DefaultVariableResolver(variable -> lambdaVariables.getOrDefault(variable, state.variableResolver.resolve(variable)), variable -> true);
StellarCompiler.ExpressionState localState = new StellarCompiler.ExpressionState(state.context, state.functionResolver, variableResolver);
return apply(localState);
}
use of org.apache.metron.stellar.dsl.DefaultVariableResolver in project metron by apache.
the class StellarStatisticsFunctionsTest method run.
/**
* Runs a Stellar expression.
* @param expr The expression to run.
* @param variables The variables available to the expression.
*/
private static Object run(String expr, Map<String, Object> variables) {
StellarProcessor processor = new StellarProcessor();
Object ret = processor.parse(expr, new DefaultVariableResolver(x -> variables.get(x), x -> variables.containsKey(x)), StellarFunctions.FUNCTION_RESOLVER(), Context.EMPTY_CONTEXT());
byte[] raw = SerDeUtils.toBytes(ret);
Object actual = SerDeUtils.fromBytes(raw, Object.class);
if (ret instanceof StatisticsProvider) {
StatisticsProvider left = (StatisticsProvider) ret;
StatisticsProvider right = (StatisticsProvider) actual;
// N
tolerantAssertEquals(prov -> prov.getCount(), left, right);
// sum
tolerantAssertEquals(prov -> prov.getSum(), left, right, 1e-3);
// sum of squares
tolerantAssertEquals(prov -> prov.getSumSquares(), left, right, 1e-3);
// sum of squares
tolerantAssertEquals(prov -> prov.getSumLogs(), left, right, 1e-3);
// Mean
tolerantAssertEquals(prov -> prov.getMean(), left, right, 1e-3);
// Quadratic Mean
tolerantAssertEquals(prov -> prov.getQuadraticMean(), left, right, 1e-3);
// SD
tolerantAssertEquals(prov -> prov.getStandardDeviation(), left, right, 1e-3);
// Variance
tolerantAssertEquals(prov -> prov.getVariance(), left, right, 1e-3);
// Min
tolerantAssertEquals(prov -> prov.getMin(), left, right, 1e-3);
// Max
tolerantAssertEquals(prov -> prov.getMax(), left, right, 1e-3);
// Kurtosis
tolerantAssertEquals(prov -> prov.getKurtosis(), left, right, 1e-3);
// Skewness
tolerantAssertEquals(prov -> prov.getSkewness(), left, right, 1e-3);
for (double d = 10.0; d < 100.0; d += 10) {
final double pctile = d;
// This is a sketch, so we're a bit more forgiving here in our choice of \epsilon.
tolerantAssertEquals(prov -> prov.getPercentile(pctile), left, right, 1e-2);
}
}
return ret;
}
Aggregations