use of org.drools.compiler.compiler.MissingDependencyError in project drools by kiegroup.
the class AbstractASMConsequenceBuilder method consequenceContext.
private Map<String, Object> consequenceContext(RuleBuildContext context, String consequenceName) {
String className = consequenceName + "Consequence";
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule(), consequenceName);
JavaAnalysisResult analysis = JavaRuleBuilderHelper.createJavaAnalysisResult(context, consequenceName, decls);
if (analysis == null) {
// not possible to get the analysis results
return null;
}
// this will fix modify, retract, insert, update, entrypoints and channels
try {
String fixedConsequence = KnowledgeHelperFixer.fix(AsmUtil.fixBlockDescr(context, analysis, decls));
return JavaRuleBuilderHelper.createConsequenceContext(context, consequenceName, className, fixedConsequence, decls, analysis.getBoundIdentifiers());
} catch (MissingDependencyException e) {
context.addError(new MissingDependencyError(context.getRuleDescr().getResource(), e));
}
return null;
}
use of org.drools.compiler.compiler.MissingDependencyError in project drools by kiegroup.
the class Consequence method rewriteRHS.
private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
AtomicBoolean requireDrools = new AtomicBoolean(false);
List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class);
List<AssignExpr> assignExprs = rhs.findAll(AssignExpr.class);
List<MethodCallExpr> updateExprs = new ArrayList<>();
Map<String, Type> rhsBodyDeclarations = new HashMap<>();
for (VariableDeclarator variableDeclarator : rhs.findAll(VariableDeclarator.class)) {
rhsBodyDeclarations.put(variableDeclarator.getNameAsString(), variableDeclarator.getType());
}
for (MethodCallExpr methodCallExpr : methodCallExprs) {
if (!methodCallExpr.getScope().isPresent() && isImplicitDroolsMethod(methodCallExpr)) {
if (methodCallExpr.getNameAsString().equals("insertLogical") && !TruthMaintenanceSystemFactory.present()) {
context.addCompilationError(new MissingDependencyError(TruthMaintenanceSystemFactory.NO_TMS));
}
methodCallExpr.setScope(new NameExpr("drools"));
}
if (hasDroolsScope(methodCallExpr) || hasDroolsAsParameter(methodCallExpr)) {
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();
Class<?> updatedClass = classFromRHSDeclarations(rhsBodyDeclarations, updatedVar);
// We might need to generate the domain metadata class for types used in consequence
// without an explicit pattern. See CompilerTest.testConsequenceInsertThenUpdate
context.getPackageModel().registerDomainClass(updatedClass);
if (context.isPropertyReactive(updatedClass)) {
if (!initializedBitmaskFields.contains(updatedVar)) {
Set<String> modifiedProps = findModifiedProperties(methodCallExprs, updateExpr, updatedVar, updatedClass);
modifiedProps.addAll(findModifiedPropertiesFromAssignment(assignExprs, updateExpr, updatedVar, updatedClass));
MethodCallExpr bitMaskCreation = createBitMaskInitialization(updatedClass, modifiedProps);
AssignExpr bitMaskAssign = createBitMaskField(updatedVar, bitMaskCreation);
if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, bitMaskAssign)) {
ruleBlock.addStatement(bitMaskAssign);
}
}
updateExpr.addArgument("mask_" + updatedVar);
initializedBitmaskFields.add(updatedVar);
}
}
}
return requireDrools.get();
}
Aggregations