Search in sources :

Example 1 with ImportDescr

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

the class RuleParserTest method testGlobal1.

@Test
public void testGlobal1() throws Exception {
    final String source = "package foo.bar.baz\n" + "import com.foo.Bar\n" + "global java.util.List<java.util.Map<String,Integer>> aList;\n" + "global Integer aNumber";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("foo.bar.baz", pkg.getName());
    assertEquals(1, 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());
    assertEquals(2, pkg.getGlobals().size());
    GlobalDescr global = pkg.getGlobals().get(0);
    assertEquals("java.util.List<java.util.Map<String,Integer>>", global.getType());
    assertEquals("aList", global.getIdentifier());
    assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
    assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
    global = pkg.getGlobals().get(1);
    assertEquals("Integer", global.getType());
    assertEquals("aNumber", global.getIdentifier());
    assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
    assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) 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)

Example 2 with ImportDescr

use of org.drools.compiler.lang.descr.ImportDescr in project kie-wb-common by kiegroup.

the class PackageDescrIndexVisitor method getFullyQualifiedClassName.

private String getFullyQualifiedClassName(final String typeName) {
    if (typeName.contains(".")) {
        return typeName;
    }
    for (ImportDescr importDescr : packageDescr.getImports()) {
        if (importDescr.getTarget().endsWith(typeName)) {
            return importDescr.getTarget();
        }
    }
    // We are guessing, like a blindman playing blackjack... there has to be a better way..
    for (Entry<String, ModelField[]> entry : dmo.getModuleModelFields().entrySet()) {
        ModelField[] mfs = entry.getValue();
        String key = entry.getKey();
        for (ModelField mf : mfs) {
            if (mf.getClassType().equals(ModelField.FIELD_CLASS_TYPE.REGULAR_CLASS)) {
                if (mf.getClassName().endsWith("." + typeName)) {
                    return mf.getClassName();
                } else if (mf.getType().endsWith("." + typeName)) {
                    return mf.getType();
                }
            }
        }
    }
    return packageDescr.getName() + "." + typeName;
}
Also used : ModelField(org.kie.soup.project.datamodel.oracle.ModelField) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr)

Example 3 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 4 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 5 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)6 FunctionImportDescr (org.drools.compiler.lang.descr.FunctionImportDescr)5 AccumulateImportDescr (org.drools.compiler.lang.descr.AccumulateImportDescr)4 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 Test (org.junit.Test)4 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)1 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)1 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)1 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)1