Search in sources :

Example 1 with DroolsAssemblerContext

use of org.drools.compiler.builder.DroolsAssemblerContext 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();
    DroolsAssemblerContext 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<>();
    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 : DroolsAssemblerContext(org.drools.compiler.builder.DroolsAssemblerContext) ImportDescr(org.drools.drl.ast.descr.ImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) HashSet(java.util.HashSet)

Example 2 with DroolsAssemblerContext

use of org.drools.compiler.builder.DroolsAssemblerContext in project drools by kiegroup.

the class PatternBuilder method getTypeDeclaration.

private TypeDeclaration getTypeDeclaration(RuleBuildContext context, Class<?> userProvidedClass) {
    String packageName = ClassUtils.getPackage(userProvidedClass);
    DroolsAssemblerContext kbuilder = context.getKnowledgeBuilder();
    PackageRegistry pkgr = kbuilder.getPackageRegistry(packageName);
    TypeDeclaration typeDeclaration = pkgr != null ? pkgr.getPackage().getTypeDeclaration(userProvidedClass) : null;
    if (typeDeclaration == null && kbuilder.getKnowledgeBase() != null) {
        // check if the type declaration is contained only in the already existing kbase (possible during incremental compilation)
        InternalKnowledgePackage pkg = kbuilder.getKnowledgeBase().getPackage(packageName);
        typeDeclaration = pkg != null ? pkg.getTypeDeclaration(userProvidedClass) : null;
    }
    if (typeDeclaration == null) {
        typeDeclaration = context.getPkg().getTypeDeclaration(userProvidedClass);
    }
    return typeDeclaration;
}
Also used : DroolsAssemblerContext(org.drools.compiler.builder.DroolsAssemblerContext) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

DroolsAssemblerContext (org.drools.compiler.builder.DroolsAssemblerContext)2 HashSet (java.util.HashSet)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 ImportDescr (org.drools.drl.ast.descr.ImportDescr)1 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1