Repository: nifi
Updated Branches:
  refs/heads/master 9b08f23b2 -> e4a3e0964


NIFI-1662: Added support for decimal literal in Expression Language


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/557e0b9f
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/557e0b9f
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/557e0b9f

Branch: refs/heads/master
Commit: 557e0b9f27fe8fd99aa0ac6feda597ea26fc3357
Parents: 94ab999
Author: Matt Burgess <mattyb...@apache.org>
Authored: Tue Oct 18 09:47:17 2016 -0400
Committer: Matt Burgess <mattyb...@apache.org>
Committed: Mon Oct 24 10:20:21 2016 -0400

----------------------------------------------------------------------
 .../language/antlr/AttributeExpressionLexer.g   |  7 ++++
 .../language/antlr/AttributeExpressionParser.g  |  2 +-
 .../attribute/expression/language/Query.java    |  5 +++
 .../literals/DecimalLiteralEvaluator.java       | 44 ++++++++++++++++++++
 .../expression/language/TestQuery.java          |  3 ++
 5 files changed, 60 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/557e0b9f/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g
 
b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g
index 31dfe90..34ef48b 100644
--- 
a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g
+++ 
b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionLexer.g
@@ -79,6 +79,13 @@ DOT          : '.';
 SEMICOLON : ';';
 WHOLE_NUMBER   : ('0'..'9')+;
 
+DECIMAL :    ('0'..'9')+ '.' ('0'..'9')* EXP?
+           | '.' ('0'..'9')+ EXP?
+           | ('0'..'9')+ EXP
+           | ('0'..'9')+ ;
+
+fragment EXP : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
+
 TRUE   : 'true';
 FALSE  : 'false';
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/557e0b9f/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g
 
b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g
index 2eb8e7b..64644c6 100644
--- 
a/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g
+++ 
b/nifi-commons/nifi-expression-language/src/main/antlr3/org/apache/nifi/attribute/expression/language/antlr/AttributeExpressionParser.g
@@ -100,7 +100,7 @@ stringFunctionRef : zeroArgString | oneArgString | 
twoArgString | fiveArgString;
 booleanFunctionRef : zeroArgBool | oneArgBool | multiArgBool;
 numberFunctionRef : zeroArgNum | oneArgNum;
 
-anyArg : WHOLE_NUMBER | numberFunctionRef | STRING_LITERAL | zeroArgString | 
oneArgString | twoArgString | fiveArgString | booleanLiteral | zeroArgBool | 
oneArgBool | multiArgBool | expression;
+anyArg : WHOLE_NUMBER | DECIMAL | numberFunctionRef | STRING_LITERAL | 
zeroArgString | oneArgString | twoArgString | fiveArgString | booleanLiteral | 
zeroArgBool | oneArgBool | multiArgBool | expression;
 stringArg : STRING_LITERAL | zeroArgString | oneArgString | twoArgString | 
expression;
 functionRef : stringFunctionRef | booleanFunctionRef | numberFunctionRef;
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/557e0b9f/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java
 
b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java
index b0c2ab6..e8b1f31 100644
--- 
a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java
+++ 
b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/Query.java
@@ -97,6 +97,7 @@ import 
org.apache.nifi.attribute.expression.language.evaluation.functions.Base64
 import 
org.apache.nifi.attribute.expression.language.evaluation.functions.Base64EncodeEvaluator;
 import 
org.apache.nifi.attribute.expression.language.evaluation.functions.UuidEvaluator;
 import 
org.apache.nifi.attribute.expression.language.evaluation.literals.BooleanLiteralEvaluator;
+import 
org.apache.nifi.attribute.expression.language.evaluation.literals.DecimalLiteralEvaluator;
 import 
org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator;
 import 
org.apache.nifi.attribute.expression.language.evaluation.literals.ToLiteralEvaluator;
 import 
org.apache.nifi.attribute.expression.language.evaluation.literals.WholeNumberLiteralEvaluator;
@@ -132,6 +133,7 @@ import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpre
 import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.ATTRIBUTE_REFERENCE;
 import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.ATTR_NAME;
 import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.CONTAINS;
+import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.DECIMAL;
 import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.IN;
 import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.COUNT;
 import static 
org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.DIVIDE;
@@ -680,6 +682,9 @@ public class Query {
             case STRING_LITERAL: {
                 return newStringLiteralEvaluator(tree.getText());
             }
+            case DECIMAL: {
+                return new DecimalLiteralEvaluator(tree.getText());
+            }
             case TRUE:
             case FALSE:
                 return buildBooleanEvaluator(tree);

http://git-wip-us.apache.org/repos/asf/nifi/blob/557e0b9f/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java
 
b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java
new file mode 100644
index 0000000..9673e16
--- /dev/null
+++ 
b/nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/literals/DecimalLiteralEvaluator.java
@@ -0,0 +1,44 @@
+/*
+ * 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.nifi.attribute.expression.language.evaluation.literals;
+
+import 
org.apache.nifi.attribute.expression.language.evaluation.DecimalEvaluator;
+import 
org.apache.nifi.attribute.expression.language.evaluation.DecimalQueryResult;
+import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
+import org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
+
+import java.util.Map;
+
+
+public class DecimalLiteralEvaluator extends DecimalEvaluator {
+
+    private final double literal;
+
+    public DecimalLiteralEvaluator(final String value) {
+        this.literal = Double.parseDouble(value);
+    }
+
+    @Override
+    public QueryResult<Double> evaluate(final Map<String, String> attributes) {
+        return new DecimalQueryResult(literal);
+    }
+
+    @Override
+    public Evaluator<?> getSubjectEvaluator() {
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/nifi/blob/557e0b9f/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java
 
b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java
index 0eefab5..01183ec 100644
--- 
a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java
+++ 
b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java
@@ -807,6 +807,9 @@ public class TestQuery {
         // The expected resulted is calculated instead of a set number due to 
the inaccuracy of double arithmetic
         verifyEquals("${ten:divide(${two:plus(3)}):toDecimal()}", attributes, 
(10.1 / (2.2 + 3)));
 
+        // The expected resulted is calculated instead of a set number due to 
the inaccuracy of double arithmetic
+        verifyEquals("${ten:divide(${two:plus(3.1)}):toDecimal()}", 
attributes, (10.1 / (2.2 + 3.1)));
+
         verifyEquals("${ten:divide(${two:plus(3)}):toDate():format(\"SSS\")}", 
attributes, "001");
     }
 

Reply via email to