Search in sources :

Example 96 with RuleDescr

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

the class KiePMMLDescrFactoryTest method checkRules.

private void checkRules(List<RuleDescr> toCheck) {
    assertEquals(1, toCheck.size());
    RuleDescr retrieved = toCheck.get(0);
    assertEquals(RULE_NAME, retrieved.getName());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 97 with RuleDescr

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

the class KnowledgeBuilderImpl method reportHierarchyErrors.

private void reportHierarchyErrors(Map<String, List<RuleDescr>> parents, Map<String, RuleDescr> sorted) {
    boolean circularDep = false;
    for (List<RuleDescr> rds : parents.values()) {
        for (RuleDescr ruleDescr : rds) {
            if (parents.get(ruleDescr.getParentName()) != null && (sorted.containsKey(ruleDescr.getName()) || parents.containsKey(ruleDescr.getName()))) {
                circularDep = true;
                results.add(new RuleBuildError(descrToRule(ruleDescr), ruleDescr, null, "Circular dependency in rules hierarchy"));
                break;
            }
            manageUnresolvedExtension(ruleDescr, sorted.values());
        }
        if (circularDep) {
            break;
        }
    }
}
Also used : RuleBuildError(org.drools.compiler.compiler.RuleBuildError) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 98 with RuleDescr

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

the class KnowledgeBuilderImpl method sortRulesByDependency.

private SortedRules sortRulesByDependency(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    // Using a topological sorting algorithm
    // see http://en.wikipedia.org/wiki/Topological_sorting
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    List<RuleDescr> roots = new ArrayList<>();
    Map<String, List<RuleDescr>> children = new HashMap<>();
    LinkedHashMap<String, RuleDescr> sorted = new LinkedHashMap<>();
    List<RuleDescr> queries = new ArrayList<>();
    Set<String> compiledRules = new HashSet<>();
    for (RuleDescr ruleDescr : packageDescr.getRules()) {
        if (ruleDescr.isQuery()) {
            queries.add(ruleDescr);
        } else if (!ruleDescr.hasParent()) {
            roots.add(ruleDescr);
        } else {
            if (pkg.getRule(ruleDescr.getParentName()) != null) {
                // The parent of this rule has been already compiled
                compiledRules.add(ruleDescr.getParentName());
            }
            children.computeIfAbsent(ruleDescr.getParentName(), k -> new ArrayList<>()).add(ruleDescr);
        }
    }
    SortedRules sortedRules = new SortedRules();
    sortedRules.queries = queries;
    if (children.isEmpty()) {
        // Sorting not necessary
        if (!queries.isEmpty()) {
            // Build all queries first
            packageDescr.getRules().removeAll(queries);
            packageDescr.getRules().addAll(0, queries);
            sortedRules.rules.add(packageDescr.getRules().subList(queries.size(), packageDescr.getRules().size()));
        } else {
            sortedRules.rules.add(packageDescr.getRules());
        }
        return sortedRules;
    }
    for (String compiledRule : compiledRules) {
        List<RuleDescr> childz = children.remove(compiledRule);
        roots.addAll(childz);
    }
    List<RuleDescr> rulesLevel = roots;
    while (!rulesLevel.isEmpty()) {
        rulesLevel = sortRulesLevel(rulesLevel, sorted, sortedRules, children);
        sortedRules.newLevel();
    }
    reportHierarchyErrors(children, sorted);
    packageDescr.getRules().clear();
    packageDescr.getRules().addAll(queries);
    for (RuleDescr descr : sorted.values()) {
        packageDescr.getRules().add(descr);
    }
    return sortedRules;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Example 99 with RuleDescr

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

the class KnowledgeBuilderImpl method normalizeRuleAnnotations.

public void normalizeRuleAnnotations(PackageDescr packageDescr, TypeResolver typeResolver) {
    boolean isStrict = configuration.getLanguageLevel().useJavaAnnotations();
    for (RuleDescr ruleDescr : packageDescr.getRules()) {
        normalizeAnnotations(ruleDescr, typeResolver, isStrict);
        traverseAnnotations(ruleDescr.getLhs(), typeResolver, isStrict);
    }
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 100 with RuleDescr

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

the class KnowledgeBuilderImpl method compileRulesLevel.

private void compileRulesLevel(PackageDescr packageDescr, PackageRegistry pkgRegistry, List<RuleDescr> rules) {
    boolean parallelRulesBuild = this.kBase == null && parallelRulesBuildThreshold != -1 && rules.size() > parallelRulesBuildThreshold;
    if (parallelRulesBuild) {
        Map<String, RuleBuildContext> ruleCxts = new ConcurrentHashMap<>();
        try {
            ForkJoinPoolHolder.COMPILER_POOL.submit(() -> rules.stream().parallel().filter(ruleDescr -> filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())).forEach(ruleDescr -> {
                initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
                RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
                ruleCxts.put(ruleDescr.getName(), context);
                List<? extends KnowledgeBuilderResult> results = addRule(context);
                if (!results.isEmpty()) {
                    synchronized (this.results) {
                        this.results.addAll(results);
                    }
                }
            })).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException("Rules compilation failed or interrupted", e);
        }
        for (RuleDescr ruleDescr : rules) {
            RuleBuildContext context = ruleCxts.get(ruleDescr.getName());
            if (context != null) {
                pkgRegistry.getPackage().addRule(context.getRule());
            }
        }
    } else {
        for (RuleDescr ruleDescr : rules) {
            if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
                initRuleDescr(packageDescr, pkgRegistry, ruleDescr);
                RuleBuildContext context = buildRuleBuilderContext(pkgRegistry, ruleDescr);
                this.results.addAll(addRule(context));
                pkgRegistry.getPackage().addRule(context.getRule());
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleBuildContext.descrToRule(org.drools.compiler.rule.builder.RuleBuildContext.descrToRule) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) DroolsParserException(org.drools.drl.parser.DroolsParserException) CoreComponentFactory(org.drools.core.reteoo.CoreComponentFactory) DroolsWarningWrapper(org.drools.compiler.compiler.DroolsWarningWrapper) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateRule(org.drools.compiler.compiler.DuplicateRule) Map(java.util.Map) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) GuidedRuleTemplateProvider(org.drools.drl.extensions.GuidedRuleTemplateProvider) KnowledgeBaseImpl.registerFunctionClassAndInnerClasses(org.drools.core.impl.KnowledgeBaseImpl.registerFunctionClassAndInnerClasses) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Set(java.util.Set) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) KieAssemblers(org.kie.api.internal.assembler.KieAssemblers) Reader(java.io.Reader) DialectError(org.drools.compiler.rule.builder.dialect.DialectError) EntryPointDeclarationDescr(org.drools.drl.ast.descr.EntryPointDeclarationDescr) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceConfiguration(org.kie.api.io.ResourceConfiguration) UncheckedIOException(java.io.UncheckedIOException) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DroolsWarning(org.drools.compiler.compiler.DroolsWarning) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) BuildContext(org.drools.compiler.kie.builder.impl.BuildContext) AccessController(java.security.AccessController) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ObjectType(org.drools.core.spi.ObjectType) ChangeSet(org.kie.internal.ChangeSet) ResourceType(org.kie.api.io.ResourceType) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ConfigurableSeverityResult(org.drools.compiler.compiler.ConfigurableSeverityResult) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) DrlParser(org.drools.drl.parser.DrlParser) PackageBuilderResults(org.drools.compiler.compiler.PackageBuilderResults) DecisionTableConfigurationImpl(org.drools.core.builder.conf.impl.DecisionTableConfigurationImpl) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) ImportDeclaration(org.drools.core.rule.ImportDeclaration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) IOException(java.io.IOException) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) File(java.io.File) WindowDeclaration(org.drools.core.rule.WindowDeclaration) KnowledgeBaseFactory(org.drools.kiesession.rulebase.KnowledgeBaseFactory) ExecutionException(java.util.concurrent.ExecutionException) Dialect(org.drools.compiler.compiler.Dialect) StringReader(java.io.StringReader) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ForkJoinPool(java.util.concurrent.ForkJoinPool) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) DroolsErrorWrapper(org.drools.compiler.compiler.DroolsErrorWrapper) ProcessBuilder(org.drools.compiler.compiler.ProcessBuilder) Array(java.lang.reflect.Array) ResourceChange(org.kie.internal.builder.ResourceChange) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) Function(org.drools.core.rule.Function) TypeDeclaration(org.drools.core.rule.TypeDeclaration) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) TypeResolver(org.drools.core.addon.TypeResolver) AnnotatedBaseDescr(org.drools.drl.ast.descr.AnnotatedBaseDescr) ResourceWithConfiguration(org.kie.api.io.ResourceWithConfiguration) LoggerFactory(org.slf4j.LoggerFactory) PatternDescr(org.drools.drl.ast.descr.PatternDescr) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) DecisionTableFactory(org.drools.drl.extensions.DecisionTableFactory) ReleaseId(org.kie.api.builder.ReleaseId) ImportDescr(org.drools.drl.ast.descr.ImportDescr) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) RuleBaseFactory(org.drools.core.impl.RuleBaseFactory) ResourceConversionResult(org.drools.drl.extensions.ResourceConversionResult) Method(java.lang.reflect.Method) MissingImplementationException(org.drools.compiler.builder.impl.errors.MissingImplementationException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) Collection(java.util.Collection) Process(org.kie.api.definition.process.Process) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) UUID(java.util.UUID) PrivilegedAction(java.security.PrivilegedAction) BaseDescr(org.drools.drl.ast.descr.BaseDescr) Resource(org.kie.api.io.Resource) List(java.util.List) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DroolsStreamUtils(org.drools.core.util.DroolsStreamUtils) SAXException(org.xml.sax.SAXException) DroolsError(org.drools.drl.parser.DroolsError) Optional(java.util.Optional) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ResourceTypeDeclarationWarning(org.drools.compiler.compiler.ResourceTypeDeclarationWarning) StringUtils(org.drools.core.util.StringUtils) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ClassObjectType(org.drools.core.base.ClassObjectType) ReaderResource(org.drools.core.io.impl.ReaderResource) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) HashMap(java.util.HashMap) RuleBuilder(org.drools.compiler.rule.builder.RuleBuilder) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) WindowDeclarationDescr(org.drools.drl.ast.descr.WindowDeclarationDescr) Stack(java.util.Stack) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) PatternDestinationDescr(org.drools.drl.ast.descr.PatternDestinationDescr) HashSet(java.util.HashSet) IoUtils(org.drools.core.util.IoUtils) DSLMappingFile(org.drools.drl.parser.lang.dsl.DSLMappingFile) GuidedRuleTemplateFactory(org.drools.drl.extensions.GuidedRuleTemplateFactory) ResultSeverity(org.kie.internal.builder.ResultSeverity) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) KieBase(org.kie.api.KieBase) KiePackage(org.kie.api.definition.KiePackage) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) RuleBase(org.drools.core.impl.RuleBase) KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) Logger(org.slf4j.Logger) InternalResource(org.drools.core.io.internal.InternalResource) ComponentsFactory(org.drools.wiring.api.ComponentsFactory) KieService(org.kie.api.internal.utils.KieService) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) PackageBuilderErrors(org.drools.compiler.compiler.PackageBuilderErrors) GlobalError(org.drools.compiler.compiler.GlobalError) AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) InternalKnowledgeBuilder(org.drools.compiler.builder.InternalKnowledgeBuilder) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ProcessBuilderFactory(org.drools.compiler.compiler.ProcessBuilderFactory) ExpanderException(org.drools.drl.parser.lang.ExpanderException) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) ParserError(org.drools.drl.parser.ParserError) InputStream(java.io.InputStream) BaseKnowledgeBuilderResultImpl(org.drools.drl.parser.BaseKnowledgeBuilderResultImpl) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExecutionException(java.util.concurrent.ExecutionException)

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