use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class RhsParser method parse.
public void parse(RuleDescr ruleDescr, RuleContext context, Rule rule) {
BlockStmt ruleVariablesBlock = new BlockStmt();
MethodCallExpr consequenceExpr = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals("drools")).orElse(false)).map(m -> processStatement(context, consequenceExpr, m, ruleVariablesBlock)).filter(Objects::nonNull).forEach(a -> rule.getRhs().addAction(a));
}
use of org.drools.drl.ast.descr.RuleDescr 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.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class ModelGenerator method getExtendedLhs.
private static AndDescr getExtendedLhs(PackageDescr packageDescr, RuleDescr ruleDescr) {
if (ruleDescr.getParentName() == null) {
return ruleDescr.getLhs();
}
RuleDescr parent = packageDescr.getRules().stream().filter(r -> r.getName().equals(ruleDescr.getParentName())).findFirst().orElseThrow(() -> new RuntimeException("Rule " + ruleDescr.getName() + " extends an unknown rule " + ruleDescr.getParentName()));
AndDescr extendedLhs = new AndDescr();
getExtendedLhs(packageDescr, parent).getDescrs().forEach(extendedLhs::addDescr);
ruleDescr.getLhs().getDescrs().forEach(extendedLhs::addDescr);
return extendedLhs;
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class ModelGenerator method generateModel.
public static void generateModel(KnowledgeBuilderImpl kbuilder, InternalKnowledgePackage pkg, PackageDescr packageDescr, PackageModel packageModel) {
TypeResolver typeResolver = pkg.getTypeResolver();
List<RuleDescr> ruleDescrs = packageDescr.getRules();
if (ruleDescrs.isEmpty()) {
return;
}
for (RuleDescr descr : ruleDescrs) {
RuleContext context = new RuleContext(kbuilder, packageModel, typeResolver, descr);
context.setDialectFromAttributes(packageDescr.getAttributes());
if (descr instanceof QueryDescr) {
QueryGenerator.processQueryDef(packageModel, context);
}
}
int parallelRulesBuildThreshold = kbuilder.getBuilderConfiguration().getParallelRulesBuildThreshold();
boolean parallelRulesBuild = parallelRulesBuildThreshold != -1 && ruleDescrs.size() > parallelRulesBuildThreshold;
if (parallelRulesBuild) {
List<RuleContext> ruleContexts = new ArrayList<>();
int i = 0;
for (RuleDescr ruleDescr : packageDescr.getRules()) {
ruleContexts.add(new RuleContext(kbuilder, packageModel, typeResolver, ruleDescr, i++));
}
KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.submit(() -> ruleContexts.parallelStream().forEach(context -> processRuleDescr(context, packageDescr))).join();
} else {
int i = 0;
for (RuleDescr ruleDescr : packageDescr.getRules()) {
processRuleDescr(new RuleContext(kbuilder, packageModel, typeResolver, ruleDescr, i++), packageDescr);
}
}
}
use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.
the class ConsequenceOffsetTest method testConsequenceOffset.
@Test
public void testConsequenceOffset() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset.drl")), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
int offset = -1;
assertFalse(kbuilder.hasErrors());
for (final RuleDescr rule : ((KnowledgeBuilderImpl) kbuilder).getPackageDescrs("com.sample").get(0).getRules()) {
if (rule.getName().equals("test")) {
offset = rule.getConsequenceOffset();
}
}
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset2.drl")), ResourceType.DRL);
kbuilder.add(ResourceFactory.newInputStreamResource(ConsequenceOffsetTest.class.getResourceAsStream("test_consequenceOffset.drl")), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
for (final RuleDescr rule : ((KnowledgeBuilderImpl) kbuilder).getPackageDescrs("com.sample").get(0).getRules()) {
if (rule.getName().equals("test")) {
assertEquals(offset, rule.getConsequenceOffset());
return;
}
}
fail();
}
Aggregations