Skip to content

Commit

Permalink
[incubator-kie-issues#968] Support ConditionalExpr in Drools executab…
Browse files Browse the repository at this point in the history
…le model (#5746)

Co-authored-by: Gabriele-Cardosi <[email protected]>
(cherry picked from commit 7b8a70f)
  • Loading branch information
gitgabrio authored and Gabriele-Cardosi committed Mar 5, 2024
1 parent 8de49a3 commit d84b635
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.github.javaparser.ast.expr.CastExpr;
import com.github.javaparser.ast.expr.CharLiteralExpr;
import com.github.javaparser.ast.expr.ClassExpr;
import com.github.javaparser.ast.expr.ConditionalExpr;
import com.github.javaparser.ast.expr.DoubleLiteralExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression;
Expand Down Expand Up @@ -379,6 +380,10 @@ private Optional<TypedExpression> toTypedExpressionRec(Expression drlxExpr) {
return of(new TypedExpression(drlxExpr, type));
}

if (drlxExpr instanceof ConditionalExpr) {
return of(new TypedExpression(drlxExpr, Boolean.class));
}

if (drlxExpr.isAssignExpr()) {
AssignExpr assignExpr = drlxExpr.asAssignExpr();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.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 ConditionalExprTest extends BaseModelTest {

private static final String RULE_STRING = "package constraintexpression\n" +
"\n" +
"import " + Person.class.getCanonicalName() + "\n" +
"import java.util.List; \n" +
"global List<Boolean> booleanListGlobal; \n" +
"rule \"r1\"\n" +
"when \n" +
" $p : Person($booleanVariable: (name != null ? true : false))\n" +
"then \n" +
" System.out.println($booleanVariable); \n" +
" System.out.println($p); \n" +
" booleanListGlobal.add($booleanVariable); \n " +
"end \n";

private KieSession ksession;
private List<Boolean> booleanListGlobal;

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

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

@Test
public void testConditionalExpressionWithNamedPerson() {
try {
Person person = new Person("someName");
ksession.insert(person);
int rulesFired = ksession.fireAllRules();
assertThat(rulesFired).isEqualTo(1);
assertThat(booleanListGlobal).isNotEmpty().containsExactly(Boolean.TRUE);
} finally {
ksession.dispose();
}
}

@Test
public void testConditionalExpressionWithUnnamedPerson() {
try {
Person person = new Person();
ksession.insert(person);
int rulesFired = ksession.fireAllRules();
assertThat(rulesFired).isEqualTo(1);
assertThat(booleanListGlobal).isNotEmpty().containsExactly(Boolean.FALSE);
} finally {
ksession.dispose();
}
}

}

0 comments on commit d84b635

Please sign in to comment.