Search in sources :

Example 1 with WorkflowProcess

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

Example 2 with WorkflowProcess

use of org.kie.api.definition.process.WorkflowProcess in project jbpm by kiegroup.

the class LaneHandler method start.

@SuppressWarnings("unchecked")
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    String id = attrs.getValue("id");
    String name = attrs.getValue("name");
    WorkflowProcess process = (WorkflowProcess) parser.getParent();
    List<Lane> lanes = (List<Lane>) ((RuleFlowProcess) process).getMetaData(LaneHandler.LANES);
    if (lanes == null) {
        lanes = new ArrayList<Lane>();
        ((RuleFlowProcess) process).setMetaData(LaneHandler.LANES, lanes);
    }
    Lane lane = new Lane(id);
    lane.setName(name);
    lanes.add(lane);
    return lane;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) Lane(org.jbpm.bpmn2.core.Lane) ArrayList(java.util.ArrayList) List(java.util.List) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess)

Example 3 with WorkflowProcess

use of org.kie.api.definition.process.WorkflowProcess in project jbpm by kiegroup.

the class WorkflowProcessInstanceUpgrader method upgradeProcessInstance.

public static void upgradeProcessInstance(KieRuntime kruntime, long processInstanceId, String processId, Map<String, Long> nodeMapping) {
    if (nodeMapping == null) {
        nodeMapping = new HashMap<String, Long>();
    }
    WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kruntime.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new IllegalArgumentException("Could not find process instance " + processInstanceId);
    }
    if (processId == null) {
        throw new IllegalArgumentException("Null process id");
    }
    WorkflowProcess process = (WorkflowProcess) kruntime.getKieBase().getProcess(processId);
    if (process == null) {
        throw new IllegalArgumentException("Could not find process " + processId);
    }
    if (processInstance.getProcessId().equals(processId)) {
        return;
    }
    synchronized (processInstance) {
        org.kie.api.definition.process.Process oldProcess = processInstance.getProcess();
        processInstance.disconnect();
        processInstance.setProcess(oldProcess);
        updateNodeInstances(processInstance, nodeMapping);
        processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) kruntime);
        processInstance.setProcess(process);
        processInstance.reconnect();
    }
}
Also used : Process(org.kie.api.definition.process.Process) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess)

Example 4 with WorkflowProcess

use of org.kie.api.definition.process.WorkflowProcess in project jbpm by kiegroup.

the class MigrateProcessInstanceCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) ksession.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new IllegalArgumentException("Could not find process instance " + processInstanceId);
    }
    if (processId == null) {
        throw new IllegalArgumentException("Null process id");
    }
    WorkflowProcess process = (WorkflowProcess) ksession.getKieBase().getProcess(processId);
    if (process == null) {
        throw new IllegalArgumentException("Could not find process " + processId);
    }
    if (processInstance.getProcessId().equals(processId)) {
        return null;
    }
    synchronized (processInstance) {
        org.kie.api.definition.process.Process oldProcess = processInstance.getProcess();
        processInstance.disconnect();
        processInstance.setProcess(oldProcess);
        if (nodeMapping == null) {
            nodeMapping = new HashMap<String, Long>();
        }
        updateNodeInstances(processInstance, nodeMapping);
        processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);
        processInstance.setProcess(process);
        processInstance.reconnect();
    }
    return null;
}
Also used : WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess)

Example 5 with WorkflowProcess

use of org.kie.api.definition.process.WorkflowProcess in project jbpm by kiegroup.

the class WorkflowProcessInstanceUpgrader method upgradeProcessInstanceByNodeNames.

/**
 * Do the same as upgradeProcessInstance() but user provides mapping by node names, not by node id's
 * @param kruntime
 * @param activeProcessId
 * @param newProcessId
 * @param nodeNamesMapping
 */
public static void upgradeProcessInstanceByNodeNames(KieRuntime kruntime, Long fromProcessId, String toProcessId, Map<String, String> nodeNamesMapping) {
    Map<String, Long> nodeIdMapping = new HashMap<String, Long>();
    String fromProcessIdString = kruntime.getProcessInstance(fromProcessId).getProcessId();
    Process processFrom = kruntime.getKieBase().getProcess(fromProcessIdString);
    Process processTo = kruntime.getKieBase().getProcess(toProcessId);
    for (Map.Entry<String, String> entry : nodeNamesMapping.entrySet()) {
        String from = null;
        Long to = null;
        if (processFrom instanceof WorkflowProcess) {
            from = getNodeId(((WorkflowProcess) processFrom).getNodes(), entry.getKey(), true);
        } else if (processFrom instanceof RuleFlowProcess) {
            from = getNodeId(((RuleFlowProcess) processFrom).getNodes(), entry.getKey(), true);
        } else if (processFrom != null) {
            throw new IllegalArgumentException("Suported processes are WorkflowProcess and RuleFlowProcess, it was:" + processFrom.getClass());
        } else {
            throw new IllegalArgumentException("Can not find process with id: " + fromProcessIdString);
        }
        if (processTo instanceof WorkflowProcess) {
            to = Long.valueOf(getNodeId(((WorkflowProcess) processTo).getNodes(), entry.getValue(), false));
        } else if (processTo instanceof RuleFlowProcess) {
            to = Long.valueOf(getNodeId(((RuleFlowProcess) processTo).getNodes(), entry.getValue(), false));
        } else if (processTo != null) {
            throw new IllegalArgumentException("Suported processes are WorkflowProcess and RuleFlowProcess, it was:" + processTo.getClass());
        } else {
            throw new IllegalArgumentException("Can not find process with id: " + toProcessId);
        }
        nodeIdMapping.put(from, to);
    }
    upgradeProcessInstance(kruntime, fromProcessId, toProcessId, nodeIdMapping);
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) HashMap(java.util.HashMap) Process(org.kie.api.definition.process.Process) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) HashMap(java.util.HashMap) Map(java.util.Map) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess)

Aggregations

WorkflowProcess (org.kie.api.definition.process.WorkflowProcess)9 HashMap (java.util.HashMap)4 Process (org.kie.api.definition.process.Process)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)3 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)3 VariableScope (org.jbpm.process.core.context.variable.VariableScope)2 KieSession (org.kie.api.runtime.KieSession)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 DuplicateProcess (org.drools.compiler.compiler.DuplicateProcess)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 ParserError (org.drools.compiler.compiler.ParserError)1 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)1