Search in sources :

Example 1 with JavaDialectConfiguration

use of org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration in project drools by kiegroup.

the class KieBuilderImpl method buildPMMLPojos.

private void buildPMMLPojos(List<String> pmmlFileNames, KieProject kProject) {
    PMMLCompiler compiler = PMMLCompilerFactory.getPMMLCompiler();
    System.out.println("!!!!! PMMLCompiler version = " + compiler.getCompilerVersion() + " !!!!!");
    KieFileSystem javaSource = KieServices.Factory.get().newKieFileSystem();
    Map<String, String> javaSources = new HashMap<>();
    for (String fname : pmmlFileNames) {
        Map<String, String> modelSources = null;
        Resource pmmlContent = ResourceFactory.newByteArrayResource(srcMfs.getBytes(fname));
        if (pmmlContent != null) {
            try {
                modelSources = compiler.getJavaClasses(pmmlContent.getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            modelSources = compiler.getJavaClasses(fname);
        }
        if (modelSources != null && !modelSources.isEmpty()) {
            javaSources.putAll(modelSources);
        }
    }
    for (String key : javaSources.keySet()) {
        String javaCode = javaSources.get(key);
        if (javaCode != null && !javaCode.trim().isEmpty()) {
            Resource res = ResourceFactory.newByteArrayResource(javaCode.getBytes()).setResourceType(ResourceType.JAVA);
            String sourcePath = key.replaceAll("\\.", "/") + ".java";
            res.setSourcePath(sourcePath);
            javaSource.write(res);
        }
    }
    ResourceReader src = ((KieFileSystemImpl) javaSource).asMemoryFileSystem();
    List<String> javaFileNames = getJavaFileNames(src);
    if (javaFileNames != null && !javaFileNames.isEmpty()) {
        ClassLoader classLoader = kProject.getClassLoader();
        KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl(classLoader);
        JavaDialectConfiguration javaConf = (JavaDialectConfiguration) kconf.getDialectConfiguration("java");
        compileJavaClasses(javaConf, classLoader, javaFileNames, JAVA_ROOT, src);
    }
}
Also used : ResourceReader(org.drools.compiler.commons.jci.readers.ResourceReader) DiskResourceReader(org.drools.compiler.commons.jci.readers.DiskResourceReader) KieFileSystem(org.kie.api.builder.KieFileSystem) HashMap(java.util.HashMap) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) Resource(org.kie.api.io.Resource) PMMLResource(org.drools.compiler.compiler.PMMLResource) IOException(java.io.IOException) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration)

Example 2 with JavaDialectConfiguration

use of org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration in project drools by kiegroup.

the class KieBuilderImpl method compileJavaClasses.

private void compileJavaClasses(ClassLoader classLoader, Predicate<String> classFilter) {
    List<String> classFiles = new ArrayList<String>();
    for (String fileName : srcMfs.getFileNames()) {
        if (fileName.endsWith(".class")) {
            trgMfs.write(fileName, srcMfs.getBytes(fileName), true);
            classFiles.add(fileName.substring(0, fileName.length() - ".class".length()));
        }
    }
    List<String> javaFiles = new ArrayList<String>();
    List<String> javaTestFiles = new ArrayList<String>();
    for (String fileName : srcMfs.getFileNames()) {
        if (isJavaSourceFile(fileName) && noClassFileForGivenSourceFile(classFiles, fileName) && notVetoedByFilter(classFilter, fileName)) {
            fileName = fileName.replace(File.separatorChar, '/');
            if (!fileName.startsWith(JAVA_ROOT) && !fileName.startsWith(JAVA_TEST_ROOT)) {
                results.addMessage(Level.WARNING, fileName, "Found Java file out of the Java source folder: \"" + fileName + "\"");
            } else if (fileName.substring(JAVA_ROOT.length()).indexOf('/') < 0) {
                results.addMessage(Level.ERROR, fileName, "A Java class must have a package: " + fileName.substring(JAVA_ROOT.length()) + " is not allowed");
            } else {
                if (fileName.startsWith(JAVA_ROOT)) {
                    javaFiles.add(fileName);
                } else {
                    javaTestFiles.add(fileName);
                }
            }
        }
    }
    if (!javaFiles.isEmpty() || !javaTestFiles.isEmpty()) {
        KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl(classLoader);
        JavaDialectConfiguration javaConf = (JavaDialectConfiguration) kconf.getDialectConfiguration("java");
        compileJavaClasses(javaConf, classLoader, javaFiles, JAVA_ROOT);
        compileJavaClasses(javaConf, classLoader, javaTestFiles, JAVA_TEST_ROOT);
    }
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration)

Example 3 with JavaDialectConfiguration

use of org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration in project drools by kiegroup.

the class KnowledgeBuilderTest method testCompilerConfiguration.

@Test
public void testCompilerConfiguration() throws Exception {
    // test default is eclipse jdt core
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
    builder.addPackage(pkgDescr);
    DialectCompiletimeRegistry reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    final Field dialectField = builder.getClass().getDeclaredField("defaultDialect");
    dialectField.setAccessible(true);
    String dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    Dialect dialect = reg.getDialect(dialectName);
    final Field compilerField = dialect.getClass().getDeclaredField("compiler");
    compilerField.setAccessible(true);
    JavaCompiler compiler = (JavaCompiler) compilerField.get(dialect);
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    JavaDialectConfiguration javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertSame(NativeJavaCompiler.class, compiler.getClass());
            break;
        case ECLIPSE:
            assertSame(EclipseJavaCompiler.class, compiler.getClass());
            break;
        case JANINO:
            assertSame(JaninoJavaCompiler.class, compiler.getClass());
            break;
        default:
            fail("Unrecognized java compiler");
    }
    // test JANINO with property settings
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
    builder = new KnowledgeBuilderImpl(conf);
    builder.addPackage(pkgDescr);
    dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    dialect = reg.getDialect(dialectName);
    compiler = (JavaCompiler) compilerField.get(dialect);
    assertSame(JaninoJavaCompiler.class, compiler.getClass());
    // test eclipse jdt core with property settings and default source level
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.CompilerType.ECLIPSE);
    builder = new KnowledgeBuilderImpl(conf);
    builder.addPackage(pkgDescr);
    dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    dialect = reg.getDialect(dialectName);
    compiler = (JavaCompiler) compilerField.get(dialect);
    assertSame(EclipseJavaCompiler.class, compiler.getClass());
}
Also used : FactField(org.kie.api.definition.type.FactField) Field(java.lang.reflect.Field) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) Dialect(org.drools.compiler.compiler.Dialect) NativeJavaCompiler(org.drools.compiler.commons.jci.compilers.NativeJavaCompiler) EclipseJavaCompiler(org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler) JaninoJavaCompiler(org.drools.compiler.commons.jci.compilers.JaninoJavaCompiler) JavaCompiler(org.drools.compiler.commons.jci.compilers.JavaCompiler) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with JavaDialectConfiguration

use of org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration in project drools by kiegroup.

the class KnowledgeBuilderTest method testJaninoWithStaticImports.

@Test
public void testJaninoWithStaticImports() throws Exception {
    KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
    JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
    KnowledgeBuilderImpl bldr = new KnowledgeBuilderImpl(cfg);
    bldr.addPackageFromDrl(new StringReader("package testBuilderPackageConfig \n import java.util.List"));
    bldr.addPackageFromDrl(new StringReader("package testBuilderPackageConfig \n function void doSomething() {\n System.err.println(List.class.toString()); }"));
    assertFalse(bldr.hasErrors());
}
Also used : StringReader(java.io.StringReader) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 5 with JavaDialectConfiguration

use of org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration in project drools by kiegroup.

the class PackageBuilderConfigurationTest method testSystemProperties.

@Test
public void testSystemProperties() {
    KnowledgeBuilderConfigurationImpl cfg;
    JavaDialectConfiguration javaConf;
    System.setProperty("drools.dialect.java.compiler", "JANINO");
    cfg = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
    assertEquals(JavaDialectConfiguration.CompilerType.JANINO, javaConf.getCompiler());
    KnowledgeBuilderConfigurationImpl cfg2 = new KnowledgeBuilderConfigurationImpl();
    JavaDialectConfiguration javaConf2 = (JavaDialectConfiguration) cfg2.getDialectConfiguration("java");
    assertEquals(javaConf.getCompiler(), javaConf2.getCompiler());
    System.setProperty("drools.dialect.java.compiler", "ECLIPSE");
    cfg = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
    assertEquals(JavaDialectConfiguration.CompilerType.ECLIPSE, javaConf.getCompiler());
    javaConf2.setCompiler(JavaDialectConfiguration.CompilerType.ECLIPSE);
    assertEquals(JavaDialectConfiguration.CompilerType.ECLIPSE, javaConf2.getCompiler());
    javaConf2.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
    assertEquals(JavaDialectConfiguration.CompilerType.JANINO, javaConf2.getCompiler());
    final KnowledgeBuilderConfigurationImpl cfg3 = new KnowledgeBuilderConfigurationImpl();
    JavaDialectConfiguration javaConf3 = (JavaDialectConfiguration) cfg3.getDialectConfiguration("java");
    assertEquals(javaConf.getCompiler(), javaConf3.getCompiler());
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration) Test(org.junit.Test)

Aggregations

JavaDialectConfiguration (org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)5 Test (org.junit.Test)5 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 EclipseJavaCompiler (org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler)1 JaninoJavaCompiler (org.drools.compiler.commons.jci.compilers.JaninoJavaCompiler)1 JavaCompiler (org.drools.compiler.commons.jci.compilers.JavaCompiler)1 NativeJavaCompiler (org.drools.compiler.commons.jci.compilers.NativeJavaCompiler)1 DiskResourceReader (org.drools.compiler.commons.jci.readers.DiskResourceReader)1 ResourceReader (org.drools.compiler.commons.jci.readers.ResourceReader)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 PMMLCompiler (org.drools.compiler.compiler.PMMLCompiler)1 PMMLResource (org.drools.compiler.compiler.PMMLResource)1