use of org.drools.drl.ast.descr.ProcessDescr in project kogito-runtimes by kiegroup.
the class ProcessBuilderImpl method buildProcess.
public void buildProcess(final Process process, Resource resource) {
if (resource != null) {
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.getType());
logger.warn("Continuing without validation of the process {} [{}]", process.getName(), process.getId());
} else {
ProcessValidationError[] errors = validator.validateProcess(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.getOrCreatePackageRegistry(new PackageDescr(process.getPackageName()));
if (pkgRegistry != null) {
InternalKnowledgePackage p = pkgRegistry.getPackage();
ResourceTypePackageRegistry resourceTypePackages = p.getResourceTypePackages();
ProcessPackage rpkg = ProcessPackage.getOrCreate(resourceTypePackages);
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 = rpkg.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));
}
}
rpkg.add(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.drl.ast.descr.ProcessDescr in project kogito-runtimes by kiegroup.
the class JavaActionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("list.add( \"hello world\" );");
builder = new KnowledgeBuilderImpl(pkg, new SemanticKnowledgeBuilderConfigurationImpl());
DialectCompiletimeRegistry dialectRegistry = builder.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(builder, builder.getPackage("pkg1"), null, processDescr, dialectRegistry, javaDialect);
context.init(builder, pkg, null, dialectRegistry, javaDialect, null);
builder.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());
KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
List<String> list = new ArrayList<String>();
kruntime.getKieSession().setGlobal("list", list);
KogitoProcessContext processContext = new KogitoProcessContextImpl(kruntime.getKieRuntime());
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("hello world", list.get(0));
}
use of org.drools.drl.ast.descr.ProcessDescr in project kogito-runtimes by kiegroup.
the class JavaReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.
@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
final InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
ProcessDescr processDescr = new ProcessDescr();
processDescr.setClassName("Process1");
processDescr.setName("Process1");
WorkflowProcessImpl process = new WorkflowProcessImpl();
process.setName("Process1");
process.setPackageName("pkg1");
ReturnValueDescr descr = new ReturnValueDescr();
descr.setText("return value;");
builder = new KnowledgeBuilderImpl(pkg, new SemanticKnowledgeBuilderConfigurationImpl());
DialectCompiletimeRegistry dialectRegistry = builder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
JavaDialect javaDialect = (JavaDialect) dialectRegistry.getDialect("java");
ProcessBuildContext context = new ProcessBuildContext(builder, pkg, process, processDescr, dialectRegistry, javaDialect);
builder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));
ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
final JavaReturnValueEvaluatorBuilder evaluatorBuilder = new JavaReturnValueEvaluatorBuilder();
evaluatorBuilder.build(context, node, descr, null);
ProcessDialectRegistry.getDialect(JavaDialect.ID).addProcess(context);
javaDialect.compileAll();
assertEquals(0, javaDialect.getResults().size());
KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
kruntime.getKieSession().setGlobal("value", true);
RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) kruntime.getKieSession());
SplitInstance splitInstance = new SplitInstance();
splitInstance.setProcessInstance(processInstance);
assertTrue(node.evaluate(splitInstance, null, null));
kruntime.getKieSession().setGlobal("value", false);
assertFalse(node.evaluate(splitInstance, null, null));
}
use of org.drools.drl.ast.descr.ProcessDescr in project kogito-runtimes 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 );
}
use of org.drools.drl.ast.descr.ProcessDescr in project kogito-runtimes 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);
if (context.getProcess() != null && ((org.jbpm.process.core.Process) context.getProcess()).getImports() != null) {
for (String decl : ((org.jbpm.process.core.Process) context.getProcess()).getImports()) {
buffer.append("import " + decl + ";" + 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();
}
Aggregations