use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(PackageDescr packageDescr, RuleContext context) {
PackageModel packageModel = context.getPackageModel();
RuleDescr ruleDescr = context.getRuleDescr();
context.addGlobalDeclarations();
context.setDialectFromAttributes(ruleDescr.getAttributes().values());
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
BlockStmt ruleVariablesBlock = context.getRuleVariablesBlock();
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
if (context.hasCompilationError()) {
return;
}
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier()), toClassOrInterfaceType(Rule.class), ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(toClassOrInterfaceType(Rule.class), "rule");
MethodCallExpr ruleCall = createDslTopLevelMethod(RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(toStringLiteral(ruleDescr.getNamespace()));
}
ruleCall.addArgument(toStringLiteral(ruleDescr.getName()));
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(toClassOrInterfaceType(ruleUnitDescr.getCanonicalName()))) : ruleCall;
for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
attributeExpr.setScope(buildCallScope);
buildCallScope = attributeExpr;
}
for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
metaAttributeExpr.setScope(buildCallScope);
buildCallScope = metaAttributeExpr;
}
MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
createVariables(ruleVariablesBlock, packageModel, context);
ruleMethod.setBody(ruleVariablesBlock);
MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
buildCall.addArgument(executeCall);
ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
ruleVariablesBlock.addStatement(new ReturnStmt("rule"));
packageModel.putRuleMethod(ruleUnitDescr != null ? ruleUnitDescr.getSimpleName() : DEFAULT_RULE_UNIT, ruleMethod, context.getRuleIndex());
}
use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.
the class ModelGenerator method processRuleDescr.
private static void processRuleDescr(RuleContext context, PackageDescr packageDescr) {
if (context.getRuleDescr() instanceof QueryDescr) {
QueryGenerator.processQuery(context.getPackageModel(), (QueryDescr) context.getRuleDescr());
return;
}
context.setDialectFromAttributes(packageDescr.getAttributes());
processRule(packageDescr, context);
RuleUnitDescription rud = context.getRuleUnitDescr();
if (rud != null) {
context.getPackageModel().addRuleUnit(rud);
}
}
use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.
the class RuleContext method findUnitDescr.
private void findUnitDescr() {
if (ruleDescr == null) {
return;
}
boolean useNamingConvention = false;
String unitName;
AnnotationDescr unitAnn = ruleDescr.getAnnotation("Unit");
if (unitAnn != null) {
unitName = (String) unitAnn.getValue();
unitName = unitName.substring(0, unitName.length() - ".class".length());
} else if (ruleDescr.getUnit() != null) {
unitName = ruleDescr.getUnit().getTarget();
} else {
if (ruleDescr.getResource() == null) {
return;
}
String drlFile = ruleDescr.getResource().getSourcePath();
if (drlFile != null) {
String drlFileName = drlFile.substring(drlFile.lastIndexOf('/') + 1);
unitName = packageModel.getName() + '.' + drlFileName.substring(0, drlFileName.length() - ".drl".length());
useNamingConvention = true;
} else {
return;
}
}
RuleUnitDescriptionLoader ruleUnitDescriptionLoader = kbuilder.getPackageRegistry(packageModel.getName()).getPackage().getRuleUnitDescriptionLoader();
Optional<RuleUnitDescription> ruDescr = ruleUnitDescriptionLoader.getDescription(unitName);
if (ruDescr.isPresent()) {
ruleUnitDescr = ruDescr.get();
} else if (!useNamingConvention) {
throw new UnknownRuleUnitException(unitName);
}
}
use of org.kie.internal.ruleunit.RuleUnitDescription in project drools by kiegroup.
the class QueryGenerator method processQuery.
public static void processQuery(PackageModel packageModel, QueryDescr queryDescr) {
String queryDefVariableName = toQueryDef(queryDescr.getName());
RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
context.addGlobalDeclarations();
context.setDialectFromAttributes(queryDescr.getAttributes().values());
new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
if (context.getRuleUnitDescr() != null) {
Map<String, Class<?>> queryBindings = new HashMap<>();
for (DeclarationSpec declr : context.getAllDeclarations()) {
if (!declr.isGlobal() && !declr.getBindingId().startsWith(GENERATED_VARIABLE_PREFIX)) {
queryBindings.put(declr.getBindingId(), declr.getDeclarationClass());
}
}
QueryModel queryModel = new QueryModel(queryDescr.getName(), queryDescr.getNamespace(), queryDescr.getParameters(), queryBindings);
packageModel.addQueryInRuleUnit(context.getRuleUnitDescr(), queryModel);
}
final Type queryType = toClassOrInterfaceType(Query.class);
MethodDeclaration queryMethod = new MethodDeclaration(NodeList.nodeList(Modifier.privateModifier()), queryType, QUERY_METHOD_PREFIX + toId(queryDescr.getName()));
BlockStmt queryBody = new BlockStmt();
ModelGenerator.createVariables(queryBody, packageModel, context);
queryMethod.setBody(queryBody);
String queryBuildVarName = toId(queryDescr.getName()) + "_build";
VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
context.getExpressions().forEach(buildCall::addArgument);
AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
queryBody.addStatement(queryBuildAssign);
queryBody.addStatement(new ReturnStmt(queryBuildVarName));
packageModel.putQueryMethod(queryMethod);
RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
if (ruleUnitDescr != null) {
packageModel.putRuleUnit(ruleUnitDescr.getSimpleName());
}
}
Aggregations