Search in sources :

Example 1 with DuplicateProcess

use of org.jbpm.assembler.DuplicateProcess 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)

Aggregations

IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 ProcessPackage (org.drools.core.definitions.ProcessPackage)1 ResourceTypePackageRegistry (org.drools.core.definitions.ResourceTypePackageRegistry)1 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1 ProcessDescr (org.drools.drl.ast.descr.ProcessDescr)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1 ParserError (org.drools.drl.parser.ParserError)1 JavaDialect (org.drools.mvel.java.JavaDialect)1 DuplicateProcess (org.jbpm.assembler.DuplicateProcess)1 ProcessBuildContext (org.jbpm.process.builder.ProcessBuildContext)1 ProcessDialect (org.jbpm.process.builder.dialect.ProcessDialect)1 ProcessValidationError (org.jbpm.process.core.validation.ProcessValidationError)1 ProcessValidator (org.jbpm.process.core.validation.ProcessValidator)1 Constraint (org.jbpm.workflow.core.Constraint)1 Process (org.kie.api.definition.process.Process)1