Github user cestella commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/395#discussion_r93146298
--- Diff:
metron-platform/metron-common/src/test/java/org/apache/metron/common/utils/StellarExecutorUtils.java
---
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.metron.common.utils;
+
+import org.apache.metron.common.dsl.Context;
+import org.apache.metron.common.dsl.MapVariableResolver;
+import org.apache.metron.common.dsl.StellarFunctions;
+import org.apache.metron.common.dsl.VariableResolver;
+import org.apache.metron.common.stellar.StellarPredicateProcessor;
+import org.junit.Assert;
+
+import java.util.Map;
+
+public class StellarExecutorUtils {
+
+ /**
+ * Execute a Stellar expression.
+ * @param expression The Stellar expression to execute.
+ * @param variables The variables to expose to the expression.
+ * @param context The execution context.
+ * @return The result of executing the Stellar expression.
+ */
+ public static Object run(String expression, Map<String, Object>
variables, Context context) {
+ return new StellarExecutor()
+ .withVariables(variables)
+ .withContext(context)
+ .execute(expression, Object.class);
+ }
+
+ /**
+ * Execute a Stellar expression.
+ * @param expression The Stellar expression to execute.
+ * @param variables The variables to expose to the expression.
+ * @return The result of executing the Stellar expression.
+ */
+ public static Object run(String expression, Map<String, Object>
variables) {
+ return new StellarExecutor()
+ .withVariables(variables)
+ .execute(expression, Object.class);
+ }
+
+ public static boolean runPredicate(String rule, Map resolver) {
+ return runPredicate(rule, resolver, Context.EMPTY_CONTEXT());
+ }
+
+ public static boolean runPredicate(String rule, Map resolver, Context
context) {
+ return runPredicate(rule, new MapVariableResolver(resolver), context);
+ }
+
+ public static boolean runPredicate(String rule, VariableResolver
resolver) {
+ return runPredicate(rule, resolver, Context.EMPTY_CONTEXT());
+ }
+
+ public static boolean runPredicate(String rule, VariableResolver
resolver, Context context) {
+ StellarPredicateProcessor processor = new StellarPredicateProcessor();
--- End diff --
If you intend on making this a replacement to `StellarProcessorUtils` then
we need to ensure that the same set of validations are done (e.g. `validate` is
called, serialization and deserialization are ensured):
```
/**
* This 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
*/
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, x -> variables.get(x),
StellarFunctions.FUNCTION_RESOLVER(), context);
byte[] raw = SerDeUtils.toBytes(ret);
Object actual = SerDeUtils.fromBytes(raw, Object.class);
Assert.assertEquals(ret, actual);
return ret;
}
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---