use of org.drools.compiler.lang.descr.ProcessDescr 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.lang.descr.ProcessDescr 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.lang.descr.ProcessDescr in project jbpm by kiegroup.
the class JavaScriptActionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("var testString; print('Hello')");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
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, null);
context.init(pkgBuilder, pkg, null, dialectRegistry, null, null);
pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal String testField;\n"));
ActionNode actionNode = new ActionNode();
DroolsAction action = new DroolsConsequenceAction("JavaScript", null);
actionNode.setAction(action);
ProcessDialect dialect = ProcessDialectRegistry.getDialect("JavaScript");
dialect.getActionBuilder().build(context, action, actionDescr, actionNode);
dialect.addProcess(context);
final JavaScriptActionBuilder builder = new JavaScriptActionBuilder();
builder.build(context, action, actionDescr, actionNode);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession wm = kbase.newKieSession();
wm.setGlobal("testField", "vagon");
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("vagon", wm.getGlobal("testField").toString());
}
use of org.drools.compiler.lang.descr.ProcessDescr in project jbpm by kiegroup.
the class JavaProcessClassBuilder method buildRule.
/* (non-Javadoc)
* @see org.drools.core.rule.builder.dialect.java.RuleClassBuilder#buildRule(org.drools.core.rule.builder.BuildContext, org.drools.core.rule.builder.dialect.java.BuildUtils, RuleDescr)
*/
public String buildRule(final ProcessBuildContext context) {
// If there is no compiled code, return
if (context.getMethods().isEmpty()) {
return null;
}
final String lineSeparator = System.getProperty("line.separator");
final StringBuilder buffer = new StringBuilder();
buffer.append("package " + context.getPkg().getName() + ";" + lineSeparator);
for (ImportDeclaration decl : context.getPkg().getImports().values()) {
buffer.append("import " + decl.getTarget() + ";" + lineSeparator);
}
for (String systemImport : systemImports) {
if (!context.getPkg().getImports().containsKey(systemImport)) {
buffer.append("import ").append(systemImport).append(";").append(lineSeparator);
}
}
for (final Iterator it = context.getPkg().getStaticImports().iterator(); it.hasNext(); ) {
buffer.append("import static " + it.next() + ";" + lineSeparator);
}
final ProcessDescr processDescr = context.getProcessDescr();
buffer.append("public class " + StringUtils.ucFirst(processDescr.getClassName()) + " {" + lineSeparator);
buffer.append(" private static final long serialVersionUID = 510l;" + lineSeparator);
// @TODO record line numbers for each Action method
for (int i = 0, size = context.getMethods().size(); i < size; i++) {
buffer.append(context.getMethods().get(i) + lineSeparator);
}
final String[] lines = buffer.toString().split(lineSeparator);
buffer.append("}");
return buffer.toString();
}
use of org.drools.compiler.lang.descr.ProcessDescr 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