Source:
private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) { AtomicBoolean requireDrools = new AtomicBoolean(false); List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class); List<MethodCallExpr> updateExprs = new ArrayList<>(); Map<String, String> newDeclarations = new HashMap<>(); for (VariableDeclarator variableDeclarator : rhs.findAll(VariableDeclarator.class)) { variableDeclarator.getInitializer().ifPresent( init -> newDeclarations.put( variableDeclarator.getNameAsString(), init.toString() ) ); } for (MethodCallExpr methodCallExpr : methodCallExprs) { if (isDroolsMethod(methodCallExpr)) { if (!methodCallExpr.getScope().isPresent()) { methodCallExpr.setScope(new NameExpr("drools")); } if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) { methodCallExpr.setScope(createAsKnoledgeHelperExpression()); } else if (methodCallExpr.getNameAsString().equals("update")) { if (methodCallExpr.toString().contains( "FactHandle" )) { methodCallExpr.setScope( new NameExpr( "((org.drools.modelcompiler.consequence.DroolsImpl) drools)" ) ); } updateExprs.add(methodCallExpr); } else if (methodCallExpr.getNameAsString().equals("retract")) { methodCallExpr.setName(new SimpleName("delete")); } requireDrools.set(true); } } Set<String> initializedBitmaskFields = new HashSet<>(); for (MethodCallExpr updateExpr : updateExprs) { Expression argExpr = updateExpr.getArgument(0); if (argExpr instanceof NameExpr) { String updatedVar = ((NameExpr) argExpr).getNameAsString(); Set<String> modifiedProps = findModifiedProperties( methodCallExprs, updateExpr, updatedVar ); if (!initializedBitmaskFields.contains(updatedVar)) { MethodCallExpr bitMaskCreation = createBitMaskInitialization(newDeclarations, updatedVar, modifiedProps); ruleBlock.addStatement(createBitMaskField(updatedVar, bitMaskCreation)); } updateExpr.addArgument("mask_" + updatedVar); initializedBitmaskFields.add(updatedVar); } } return requireDrools.get(); }

Target:
private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) { AtomicBoolean requireDrools = new AtomicBoolean(false); List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class); List<MethodCallExpr> updateExprs = new ArrayList<>(); Map<String, String> newDeclarations = new HashMap<>(); for (VariableDeclarator variableDeclarator : rhs.findAll(VariableDeclarator.class)) { variableDeclarator.getInitializer().ifPresent( init -> newDeclarations.put( variableDeclarator.getNameAsString(), init.toString() ) ); } for (MethodCallExpr methodCallExpr : methodCallExprs) { if (isDroolsMethod(methodCallExpr)) { if (!methodCallExpr.getScope().isPresent()) { methodCallExpr.setScope(new NameExpr("drools")); } if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) { methodCallExpr.setScope(createAsKnowledgeHelperExpression()); } else if (methodCallExpr.getNameAsString().equals("update")) { if (methodCallExpr.toString().contains( "FactHandle" )) { methodCallExpr.setScope( new NameExpr( "((org.drools.modelcompiler.consequence.DroolsImpl) drools)" ) ); } updateExprs.add(methodCallExpr); } else if (methodCallExpr.getNameAsString().equals("retract")) { methodCallExpr.setName(new SimpleName("delete")); } requireDrools.set(true); } } Set<String> initializedBitmaskFields = new HashSet<>(); for (MethodCallExpr updateExpr : updateExprs) { Expression argExpr = updateExpr.getArgument(0); if (argExpr instanceof NameExpr) { String updatedVar = ((NameExpr) argExpr).getNameAsString(); Set<String> modifiedProps = findModifiedProperties( methodCallExprs, updateExpr, updatedVar ); if (!initializedBitmaskFields.contains(updatedVar)) { MethodCallExpr bitMaskCreation = createBitMaskInitialization(newDeclarations, updatedVar, modifiedProps); ruleBlock.addStatement(createBitMaskField(updatedVar, bitMaskCreation)); } updateExpr.addArgument("mask_" + updatedVar); initializedBitmaskFields.add(updatedVar); } } return requireDrools.get(); }