Skip to content

Commit

Permalink
Fix flaky test in AccumulateTest#testInlineAccumulateWithAnd by using…
Browse files Browse the repository at this point in the history
… TreeSet to ensure order consistency

- Changed extractUsedDeclarations method to use TreeSet instead of Set to guarantee a consistent order of declarations.
- This resolves the flaky behavior encountered when running with NonDex, which caused Drools to fail compiling the Java file.
- The issue stemmed from the fact that the original Set did not maintain order, which is crucial when the order of arguments is important in rule compilation.

Signed-off-by: Yusen Wang <[email protected]>
  • Loading branch information
everbrightw committed Sep 9, 2024
1 parent 6be58bc commit effc20d
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ast.Modifier;
Expand Down Expand Up @@ -65,7 +67,6 @@
import org.drools.util.StringUtils;

import static com.github.javaparser.StaticJavaParser.parseExpression;
import static java.util.stream.Collectors.toSet;
import static org.drools.model.codegen.execmodel.PackageModel.DOMAIN_CLASSESS_METADATA_FILE_NAME;
import static org.drools.model.codegen.execmodel.PackageModel.DOMAIN_CLASS_METADATA_INSTANCE;
import static org.drools.model.codegen.execmodel.generator.DrlxParseUtil.addCurlyBracesToBlock;
Expand Down Expand Up @@ -251,18 +252,22 @@ private void rewriteChannels(BlockStmt consequence) {
}

private Set<String> extractUsedDeclarations(BlockStmt ruleConsequence, String consequenceString) {
Set<String> existingDecls = new HashSet<>();
Set<String> existingDecls = new TreeSet<>();
existingDecls.addAll(context.getAvailableBindings());
existingDecls.addAll(context.getGlobals().keySet());
if (context.getRuleUnitDescr() != null) {
existingDecls.addAll(context.getRuleUnitDescr().getUnitVars());
}

if (context.getRuleDialect() == RuleContext.RuleDialect.MVEL) {
return existingDecls.stream().filter(d -> containsWord(d, consequenceString)).collect(toSet());
return existingDecls.stream()
.filter(d -> containsWord(d, consequenceString))
.collect(Collectors.toCollection(TreeSet::new));
} else if (ruleConsequence != null) {
Set<String> declUsedInRHS = ruleConsequence.findAll(NameExpr.class).stream().map(NameExpr::getNameAsString).collect(toSet());
return existingDecls.stream().filter(declUsedInRHS::contains).collect(toSet());
Set<String> declUsedInRHS = ruleConsequence.findAll(NameExpr.class).stream().map(NameExpr::getNameAsString).collect(Collectors.toCollection(TreeSet::new));
return existingDecls.stream()
.filter(declUsedInRHS::contains)
.collect(Collectors.toCollection(TreeSet::new));
}

throw new IllegalArgumentException("Unknown rule dialect " + context.getRuleDialect() + "!");
Expand Down

0 comments on commit effc20d

Please sign in to comment.