Skip to content

Commit

Permalink
[8.40.x_incubator-kie-issues#988] Fix cherry pick
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco authored and Gabriele-Cardosi committed Mar 5, 2024
1 parent d84b635 commit 053729a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void toPatternExpr(String bindingId, List<DrlxParseResult> list, DrlxPar
expr.setScope( patternExpr );
patternExpr = expr;
}
if (singleDrlx.getExpr() != null && !(singleDrlx.getExpr() instanceof NameExpr)) {
if (singleDrlx.getExpr() != null && singleDrlx.isPredicate()) {
MethodCallExpr expr = expressionBuilder.buildExpressionWithIndexing( singleDrlx );
expr.setScope( patternExpr );
patternExpr = expr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/*
* Copyright 2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.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
*
* Unless required by applicable law or agreed to in writing, software
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* 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.model.codegen.execmodel.generator.drlxparse;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -505,21 +506,23 @@ private DrlxParseResult parseNameExpr(DrlNameExpr nameExpr, Class<?> patternType
Expression withThis = DrlxParseUtil.prepend(new NameExpr(THIS_PLACEHOLDER), converted.getExpression());

if (hasBind) {
return new SingleDrlxParseSuccess(patternType, bindingId, null, converted.getType() )
return new SingleDrlxParseSuccess(patternType, bindingId, withThis, converted.getType() )
.setLeft( new TypedExpression( withThis, converted.getType() ) )
.addReactOnProperty( lcFirstForBean(nameExpr.getNameAsString()) );
} else if (context.hasDeclaration( expression )) {
Optional<DeclarationSpec> declarationSpec = context.getDeclarationById(expression);
}

if (context.hasDeclaration( expression )) {
Optional<TypedDeclarationSpec> declarationSpec = context.getTypedDeclarationById(expression);
if (declarationSpec.isPresent()) {
return new SingleDrlxParseSuccess(patternType, bindingId, context.getVarExpr(printNode(drlxExpr)), declarationSpec.get().getDeclarationClass() ).setIsPredicate(true);
} else {
throw new IllegalArgumentException("Cannot find declaration specification by specified expression " + expression + "!");
}
} else {
return new SingleDrlxParseSuccess(patternType, bindingId, withThis, converted.getType() )
.addReactOnProperty( nameExpr.getNameAsString() )
.setIsPredicate(true);
}

return new SingleDrlxParseSuccess(patternType, bindingId, withThis, converted.getType() )
.addReactOnProperty( nameExpr.getNameAsString() )
.setIsPredicate(true);
}

private DrlxParseResult parseFieldAccessExpr( FieldAccessExpr fieldCallExpr, Class<?> patternType, String bindingId ) {
Expand Down Expand Up @@ -1017,7 +1020,7 @@ private Optional<DrlxParseFail> convertBigDecimalArithmetic(MethodCallExpr metho
List<BinaryExpr> binaryExprList = methodCallExpr.findAll(BinaryExpr.class);
for (BinaryExpr binaryExpr : binaryExprList) {
Operator operator = binaryExpr.getOperator();
boolean arithmeticExpr = ARITHMETIC_OPERATORS.contains(operator);
boolean arithmeticExpr = isArithmeticOperator(operator);
if (arithmeticExpr) {
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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.drools.model.codegen.execmodel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.drools.model.codegen.execmodel.domain.Person;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.runtime.KieSession;

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

public class ConstraintTest extends BaseModelTest {

private static final String RULE_STRING = "package constraintexpression\n" +
"\n" +
"import " + Person.class.getCanonicalName() + "\n" +
"import java.util.List; \n" +
"import java.math.BigDecimal; \n" +
"global List<BigDecimal> bigDecimalListGlobal; \n" +
"rule \"r1\"\n" +
"when \n" +
" $p : Person($amount: (money == null ? BigDecimal.valueOf(100.0) : money))\n" +
"then \n" +
" System.out.println($amount); \n" +
" System.out.println($p); \n" +
" bigDecimalListGlobal.add($amount); \n " +
"end \n";

private KieSession ksession;

private List<BigDecimal> bigDecimalListGlobal;

public ConstraintTest(RUN_TYPE testRunType) {
super(testRunType);
}

@Before
public void setup() {
ksession = getKieSession(RULE_STRING);
bigDecimalListGlobal = new ArrayList<>();
ksession.setGlobal("bigDecimalListGlobal", bigDecimalListGlobal);
}

@Test
public void testConstraintWithMoney() {
try {
BigDecimal money = BigDecimal.valueOf(34.45);
Person person = new Person("", money);
ksession.insert(person);
int rulesFired = ksession.fireAllRules();
assertThat(rulesFired).isEqualTo(1);
assertThat(bigDecimalListGlobal).isNotEmpty().containsExactly(money);
} finally {
ksession.dispose();
}
}

@Test
public void testConstraintWithoutMoney() {
try {
Person person = new Person();
ksession.insert(person);
int rulesFired = ksession.fireAllRules();
assertThat(rulesFired).isEqualTo(1);
assertThat(bigDecimalListGlobal).isNotEmpty().containsExactly(BigDecimal.valueOf(100.0));
} finally {
ksession.dispose();
}
}
}

0 comments on commit 053729a

Please sign in to comment.