Search in sources :

Example 6 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage 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()) {
                kBase.removeRules(rulesToBeRemoved);
            }
        });
    }
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Example 7 with InternalKnowledgePackage

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

the class KnowledgeBuilderImpl method addPackage.

public synchronized void addPackage(InternalKnowledgePackage newPkg) {
    PackageRegistry pkgRegistry = this.pkgRegistryMap.get(newPkg.getName());
    InternalKnowledgePackage pkg = null;
    if (pkgRegistry != null) {
        pkg = pkgRegistry.getPackage();
    }
    if (pkg == null) {
        PackageDescr packageDescr = new PackageDescr(newPkg.getName());
        pkgRegistry = getOrCreatePackageRegistry(packageDescr);
        mergePackage(this.pkgRegistryMap.get(packageDescr.getNamespace()), packageDescr);
        pkg = pkgRegistry.getPackage();
    }
    // first merge anything related to classloader re-wiring
    pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader);
    if (newPkg.getFunctions() != null) {
        for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
            if (pkg.getFunctions().containsKey(entry.getKey())) {
                addBuilderResult(new DuplicateFunction(entry.getValue(), this.configuration));
            }
            pkg.addFunction(entry.getValue());
        }
    }
    pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
    pkg.getDialectRuntimeRegistry().onBeforeExecute();
    // we have to do this before the merging, as it does some classloader resolving
    TypeDeclaration lastType = null;
    try {
        // Resolve the class for the type declaation
        if (newPkg.getTypeDeclarations() != null) {
            // add type declarations
            for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
                lastType = type;
                type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
            }
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("unable to resolve Type Declaration class '" + lastType.getTypeName() + "'");
    }
    // now merge the new package into the existing one
    mergePackage(pkg, newPkg);
}
Also used : AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) Function(org.drools.core.rule.Function) DuplicateFunction(org.drools.compiler.compiler.DuplicateFunction) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 8 with InternalKnowledgePackage

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

the class KnowledgeBuilderImpl method processGlobals.

private void processGlobals(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    Set<String> existingGlobals = new HashSet<String>(pkg.getGlobals().keySet());
    for (final GlobalDescr global : packageDescr.getGlobals()) {
        final String identifier = global.getIdentifier();
        existingGlobals.remove(identifier);
        String className = global.getType();
        // JBRULES-3039: can't handle type name with generic params
        while (className.indexOf('<') >= 0) {
            className = className.replaceAll("<[^<>]+?>", "");
        }
        try {
            Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(className);
            if (clazz.isPrimitive()) {
                addBuilderResult(new GlobalError(global, " Primitive types are not allowed in globals : " + className));
                return;
            }
            pkg.addGlobal(identifier, clazz);
            addGlobal(identifier, clazz);
            if (kBase != null) {
                kBase.addGlobal(identifier, clazz);
            }
        } catch (final ClassNotFoundException e) {
            addBuilderResult(new GlobalError(global, e.getMessage()));
            e.printStackTrace();
        }
    }
    for (String toBeRemoved : existingGlobals) {
        if (filterAcceptsRemoval(ResourceChange.Type.GLOBAL, pkg.getName(), toBeRemoved)) {
            pkg.removeGlobal(toBeRemoved);
            if (kBase != null) {
                kBase.removeGlobal(toBeRemoved);
            }
        }
    }
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) GlobalError(org.drools.compiler.compiler.GlobalError) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Example 9 with InternalKnowledgePackage

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

the class KnowledgeBuilderImpl method getPackages.

public InternalKnowledgePackage[] getPackages() {
    InternalKnowledgePackage[] pkgs = new InternalKnowledgePackage[this.pkgRegistryMap.size()];
    String errors = null;
    if (!getErrors().isEmpty()) {
        errors = getErrors().toString();
    }
    int i = 0;
    for (PackageRegistry pkgRegistry : this.pkgRegistryMap.values()) {
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        pkg.getDialectRuntimeRegistry().onBeforeExecute();
        if (errors != null) {
            pkg.setError(errors);
        }
        pkgs[i++] = pkg;
    }
    return pkgs;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 10 with InternalKnowledgePackage

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

the class TypeDeclarationBuilder method processUnresolvedTypes.

private void processUnresolvedTypes(PackageDescr packageDescr, PackageRegistry pkgRegistry, Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    // merge "duplicate" definitions and declarations
    unsortedDescrs = compactDefinitionsAndDeclarations(unsortedDescrs, unprocesseableDescrs);
    // now sort declarations by mutual dependencies
    ClassHierarchyManager classHierarchyManager = new ClassHierarchyManager(unsortedDescrs, kbuilder);
    for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
        PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
        createBean(typeDescr, pkgReg, classHierarchyManager, unresolvedTypes, unprocesseableDescrs);
    }
    for (AbstractClassTypeDeclarationDescr typeDescr : classHierarchyManager.getSortedDescriptors()) {
        if (!unprocesseableDescrs.containsKey(typeDescr.getType().getFullName())) {
            PackageRegistry pkgReg = getPackageRegistry(pkgRegistry, packageDescr, typeDescr);
            InternalKnowledgePackage pkg = pkgReg.getPackage();
            TypeDeclaration type = pkg.getTypeDeclaration(typeDescr.getType().getName());
            typeDeclarationConfigurator.wireFieldAccessors(pkgReg, typeDescr, type);
            if (kbuilder.getKnowledgeBase() != null) {
                // in case of incremental compilatoin (re)register the new type declaration on the existing kbase
                kbuilder.getKnowledgeBase().registerTypeDeclaration(type, pkg);
            }
        }
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) 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