Search in sources :

Example 11 with ImportDescr

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

the class ImpactModelBuilderImpl method addPackage.

@Override
public void addPackage(final PackageDescr packageDescr) {
    if (compositePackagesMap == null) {
        compositePackagesMap = new HashMap<>();
        if (compositePackages != null) {
            for (CompositePackageDescr pkg : compositePackages) {
                compositePackagesMap.put(pkg.getNamespace(), pkg);
            }
        } else {
            compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
        }
        compositePackages = null;
    }
    CompositePackageDescr pkgDescr = compositePackagesMap.get(packageDescr.getNamespace());
    if (pkgDescr == null) {
        compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
    } else {
        pkgDescr.addPackageDescr(packageDescr.getResource(), packageDescr);
    }
    PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    for (final ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.addImport(importDescr);
    }
    for (GlobalDescr globalDescr : packageDescr.getGlobals()) {
        try {
            Class<?> globalType = pkg.getTypeResolver().resolveType(globalDescr.getType());
            addGlobal(globalDescr.getIdentifier(), globalType);
            pkg.addGlobal(globalDescr.getIdentifier(), globalType);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 12 with ImportDescr

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

the class KnowledgeBuilderImpl method initPackage.

private void initPackage(PackageDescr packageDescr) {
    // Gather all imports for all PackageDescrs for the current package and replicate into
    // all PackageDescrs for the current package, thus maintaining a complete list of
    // ImportDescrs for all PackageDescrs for the current package.
    List<PackageDescr> packageDescrsForPackage = packages.computeIfAbsent(packageDescr.getName(), k -> new ArrayList<>());
    packageDescrsForPackage.add(packageDescr);
    Set<ImportDescr> imports = new HashSet<>();
    for (PackageDescr pd : packageDescrsForPackage) {
        imports.addAll(pd.getImports());
    }
    for (PackageDescr pd : packageDescrsForPackage) {
        pd.getImports().clear();
        pd.addAllImports(imports);
    }
    // Copy package level attributes for inclusion on individual rules
    if (!packageDescr.getAttributes().isEmpty()) {
        Map<String, AttributeDescr> pkgAttributes = packageAttributes.get(packageDescr.getNamespace());
        if (pkgAttributes == null) {
            pkgAttributes = new HashMap<>();
            this.packageAttributes.put(packageDescr.getNamespace(), pkgAttributes);
        }
        for (AttributeDescr attr : packageDescr.getAttributes()) {
            pkgAttributes.put(attr.getName(), attr);
        }
    }
}
Also used : ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) HashSet(java.util.HashSet)

Example 13 with ImportDescr

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

the class KnowledgeBuilderImpl method mergePackage.

void mergePackage(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (final ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.addImport(importDescr);
    }
    normalizeTypeDeclarationAnnotations(packageDescr, pkgRegistry.getTypeResolver());
    processAccumulateFunctions(pkgRegistry, packageDescr);
    processEntryPointDeclarations(pkgRegistry, packageDescr);
    Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs = new HashMap<>();
    List<TypeDefinition> unresolvedTypes = new ArrayList<>();
    List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<>();
    unsortedDescrs.addAll(packageDescr.getTypeDeclarations());
    unsortedDescrs.addAll(packageDescr.getEnumDeclarations());
    typeBuilder.processTypeDeclarations(packageDescr, pkgRegistry, unsortedDescrs, unresolvedTypes, unprocesseableDescrs);
    for (AbstractClassTypeDeclarationDescr descr : unprocesseableDescrs.values()) {
        this.addBuilderResult(new TypeDeclarationError(descr, "Unable to process type " + descr.getTypeName()));
    }
    processOtherDeclarations(pkgRegistry, packageDescr);
    normalizeRuleAnnotations(packageDescr, pkgRegistry.getTypeResolver());
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ArrayList(java.util.ArrayList) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr)

Example 14 with ImportDescr

use of org.drools.drl.ast.descr.ImportDescr 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 = CoreComponentFactory.get().createKnowledgePackage((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.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) ExecutionException(java.util.concurrent.ExecutionException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 15 with ImportDescr

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

the class TypeDeclarationUtils method resolveType.

/**
 * Tries to determine the namespace (package) of a simple type chosen to be
 * the superclass of a declared bean. Looks among imports, local
 * declarations and previous declarations. Means that a class can't extend
 * another class declared in package that has not been loaded yet.
 *
 * @param klass        the simple name of the class
 * @param packageDescr the descriptor of the package the base class is declared in
 * @param pkgRegistry  the current package registry
 * @return the fully qualified name of the superclass
 */
public static String resolveType(String klass, PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    String arraySuffix = "";
    int arrayIndex = klass.indexOf("[");
    if (arrayIndex >= 0) {
        arraySuffix = klass.substring(arrayIndex);
        klass = klass.substring(0, arrayIndex);
    }
    // look among imports
    String temp = klass;
    while (temp.length() > 0) {
        for (ImportDescr id : packageDescr.getImports()) {
            String fqKlass = id.getTarget();
            if (fqKlass.endsWith("." + temp)) {
                // logger.info("Replace supertype " + sup + " with full name " + id.getTarget());
                fqKlass = fqKlass.substring(0, fqKlass.lastIndexOf(temp)) + klass;
                return arrayIndex < 0 ? fqKlass : fqKlass + arraySuffix;
            }
        }
        temp = temp.substring(0, Math.max(0, temp.lastIndexOf('.')));
    }
    // look among local declarations
    if (pkgRegistry != null) {
        for (String declaredName : pkgRegistry.getPackage().getTypeDeclarations().keySet()) {
            if (declaredName.equals(klass)) {
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(declaredName);
                if (typeDeclaration.getTypeClass() != null) {
                    klass = typeDeclaration.getTypeClass().getName();
                }
            }
        }
    }
    if (!klass.contains(".") && packageDescr.getNamespace() != null && !packageDescr.getNamespace().isEmpty()) {
        for (AbstractClassTypeDeclarationDescr td : packageDescr.getClassAndEnumDeclarationDescrs()) {
            if (klass.equals(td.getTypeName())) {
                if (td.getType().getFullName().contains(".")) {
                    klass = td.getType().getFullName();
                }
            }
        }
    }
    return arrayIndex < 0 ? klass : klass + arraySuffix;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

ImportDescr (org.drools.drl.ast.descr.ImportDescr)20 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)11 PackageDescr (org.drools.drl.ast.descr.PackageDescr)9 AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)8 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)4 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)4 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1