You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2019/11/08 15:46:17 UTC

[GitHub] [nifi] mattyb149 commented on a change in pull request #3876: NIFI-6854 - Added option to ignore condition errors in rules. Test co…

mattyb149 commented on a change in pull request #3876: NIFI-6854 - Added option to ignore condition errors in rules. Test co…
URL: https://github.com/apache/nifi/pull/3876#discussion_r344237295
 
 

 ##########
 File path: nifi-nar-bundles/nifi-easyrules-bundle/nifi-easyrules-service/src/main/java/org/apache/nifi/rules/RulesSPELCondition.java
 ##########
 @@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.rules;
+
+import org.jeasy.rules.api.Condition;
+import org.jeasy.rules.api.Facts;
+import org.jeasy.rules.spel.SpELCondition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.expression.Expression;
+import org.springframework.expression.ExpressionParser;
+import org.springframework.expression.ParserContext;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.expression.spel.support.StandardEvaluationContext;
+
+public class RulesSPELCondition implements Condition {
+    private static final Logger LOGGER = LoggerFactory.getLogger(SpELCondition.class);
+    private final ExpressionParser parser = new SpelExpressionParser();
+    private String expression;
+    private Expression compiledExpression;
+    private boolean ignoreConditionErrors;
+
+
+    public RulesSPELCondition(String expression) {
+        this.expression = expression;
+        this.compiledExpression = this.parser.parseExpression(expression);
+    }
+
+    public RulesSPELCondition(String expression, ParserContext parserContext) {
+        this.expression = expression;
+        this.compiledExpression = this.parser.parseExpression(expression, parserContext);
+    }
+
+    public RulesSPELCondition(String expression, boolean ignoreConditionErrors) {
+        this.expression = expression;
+        this.compiledExpression =  this.parser.parseExpression(expression);
+        this.ignoreConditionErrors = ignoreConditionErrors;
+    }
+
+    public RulesSPELCondition(String expression, ParserContext parserContext, boolean ignoreConditionErrors) {
+        this.expression = expression;
+        this.compiledExpression =  this.parser.parseExpression(expression, parserContext);
+        this.ignoreConditionErrors = ignoreConditionErrors;
+    }
+
+    public boolean evaluate(Facts facts) {
+        try {
+            StandardEvaluationContext context = new StandardEvaluationContext();
+            context.setRootObject(facts.asMap());
+            context.setVariables(facts.asMap());
+            return this.compiledExpression.getValue(context, Boolean.class);
+        } catch (Exception ex) {
+            if(ignoreConditionErrors) {
+                LOGGER.warn("Unable to evaluate expression: '" + this.expression + "' on facts: " + facts, ex);
 
 Review comment:
   These should probably be at `DEBUG` level or something lower than WARN. Now that you can specify that you want to ignore it, having a warning in the log implies we’re telling you about it even though you chose to ignore it.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services