Search in sources :

Example 86 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.

the class JavaRuleBuilderHelper method createJavaAnalysisResult.

public static JavaAnalysisResult createJavaAnalysisResult(final RuleBuildContext context, String consequenceName, Map<String, Declaration> decls) {
    final RuleDescr ruleDescr = context.getRuleDescr();
    BoundIdentifiers bindings = new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context, Collections.EMPTY_MAP, KnowledgeHelper.class);
    String consequenceStr = (RuleImpl.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName)) ? (String) ruleDescr.getConsequence() : (String) ruleDescr.getNamedConsequences().get(consequenceName);
    consequenceStr = consequenceStr + "\n";
    return (JavaAnalysisResult) context.getDialect().analyzeBlock(context, ruleDescr, consequenceStr, bindings);
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 87 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.

the class JavaConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.mvel.compiler.Cheese"));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    // this test was originally intended with PropertyReactive.ALLOWED:
    conf.setOption(PropertySpecificOption.ALLOWED);
    KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = descrToRule(ruleDescr);
    PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
    Declaration declr = p.addDeclaration("age");
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
    rule.addPattern(p);
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    kBuilder.compileAll();
    kBuilder.reloadAll();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.drl.ast.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.drl.ast.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Person(org.drools.mvel.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 88 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method preProcessRules.

private void preProcessRules(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    if (this.kBase == null) {
        return;
    }
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    boolean needsRemoval = false;
    // first, check if any rules no longer exist
    for (org.kie.api.definition.rule.Rule rule : pkg.getRules()) {
        if (filterAcceptsRemoval(ResourceChange.Type.RULE, rule.getPackageName(), rule.getName())) {
            needsRemoval = true;
            break;
        }
    }
    if (!needsRemoval) {
        for (RuleDescr ruleDescr : packageDescr.getRules()) {
            if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
                if (pkg.getRule(ruleDescr.getName()) != null) {
                    needsRemoval = true;
                    break;
                }
            }
        }
    }
    if (needsRemoval) {
        kBase.enqueueModification(() -> {
            Collection<RuleImpl> rulesToBeRemoved = new HashSet<>();
            for (org.kie.api.definition.rule.Rule rule : pkg.getRules()) {
                if (filterAcceptsRemoval(ResourceChange.Type.RULE, rule.getPackageName(), rule.getName())) {
                    rulesToBeRemoved.add(((RuleImpl) rule));
                }
            }
            rulesToBeRemoved.forEach(pkg::removeRule);
            for (RuleDescr ruleDescr : packageDescr.getRules()) {
                if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
                    RuleImpl rule = pkg.getRule(ruleDescr.getName());
                    if (rule != null) {
                        rulesToBeRemoved.add(rule);
                    }
                }
            }
            if (!rulesToBeRemoved.isEmpty()) {
                rulesToBeRemoved.addAll(findChildrenRulesToBeRemoved(packageDescr, rulesToBeRemoved));
                kBase.removeRules(rulesToBeRemoved);
            }
        });
    }
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Example 89 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method manageUnresolvedExtension.

private void manageUnresolvedExtension(RuleDescr ruleDescr, Collection<RuleDescr> candidates) {
    List<String> candidateRules = new ArrayList<>();
    for (RuleDescr r : candidates) {
        if (StringUtils.stringSimilarity(ruleDescr.getParentName(), r.getName(), StringUtils.SIMILARITY_STRATS.DICE) >= 0.75) {
            candidateRules.add(r.getName());
        }
    }
    String msg = "Unresolved parent name " + ruleDescr.getParentName();
    if (!candidateRules.isEmpty()) {
        msg += " >> did you mean any of :" + candidateRules;
    }
    results.add(new RuleBuildError(descrToRule(ruleDescr), ruleDescr, msg, "Unable to resolve parent rule, please check that both rules are in the same package"));
}
Also used : RuleBuildError(org.drools.compiler.compiler.RuleBuildError) ArrayList(java.util.ArrayList) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 90 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method compileAllQueries.

private void compileAllQueries(PackageDescr packageDescr, PackageRegistry pkgRegistry, List<RuleDescr> rules) {
    Map<String, RuleBuildContext> ruleCxts = buildRuleBuilderContexts(rules, pkgRegistry);
    for (RuleDescr ruleDescr : rules) {
        if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
            initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
            this.results.addAll(addRule(ruleCxts.get(ruleDescr.getName())));
        }
    }
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

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