Search in sources :

Example 41 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBuilderImpl method createPackageRegistry.

private PackageRegistry createPackageRegistry(PackageDescr packageDescr) {
    initPackage(packageDescr);
    InternalKnowledgePackage pkg;
    if (this.kBase == null || (pkg = this.kBase.getPackage(packageDescr.getName())) == null) {
        // there is no rulebase or it does not define this package so define it
        pkg = new KnowledgePackageImpl(packageDescr.getName());
        pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(this.rootClassLoader));
        // if there is a rulebase then add the package.
        if (this.kBase != null) {
            try {
                pkg = (InternalKnowledgePackage) this.kBase.addPackage(pkg).get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        } else {
            // the RuleBase will also initialise the
            pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
        }
    }
    PackageRegistry pkgRegistry = new PackageRegistry(rootClassLoader, configuration, pkg);
    // add default import for this namespace
    pkgRegistry.addImport(new ImportDescr(packageDescr.getNamespace() + ".*"));
    for (ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.registerImport(importDescr.getTarget());
    }
    return pkgRegistry;
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) ExecutionException(java.util.concurrent.ExecutionException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 42 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBuilderImpl method validateUniqueRuleNames.

protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
    final Set<String> names = new HashSet<String>();
    PackageRegistry packageRegistry = this.pkgRegistryMap.get(packageDescr.getNamespace());
    InternalKnowledgePackage pkg = null;
    if (packageRegistry != null) {
        pkg = packageRegistry.getPackage();
    }
    for (final RuleDescr rule : packageDescr.getRules()) {
        validateRule(packageDescr, rule);
        final String name = rule.getName();
        if (names.contains(name)) {
            addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
        }
        if (pkg != null) {
            RuleImpl duplicatedRule = pkg.getRule(name);
            if (duplicatedRule != null) {
                Resource resource = rule.getResource();
                Resource duplicatedResource = duplicatedRule.getResource();
                if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
                    addBuilderResult(new DuplicateRule(rule, packageDescr, this.configuration));
                } else {
                    addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
                }
            }
        }
        names.add(name);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) DuplicateRule(org.drools.compiler.compiler.DuplicateRule) ParserError(org.drools.compiler.compiler.ParserError) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Resource(org.kie.api.io.Resource) ReaderResource(org.drools.core.io.impl.ReaderResource) PMMLResource(org.drools.compiler.compiler.PMMLResource) InternalResource(org.drools.core.io.internal.InternalResource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) HashSet(java.util.HashSet) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 43 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBuilderImpl method rewireAllClassObjectTypes.

public void rewireAllClassObjectTypes() {
    if (kBase != null) {
        for (InternalKnowledgePackage pkg : kBase.getPackagesMap().values()) {
            pkg.getDialectRuntimeRegistry().getDialectData("java").setDirty(true);
            pkg.getClassFieldAccessorStore().wire();
        }
    }
}
Also used : InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 44 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage 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 LinkedList<RuleDescr>();
    Map<String, List<RuleDescr>> children = new HashMap<String, List<RuleDescr>>();
    LinkedHashMap<String, RuleDescr> sorted = new LinkedHashMap<String, RuleDescr>();
    List<RuleDescr> queries = new ArrayList<RuleDescr>();
    Set<String> compiledRules = new HashSet<String>();
    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) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Example 45 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class KnowledgeBuilderImpl method processWindowDeclarations.

private void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
        WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
        // TODO: process annotations
        // process pattern
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
        RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
        dummy.setResource(packageDescr.getResource());
        dummy.addAttribute(new AttributeDescr("dialect", "java"));
        RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
        if (builder != null) {
            final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
            if (pattern.getXpathConstraint() != null) {
                context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
            }
            window.setPattern(pattern);
        } else {
            throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
        }
        if (!context.getErrors().isEmpty()) {
            for (DroolsError error : context.getErrors()) {
                addBuilderResult(error);
            }
        } else {
            pkgRegistry.getPackage().addWindowDeclaration(window);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) DroolsError(org.drools.compiler.compiler.DroolsError) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) WindowDeclaration(org.drools.core.rule.WindowDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) WindowDeclarationDescr(org.drools.compiler.lang.descr.WindowDeclarationDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)97 Test (org.junit.Test)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)28 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)26 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)24 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)21 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)20 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)16 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 TypeDeclaration (org.drools.core.rule.TypeDeclaration)13 HashMap (java.util.HashMap)12 Pattern (org.drools.core.rule.Pattern)12 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)11 ClassObjectType (org.drools.core.base.ClassObjectType)10 ArrayList (java.util.ArrayList)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)9 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)8