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);
}
}
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);
}
}
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());
}
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());
}
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());
}
Aggregations