use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project drools by kiegroup.
the class ObjectTypeNodeCompiler method compile.
/**
* Creates a {@link CompiledNetwork} for the specified {@link ObjectTypeNode}. The {@link PackageBuilder} is used
* to compile the generated source and load the class.
*
* @param kBuilder builder used to compile and load class
* @param objectTypeNode OTN we are generating a compiled network for
* @return CompiledNetwork
*/
public static CompiledNetwork compile(KnowledgeBuilderImpl kBuilder, ObjectTypeNode objectTypeNode) {
if (objectTypeNode == null) {
throw new IllegalArgumentException("ObjectTypeNode cannot be null!");
}
if (kBuilder == null) {
throw new IllegalArgumentException("PackageBuilder cannot be null!");
}
ObjectTypeNodeCompiler compiler = new ObjectTypeNodeCompiler(objectTypeNode);
String packageName = compiler.getPackageName();
PackageRegistry pkgReg = kBuilder.getPackageRegistry(packageName);
if (pkgReg == null) {
kBuilder.addPackage(new PackageDescr(packageName));
pkgReg = kBuilder.getPackageRegistry(packageName);
}
String source = compiler.generateSource();
String generatedSourceName = compiler.getName();
JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
dialect.addSrc(compiler.getBinaryName(), source.getBytes(IoUtils.UTF8_CHARSET));
kBuilder.compileAll();
kBuilder.updateResults();
CompiledNetwork network;
try {
network = (CompiledNetwork) Class.forName(generatedSourceName, true, kBuilder.getRootClassLoader()).newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
} catch (InstantiationException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
}
return network;
}
use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.
the class JavaActionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("list.add( \"hello world\" );");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
JavaDialect javaDialect = (JavaDialect) dialectRegistry.getDialect("java");
ProcessDescr processDescr = new ProcessDescr();
processDescr.setClassName("Process1");
processDescr.setName("Process1");
WorkflowProcessImpl process = new WorkflowProcessImpl();
process.setName("Process1");
process.setPackageName("pkg1");
ProcessBuildContext context = new ProcessBuildContext(pkgBuilder, pkgBuilder.getPackage("pkg1"), null, processDescr, dialectRegistry, javaDialect);
context.init(pkgBuilder, pkg, null, dialectRegistry, javaDialect, null);
pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal java.util.List list;\n"));
ActionNode actionNode = new ActionNode();
DroolsAction action = new DroolsConsequenceAction("java", null);
actionNode.setAction(action);
ProcessDialect dialect = ProcessDialectRegistry.getDialect("java");
dialect.getActionBuilder().build(context, action, actionDescr, actionNode);
dialect.addProcess(context);
javaDialect.compileAll();
assertEquals(0, javaDialect.getResults().size());
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession wm = kbase.newKieSession();
List<String> list = new ArrayList<String>();
wm.setGlobal("list", list);
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("hello world", list.get(0));
}
use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.
the class JavaActionBuilder method getAnalysis.
protected AnalysisResult getAnalysis(final PackageBuildContext context, final ActionDescr actionDescr) {
JavaDialect dialect = (JavaDialect) context.getDialect("java");
Map<String, Class<?>> variables = new HashMap<String, Class<?>>();
BoundIdentifiers boundIdentifiers = new BoundIdentifiers(variables, context);
AnalysisResult analysis = dialect.analyzeBlock(context, actionDescr, actionDescr.getText(), boundIdentifiers);
return analysis;
}
use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.
the class JavaReturnValueEvaluatorBuilder method getAnalysis.
protected AnalysisResult getAnalysis(final PackageBuildContext context, final ReturnValueDescr descr) {
JavaDialect dialect = (JavaDialect) context.getDialect("java");
Map<String, Class<?>> variables = new HashMap<String, Class<?>>();
BoundIdentifiers boundIdentifiers = new BoundIdentifiers(variables, context);
AnalysisResult analysis = dialect.analyzeBlock(context, descr, descr.getText(), boundIdentifiers);
return analysis;
}
use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.
the class JavaProcessDialect method addProcess.
public void addProcess(final ProcessBuildContext context) {
JavaDialect javaDialect = (JavaDialect) context.getDialectRegistry().getDialect("java");
String processClass = processClassBuilder.buildRule(context);
if (processClass == null) {
// nothing to compile.
return;
}
final Process process = context.getProcess();
final ProcessDescr processDescr = context.getProcessDescr();
// The compilation result is for the entire rule, so difficult to
// associate with any descr
javaDialect.addClassCompileTask(context.getPkg().getName() + "." + processDescr.getClassName(), processDescr, processClass, null, new ProcessErrorHandler(processDescr, process, "Process Compilation error"));
JavaDialectRuntimeData data = (JavaDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData(javaDialect.getId());
for (final Iterator it = context.getInvokers().keySet().iterator(); it.hasNext(); ) {
final String className = (String) it.next();
// Check if an invoker - Action has been associated
// If so we add it to the PackageCompilationData as it will get
// wired up on compilation
final Object invoker = context.getInvokerLookup(className);
if (invoker != null && invoker instanceof Wireable) {
data.putInvoker(className, (Wireable) invoker);
}
final String text = (String) context.getInvokers().get(className);
final BaseDescr descr = (BaseDescr) context.getDescrLookup(className);
javaDialect.addClassCompileTask(className, descr, text, null, new ProcessInvokerErrorHandler(processDescr, process, "Unable to generate action invoker."));
}
// setup the line mappins for this rule
// TODO @TODO must setup mappings
// final String name = this.pkg.getName() + "." + StringUtils.ucFirst(
// ruleDescr.getClassName() );
// final LineMappings mapping = new LineMappings( name );
// mapping.setStartLine( ruleDescr.getConsequenceLine() );
// mapping.setOffset( ruleDescr.getConsequenceOffset() );
//
// context.getPkg().getPackageCompilationData().getLineMappings().put(
// name,
// mapping );
}
Aggregations