Skip to content

Commit

Permalink
[DROOLS-5851] fix binding of a constant enum value during executable …
Browse files Browse the repository at this point in the history
…model generation (apache#3266)
  • Loading branch information
mariofusco committed Nov 25, 2020
1 parent f1025b6 commit 6594db1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 6 deletions.
Expand Up @@ -75,6 +75,7 @@
import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType;
import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType;
import static org.drools.modelcompiler.builder.generator.drlxparse.SpecialComparisonCase.specialComparisonFactory;
import static org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.tryParseAsConstantField;
import static org.drools.mvel.parser.printer.PrintUtil.printConstraint;

public class ConstraintParser {
Expand Down Expand Up @@ -260,13 +261,18 @@ private DrlxParseResult parseNameExpr(DrlNameExpr nameExpr, Class<?> patternType
}

private DrlxParseResult parseFieldAccessExpr( FieldAccessExpr fieldCallExpr, Class<?> patternType, String bindingId ) {
ToMethodCall toMethodCall = new ToMethodCall(context);
TypedExpression converted = toMethodCall.toMethodCallWithClassCheck(fieldCallExpr, bindingId, patternType);
Expression withThis = DrlxParseUtil.prepend(new NameExpr(THIS_PLACEHOLDER), converted.getExpression());
try {
ToMethodCall toMethodCall = new ToMethodCall(context);
TypedExpression converted = toMethodCall.toMethodCallWithClassCheck(fieldCallExpr, bindingId, patternType);
Expression withThis = DrlxParseUtil.prepend(new NameExpr(THIS_PLACEHOLDER), converted.getExpression());

return new SingleDrlxParseSuccess(patternType, bindingId, withThis, converted.getType())
.setLeft(converted)
.setImplicitCastExpression(toMethodCall.getImplicitCastExpression());
return new SingleDrlxParseSuccess(patternType, bindingId, withThis, converted.getType())
.setLeft(converted)
.setImplicitCastExpression(toMethodCall.getImplicitCastExpression());
} catch(ToMethodCall.CannotConvertException e) {
Optional<TypedExpression> parsed = tryParseAsConstantField(context.getTypeResolver(), fieldCallExpr.getScope(), fieldCallExpr.getNameAsString());
return parsed.map( expr -> new SingleDrlxParseSuccess(patternType, bindingId, expr.getExpression(), expr.getType()).setLeft(expr) ).orElseThrow( () -> e );
}
}

private DrlxParseResult parsePointFreeExpr(PointFreeExpr pointFreeExpr, Class<?> patternType, String bindingId, boolean isPositional) {
Expand Down
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2020. 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.modelcompiler;

import org.junit.Test;
import org.kie.api.runtime.KieSession;

import static org.junit.Assert.assertEquals;

public class EnumTest extends BaseModelTest {

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

@Test
public void testMatchEnum() {
String str =
"import " + Bus.class.getCanonicalName() + ";" +
"rule bus2 when\n" +
" $bus : Bus( $maker : maker == Bus.Maker.HINO )\n" +
" then\n" +
" System.out.println(\"bus=\" + $bus + \", maker=\" + $maker);\n" +
"end";

KieSession ksession = getKieSession( str );

Bus a = new Bus("blue", 25, Bus.Maker.HINO);
ksession.insert(a);
Bus b = new Bus("red", 25, Bus.Maker.ISUZU);
ksession.insert(b);

assertEquals(1, ksession.fireAllRules());
}

@Test
public void testBindEnum() {
// DROOLS-5851
String str =
"import " + Bus.class.getCanonicalName() + ";" +
"rule bus2 when\n" +
" $bus : Bus( $maker : Bus.Maker.HINO )\n" +
" then\n" +
" System.out.println(\"bus=\" + $bus + \", maker=\" + $maker);\n" +
"end";

KieSession ksession = getKieSession( str );

Bus a = new Bus("blue", 25, Bus.Maker.HINO);
ksession.insert(a);
Bus b = new Bus("red", 25, Bus.Maker.ISUZU);
ksession.insert(b);

assertEquals(2, ksession.fireAllRules());
}

public static class Bus {
private String name;
private int person;
private Maker maker;

public enum Maker {
FUSO,
HINO,
ISUZU
}

public Bus(String name, int person) {
this.name = name;
this.person = person;
}

public Bus(String name, int person, Maker maker) {
this.name = name;
this.person = person;
this.maker = maker;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPerson() {
return person;
}

public void setPerson(int person) {
this.person = person;
}

public Maker getMaker() {
return maker;
}

public void setMaker(Maker maker) {
this.maker = maker;
}
}
}

0 comments on commit 6594db1

Please sign in to comment.