use of org.drools.compiler.compiler.Dialect in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testMVELDebugSymbols.
@Test
public void testMVELDebugSymbols() throws DroolsParserException {
MVELDebugHandler.setDebugMode(true);
try {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("mvel_rule.drl")));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
Dialect dialect = dialectRegistry.getDialect("mvel");
RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
RuleBuilder.build(context);
assertTrue(context.getErrors().toString(), context.getErrors().isEmpty());
final RuleImpl rule = context.getRule();
MVELConsequence mvelCons = (MVELConsequence) rule.getConsequence();
mvelCons.compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
String s = DebugTools.decompile(mvelCons.getCompExpr());
int fromIndex = 0;
int count = 0;
while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
count++;
}
assertEquals(4, count);
} finally {
MVELDebugHandler.setDebugMode(false);
}
}
use of org.drools.compiler.compiler.Dialect in project drools by kiegroup.
the class MVELConstraintBuilder method buildCompilationUnit.
public MVELCompilationUnit buildCompilationUnit(RuleBuildContext context, Pattern pattern, String expression, Map<String, OperatorDescr> aliases) {
Dialect dialect = context.getDialect();
context.setDialect(context.getDialect("mvel"));
try {
PredicateDescr predicateDescr = new PredicateDescr(context.getRuleDescr().getResource(), expression);
AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
if (analysis == null) {
// something bad happened
return null;
}
Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
return buildCompilationUnit(context, usedDeclarations[0], usedDeclarations[1], predicateDescr, analysis);
} finally {
context.setDialect(dialect);
}
}
use of org.drools.compiler.compiler.Dialect 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.compiler.Dialect in project jbpm by kiegroup.
the class ProcessBuilderImpl method buildProcess.
public void buildProcess(final Process process, Resource resource) {
if (resource != null) {
((org.jbpm.process.core.Process) process).setResource(resource);
}
boolean hasErrors = false;
ProcessValidator validator = ProcessValidatorRegistry.getInstance().getValidator(process, resource);
if (validator == null) {
logger.warn("Could not find validator for process {}.", ((Process) process).getType());
logger.warn("Continuing without validation of the process {} [{}]", process.getName(), process.getId());
} else {
ProcessValidationError[] errors = validator.validateProcess((WorkflowProcess) process);
if (errors.length != 0) {
hasErrors = true;
for (int i = 0; i < errors.length; i++) {
this.errors.add(new ParserError(resource, errors[i].toString(), -1, -1));
}
}
}
if (!hasErrors) {
// generate and add rule for process
String rules = "package " + process.getPackageName() + "\n";
// NPE for validator
if (validator != null && validator.compilationSupported()) {
rules = generateRules(process);
}
try {
knowledgeBuilder.addPackageFromDrl(new StringReader(rules), resource);
} catch (IOException e) {
// should never occur
logger.error("IOException during addPackageFromDRL", e);
} catch (DroolsParserException e) {
// should never occur
logger.error("DroolsParserException during addPackageFromDRL", e);
}
PackageRegistry pkgRegistry = this.knowledgeBuilder.getPackageRegistry(process.getPackageName());
if (pkgRegistry != null) {
InternalKnowledgePackage p = pkgRegistry.getPackage();
if (p != null) {
if (validator != null) {
// NPE for validator
if (validator.compilationSupported()) {
ProcessDescr processDescr = new ProcessDescr();
processDescr.setName(process.getPackageName() + "." + process.getName());
processDescr.setResource(resource);
processDescr.setProcessId(process.getId());
DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
Dialect dialect = dialectRegistry.getDialect("java");
dialect.init(processDescr);
ProcessBuildContext buildContext = new ProcessBuildContext(this.knowledgeBuilder, p, process, processDescr, dialectRegistry, dialect);
buildContexts((ContextContainer) process, buildContext);
if (process instanceof WorkflowProcess) {
buildNodes((WorkflowProcess) process, buildContext);
}
}
Process duplicateProcess = p.getRuleFlows().get(process.getId());
if (duplicateProcess != null) {
Resource duplicatedResource = duplicateProcess.getResource();
if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
this.errors.add(new DuplicateProcess(process, this.knowledgeBuilder.getBuilderConfiguration()));
} else {
this.errors.add(new ParserError(resource, "Process with same id already exists: " + process.getId(), -1, -1));
}
}
p.addProcess(process);
// NPE for validator
if (validator.compilationSupported()) {
pkgRegistry.compileAll();
pkgRegistry.getDialectRuntimeRegistry().onBeforeExecute();
}
}
}
} else {
// name of the process
throw new RuntimeException("invalid package name");
}
}
}
use of org.drools.compiler.compiler.Dialect 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.mvel.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();
JavaForMvelDialectConfiguration javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
switch(javaConf.getCompiler()) {
case NATIVE:
assertSame(NativeJavaCompiler.class, compiler.getClass());
break;
case ECLIPSE:
assertSame(EclipseJavaCompiler.class, compiler.getClass());
break;
default:
fail("Unrecognized java compiler");
}
// test eclipse jdt core with property settings and default source level
conf = new KnowledgeBuilderConfigurationImpl();
javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
javaConf.setCompiler(JavaForMvelDialectConfiguration.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());
}
Aggregations