Search in sources :

Example 1 with PackageRegistry

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

the class ClassHierarchyManager method addDeclarationToPackagePreservingOrder.

public void addDeclarationToPackagePreservingOrder(TypeDeclaration type, AbstractClassTypeDeclarationDescr typeDescr, InternalKnowledgePackage tgtPackage, Map<String, PackageRegistry> pkgRegistryMap) {
    Collection<QualifiedName> parents = taxonomy.get(new QualifiedName(type.getFullName()));
    int index = getSortedDescriptors().indexOf(typeDescr);
    if (parents != null && !parents.isEmpty()) {
        for (QualifiedName parentName : parents) {
            String nameSpace = parentName.getNamespace();
            String name = parentName.getName();
            PackageRegistry parentPkgRegistry = pkgRegistryMap.get(nameSpace);
            if (parentPkgRegistry != null) {
                TypeDeclaration parentDeclaration = parentPkgRegistry.getPackage().getTypeDeclaration(name);
                if (parentDeclaration != null && parentDeclaration.getNature() == TypeDeclaration.Nature.DEFINITION) {
                    index = Math.max(index, parentDeclaration.getOrder());
                }
            }
        }
    }
    type.setOrder(index + 1);
    tgtPackage.addTypeDeclaration(type);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with PackageRegistry

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

the class KnowledgeBuilderImpl method buildOtherDeclarations.

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

Example 3 with PackageRegistry

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

the class KnowledgeBuilderImpl method removeObjectsGeneratedFromResource.

public ResourceRemovalResult removeObjectsGeneratedFromResource(Resource resource) {
    boolean modified = false;
    if (pkgRegistryMap != null) {
        for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
            modified = packageRegistry.removeObjectsGeneratedFromResource(resource) || modified;
        }
    }
    if (results != null) {
        Iterator<KnowledgeBuilderResult> i = results.iterator();
        while (i.hasNext()) {
            if (resource.equals(i.next().getResource())) {
                i.remove();
            }
        }
    }
    if (processBuilder != null && processBuilder.getErrors() != null) {
        Iterator<? extends KnowledgeBuilderResult> i = processBuilder.getErrors().iterator();
        while (i.hasNext()) {
            if (resource.equals(i.next().getResource())) {
                i.remove();
            }
        }
    }
    if (results.size() == 0) {
        // TODO Error attribution might be bugged
        for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
            packageRegistry.getPackage().resetErrors();
        }
    }
    Collection<String> removedTypes = typeBuilder.removeTypesGeneratedFromResource(resource);
    for (List<PackageDescr> pkgDescrs : packages.values()) {
        for (PackageDescr pkgDescr : pkgDescrs) {
            pkgDescr.removeObjectsGeneratedFromResource(resource);
        }
    }
    if (kBase != null) {
        modified = kBase.removeObjectsGeneratedFromResource(resource) || modified;
    }
    return new ResourceRemovalResult(modified, removedTypes);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 4 with PackageRegistry

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

the class KnowledgeBuilderImpl method addPackage.

/**
 * This adds a package from a Descr/AST This will also trigger a compile, if
 * there are any generated classes to compile of course.
 */
public void addPackage(final PackageDescr packageDescr) {
    PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
    if (pkgRegistry == null) {
        return;
    }
    // merge into existing package
    mergePackage(pkgRegistry, packageDescr);
    compileKnowledgePackages(packageDescr, pkgRegistry);
    wireAllRules();
    compileRete(packageDescr);
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry)

Example 5 with PackageRegistry

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

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