Search in sources :

Example 21 with PackageRegistry

use of org.drools.compiler.compiler.PackageRegistry 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 22 with PackageRegistry

use of org.drools.compiler.compiler.PackageRegistry 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 23 with PackageRegistry

use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.

the class KnowledgeBuilderImpl method buildRules.

protected void buildRules(Collection<CompositePackageDescr> packages) {
    for (CompositePackageDescr packageDescr : packages) {
        setAssetFilter(packageDescr.getFilter());
        PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
        compileKnowledgePackages(packageDescr, pkgRegistry);
        setAssetFilter(null);
    }
    wireAllRules();
    processKieBaseTypes();
    for (CompositePackageDescr packageDescr : packages) {
        setAssetFilter(packageDescr.getFilter());
        compileRete(packageDescr);
        setAssetFilter(null);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 24 with PackageRegistry

use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.

the class TypeDeclarationCache method getExistingTypeDeclaration.

private TypeDeclaration getExistingTypeDeclaration(Class<?> cls) {
    TypeDeclaration typeDeclaration = null;
    PackageRegistry pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(cls));
    if (pkgReg != null) {
        String className = cls.getName();
        String typeName = className.substring(className.lastIndexOf(".") + 1);
        typeDeclaration = pkgReg.getPackage().getTypeDeclaration(typeName);
    }
    return typeDeclaration;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 25 with PackageRegistry

use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.

the class TypeDeclarationCache method createTypeDeclarationForBean.

private TypeDeclaration createTypeDeclarationForBean(Class<?> cls) {
    Annotated annotated = new Annotated.ClassAdapter(cls);
    TypeDeclaration typeDeclaration = TypeDeclaration.createTypeDeclarationForBean(cls, annotated, kbuilder.getBuilderConfiguration().getPropertySpecificOption());
    String namespace = ClassUtils.getPackage(cls);
    PackageRegistry pkgRegistry = kbuilder.getOrCreatePackageRegistry(new PackageDescr(namespace));
    processMvelBasedAccessors(kbuilder, pkgRegistry, annotated, typeDeclaration);
    return typeDeclaration;
}
Also used : Annotated(org.drools.core.rule.Annotated) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

PackageRegistry (org.drools.compiler.compiler.PackageRegistry)33 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)17 TypeDeclaration (org.drools.core.rule.TypeDeclaration)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)10 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)7 ArrayList (java.util.ArrayList)6 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)6 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)3 ClassObjectType (org.drools.core.base.ClassObjectType)3