Search in sources :

Example 6 with ImportDescr

use of org.drools.compiler.lang.descr.ImportDescr in project drools by kiegroup.

the class PackageDescrVisitor method visitImports.

private void visitImports(List<ImportDescr> importDescrs) throws IOException, ClassNotFoundException {
    HashSet<String> imports = new HashSet<String>();
    for (ImportDescr i : importDescrs) {
        String fullName = i.getTarget();
        String name = fullName.substring(fullName.lastIndexOf(".") + 1);
        imports.add(fullName);
        Import objectImport = new Import(i, rulePackage);
        objectImport.setName(fullName);
        objectImport.setShortName(name);
        data.add(objectImport);
        ObjectType objectType = this.data.getObjectTypeByFullName(fullName);
        if (objectType == null) {
            objectType = new ObjectType(i);
        }
        objectType.setName(name);
        objectType.setFullName(fullName);
        data.add(objectType);
    }
    PackageHeaderLoader packageHeaderLoader = new PackageHeaderLoader(imports, jars);
    for (String factTypeName : packageHeaderLoader.getClassNames()) {
        String name = factTypeName.substring(factTypeName.lastIndexOf(".") + 1);
        Collection<String> fieldNames = packageHeaderLoader.getFieldNames(factTypeName);
        for (String fieldName : fieldNames) {
            ObjectType objectType = this.data.getObjectTypeByObjectTypeNameAndPackageName(name, rulePackage.getName());
            Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
            if (field == null) {
                field = ObjectTypeFactory.createField(objectType.getDescr(), fieldName, objectType);
                field.setFieldType(packageHeaderLoader.getFieldType(objectType.getName(), fieldName));
                data.add(field);
            }
        }
    }
}
Also used : PackageHeaderLoader(org.drools.verifier.jarloader.PackageHeaderLoader) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) HashSet(java.util.HashSet)

Example 7 with ImportDescr

use of org.drools.compiler.lang.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<PackageDescr>());
    packageDescrsForPackage.add(packageDescr);
    Set<ImportDescr> imports = new HashSet<ImportDescr>();
    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<String, AttributeDescr>();
            this.packageAttributes.put(packageDescr.getNamespace(), pkgAttributes);
        }
        for (AttributeDescr attr : packageDescr.getAttributes()) {
            pkgAttributes.put(attr.getName(), attr);
        }
    }
}
Also used : ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) HashSet(java.util.HashSet)

Example 8 with ImportDescr

use of org.drools.compiler.lang.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 = 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 9 with ImportDescr

use of org.drools.compiler.lang.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<String, AbstractClassTypeDeclarationDescr>();
    List<TypeDefinition> unresolvedTypes = new ArrayList<TypeDefinition>();
    List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<AbstractClassTypeDeclarationDescr>();
    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.compiler.lang.descr.AbstractClassTypeDeclarationDescr) ArrayList(java.util.ArrayList) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr)

Example 10 with ImportDescr

use of org.drools.compiler.lang.descr.ImportDescr in project drools by kiegroup.

the class DialectUtil method findClassByName.

public static Class<?> findClassByName(RuleBuildContext context, String className) {
    if (className == null) {
        return null;
    }
    String namespace = context.getRuleDescr().getNamespace();
    KnowledgeBuilderImpl packageBuilder = context.getKnowledgeBuilder();
    Class<?> clazz = null;
    try {
        clazz = Class.forName(className.indexOf('.') < 0 ? namespace + "." + className : className, false, packageBuilder.getRootClassLoader());
    } catch (ClassNotFoundException e) {
    }
    if (clazz != null) {
        return clazz;
    }
    Set<String> imports = new HashSet<String>();
    List<PackageDescr> pkgDescrs = packageBuilder.getPackageDescrs(namespace);
    if (pkgDescrs == null) {
        return null;
    }
    for (PackageDescr pkgDescr : pkgDescrs) {
        for (ImportDescr importDescr : pkgDescr.getImports()) {
            imports.add(importDescr.getTarget());
        }
    }
    return findClass(className, imports, packageBuilder.getRootClassLoader());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashSet(java.util.HashSet)

Aggregations

ImportDescr (org.drools.compiler.lang.descr.ImportDescr)16 FunctionImportDescr (org.drools.compiler.lang.descr.FunctionImportDescr)10 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)8 AccumulateImportDescr (org.drools.compiler.lang.descr.AccumulateImportDescr)7 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)2 EnumDeclarationDescr (org.drools.compiler.lang.descr.EnumDeclarationDescr)2 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)2 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 Iterator (java.util.Iterator)1