Search in sources :

Example 16 with ImportDescr

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

the class TypeDeclarationUtils method lookupSimpleNameByImports.

public static String lookupSimpleNameByImports(String name, AbstractClassTypeDeclarationDescr typeDescr, PackageDescr packageDescr, ClassLoader loader) {
    Class<?> typeClass = null;
    if (isQualified(name)) {
        typeClass = getClassForType(name, loader);
    }
    if (typeClass == null) {
        for (ImportDescr id : packageDescr.getImports()) {
            String imp = id.getTarget();
            int separator = imp.lastIndexOf('.');
            String tail = imp.substring(separator + 1);
            if (tail.equals(name)) {
                typeClass = getClassForType(imp, loader);
                if (typeClass != null) {
                    return typeClass.getCanonicalName();
                } else {
                    return imp;
                }
            } else if (tail.equals("*")) {
                typeClass = getClassForType(imp.substring(0, imp.length() - 1) + name, loader);
                if (typeClass != null) {
                    String resolvedNamespace = imp.substring(0, separator);
                    if (resolvedNamespace.equals(typeDescr.getNamespace())) {
                        // the class was found in the declared namespace, so stop here
                        break;
                    // here, the class was found in a different namespace. It means that the class was declared
                    // with no namespace and the initial guess was wrong, or that there is an ambiguity.
                    // So, we need to check that the resolved class is compatible with the declaration.
                    } else if (name.equals(typeDescr.getType().getName()) && !isCompatible(typeClass, typeDescr)) {
                        typeClass = null;
                    } else {
                        break;
                    }
                }
            }
        }
    }
    return typeClass != null ? typeClass.getName() : name;
}
Also used : ImportDescr(org.drools.drl.ast.descr.ImportDescr)

Example 17 with ImportDescr

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

the class JavaDialect method postCompileAddFunction.

public void postCompileAddFunction(FunctionDescr functionDescr, TypeResolver typeResolver) {
    final String functionClassName = this.pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
    ImportDescr importDescr = new ImportDescr(functionClassName + "." + functionDescr.getName());
    importDescr.setResource(functionDescr.getResource());
    importDescr.setNamespace(functionDescr.getNamespace());
    this.packageRegistry.addStaticImport(importDescr);
}
Also used : ImportDescr(org.drools.drl.ast.descr.ImportDescr)

Example 18 with ImportDescr

use of org.drools.drl.ast.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();
    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 19 with ImportDescr

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

the class RuleParserTest method testCompilationUnit.

@Test
public void testCompilationUnit() throws Exception {
    final String source = "package foo; import com.foo.Bar; import com.foo.Baz;";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("foo", pkg.getName());
    assertEquals(2, pkg.getImports().size());
    ImportDescr impdescr = pkg.getImports().get(0);
    assertEquals("com.foo.Bar", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    impdescr = pkg.getImports().get(1);
    assertEquals("com.foo.Baz", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
}
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) Test(org.junit.Test)

Example 20 with ImportDescr

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

the class CompositePackageDescr method internalAdd.

private void internalAdd(Resource resource, PackageDescr packageDescr) {
    for (ImportDescr descr : packageDescr.getImports()) {
        addImport(descr);
        descr.setResource(resource);
    }
    for (FunctionImportDescr descr : packageDescr.getFunctionImports()) {
        addFunctionImport(descr);
        descr.setResource(resource);
    }
    for (AccumulateImportDescr descr : packageDescr.getAccumulateImports()) {
        addAccumulateImport(descr);
        descr.setResource(resource);
    }
    for (AttributeDescr descr : packageDescr.getAttributes()) {
        addAttribute(descr);
        descr.setResource(resource);
    }
    for (GlobalDescr descr : packageDescr.getGlobals()) {
        addGlobal(descr);
        descr.setResource(resource);
    }
    for (FunctionDescr descr : packageDescr.getFunctions()) {
        addFunction(descr);
        descr.setResource(resource);
    }
    for (RuleDescr descr : packageDescr.getRules()) {
        addRule(descr);
        descr.setResource(resource);
    }
    // Avoid adding the same type declaration twice, see
    // TypeDeclarationTest.testDuplicatedTypeDeclarationInDifferentResources
    // IncrementalCompilationTest.testIncrementalCompilationWithAmbiguousRedeclares
    // RHDM-1738
    Set<TypeDeclarationDescr> typeDeclarationDescrs = new HashSet<>(getTypeDeclarations());
    for (TypeDeclarationDescr descr : packageDescr.getTypeDeclarations()) {
        if (!typeDeclarationDescrs.contains(descr)) {
            addTypeDeclaration(descr);
            descr.setResource(resource);
        }
    }
    for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
        addEnumDeclaration(enumDescr);
        enumDescr.setResource(resource);
    }
    for (EntryPointDeclarationDescr descr : packageDescr.getEntryPointDeclarations()) {
        addEntryPointDeclaration(descr);
        descr.setResource(resource);
    }
    for (WindowDeclarationDescr descr : packageDescr.getWindowDeclarations()) {
        addWindowDeclaration(descr);
        descr.setResource(resource);
    }
    packageDescr.getPreferredPkgUUID().ifPresent(pkgUUID -> {
        if (getPreferredPkgUUID().isPresent() && !pkgUUID.equals(getPreferredPkgUUID().get())) {
            throw new RuntimeException(String.format("Trying to overwrite preferredPkgUUID %s with a different value %s", getPreferredPkgUUID().get(), pkgUUID));
        }
        setPreferredPkgUUID(pkgUUID);
    });
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) EntryPointDeclarationDescr(org.drools.drl.ast.descr.EntryPointDeclarationDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) EnumDeclarationDescr(org.drools.drl.ast.descr.EnumDeclarationDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) WindowDeclarationDescr(org.drools.drl.ast.descr.WindowDeclarationDescr) HashSet(java.util.HashSet)

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