Copilot commented on code in PR #6469:
URL:
https://github.com/apache/incubator-kie-drools/pull/6469#discussion_r2397728169
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/util/lambdareplace/MaterializedLambdaPredicate.java:
##########
@@ -76,6 +79,22 @@ private void createTestMethod(EnumDeclaration
classDeclaration) {
methodDeclaration.setBody(new BlockStmt(NodeList.nodeList(new
ReturnStmt(clone.getExpression()))));
}
+ /**
+ * recursively traverse the node tree and collect all RegexExpr
+ * @param node
+ * @return
+ */
+ private Collection<RegexExpr> collectRegexExpressions(Node node) {
+ if (node instanceof RegexExpr) {
+ return Collections.singletonList((RegexExpr) node);
+ } else {
+ return node.getChildNodes().stream()
+ .map(this::collectRegexExpressions)
+ .flatMap(Collection::stream)
+ .toList();
Review Comment:
Stream.toList() requires Java 16+. Drools builds target Java 11; this will
not compile there. Replace with Collectors.toList() and add import for
java.util.stream.Collectors.
##########
drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/RegexExpr.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.mvel.parser.ast.expr;
+
+import com.github.javaparser.ast.AllFieldsConstructor;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.FieldDeclaration;
+import com.github.javaparser.ast.expr.BinaryExpr;
Review Comment:
Unused import: BinaryExpr is not referenced in this class. Please remove to
avoid warnings.
```suggestion
```
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/operatorspec/RegexOperatorSpec.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.generator.operatorspec;
+
+import com.github.javaparser.ast.Modifier;
+import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.body.FieldDeclaration;
+import com.github.javaparser.ast.body.VariableDeclarator;
+import com.github.javaparser.ast.expr.*;
+import org.drools.model.codegen.execmodel.generator.RuleContext;
+import org.drools.model.codegen.execmodel.generator.TypedExpression;
+import
org.drools.model.codegen.execmodel.generator.expressiontyper.ExpressionTyper;
+import org.drools.mvel.parser.ast.expr.PointFreeExpr;
+import org.drools.mvel.parser.ast.expr.RegexExpr;
+
+import java.util.regex.Pattern;
+
+import static
org.drools.model.codegen.execmodel.generator.DrlxParseUtil.toClassOrInterfaceType;
+import static org.drools.util.StringUtils.md5Hash;
+
+public class RegexOperatorSpec extends NativeOperatorSpec {
+ public static final RegexOperatorSpec INSTANCE = new RegexOperatorSpec();
+
+ public Expression getExpression(RuleContext context, PointFreeExpr
pointFreeExpr, TypedExpression left, ExpressionTyper expressionTyper) {
+ if (shouldInlineRegex(pointFreeExpr.getRight())) {
+ return super.getExpression(context, pointFreeExpr, left,
expressionTyper);
+ } else {
+ // TODO(?) throw some exception instead of creating
regexp_failed_to_parse expressions
+ Expression regexExpression = pointFreeExpr.getRight().getFirst()
+ .orElse(new StringLiteralExpr("regexp_failed_to_parse"));
+
+ NameExpr regexFieldName = new NameExpr("regexp_" +
md5Hash(regexExpression.toString()));
+
+ return new RegexExpr(
+ getClassMemberFieldDeclaration(regexFieldName,
regexExpression),
+ getClassMemberInvocationExpression(pointFreeExpr, left,
regexFieldName)
+ );
+ }
+ }
+
+ private EnclosedExpr getClassMemberInvocationExpression(PointFreeExpr
pointFreeExpr, TypedExpression left, NameExpr regexFieldName) {
+ // we need to cast the left expression to CharSequence, otherwise it
might fail in cases of missing type safety
+ // e.g. test method parameters (_this) is of a raw type Map
+ EnclosedExpr typeSafeStringToMatch = new EnclosedExpr(new
CastExpr(toClassOrInterfaceType(CharSequence.class), left.getExpression()));
+
+ MethodCallExpr matchesCallExpr = new MethodCallExpr(
+ new MethodCallExpr(regexFieldName, "matcher",
NodeList.nodeList(typeSafeStringToMatch)),
+ "matches"
+ );
+
+ // null string should not match
+ BinaryExpr compoundRegexCallExpr = new BinaryExpr(
+ new BinaryExpr(left.getExpression(), new NullLiteralExpr(),
BinaryExpr.Operator.NOT_EQUALS),
+ pointFreeExpr.isNegated() ? new UnaryExpr(matchesCallExpr,
UnaryExpr.Operator.LOGICAL_COMPLEMENT) : matchesCallExpr,
+ BinaryExpr.Operator.AND
+ );
+
+ return new EnclosedExpr(compoundRegexCallExpr);
+ }
+
+ private FieldDeclaration getClassMemberFieldDeclaration(NameExpr
regexFieldName, Expression regexExpression) {
+
+ MethodCallExpr compiledRegexField = new MethodCallExpr(
+ new NameExpr(Pattern.class.getName()), "compile",
NodeList.nodeList(regexExpression)
+ );
+
+ VariableDeclarator variableDeclarator = new VariableDeclarator(
+ toClassOrInterfaceType(Pattern.class),
+ regexFieldName.getName(),
+ compiledRegexField
+ );
+
+ return new FieldDeclaration(
+ NodeList.nodeList(Modifier.privateModifier(),
Modifier.staticModifier(), Modifier.finalModifier()),
+ variableDeclarator
+ );
+ }
+
+ /**
+ * Recursively check if the regexPatternExpression contains a method call
on the fact.
+ * If so, we consider it a dynamic regex, and cannot inline the regex
expression.
+ *
+ * @param regexPatternExpression the regex pattern expression under
analysis
+ * @return true if the regex pattern expression is dynamic, false otherwise
+ */
+ private boolean shouldInlineRegex(NodeList<Expression>
regexPatternExpression) {
+ for (Expression expression : regexPatternExpression) {
+ if (expression instanceof MethodCallExpr methodCallExpr) {
+ boolean isInFactScope = methodCallExpr.getScope()
+ .filter(Expression::isNameExpr)
+ .map(Expression::asNameExpr)
+ .map(NameExpr::getNameAsString)
+ .filter("_this"::equals)
+ .isPresent();
+
+ return isInFactScope ||
shouldInlineRegex(methodCallExpr.getArguments());
+ }
+
+ return false;
+ }
+
+ return false;
+ }
Review Comment:
This loop returns false on the first non-MethodCallExpr, so subsequent
expressions are never inspected. Move the return false outside the loop and
only return true when a matching condition is found. Example fix: return true
inside the if when condition matches; otherwise continue the loop; finally
return false after the loop.
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/util/lambdareplace/MaterializedLambdaPredicate.java:
##########
@@ -65,6 +64,10 @@ void createMethodsDeclaration(EnumDeclaration
classDeclaration) {
}
private void createTestMethod(EnumDeclaration classDeclaration) {
+ collectRegexExpressions(lambdaExpr).stream()
+ .map(RegexExpr::getCompiledRegexMember)
Review Comment:
[nitpick] If the same regex literal is used multiple times in a single
lambda, this will add duplicate field declarations and cause a compilation
error. Deduplicate by the variable name before adding, e.g., map to the
VariableDeclarator name and distinct.
```suggestion
private void createTestMethod(EnumDeclaration classDeclaration) {
// Deduplicate regex member declarations by field name
java.util.Set<String> regexMemberNames = new java.util.HashSet<>();
collectRegexExpressions(lambdaExpr).stream()
.map(RegexExpr::getCompiledRegexMember)
.filter(member ->
regexMemberNames.add(member.getVariable(0).getNameAsString()))
```
##########
drools-model/drools-mvel-parser/src/main/java/org/drools/mvel/parser/ast/expr/RegexExpr.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.mvel.parser.ast.expr;
+
+import com.github.javaparser.ast.AllFieldsConstructor;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.FieldDeclaration;
+import com.github.javaparser.ast.expr.BinaryExpr;
+import com.github.javaparser.ast.expr.EnclosedExpr;
+import com.github.javaparser.ast.expr.LiteralExpr;
+import com.github.javaparser.ast.visitor.CloneVisitor;
+import com.github.javaparser.ast.visitor.GenericVisitor;
+import com.github.javaparser.ast.visitor.VoidVisitor;
+
+public final class RegexExpr extends LiteralExpr {
+
+ private final EnclosedExpr matchesEvaluationExpr;
+ private final FieldDeclaration compiledRegexMember;
+
+ @AllFieldsConstructor
+ public RegexExpr(FieldDeclaration compiledRegexMember, EnclosedExpr
matchesEvaluationExpr) {
+ super();
+
+ this.matchesEvaluationExpr = matchesEvaluationExpr;
+ this.compiledRegexMember = compiledRegexMember;
+ }
Review Comment:
[nitpick] @AllFieldsConstructor is redundant given the explicit constructor
below; if an annotation processor is enabled, this could generate a duplicate
constructor. Consider removing the annotation.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]