http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarAssignment.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarAssignment.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarAssignment.java deleted file mode 100644 index c28ebfb..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarAssignment.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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.stellar; - - -import java.util.Map; - -public class StellarAssignment implements Map.Entry<String, Object>{ - private String variable; - private String statement; - - public StellarAssignment(String variable, String statement) { - this.variable = variable; - this.statement = statement; - } - - public String getVariable() { - return variable; - } - - public String getStatement() { - return statement; - } - - public static boolean isAssignment(String statement) { - return statement != null && statement.contains(":="); - } - - public static StellarAssignment from(String statement) { - if(statement == null || statement.length() == 0) { - return new StellarAssignment(null, null); - } - char prev = statement.charAt(0); - char curr; - String variable = "" + prev; - String s = null; - boolean isAssignment = false; - for(int i = 1;i < statement.length();++i,prev=curr) { - curr = statement.charAt(i); - if(prev == ':' && curr == '=') { - isAssignment = true; - variable = variable.substring(0, variable.length() - 1); - s = ""; - continue; - } - if(!isAssignment) { - variable += curr; - } - else { - s += curr; - } - } - - if(!isAssignment) { - s = variable; - variable = null; - } - - if(s != null) { - s = s.trim(); - } - if(variable != null) { - variable = variable.trim(); - } - return new StellarAssignment(variable, s); - } - - /** - * Returns the key corresponding to this entry. - * - * @return the key corresponding to this entry - * @throws IllegalStateException implementations may, but are not - * required to, throw this exception if the entry has been - * removed from the backing map. - */ - @Override - public String getKey() { - return variable; - } - - /** - * Returns the value corresponding to this entry. If the mapping - * has been removed from the backing map (by the iterator's - * <tt>remove</tt> operation), the results of this call are undefined. - * - * @return the value corresponding to this entry - * @throws IllegalStateException implementations may, but are not - * required to, throw this exception if the entry has been - * removed from the backing map. - */ - @Override - public Object getValue() { - return statement; - } - - /** - * Replaces the value corresponding to this entry with the specified - * value (optional operation). (Writes through to the map.) The - * behavior of this call is undefined if the mapping has already been - * removed from the map (by the iterator's <tt>remove</tt> operation). - * - * @param value new value to be stored in this entry - * @return old value corresponding to the entry - * @throws UnsupportedOperationException if the <tt>put</tt> operation - * is not supported by the backing map - * @throws ClassCastException if the class of the specified value - * prevents it from being stored in the backing map - * @throws NullPointerException if the backing map does not permit - * null values, and the specified value is null - * @throws IllegalArgumentException if some property of this value - * prevents it from being stored in the backing map - * @throws IllegalStateException implementations may, but are not - * required to, throw this exception if the entry has been - * removed from the backing map. - */ - @Override - public String setValue(Object value) { - throw new UnsupportedOperationException("Assignments are immutable."); - } -}
http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java deleted file mode 100644 index f4e101b..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java +++ /dev/null @@ -1,719 +0,0 @@ -/* - * 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.stellar; - -import org.antlr.v4.runtime.ParserRuleContext; -import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.dsl.VariableResolver; -import org.apache.metron.common.dsl.functions.resolver.FunctionResolver; -import org.apache.metron.common.stellar.evaluators.ArithmeticEvaluator; -import org.apache.metron.common.stellar.evaluators.ComparisonExpressionWithOperatorEvaluator; -import org.apache.metron.common.stellar.evaluators.NumberLiteralEvaluator; -import org.apache.metron.common.stellar.generated.StellarBaseListener; -import org.apache.metron.common.stellar.generated.StellarParser; -import com.google.common.base.Joiner; -import org.apache.commons.lang3.tuple.Pair; -import org.apache.metron.common.dsl.FunctionMarker; -import org.apache.metron.common.dsl.ParseException; -import org.apache.metron.common.dsl.StellarFunction; -import org.apache.metron.common.utils.ConversionUtils; - -import java.io.Serializable; -import java.util.*; - -import static java.lang.String.format; - -public class StellarCompiler extends StellarBaseListener { - private static Token<?> EXPRESSION_REFERENCE = new Token<>(null, Object.class); - private static Token<?> LAMBDA_VARIABLES = new Token<>(null, Object.class); - - private Expression expression; - private final ArithmeticEvaluator arithmeticEvaluator; - private final NumberLiteralEvaluator numberLiteralEvaluator; - private final ComparisonExpressionWithOperatorEvaluator comparisonExpressionWithOperatorEvaluator; - - public interface ShortCircuitOp {} - - public static class ShortCircuitFrame {} - public static class BooleanArg implements ShortCircuitOp {} - public static class IfExpr implements ShortCircuitOp {} - public static class ThenExpr implements ShortCircuitOp {} - public static class ElseExpr implements ShortCircuitOp {} - public static class EndConditional implements ShortCircuitOp {} - - public static class ExpressionState { - Context context; - FunctionResolver functionResolver; - VariableResolver variableResolver; - public ExpressionState(Context context - , FunctionResolver functionResolver - , VariableResolver variableResolver - ) { - this.context = context; - this.variableResolver = variableResolver; - this.functionResolver = functionResolver; - } - } - - public static class Expression implements Serializable { - final Deque<Token<?>> tokenDeque; - final Deque<FrameContext.Context> multiArgumentState; - final Set<String> variablesUsed; - public Expression(Deque<Token<?>> tokenDeque) { - this.tokenDeque = tokenDeque; - this.variablesUsed = new HashSet<>(); - this.multiArgumentState = new ArrayDeque<>(); - } - - public void clear() { - tokenDeque.clear(); - variablesUsed.clear(); - multiArgumentState.clear(); - } - - public Deque<Token<?>> getTokenDeque() { - return tokenDeque; - } - - public Object apply(ExpressionState state) { - Deque<Token<?>> instanceDeque = new ArrayDeque<>(); - { - boolean skipElse = false; - Token<?> token = null; - for (Iterator<Token<?>> it = getTokenDeque().descendingIterator(); it.hasNext(); ) { - token = it.next(); - //if we've skipped an else previously, then we need to skip the deferred tokens associated with the else. - if(skipElse && token.getUnderlyingType() == ElseExpr.class) { - while(it.hasNext()) { - token = it.next(); - if(token.getUnderlyingType() == EndConditional.class) { - break; - } - } - skipElse = false; - } - /* - curr is the current value on the stack. This is the non-deferred actual evaluation for this expression - and with the current context. - */ - Token<?> curr = instanceDeque.peek(); - if( curr != null - && curr.getValue() != null && curr.getValue() instanceof Boolean - && ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType()) - ) { - //if we have a boolean as the current value and the next non-contextual token is a short circuit op - //then we need to short circuit possibly - if(token.getUnderlyingType() == BooleanArg.class) { - if (curr.getMultiArgContext() != null - && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_OR - && (Boolean) (curr.getValue()) - ) { - //short circuit the or - FrameContext.Context context = curr.getMultiArgContext(); - shortCircuit(it, context); - } else if (curr.getMultiArgContext() != null - && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_AND - && !(Boolean) (curr.getValue()) - ) { - //short circuit the and - FrameContext.Context context = curr.getMultiArgContext(); - shortCircuit(it, context); - } - } - else if(token.getUnderlyingType() == IfExpr.class) { - //short circuit the if/then/else - instanceDeque.pop(); - if((Boolean)curr.getValue()) { - //choose then - skipElse = true; - } - else { - //choose else - while(it.hasNext()) { - Token<?> t = it.next(); - if(t.getUnderlyingType() == ElseExpr.class) { - break; - } - } - } - } - } - if (token.getUnderlyingType() == DeferredFunction.class) { - DeferredFunction func = (DeferredFunction) token.getValue(); - func.apply(instanceDeque, state); - } - else if(token.getUnderlyingType() != ShortCircuitFrame.class - && !ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType()) - ) { - instanceDeque.push(token); - } - - } - } - - if (instanceDeque.isEmpty()) { - throw new ParseException("Invalid predicate: Empty stack."); - } - Token<?> token = instanceDeque.pop(); - if (instanceDeque.isEmpty()) { - return token.getValue(); - } - if (instanceDeque.isEmpty()) { - throw new ParseException("Invalid parse, stack not empty: " + Joiner.on(',').join(instanceDeque)); - } else { - throw new ParseException("Invalid parse, found " + token); - } - } - - public void shortCircuit(Iterator<Token<?>> it, FrameContext.Context context) { - while (it.hasNext()) { - Token<?> token = it.next(); - if (token.getUnderlyingType() == ShortCircuitFrame.class && token.getMultiArgContext() == context) { - break; - } - } - } - } - - interface DeferredFunction { - void apply( Deque<Token<?>> tokenDeque - , ExpressionState state - ); - } - - public StellarCompiler( - final ArithmeticEvaluator arithmeticEvaluator, - final NumberLiteralEvaluator numberLiteralEvaluator, - final ComparisonExpressionWithOperatorEvaluator comparisonExpressionWithOperatorEvaluator - ){ - this(new Expression(new ArrayDeque<>()), arithmeticEvaluator, numberLiteralEvaluator, comparisonExpressionWithOperatorEvaluator); - } - - public StellarCompiler( - final Expression expression, - final ArithmeticEvaluator arithmeticEvaluator, - final NumberLiteralEvaluator numberLiteralEvaluator, - final ComparisonExpressionWithOperatorEvaluator comparisonExpressionWithOperatorEvaluator - ){ - this.expression = expression; - this.arithmeticEvaluator = arithmeticEvaluator; - this.numberLiteralEvaluator = numberLiteralEvaluator; - this.comparisonExpressionWithOperatorEvaluator = comparisonExpressionWithOperatorEvaluator; - } - - @Override - public void enterTransformation(StellarParser.TransformationContext ctx) { - expression.clear(); - } - - private boolean handleIn(final Token<?> left, final Token<?> right) { - Object key = right.getValue(); - - - if (left.getValue() != null) { - if (left.getValue() instanceof String && key instanceof String) { - return ((String) left.getValue()).contains(key.toString()); - } - else if (left.getValue() instanceof Collection) { - return ((Collection) left.getValue()).contains(key); - } - else if (left.getValue() instanceof Map) { - return ((Map) left.getValue()).containsKey(key); - } - else { - if (key == null) { - return key == left.getValue(); - } - else { - return key.equals(left.getValue()); - } - } - } else { - return false; - } - } - - @Override - public void exitNullConst(StellarParser.NullConstContext ctx) { - expression.tokenDeque.push(new Token<>(null, Object.class, getArgContext())); - } - - @Override - public void exitArithExpr_plus(StellarParser.ArithExpr_plusContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>((tokenDeque, state) -> { - Pair<Token<? extends Number>, Token<? extends Number>> p = getArithExpressionPair(tokenDeque); - tokenDeque.push(arithmeticEvaluator.evaluate(ArithmeticEvaluator.ArithmeticEvaluatorFunctions.addition(context), p)); - }, DeferredFunction.class, context)); - } - - @Override - public void exitArithExpr_minus(StellarParser.ArithExpr_minusContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Pair<Token<? extends Number>, Token<? extends Number>> p = getArithExpressionPair(tokenDeque); - tokenDeque.push(arithmeticEvaluator.evaluate(ArithmeticEvaluator.ArithmeticEvaluatorFunctions.subtraction(context), p)); - }, DeferredFunction.class, context)); - } - - @Override - public void exitArithExpr_div(StellarParser.ArithExpr_divContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Pair<Token<? extends Number>, Token<? extends Number>> p = getArithExpressionPair(tokenDeque); - tokenDeque.push(arithmeticEvaluator.evaluate(ArithmeticEvaluator.ArithmeticEvaluatorFunctions.division(context), p)); - }, DeferredFunction.class, context)); - } - - @Override - public void exitArithExpr_mul(StellarParser.ArithExpr_mulContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Pair<Token<? extends Number>, Token<? extends Number>> p = getArithExpressionPair(tokenDeque); - tokenDeque.push(arithmeticEvaluator.evaluate(ArithmeticEvaluator.ArithmeticEvaluatorFunctions.multiplication(context), p)); - }, DeferredFunction.class, context)); - } - - @SuppressWarnings("unchecked") - private Pair<Token<? extends Number>, Token<? extends Number>> getArithExpressionPair(Deque<Token<?>> tokenDeque) { - Token<? extends Number> right = (Token<? extends Number>) popDeque(tokenDeque); - Token<? extends Number> left = (Token<? extends Number>) popDeque(tokenDeque); - return Pair.of(left, right); - } - - @Override - public void exitIf_expr(StellarParser.If_exprContext ctx) { - expression.tokenDeque.push(new Token<>(new IfExpr(), IfExpr.class, getArgContext())); - } - - @Override - public void enterThen_expr(StellarParser.Then_exprContext ctx) { - expression.tokenDeque.push(new Token<>(new ThenExpr(), ThenExpr.class, getArgContext())); - } - - @Override - public void enterElse_expr(StellarParser.Else_exprContext ctx) { - expression.tokenDeque.push(new Token<>(new ElseExpr(), ElseExpr.class, getArgContext())); - } - - @Override - public void exitElse_expr(StellarParser.Else_exprContext ctx) { - expression.tokenDeque.push(new Token<>(new EndConditional(), EndConditional.class, getArgContext())); - } - - @Override - public void exitInExpressionStatement(StellarParser.InExpressionStatementContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Token<?> left = popDeque(tokenDeque); - Token<?> right = popDeque(tokenDeque); - tokenDeque.push(new Token<>(handleIn(left, right), Boolean.class, context)); - }, DeferredFunction.class, context)); - } - - - @Override - public void exitNInExpressionStatement(StellarParser.NInExpressionStatementContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Token<?> left = popDeque(tokenDeque); - Token<?> right = popDeque(tokenDeque); - tokenDeque.push(new Token<>(!handleIn(left, right), Boolean.class, context)); - }, DeferredFunction.class, context)); - } - - @Override - public void exitNotFunc(StellarParser.NotFuncContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Token<Boolean> arg = (Token<Boolean>) popDeque(tokenDeque); - tokenDeque.push(new Token<>(!arg.getValue(), Boolean.class, context)); - }, DeferredFunction.class, context)); - } - - - @Override - public void exitVariable(StellarParser.VariableContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - tokenDeque.push(new Token<>(state.variableResolver.resolve(ctx.getText()), Object.class, context)); - }, DeferredFunction.class, context)); - expression.variablesUsed.add(ctx.getText()); - } - - @Override - public void exitStringLiteral(StellarParser.StringLiteralContext ctx) { - String rawToken = ctx.getText(); - String literal = StringEscapeUtils.UNESCAPE_JSON.translate(rawToken); - expression.tokenDeque.push(new Token<>(literal.substring(1, literal.length()-1), String.class, getArgContext())); - } - - @Override - public void exitIntLiteral(StellarParser.IntLiteralContext ctx) { - expression.tokenDeque.push(numberLiteralEvaluator.evaluate(ctx, getArgContext())); - } - - @Override - public void exitDoubleLiteral(StellarParser.DoubleLiteralContext ctx) { - expression.tokenDeque.push(numberLiteralEvaluator.evaluate(ctx, getArgContext())); - } - - @Override - public void exitFloatLiteral(StellarParser.FloatLiteralContext ctx) { - expression.tokenDeque.push(numberLiteralEvaluator.evaluate(ctx, getArgContext())); - } - - @Override - public void exitLongLiteral(StellarParser.LongLiteralContext ctx) { - expression.tokenDeque.push(numberLiteralEvaluator.evaluate(ctx, getArgContext())); - } - - @Override - public void enterB_expr(StellarParser.B_exprContext ctx) { - //Enter is not guaranteed to be called by Antlr for logical labels, so we need to - //emulate it like this. See https://github.com/antlr/antlr4/issues/802 - if(ctx.getParent() instanceof StellarParser.LogicalExpressionOrContext) { - expression.multiArgumentState.push(FrameContext.BOOLEAN_OR.create()); - } - else if(ctx.getParent() instanceof StellarParser.LogicalExpressionAndContext) { - expression.multiArgumentState.push(FrameContext.BOOLEAN_AND.create()); - } - } - - @Override - public void exitB_expr(StellarParser.B_exprContext ctx) { - if(ctx.getParent() instanceof StellarParser.LogicalExpressionOrContext - || ctx.getParent() instanceof StellarParser.LogicalExpressionAndContext - ) - { - //we want to know when the argument to the boolean expression is complete - expression.tokenDeque.push(new Token<>(new BooleanArg(), BooleanArg.class, getArgContext())); - } - } - - @Override - public void exitLogicalExpressionAnd(StellarParser.LogicalExpressionAndContext ctx) { - final FrameContext.Context context = getArgContext(); - popArgContext(); - final FrameContext.Context parentContext = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Token<?> left = popDeque(tokenDeque); - Token<?> right = popDeque(tokenDeque); - tokenDeque.push(new Token<>(booleanOp(left, right, (l, r) -> l && r, "&&"), Boolean.class, parentContext)); - }, DeferredFunction.class, context)); - expression.tokenDeque.push(new Token<>(new ShortCircuitFrame(), ShortCircuitFrame.class, context)); - } - - @Override - public void exitLogicalExpressionOr(StellarParser.LogicalExpressionOrContext ctx) { - final FrameContext.Context context = getArgContext(); - popArgContext(); - final FrameContext.Context parentContext = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - Token<?> left = popDeque(tokenDeque); - Token<?> right = popDeque(tokenDeque); - - tokenDeque.push(new Token<>(booleanOp(left, right, (l, r) -> l || r, "||"), Boolean.class, parentContext)); - }, DeferredFunction.class, context)); - expression.tokenDeque.push(new Token<>(new ShortCircuitFrame(), ShortCircuitFrame.class, context)); - } - - @Override - public void exitLogicalConst(StellarParser.LogicalConstContext ctx) { - Boolean b; - switch (ctx.getText().toUpperCase()) { - case "TRUE": - b = true; - break; - case "FALSE": - b = false; - break; - default: - throw new ParseException("Unable to process " + ctx.getText() + " as a boolean constant"); - } - expression.tokenDeque.push(new Token<>(b, Boolean.class, getArgContext())); - } - - private boolean booleanOp(final Token<?> left, final Token<?> right, final BooleanOp op, final String opName) { - Boolean l = ConversionUtils.convert(left.getValue(), Boolean.class); - Boolean r = ConversionUtils.convert(right.getValue(), Boolean.class); - if (l == null || r == null) { - throw new ParseException("Unable to operate on " + left.getValue() + " " + opName + " " + right.getValue() + ", null value"); - } - return op.op(l, r); - } - - - @Override - public void enterSingle_lambda_variable(StellarParser.Single_lambda_variableContext ctx) { - enterLambdaVariables(); - } - - @Override - public void exitSingle_lambda_variable(StellarParser.Single_lambda_variableContext ctx) { - exitLambdaVariables(); - } - - @Override - public void enterLambda_variables(StellarParser.Lambda_variablesContext ctx) { - enterLambdaVariables(); - } - - @Override - public void exitLambda_variables(StellarParser.Lambda_variablesContext ctx) { - exitLambdaVariables(); - } - - @Override - public void exitLambda_variable(StellarParser.Lambda_variableContext ctx) { - expression.tokenDeque.push(new Token<>(ctx.getText(), String.class, getArgContext())); - } - - private void enterLambdaVariables() { - expression.tokenDeque.push(LAMBDA_VARIABLES); - } - - private void exitLambdaVariables() { - Token<?> t = expression.tokenDeque.pop(); - LinkedList<String> variables = new LinkedList<>(); - for(; !expression.tokenDeque.isEmpty() && t != LAMBDA_VARIABLES; t = expression.tokenDeque.pop()) { - variables.addFirst(t.getValue().toString()); - } - expression.tokenDeque.push(new Token<>(variables, List.class, getArgContext())); - } - - private void enterLambda() { - expression.tokenDeque.push(EXPRESSION_REFERENCE); - } - - private void exitLambda(boolean hasArgs) { - final FrameContext.Context context = getArgContext(); - Token<?> t = expression.tokenDeque.pop(); - final Deque<Token<?>> instanceDeque = new ArrayDeque<>(); - for(; !expression.tokenDeque.isEmpty() && t != EXPRESSION_REFERENCE; t = expression.tokenDeque.pop()) { - instanceDeque.addLast(t); - } - final List<String> variables = hasArgs? (List<String>) instanceDeque.removeLast().getValue() :new ArrayList<>(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - LambdaExpression expr = new LambdaExpression(variables, instanceDeque, state); - tokenDeque.push(new Token<>(expr, Object.class, context)); - }, DeferredFunction.class, context) ); - } - - @Override - public void enterLambda_with_args(StellarParser.Lambda_with_argsContext ctx) { - enterLambda(); - } - - @Override - public void exitLambda_with_args(StellarParser.Lambda_with_argsContext ctx) { - exitLambda(true); - } - - @Override - public void enterLambda_without_args(StellarParser.Lambda_without_argsContext ctx) { - enterLambda(); - } - - @Override - public void exitLambda_without_args(StellarParser.Lambda_without_argsContext ctx) { - exitLambda(false); - } - - @Override - public void exitTransformationFunc(StellarParser.TransformationFuncContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - // resolve and initialize the function - String functionName = ctx.getChild(0).getText(); - StellarFunction function = resolveFunction(state.functionResolver, functionName); - initializeFunction(state.context, function, functionName); - - // fetch the args, execute, and push result onto the stack - List<Object> args = getFunctionArguments(popDeque(tokenDeque)); - Object result = function.apply(args, state.context); - tokenDeque.push(new Token<>(result, Object.class, context)); - }, DeferredFunction.class, context)); - } - - /** - * Get function arguments. - * @param token The token containing the function arguments. - * @return - */ - @SuppressWarnings("unchecked") - private List<Object> getFunctionArguments(final Token<?> token) { - if (token.getUnderlyingType().equals(List.class)) { - return (List<Object>) token.getValue(); - - } else { - throw new ParseException("Unable to process in clause because " + token.getValue() + " is not a set"); - } - } - - /** - * Resolves a function by name. - * @param funcName - * @return - */ - private StellarFunction resolveFunction(FunctionResolver functionResolver, String funcName) { - try { - return functionResolver.apply(funcName); - - } catch (Exception e) { - String valid = Joiner.on(',').join(functionResolver.getFunctions()); - String error = format("Unable to resolve function named '%s'. Valid functions are %s", funcName, valid); - throw new ParseException(error, e); - } - } - - /** - * Initialize a Stellar function. - * @param function The function to initialize. - * @param functionName The name of the functions. - */ - private void initializeFunction(Context context, StellarFunction function, String functionName) { - try { - if (!function.isInitialized()) { - function.initialize(context); - } - } catch (Throwable t) { - String error = format("Unable to initialize function '%s'", functionName); - throw new ParseException(error, t); - } - } - - @Override - public void exitExistsFunc(StellarParser.ExistsFuncContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - String variable = ctx.getChild(2).getText(); - boolean exists = state.variableResolver.resolve(variable) != null; - tokenDeque.push(new Token<>(exists, Boolean.class, context)); - }, DeferredFunction.class, context)); - String variable = ctx.getChild(2).getText(); - expression.variablesUsed.add(variable); - } - - @Override - public void enterFunc_args(StellarParser.Func_argsContext ctx) { - expression.tokenDeque.push(new Token<>(new FunctionMarker(), FunctionMarker.class, getArgContext())); - } - - @Override - public void exitFunc_args(StellarParser.Func_argsContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>((tokenDeque, state) -> { - LinkedList<Object> args = new LinkedList<>(); - while (true) { - Token<?> token = popDeque(tokenDeque); - if (token.getUnderlyingType().equals(FunctionMarker.class)) { - break; - } else { - args.addFirst(token.getValue()); - } - } - tokenDeque.push(new Token<>(args, List.class, context)); - }, DeferredFunction.class, context)); - } - - @Override - public void enterMap_entity(StellarParser.Map_entityContext ctx) { - expression.tokenDeque.push(new Token<>(new FunctionMarker(), FunctionMarker.class, getArgContext())); - } - - @Override - public void exitMap_entity(StellarParser.Map_entityContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - HashMap<String, Object> args = new HashMap<>(); - Object value = null; - for (int i = 0; true; i++) { - Token<?> token = popDeque(tokenDeque); - if (token.getUnderlyingType().equals(FunctionMarker.class)) { - break; - } else { - if (i % 2 == 0) { - value = token.getValue(); - } else { - args.put(token.getValue() + "", value); - } - } - } - tokenDeque.push(new Token<>(args, Map.class, context)); - }, DeferredFunction.class, context)); - } - - @Override - public void exitList_entity(StellarParser.List_entityContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - LinkedList<Object> args = new LinkedList<>(); - while (true) { - Token<?> token = popDeque(tokenDeque); - if (token.getUnderlyingType().equals(FunctionMarker.class)) { - break; - } else { - args.addFirst(token.getValue()); - } - } - tokenDeque.push(new Token<>(args, List.class, context)); - }, DeferredFunction.class, context)); - } - - - - @Override - public void exitComparisonExpressionWithOperator(StellarParser.ComparisonExpressionWithOperatorContext ctx) { - final FrameContext.Context context = getArgContext(); - expression.tokenDeque.push(new Token<>( (tokenDeque, state) -> { - StellarParser.Comp_operatorContext op = ctx.comp_operator(); - Token<?> right = popDeque(tokenDeque); - Token<?> left = popDeque(tokenDeque); - - tokenDeque.push(comparisonExpressionWithOperatorEvaluator.evaluate(left, right, (StellarParser.ComparisonOpContext) op, context)); - }, DeferredFunction.class, context)); - } - - @Override - public void enterList_entity(StellarParser.List_entityContext ctx) { - expression.tokenDeque.push(new Token<>(new FunctionMarker(), FunctionMarker.class, getArgContext())); - } - - private void popArgContext() { - if(!expression.multiArgumentState.isEmpty()) { - expression.multiArgumentState.pop(); - } - } - - private FrameContext.Context getArgContext() { - return expression.multiArgumentState.isEmpty()?null:expression.multiArgumentState.peek(); - } - - private Token<?> popDeque(Deque<Token<?>> tokenDeque) { - if (tokenDeque.isEmpty()) { - throw new ParseException("Unable to pop an empty stack"); - } - return tokenDeque.pop(); - } - - public Expression getExpression() {return expression;} - -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarPredicateProcessor.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarPredicateProcessor.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarPredicateProcessor.java deleted file mode 100644 index bbde172..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarPredicateProcessor.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * 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.stellar; - - -import org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.functions.resolver.FunctionResolver; -import org.apache.metron.common.dsl.VariableResolver; - -import java.util.concurrent.TimeUnit; - -import static org.apache.commons.lang3.StringUtils.isEmpty; - -/** - * The Stellar Predicate Processor is intended to allow for specific predicate transformations using the Stellar - * domain specific language. In contrast to the StellarProcessor, which is a general purpose transformation - * tool, the output of the stellar statement is always a boolean. In java parlance, this is like a - * java.util.function.Predicate - */ - -public class StellarPredicateProcessor extends BaseStellarProcessor<Boolean> { - - /** - * Create a default stellar processor. This processor uses the static expression cache. - */ - public StellarPredicateProcessor() { - super(Boolean.class); - } - - public StellarPredicateProcessor(int cacheSize, int expiryTime, TimeUnit expiryUnit) { - super(Boolean.class, cacheSize, expiryTime, expiryUnit); - } - @Override - public Boolean parse( String rule - , VariableResolver variableResolver - , FunctionResolver functionResolver - , Context context - ) - { - if(rule == null || isEmpty(rule.trim())) { - return true; - } - try { - return super.parse(rule, variableResolver, functionResolver, context); - } catch (ClassCastException e) { - // predicate must return boolean - throw new IllegalArgumentException(String.format("The rule '%s' does not return a boolean value.", rule), e); - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarProcessor.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarProcessor.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarProcessor.java deleted file mode 100644 index 22a556a..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarProcessor.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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.stellar; - -import com.google.common.cache.Cache; - -import java.util.concurrent.TimeUnit; - -/** - * The Stellar Processor is intended to allow for general transformations using the Stellar - * domain specific language. In contrast to the StellarPredicateProcessor where - * the output of the stellar statement is always a boolean, this is intended for use when you - * need non-predicate transformation. In java parlance, this is similar to a java.util.function.Function - * rather than a java.util.function.Predicate - */ -public class StellarProcessor extends BaseStellarProcessor<Object> { - - /** - * Create a default stellar processor. This processor uses the static expression cache. - */ - public StellarProcessor() { - super(Object.class); - } - - /** - * Create a stellar processor with a new expression cache. NOTE: This object should be reused to prevent - * performance regressions. - * @param cacheSize - * @param expiryTime - * @param expiryUnit - */ - public StellarProcessor(int cacheSize, int expiryTime, TimeUnit expiryUnit) { - super(Object.class, cacheSize, expiryTime, expiryUnit); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/Microbenchmark.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/Microbenchmark.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/Microbenchmark.java deleted file mode 100644 index bce5014..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/Microbenchmark.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.stellar.benchmark; - -import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; -import org.apache.commons.math3.stat.descriptive.SummaryStatistics; -import org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.VariableResolver; -import org.apache.metron.common.dsl.functions.resolver.FunctionResolver; -import org.apache.metron.common.stellar.StellarProcessor; - -import java.util.function.Consumer; - -public class Microbenchmark { - - public static class StellarStatement { - String expression; - VariableResolver variableResolver; - FunctionResolver functionResolver; - Context context; - } - - public static DescriptiveStatistics run(StellarStatement statement, int warmupRounds, int benchmarkRounds ) - { - run(warmupRounds, statement, ts -> {}); - final DescriptiveStatistics stats = new DescriptiveStatistics(); - run(benchmarkRounds, statement, ts -> { stats.addValue(ts);}); - return stats; - } - - private static void run(int numTimes, StellarStatement statement, Consumer<Long> func) { - StellarProcessor processor = new StellarProcessor(); - for(int i = 0;i < numTimes;++i) { - long start = System.nanoTime(); - processor.parse(statement.expression, statement.variableResolver, statement.functionResolver, statement.context); - func.accept((System.nanoTime() - start)/1000); - } - } - - public static String describe(DescriptiveStatistics stats, Double[] percentiles){ - StringBuilder sb = new StringBuilder(); - sb.append(String.format("round: mean of %dms [+-%d], measured %d rounds;\n", - (long)stats.getMean(), - (long)stats.getStandardDeviation(), stats.getN() )); - sb.append("\tMin - " + (long)stats.getMin() + "\n"); - for(double pctile : percentiles) { - sb.append("\t" + pctile + " - " + stats.getPercentile(pctile) + "\n"); - } - sb.append("\tMax - " + (long)stats.getMax()); - return sb.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/StellarMicrobenchmark.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/StellarMicrobenchmark.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/StellarMicrobenchmark.java deleted file mode 100644 index faa55e0..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/benchmark/StellarMicrobenchmark.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * 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.stellar.benchmark; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.google.common.base.Joiner; -import com.google.common.base.Splitter; -import com.google.common.io.Files; -import org.apache.commons.cli.*; -import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; -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.utils.JSONUtils; -import org.apache.metron.common.utils.cli.OptionHandler; - -import javax.annotation.Nullable; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.charset.Charset; -import java.util.*; - -public class StellarMicrobenchmark { - - public static int DEFAULT_WARMUP = 100; - public static int DEFAULT_NUM_TIMES = 1000; - public static Double[] DEFAULT_PERCENTILES = new Double[] { - 50d, 75d, 95d, 99d - }; - - enum BenchmarkOptions { - HELP("h", new OptionHandler<BenchmarkOptions>() { - - @Nullable - @Override - public Option apply(@Nullable String s) { - return new Option(s, "help", false, "Generate Help screen"); - } - }), - WARMUP("w", new OptionHandler<BenchmarkOptions>() { - @Nullable - @Override - public Option apply(@Nullable String s) { - Option o = new Option(s, "warmup", true, "Number of times for warmup per expression. Default: " + DEFAULT_WARMUP); - o.setArgName("NUM"); - o.setRequired(false); - return o; - } - - @Override - public Optional<Object> getValue(BenchmarkOptions option, CommandLine cli) { - return Optional.ofNullable(option.get(cli).trim()); - } - }), - PERCENTILES("p", new OptionHandler<BenchmarkOptions>() { - @Nullable - @Override - public Option apply(@Nullable String s) { - Option o = new Option(s, "percentiles", true - , "Percentiles to calculate per run. Default: " + Joiner.on(",").join(Arrays.asList(DEFAULT_PERCENTILES)) - ); - o.setArgName("NUM"); - o.setRequired(false); - return o; - } - - @Override - public Optional<Object> getValue(BenchmarkOptions option, CommandLine cli) { - return Optional.ofNullable(option.get(cli).trim()); - } - }), - NUM_TIMES("n", new OptionHandler<BenchmarkOptions>() { - @Nullable - @Override - public Option apply(@Nullable String s) { - Option o = new Option(s, "num_times", true, "Number of times to run per expression (after warmup). Default: " + DEFAULT_NUM_TIMES); - o.setArgName("NUM"); - o.setRequired(false); - return o; - } - - @Override - public Optional<Object> getValue(BenchmarkOptions option, CommandLine cli) { - return Optional.ofNullable(option.get(cli).trim()); - } - }), - EXPRESSIONS("e", new OptionHandler<BenchmarkOptions>() { - @Nullable - @Override - public Option apply(@Nullable String s) { - Option o = new Option(s, "expressions", true, "Stellar expressions"); - o.setArgName("FILE"); - o.setRequired(false); - return o; - } - - @Override - public Optional<Object> getValue(BenchmarkOptions option, CommandLine cli) { - return Optional.ofNullable(option.get(cli).trim()); - } - }), - VARIABLES("v", new OptionHandler<BenchmarkOptions>() { - @Nullable - @Override - public Option apply(@Nullable String s) { - Option o = new Option(s, "variables", true, "File containing a JSON Map of variables to use"); - o.setArgName("FILE"); - o.setRequired(false); - return o; - } - - @Override - public Optional<Object> getValue(BenchmarkOptions option, CommandLine cli) { - return Optional.ofNullable(option.get(cli).trim()); - } - }), - OUTPUT("o", new OptionHandler<BenchmarkOptions>() { - @Nullable - @Override - public Option apply(@Nullable String s) { - Option o = new Option(s, "output", true, "File to write output."); - o.setArgName("FILE"); - o.setRequired(false); - return o; - } - - @Override - public Optional<Object> getValue(BenchmarkOptions option, CommandLine cli) { - return Optional.ofNullable(option.get(cli).trim()); - } - }) - ; - ; - Option option; - String shortCode; - OptionHandler<BenchmarkOptions> handler; - BenchmarkOptions(String shortCode, OptionHandler<BenchmarkOptions> optionHandler) { - this.shortCode = shortCode; - this.handler = optionHandler; - this.option = optionHandler.apply(shortCode); - } - - public boolean has(CommandLine cli) { - return cli.hasOption(shortCode); - } - - public String get(CommandLine cli) { - return cli.getOptionValue(shortCode); - } - - public static CommandLine parse(CommandLineParser parser, String[] args) { - try { - CommandLine cli = parser.parse(getOptions(), args); - if(HELP.has(cli)) { - printHelp(); - System.exit(0); - } - return cli; - } catch (org.apache.commons.cli.ParseException e) { - System.err.println("Unable to parse args: " + Joiner.on(' ').join(args)); - e.printStackTrace(System.err); - printHelp(); - System.exit(-1); - return null; - } - } - - public static EnumMap<BenchmarkOptions, Optional<Object>> createConfig(CommandLine cli) { - EnumMap<BenchmarkOptions, Optional<Object> > ret = new EnumMap<>(BenchmarkOptions.class); - for(BenchmarkOptions option : values()) { - ret.put(option, option.handler.getValue(option, cli)); - } - return ret; - } - - public static void printHelp() { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp( "StellarBenchmark", getOptions()); - } - - public static Options getOptions() { - Options ret = new Options(); - for(BenchmarkOptions o : BenchmarkOptions.values()) { - ret.addOption(o.option); - } - return ret; - } - } - - public static void main(String... argv) throws IOException { - CommandLine cli = BenchmarkOptions.parse(new PosixParser(), argv); - if(!BenchmarkOptions.EXPRESSIONS.has(cli)) { - throw new IllegalStateException("You must at least specify an expressions file."); - } - File expressionsFile = new File(BenchmarkOptions.EXPRESSIONS.get(cli)); - Optional<File> variablesFile = Optional.ofNullable(!BenchmarkOptions.VARIABLES.has(cli) - ?null - :new File(BenchmarkOptions.VARIABLES.get(cli)) - ); - Optional<File> output = Optional.ofNullable(!BenchmarkOptions.OUTPUT.has(cli) - ?null - :new File(BenchmarkOptions.OUTPUT.get(cli)) - ); - List<String> lines = Files.readLines(expressionsFile, Charset.defaultCharset()); - Map<String, Object> variables = new HashMap<>(); - if(variablesFile.isPresent()) { - variables = JSONUtils.INSTANCE.load(new FileInputStream(variablesFile.get()), new TypeReference<Map<String, Object>>() { - }); - } - int numTimes = DEFAULT_NUM_TIMES; - if(BenchmarkOptions.NUM_TIMES.has(cli)) { - numTimes = Integer.parseInt(BenchmarkOptions.NUM_TIMES.get(cli)); - } - int warmup = DEFAULT_WARMUP; - if(BenchmarkOptions.WARMUP.has(cli)) { - warmup = Integer.parseInt(BenchmarkOptions.WARMUP.get(cli)); - } - Double[] percentiles = DEFAULT_PERCENTILES; - if(BenchmarkOptions.PERCENTILES.has(cli)) { - List<Double> percentileList = new ArrayList<>(); - for(String token : Splitter.on(",").split(BenchmarkOptions.PERCENTILES.get(cli))) { - if(token.trim().isEmpty()) { - continue; - } - Double d = Double.parseDouble(token.trim()); - percentileList.add(d); - } - percentiles = (Double[])percentileList.toArray(); - } - PrintWriter out = new PrintWriter(System.out); - if(output.isPresent()) { - out = new PrintWriter(output.get()); - } - for(String statement : lines) { - if(statement.trim().startsWith("#") || statement.trim().isEmpty()) { - continue; - } - Microbenchmark.StellarStatement s = new Microbenchmark.StellarStatement(); - s.context = Context.EMPTY_CONTEXT(); - s.expression = statement; - s.functionResolver = StellarFunctions.FUNCTION_RESOLVER(); - s.variableResolver = new MapVariableResolver(variables); - DescriptiveStatistics stats = Microbenchmark.run(s, warmup, numTimes); - out.println("Expression: " + statement); - out.println(Microbenchmark.describe(stats, percentiles)); - } - if(argv.length > 2) { - out.close(); - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ArithmeticEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ArithmeticEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ArithmeticEvaluator.java deleted file mode 100644 index b458f9c..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ArithmeticEvaluator.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.commons.lang3.tuple.Pair; -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.FrameContext; - -import java.util.function.BiFunction; - -public enum ArithmeticEvaluator { - INSTANCE; - - public Token<? extends Number> evaluate(BiFunction<Number, Number, Token<? extends Number>> function, - Pair<Token<? extends Number>, Token<? extends Number>> p) { - if (p == null || p.getKey() == null || p.getValue() == null) { - throw new IllegalArgumentException(); - } - - final Number l = p.getKey().getValue(); - final Number r = p.getValue().getValue(); - - return function.apply(l == null ? 0 : l, r == null ? 0 : r); - } - - /** - * This is a helper class that defines how to handle arithmetic operations. The conversion between number - * types is taken for the Java spec: http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.6.2 - */ - public static final class ArithmeticEvaluatorFunctions { - public static BiFunction<Number, Number, Token<? extends Number>> addition(final FrameContext.Context context) { - return (Number l, Number r) -> { - if (l instanceof Double || r instanceof Double) { - return new Token<>(l.doubleValue() + r.doubleValue(), Double.class, context); - } else if (l instanceof Float || r instanceof Float) { - return new Token<>(l.floatValue() + r.floatValue(), Float.class, context); - } else if (l instanceof Long || r instanceof Long) { - return new Token<>(l.longValue() + r.longValue(), Long.class, context); - } else { - return new Token<>(l.intValue() + r.intValue(), Integer.class, context); - } - }; - } - - public static BiFunction<Number, Number, Token<? extends Number>> multiplication(final FrameContext.Context context) { - return (Number l, Number r) -> { - if (l instanceof Double || r instanceof Double) { - return new Token<>(l.doubleValue() * r.doubleValue(), Double.class, context); - } else if (l instanceof Float || r instanceof Float) { - return new Token<>(l.floatValue() * r.floatValue(), Float.class, context); - } else if (l instanceof Long || r instanceof Long) { - return new Token<>(l.longValue() * r.longValue(), Long.class, context); - } else { - return new Token<>(l.intValue() * r.intValue(), Integer.class, context); - } - }; - } - - public static BiFunction<Number, Number, Token<? extends Number>> subtraction(final FrameContext.Context context) { - return (Number l, Number r) -> { - if (l instanceof Double || r instanceof Double) { - return new Token<>(l.doubleValue() - r.doubleValue(), Double.class, context); - } else if (l instanceof Float || r instanceof Float) { - return new Token<>(l.floatValue() - r.floatValue(), Float.class, context); - } else if (l instanceof Long || r instanceof Long) { - return new Token<>(l.longValue() - r.longValue(), Long.class, context); - } else { - return new Token<>(l.intValue() - r.intValue(), Integer.class, context); - } - }; - } - - public static BiFunction<Number, Number, Token<? extends Number>> division(FrameContext.Context context) { - return (Number l, Number r) -> { - if (l instanceof Double || r instanceof Double) { - return new Token<>(l.doubleValue() / r.doubleValue(), Double.class, context); - } else if (l instanceof Float || r instanceof Float) { - return new Token<>(l.floatValue() / r.floatValue(), Float.class, context); - } else if (l instanceof Long || r instanceof Long) { - return new Token<>(l.longValue() / r.longValue(), Long.class, context); - } else { - return new Token<>(l.intValue() / r.intValue(), Integer.class, context); - } - }; - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionEvaluator.java deleted file mode 100644 index 370920e..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionEvaluator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.generated.StellarParser; - -/** - * This is used to determine what is needed to evaluate a Stellar comparison expression. A Stellar comparison - * expression is an expression that uses operators such as {@literal '<', '<=', '>', '>=', '==', '!=' } to compare - * values in Stellar. There are two main types of comparisons in Stellar, - * {@literal equality ('==', '!=') and comparison ('<', '<=', '>', '>='). } - */ -public interface ComparisonExpressionEvaluator { - - /** - * This will compare the values of {@code left} and {@code right} using the {@code op} input to determine a value - * to return. - * @param left The token representing the left side of a comparison expression. - * @param right The token representing the right side of a comparison expression. - * @param op This is a representation of a comparison operator {@literal (eg. <, <=, >, >=, ==, !=) } - * @return True if the expression is evaluated to be true, otherwise false. An example of expressions that - * should be true are {@code 1 == 1}, {@code 1f > 0}, etc. - */ - boolean evaluate(Token<?> left, Token<?> right, StellarParser.ComparisonOpContext op); -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionWithOperatorEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionWithOperatorEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionWithOperatorEvaluator.java deleted file mode 100644 index e213eb5..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonExpressionWithOperatorEvaluator.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.ParseException; -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.FrameContext; -import org.apache.metron.common.stellar.generated.StellarParser; - -/** - * This is the evaluator used when evaluating Stellar comparison operators. - * - * @see EqualityOperatorsEvaluator - * @see ComparisonOperatorsEvaluator - */ -public enum ComparisonExpressionWithOperatorEvaluator { - /** - * The instance of {@link ComparisonExpressionWithOperatorEvaluator} used in - * order to evaluate Stellar comparison expressions. - */ - INSTANCE; - - /** - * The different strategies used to evaluate a Stellar comparison operator. They are broken into - * two categories: equality operator comparisons and comparison operator comparisons. - */ - enum Strategy { - /** - * The evaluator used to evaluate comparison operator expressions. - */ - COMPARISON_OPERATORS(new ComparisonOperatorsEvaluator()), - /** - * The evaluator used to evaluate equality operator expressions. - */ - EQUALITY_OPERATORS(new EqualityOperatorsEvaluator()), - ; - - /** - * The evaluator to be used when evaluating Stellar expressions. - */ - private ComparisonExpressionEvaluator evaluator; - - Strategy(final ComparisonExpressionEvaluator evaluator) { - this.evaluator = evaluator; - } - - /** - * - * @return The evaluator needed to evaluate Stellar comparison expressions. - */ - public ComparisonExpressionEvaluator evaluator() { - return evaluator; - } - } - - /** - * When evaluating comparison expressions with operators, they are broken into four cases: - * - * 1. Testing equality, see {@link EqualityOperatorsEvaluator} - * 2. Testing not equal, see {@link EqualityOperatorsEvaluator}. This will be the negation of {@link EqualityOperatorsEvaluator#evaluate(Token, Token, StellarParser.ComparisonOpContext)}. - * 3. Testing less than, less than or equal, greater than, and greater than or equal {@link ComparisonOperatorsEvaluator} - * 4. Otherwise thrown {@link ParseException}. - * - * @param left The value of the left side of the Stellar expression. - * @param right The value of the right side of the Stellar expression. - * @param op The operator in the Stellar expression. - * @return A token with type boolean. This is based on the comparison of the {@code right} and {@code left} values. - */ - public Token<Boolean> evaluate(final Token<?> left, final Token<?> right, final StellarParser.ComparisonOpContext op, FrameContext.Context context) { - if (op.EQ() != null) { - return new Token<>(Strategy.EQUALITY_OPERATORS.evaluator().evaluate(left, right, op), Boolean.class, context); - } else if (op.NEQ() != null) { - return new Token<>(!Strategy.EQUALITY_OPERATORS.evaluator().evaluate(left, right, op), Boolean.class, context); - } else if (op.LT() != null || op.GT() != null || op.LTE() != null || op.GTE() != null) { - return new Token<>(Strategy.COMPARISON_OPERATORS.evaluator().evaluate(left, right, op), Boolean.class, context); - } - - throw new ParseException("Unsupported operations. The following expression is invalid: " + left.getValue() + op.getText() + right.getValue()); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonOperatorsEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonOperatorsEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonOperatorsEvaluator.java deleted file mode 100644 index 3f92a26..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/ComparisonOperatorsEvaluator.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.ParseException; -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.generated.StellarParser; - -/** - * {@link ComparisonOperatorsEvaluator} is used to evaluate comparison expressions using the following - * operator {@literal '<', '<=', '>', or '>='.} There are four major cases when evaluating a comparison expression. - */ -public class ComparisonOperatorsEvaluator implements ComparisonExpressionEvaluator { - - /** - * 1. If either the left or right's value is null then return false. - * 2. If both sides of the expression are instances of {@link Number} then: - * 1. If either side is a {@link Double} then get {@link Number#doubleValue()} from both sides and compare using given operator. - * 2. Else if either side is a {@link Float} then get {@link Number#floatValue()} from both sides and compare using given operator. - * 3. Else if either side is a {@link Long} then get {@link Number#longValue()} from both sides and compare using given operator. - * 4. Otherwise get {@link Number#intValue()} from both sides and compare using given operator. - * 3. If both sides are of the same type and implement the {@link Comparable} interface then use {@code compareTo} method. - * 4. If none of the above are met then a {@link ParseException} is thrown. - * - * @param left The token representing the left side of a comparison expression. - * @param right The token representing the right side of a comparison expression. - * @param op This is a representation of a comparison operator {@literal (eg. <, <=, >, >=, ==, !=) } - * @return A boolean value based on the comparison of {@code left} and {@code right}. - */ - @Override - public boolean evaluate(final Token<?> left, final Token<?> right, final StellarParser.ComparisonOpContext op) { - if (left.getValue() == null || right.getValue() == null) { - return false; - } else if (left.getValue() instanceof Number && right.getValue() instanceof Number) { - return compareNumbers((Number) left.getValue(), (Number) right.getValue(), op); - } else if (left.getValue().getClass() == right.getValue().getClass() - && left.getValue() instanceof Comparable && right.getValue() instanceof Comparable) { - return compare((Comparable<?>) left.getValue(), (Comparable<?>) right.getValue(), op); - } - - throw new ParseException("Unsupported operations. The following expression is invalid: " + left.getValue() + op + right.getValue()); - } - - /** - * This method uses the inputs' ability to compare with one another's values by using the {@code compareTo} method. It will use this and - * the operator to evaluate the output. - * - * @param l The value of the left side of the expression. - * @param r The value of the right side of the expression. - * @param op The operator to use when comparing. - * @param <T> The type of values being compared. - * @return A boolean value representing the comparison of the two values with the given operator. For example, {@code 1 <= 1} would be true. - */ - @SuppressWarnings("unchecked") - private <T extends Comparable> boolean compare(final T l, final T r, final StellarParser.ComparisonOpContext op) { - int compareTo = l.compareTo(r); - - if (op.LT() != null) { - return compareTo < 0; - } else if (op.LTE() != null) { - return compareTo <= 0; - } else if (op.GT() != null) { - return compareTo > 0; - } else if (op.GTE() != null) { - return compareTo >= 0; - } - - throw new ParseException("Unsupported operator: " + op); - } - - /** - * This method uses the inputs' ability to compare with one another's values by using the {@code compareTo} method. It will use this and - * the operator to evaluate the output. - * - * @param l The left side of the expression. - * @param r The right side of the expression - * @param op The operator used in the expression. - * @return A boolean value representing the comparison of the two values with the given operator. For example, {@code 1 <= 1} would be true. - */ - private boolean compareNumbers(final Number l, final Number r, final StellarParser.ComparisonOpContext op) { - if (op.LT() != null) { - return lessThan(l, r); - } else if (op.LTE() != null) { - return lessThanEqual(l, r); - } else if (op.GT() != null) { - return greaterThan(l, r); - } else if (op.GTE() != null) { - return greaterThanEqual(l, r); - } - - throw new ParseException("Unsupported operator: " + op); - } - - /** - * If the left side of the expression is less than the right then true otherwise false. - * - * @param l The value of the left side of the expression. - * @param r The value of the right side of the expression. - * @return If the left side of the expression is less than the right then true otherwise false. - */ - private boolean lessThan(final Number l, final Number r) { - if (l instanceof Double || r instanceof Double) { - return l.doubleValue() < r.doubleValue(); - } else if (l instanceof Float || r instanceof Float) { - return l.floatValue() < r.floatValue(); - } else if (l instanceof Long || r instanceof Long) { - return l.longValue() < r.longValue(); - } else { - return l.intValue() < r.intValue(); - } - } - - /** - * If the left side of the expression is less than or equal to the right then true otherwise false. - * - * @param l The value of the left side of the expression. - * @param r The value of the right side of the expression. - * @return If the left side of the expression is less than or equal to the right then true otherwise false. - */ - private boolean lessThanEqual(final Number l, final Number r) { - if (l instanceof Double || r instanceof Double) { - return l.doubleValue() <= r.doubleValue(); - } else if (l instanceof Float || r instanceof Float) { - return l.floatValue() <= r.floatValue(); - } else if (l instanceof Long || r instanceof Long) { - return l.longValue() <= r.longValue(); - } else { - return l.intValue() <= r.intValue(); - } - } - - /** - * If the left side of the expression is greater than the right then true otherwise false. - * - * @param l The value of the left side of the expression. - * @param r The value of the right side of the expression. - * @return If the left side of the expression is greater than the right then true otherwise false. - */ - private boolean greaterThan(final Number l, final Number r) { - if (l instanceof Double || r instanceof Double) { - return l.doubleValue() > r.doubleValue(); - } else if (l instanceof Float || r instanceof Float) { - return l.floatValue() > r.floatValue(); - } else if (l instanceof Long || r instanceof Long) { - return l.longValue() > r.longValue(); - } else { - return l.intValue() > r.intValue(); - } - } - - /** - * If the left side of the expression is greater than or equal to the right then true otherwise false. - * - * @param l The value of the left side of the expression. - * @param r The value of the right side of the expression. - * @return If the left side of the expression is greater than or equal to the right then true otherwise false. - */ - private boolean greaterThanEqual(final Number l, final Number r) { - if (l instanceof Double || r instanceof Double) { - return l.doubleValue() >= r.doubleValue(); - } else if (l instanceof Float || r instanceof Float) { - return l.floatValue() >= r.floatValue(); - } else if (l instanceof Long || r instanceof Long) { - return l.longValue() >= r.longValue(); - } else { - return l.intValue() >= r.intValue(); - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/DoubleLiteralEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/DoubleLiteralEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/DoubleLiteralEvaluator.java deleted file mode 100644 index 653b56c..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/DoubleLiteralEvaluator.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.FrameContext; -import org.apache.metron.common.stellar.generated.StellarParser; - -public class DoubleLiteralEvaluator implements NumberEvaluator<StellarParser.DoubleLiteralContext> { - @Override - public Token<Double> evaluate(StellarParser.DoubleLiteralContext context, FrameContext.Context contextVariety) { - if (context == null) { - throw new IllegalArgumentException("Cannot evaluate a context that is null."); - } - - return new Token<>(Double.parseDouble(context.getText()), Double.class, contextVariety); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/EqualityOperatorsEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/EqualityOperatorsEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/EqualityOperatorsEvaluator.java deleted file mode 100644 index 10a4cfd..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/EqualityOperatorsEvaluator.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.generated.StellarParser; - -/** - * {@link EqualityOperatorsEvaluator} is used to evaluate equality expressions using the following operator '=='. There are - * four major cases when evaluating a equality expression. See {@link EqualityOperatorsEvaluator#evaluate(Token, Token, StellarParser.ComparisonOpContext)} - * for a description. - */ -public class EqualityOperatorsEvaluator implements ComparisonExpressionEvaluator { - - /** - * 1. If either side of the expression is null then check equality using Java's '==' expression. - * 2. Else if both sides of the expression are of type {@link Number} then: - * 1. If either side of the expression is a {@link Double} then use {@link Number#doubleValue()} to test equality. - * 2. Else if either side of the expression is a {@link Float} then use {@link Number#floatValue()} to test equality. - * 3. Else if either side of the expression is a {@link Long} then use {@link Number#longValue()} to test equality. - * 4. Otherwise use {@link Number#intValue()} to test equality - * 3. Otherwise use {@code equals} method compare the left side with the right side. - * @param left The token representing the left side of a comparison expression. - * @param right The token representing the right side of a comparison expression. - * @param op This is a representation of a comparison operator {@literal (eg. <, <=, >, >=, ==, !=) } - * @return A boolean value based on the comparison of {@code left} and {@code right}. - */ - @Override - public boolean evaluate(final Token<?> left, final Token<?> right, final StellarParser.ComparisonOpContext op) { - if (left.getValue() == null || right.getValue() == null) { - return left.getValue() == right.getValue(); - } else if (left.getValue() instanceof Number && right.getValue() instanceof Number) { - return eq((Number) left.getValue(), (Number) right.getValue()); - } else { - return left.getValue().equals(right.getValue()); - } - } - - /** - * This method follows Java's number promotions when comparing numbers. - * - * @param l The left side of the equality expression. - * @param r The right side of the equality expression. - * @return All comparisons use the '==' operator from Java. If either input is a double then compare double values. - * If either side is a float compare float values. If either side is a long compare long values. Otherwise compare - * int values. - */ - private boolean eq(final Number l, final Number r) { - if (l instanceof Double || r instanceof Double) { - return l.doubleValue() == r.doubleValue(); - } else if (l instanceof Float || r instanceof Float) { - return l.floatValue() == r.floatValue(); - } else if (l instanceof Long || r instanceof Long) { - return l.longValue() == r.longValue(); - } else { - return l.intValue() == r.intValue(); - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/FloatLiteralEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/FloatLiteralEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/FloatLiteralEvaluator.java deleted file mode 100644 index 4489d30..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/FloatLiteralEvaluator.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.FrameContext; -import org.apache.metron.common.stellar.generated.StellarParser; - -public class FloatLiteralEvaluator implements NumberEvaluator<StellarParser.FloatLiteralContext> { - @Override - public Token<Float> evaluate(StellarParser.FloatLiteralContext context, FrameContext.Context contextVariety) { - if (context == null) { - throw new IllegalArgumentException("Cannot evaluate a context that is null."); - } - - return new Token<>(Float.parseFloat(context.getText()), Float.class, contextVariety); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/IntLiteralEvaluator.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/IntLiteralEvaluator.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/IntLiteralEvaluator.java deleted file mode 100644 index 00e62e6..0000000 --- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/evaluators/IntLiteralEvaluator.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.stellar.evaluators; - -import org.apache.metron.common.dsl.Token; -import org.apache.metron.common.stellar.FrameContext; -import org.apache.metron.common.stellar.generated.StellarParser; - -public class IntLiteralEvaluator implements NumberEvaluator<StellarParser.IntLiteralContext> { - @Override - public Token<Integer> evaluate(StellarParser.IntLiteralContext context, FrameContext.Context contextVariety) { - if (context == null) { - throw new IllegalArgumentException("Cannot evaluate a context that is null."); - } - - return new Token<>(Integer.parseInt(context.getText()), Integer.class, contextVariety); - } -}
