Search in sources :

Example 91 with RuleDescr

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));
}
Also used : ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ParserUtil.literalToValue(org.drools.impact.analysis.parser.impl.ParserUtil.literalToValue) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ParserUtil.isLiteral(org.drools.impact.analysis.parser.impl.ParserUtil.isLiteral) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ParserUtil.getLiteralValue(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralValue) ParserUtil.literalType(org.drools.impact.analysis.parser.impl.ParserUtil.literalType) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) ParserUtil.objectCreationExprToValue(org.drools.impact.analysis.parser.impl.ParserUtil.objectCreationExprToValue) Method(java.lang.reflect.Method) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ParserUtil.stripEnclosedAndCast(org.drools.impact.analysis.parser.impl.ParserUtil.stripEnclosedAndCast) ClassUtils(org.drools.core.util.ClassUtils) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) Consequence(org.drools.modelcompiler.builder.generator.Consequence) Optional(java.util.Optional) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Consequence(org.drools.modelcompiler.builder.generator.Consequence) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 92 with RuleDescr

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());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PackageModel(org.drools.modelcompiler.builder.PackageModel) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) Rule(org.drools.model.Rule) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 93 with RuleDescr

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;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 94 with RuleDescr

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);
        }
    }
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) TypeResolver(org.drools.core.addon.TypeResolver) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ArrayList(java.util.ArrayList)

Example 95 with RuleDescr

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();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.drl.ast.descr.RuleDescr)191 Test (org.junit.Test)147 PackageDescr (org.drools.drl.ast.descr.PackageDescr)105 PatternDescr (org.drools.drl.ast.descr.PatternDescr)104 AndDescr (org.drools.drl.ast.descr.AndDescr)55 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)33 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)27 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)27 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)24 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)14 OrDescr (org.drools.drl.ast.descr.OrDescr)14 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)13 Pattern (org.drools.core.rule.Pattern)13 List (java.util.List)12 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 FromDescr (org.drools.drl.ast.descr.FromDescr)11