Search in sources :

Example 1 with ProcessDescr

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");
        }
    }
}
Also used : ParserError(org.drools.drl.parser.ParserError) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) Resource(org.kie.api.io.Resource) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr) Process(org.kie.api.definition.process.Process) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) DuplicateProcess(org.jbpm.assembler.DuplicateProcess) IOException(java.io.IOException) ProcessPackage(org.drools.core.definitions.ProcessPackage) ResourceTypePackageRegistry(org.drools.core.definitions.ResourceTypePackageRegistry) DroolsParserException(org.drools.drl.parser.DroolsParserException) Constraint(org.jbpm.workflow.core.Constraint) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) DuplicateProcess(org.jbpm.assembler.DuplicateProcess) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ResourceTypePackageRegistry(org.drools.core.definitions.ResourceTypePackageRegistry) StringReader(java.io.StringReader) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) JavaDialect(org.drools.mvel.java.JavaDialect) Dialect(org.drools.compiler.compiler.Dialect) ProcessBuildContext(org.jbpm.process.builder.ProcessBuildContext) PackageDescr(org.drools.drl.ast.descr.PackageDescr) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with ProcessDescr

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));
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ActionDescr(org.drools.drl.ast.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaDialect(org.drools.mvel.java.JavaDialect) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) KogitoProcessContext(org.kie.kogito.internal.process.runtime.KogitoProcessContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KogitoProcessContextImpl(org.jbpm.process.instance.KogitoProcessContextImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with ProcessDescr

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));
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ReturnValueDescr(org.drools.drl.ast.descr.ReturnValueDescr) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaDialect(org.drools.mvel.java.JavaDialect) SplitInstance(org.jbpm.workflow.instance.node.SplitInstance) SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) JavaReturnValueEvaluatorBuilder(org.jbpm.process.builder.dialect.java.JavaReturnValueEvaluatorBuilder) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with ProcessDescr

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 );
}
Also used : ProcessErrorHandler(org.jbpm.process.builder.ProcessErrorHandler) Wireable(org.drools.core.rule.accessor.Wireable) Iterator(java.util.Iterator) Process(org.kie.api.definition.process.Process) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ProcessInvokerErrorHandler(org.jbpm.process.builder.ProcessInvokerErrorHandler) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) JavaDialect(org.drools.mvel.java.JavaDialect)

Example 5 with ProcessDescr

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();
}
Also used : Iterator(java.util.Iterator) ProcessDescr(org.drools.drl.ast.descr.ProcessDescr)

Aggregations

ProcessDescr (org.drools.drl.ast.descr.ProcessDescr)5 JavaDialect (org.drools.mvel.java.JavaDialect)4 StringReader (java.io.StringReader)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 Iterator (java.util.Iterator)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 SemanticKnowledgeBuilderConfigurationImpl (org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl)2 ProcessDialect (org.jbpm.process.builder.dialect.ProcessDialect)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 WorkflowProcessImpl (org.jbpm.workflow.core.impl.WorkflowProcessImpl)2 Test (org.junit.jupiter.api.Test)2 Process (org.kie.api.definition.process.Process)2 KogitoProcessRuntime (org.kie.kogito.internal.process.runtime.KogitoProcessRuntime)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Dialect (org.drools.compiler.compiler.Dialect)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 ProcessPackage (org.drools.core.definitions.ProcessPackage)1 ResourceTypePackageRegistry (org.drools.core.definitions.ResourceTypePackageRegistry)1