Search in sources :

Example 11 with ImportDescr

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

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
    MockDialectConfiguration mockConf = new MockDialectConfiguration();
    // cfg1.buildDialectRegistry().addDialect( "mock",
    // mockConf.getDialect() );
    cfg1.addDialect("mock", mockConf);
    cfg1.setDefaultDialect("mock");
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
    MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
    assertSame(pkg, mockDialect2.getPkg());
    assertNull(mockDialect2.getRuleDescr());
    RuleDescr ruleDescr = new RuleDescr("test rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
    ruleDescr.setLhs(new AndDescr());
    EvalDescr evalDescr = new EvalDescr();
    ruleDescr.getLhs().addDescr(evalDescr);
    PackageDescr pkgDescr = new PackageDescr("org.pkg1");
    pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
    FunctionImportDescr functionImportDescr = new FunctionImportDescr();
    functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
    pkgDescr.addFunctionImport(functionImportDescr);
    pkgDescr.addRule(ruleDescr);
    builder.addPackage(pkgDescr);
    assertSame(ruleDescr, mockDialect2.getRuleDescr());
    assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
    assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
    assertEquals("eval was built", evalDescr.getContent());
    assertEquals("consequence was built", ruleDescr.getConsequence());
    assertTrue(mockDialect2.isCompileAll());
    assertNotNull(pkg.getRule("test rule"));
    // make sure there were no other general errors.
    assertFalse(builder.hasErrors());
}
Also used : FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 12 with ImportDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.ImportDescr)

Example 13 with ImportDescr

use of org.drools.compiler.lang.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 != null) && (!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.compiler.lang.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 14 with ImportDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.ImportDescr)

Example 15 with ImportDescr

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

the class RuleParserTest method testFunctionImport.

@Test
public void testFunctionImport() throws Exception {
    final String source = "package foo\n" + "import function java.lang.Math.max\n" + "import function java.lang.Math.min;\n" + "import foo.bar.*\n" + "import baz.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("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("baz.Baz", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    assertEquals(2, pkg.getFunctionImports().size());
    impdescr = pkg.getFunctionImports().get(0);
    assertEquals("java.lang.Math.max", impdescr.getTarget());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()) + ("import function " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    impdescr = pkg.getFunctionImports().get(1);
    assertEquals("java.lang.Math.min", impdescr.getTarget());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()) + ("import function " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
}
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) Test(org.junit.Test)

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