Skip to content

Commit

Permalink
[7.67.x-blue] fix mvel constraint rewriting (#70)
Browse files Browse the repository at this point in the history
* Fix + Test

* Minor change

---------

Co-authored-by: Mario Fusco <[email protected]>
  • Loading branch information
yesamer and mariofusco authored Jul 23, 2024
1 parent 4d48a98 commit 9f8a656
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Constraint buildVariableConstraint( RuleBuildContext context,
requiredDeclaration.getPattern().getObjectType().equals( new ClassObjectType( DroolsQuery.class ) ) &&
Operator.EQUAL.getOperatorString().equals( operatorDescr.getOperator() );
if (isUnification && leftValue.equals(rightValue)) {
expression = resolveUnificationAmbiguity(expression, declarations, leftValue, rightValue);
expression = resolveUnificationAmbiguity(declarations, leftValue, rightValue);
}

expression = normalizeMVELVariableExpression(expression, leftValue, rightValue, relDescr);
Expand Down Expand Up @@ -256,7 +256,7 @@ private static String normalizeDoubleNegation(String expr) {
return expr;
}

protected static String resolveUnificationAmbiguity(String expr, Declaration[] declrations, String leftValue, String rightValue) {
private static String resolveUnificationAmbiguity(Declaration[] declrations, String leftValue, String rightValue) {
// resolve ambiguity between variable and bound value with the same name in unifications
rightValue = rightValue + "__";
for (Declaration declaration : declrations) {
Expand Down Expand Up @@ -289,7 +289,8 @@ protected static String normalizeMVELLiteralExpression(ValueType vtype,
return normalizeStringOperator( leftValue, rightValue, restrictionDescr );
}
if (vtype.isDecimalNumber() && field.getValue() != null) {
expr = expr.replace( rightValue, field.getValue().toString() );
int opPos = expr.indexOf(operator) + operator.length();
expr = expr.substring(0, opPos) + expr.substring(opPos).replace(rightValue, field.getValue().toString());
}
// resolve ambiguity between mvel's "empty" keyword and constraints like: List(empty == ...)
return normalizeEmptyKeyword( expr, operator );
Expand Down Expand Up @@ -445,7 +446,7 @@ public static class BooleanConversionHandler implements ConversionHandler {
private BooleanConversionHandler() { }

public Object convertFrom(Object in) {
if (in.getClass() == Boolean.class || in.getClass() == boolean.class) {
if (in.getClass() == Boolean.class) {
return in;
}
return in instanceof String && ((String)in).equalsIgnoreCase("true");
Expand Down Expand Up @@ -602,10 +603,9 @@ private static MVELAnalysisResult analyzeExpression(String expr,

parserContext1.setStrictTypeEnforcement( false );
parserContext1.setStrongTyping( false );
Class< ? > returnType;

try {
returnType = MVEL.analyze( expr, parserContext1 );
MVEL.analyze( expr, parserContext1 );
} catch ( Exception e ) {
return null;
}
Expand Down Expand Up @@ -647,6 +647,7 @@ private static MVELAnalysisResult analyzeExpression(String expr,
parserContext2.addInput( "this", availableIdentifiers.getThisClass() );
}

Class< ? > returnType;
try {
returnType = MVEL.analyze( expr, parserContext2 );
} catch ( Exception e ) {
Expand Down Expand Up @@ -681,7 +682,7 @@ public InternalReadAccessor buildMvelFieldReadAccessor( RuleBuildContext context
final AnalysisResult analysis = context.getDialect().analyzeExpression(context,
descr,
fieldName,
new BoundIdentifiers(pattern, context, Collections.EMPTY_MAP,
new BoundIdentifiers(pattern, context, Collections.emptyMap(),
objectType.getClassType()));

if (analysis == null) {
Expand Down Expand Up @@ -819,7 +820,6 @@ public static class Expression implements QueryArgument {
private String expression;
private ParserContext parserContext;

private transient Class<?> argumentClass;
private transient MvelEvaluator<Object> evaluator;

public Expression() { }
Expand All @@ -838,7 +838,6 @@ private void init() {
}
parserContext.setInputs(inputs);

this.argumentClass = MVEL.analyze( expression, parserContext );
this.evaluator = createMvelEvaluator(MVEL.compileExpression( expression, parserContext ));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024. Red Hat, Inc. and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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.drools.mvel;

import org.drools.compiler.lang.descr.LiteralRestrictionDescr;
import org.drools.core.base.ValueType;
import org.drools.core.base.field.DoubleFieldImpl;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class MVELConstraintBuilderTest {

@Test
public void test() {
String expression = MVELConstraintBuilder.normalizeMVELLiteralExpression(ValueType.FLOAT_TYPE,
new DoubleFieldImpl(0.0),
"this.method(\"UV4014.01\") != 0",
"this.method(\"UV4014.01\")",
"!=",
"0",
false,
new LiteralRestrictionDescr("!=", "0"));

assertThat(expression).isEqualTo("this.method(\"UV4014.01\") != 0.0");
}

}

0 comments on commit 9f8a656

Please sign in to comment.