Search in sources :

Example 11 with ProcessInstanceImpl

use of org.jbpm.process.instance.impl.ProcessInstanceImpl in project jbpm by kiegroup.

the class DynamicProcessTest method testDynamicProcess.

@Test
public void testDynamicProcess() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
    factory.name("HelloWorldProcess").version("1.0").packageName("org.jbpm").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
    final RuleFlowProcess process = factory.validate().getProcess();
    Resource resource = ResourceFactory.newByteArrayResource(XmlRuleFlowProcessDumper.INSTANCE.dump(process).getBytes());
    // source path or target path must be set to be added into kbase
    resource.setSourcePath("/tmp/dynamicProcess.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(resource);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler testHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
    ksession.addEventListener(new ProcessEventListener() {

        public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
        }

        public void beforeProcessStarted(ProcessStartedEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
            logger.info("{}", arg0);
        }

        public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
            logger.info("{}", arg0);
        }

        public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
        }

        public void afterProcessStarted(ProcessStartedEvent arg0) {
        }

        public void afterProcessCompleted(ProcessCompletedEvent arg0) {
        }

        public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
        }

        public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
        }
    });
    final ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.startProcess("org.jbpm.HelloWorld");
    HumanTaskNode node = new HumanTaskNode();
    node.setName("Task2");
    node.setId(4);
    insertNodeInBetween(process, 2, 3, node);
    ((CommandBasedStatefulKnowledgeSession) ksession).getRunner().execute(new ExecutableCommand<Void>() {

        public Void execute(Context context) {
            StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
            ((ProcessInstanceImpl) ksession.getProcessInstance(processInstance.getId())).updateProcess(process);
            return null;
        }
    });
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
    assertProcessInstanceActive(processInstance);
    ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) Resource(org.kie.api.io.Resource) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessCompletedEvent(org.kie.api.event.process.ProcessCompletedEvent) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) KieBase(org.kie.api.KieBase) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) Test(org.junit.Test)

Example 12 with ProcessInstanceImpl

use of org.jbpm.process.instance.impl.ProcessInstanceImpl in project jbpm by kiegroup.

the class JPAWorkingMemoryDbLogger method afterSLAViolated.

@Override
public void afterSLAViolated(SLAViolatedEvent event) {
    if (event.getNodeInstance() != null) {
        // since node instance is set this is SLA violation for node not process instance so ignore it
        return;
    }
    long processInstanceId = event.getProcessInstance().getId();
    EntityManager em = getEntityManager(event);
    Object tx = joinTransaction(em);
    ProcessInstanceLog log = (ProcessInstanceLog) ((ProcessInstanceImpl) event.getProcessInstance()).getMetaData().get("ProcessInstanceLog");
    if (log == null) {
        List<ProcessInstanceLog> result = em.createQuery("from ProcessInstanceLog as log where log.processInstanceId = :piId and log.end is null").setParameter("piId", processInstanceId).getResultList();
        if (result != null && result.size() != 0) {
            log = result.get(result.size() - 1);
        }
    }
    if (log != null) {
        log.setSlaCompliance(((ProcessInstance) event.getProcessInstance()).getSlaCompliance());
        em.merge(log);
    }
    leaveTransaction(em, tx);
}
Also used : EntityManager(javax.persistence.EntityManager) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl)

Example 13 with ProcessInstanceImpl

use of org.jbpm.process.instance.impl.ProcessInstanceImpl in project jbpm by kiegroup.

the class JobExecutionErrorFilter method filter.

@Override
public ExecutionError filter(ExecutionErrorContext errorContext) {
    AsyncJobException exception = extract(errorContext.getCause(), AsyncJobException.class);
    String stacktrace = getStackTrace(exception);
    NodeInstance nodeInstance = errorContext.getLastExecutedNode();
    return ExecutionError.builder().type(TYPE).initActivityId(getInitActivityId(errorContext)).deploymentId(((ProcessInstanceImpl) nodeInstance.getProcessInstance()).getDeploymentId()).processInstanceId(nodeInstance.getProcessInstance().getId()).processId(nodeInstance.getProcessInstance().getProcessId()).activityId(nodeInstance.getId()).activityName(nodeName(nodeInstance)).jobId(exception.getJobId()).message(exception.getMessage()).error(stacktrace).errorDate(new Date()).build();
}
Also used : ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) AsyncJobException(org.jbpm.executor.AsyncJobException) NodeInstance(org.kie.api.runtime.process.NodeInstance) Date(java.util.Date)

Example 14 with ProcessInstanceImpl

use of org.jbpm.process.instance.impl.ProcessInstanceImpl in project jbpm by kiegroup.

the class JPAProcessInstanceManager method getProcessInstance.

public ProcessInstance getProcessInstance(long id, boolean readOnly) {
    InternalRuntimeManager manager = (InternalRuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER);
    if (manager != null) {
        manager.validate((KieSession) kruntime, ProcessInstanceIdContext.get(id));
    }
    TransactionManager txm = (TransactionManager) this.kruntime.getEnvironment().get(EnvironmentName.TRANSACTION_MANAGER);
    org.jbpm.process.instance.ProcessInstance processInstance = null;
    processInstance = (org.jbpm.process.instance.ProcessInstance) this.processInstances.get(id);
    if (processInstance != null) {
        if (((WorkflowProcessInstanceImpl) processInstance).isPersisted() && !readOnly) {
            ProcessPersistenceContextManager ppcm = (ProcessPersistenceContextManager) this.kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
            ppcm.beginCommandScopedEntityManager();
            ProcessPersistenceContext context = ppcm.getProcessPersistenceContext();
            ProcessInstanceInfo processInstanceInfo = (ProcessInstanceInfo) context.findProcessInstanceInfo(id);
            if (processInstanceInfo == null) {
                return null;
            }
            TransactionManagerHelper.addToUpdatableSet(txm, processInstanceInfo);
            processInstanceInfo.updateLastReadDate();
            EventManagerProvider.getInstance().get().update(new ProcessInstanceView(processInstance));
        }
        return processInstance;
    }
    // Make sure that the cmd scoped entity manager has started
    ProcessPersistenceContextManager ppcm = (ProcessPersistenceContextManager) this.kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
    ppcm.beginCommandScopedEntityManager();
    ProcessPersistenceContext context = ppcm.getProcessPersistenceContext();
    ProcessInstanceInfo processInstanceInfo = (ProcessInstanceInfo) context.findProcessInstanceInfo(id);
    if (processInstanceInfo == null) {
        return null;
    }
    processInstance = (org.jbpm.process.instance.ProcessInstance) processInstanceInfo.getProcessInstance(kruntime, this.kruntime.getEnvironment(), readOnly);
    if (!readOnly) {
        processInstanceInfo.updateLastReadDate();
        TransactionManagerHelper.addToUpdatableSet(txm, processInstanceInfo);
        EventManagerProvider.getInstance().get().update(new ProcessInstanceView(processInstance));
    }
    if (((ProcessInstanceImpl) processInstance).getProcessXml() == null) {
        Process process = kruntime.getKieBase().getProcess(processInstance.getProcessId());
        if (process == null) {
            throw new IllegalArgumentException("Could not find process " + processInstance.getProcessId());
        }
        processInstance.setProcess(process);
    }
    if (processInstance.getKnowledgeRuntime() == null) {
        Long parentProcessInstanceId = (Long) ((ProcessInstanceImpl) processInstance).getMetaData().get("ParentProcessInstanceId");
        if (parentProcessInstanceId != null) {
            kruntime.getProcessInstance(parentProcessInstanceId);
        }
        processInstance.setKnowledgeRuntime(kruntime);
        ((ProcessInstanceImpl) processInstance).reconnect();
        if (readOnly) {
            internalRemoveProcessInstance(processInstance);
        }
    }
    return processInstance;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) Process(org.kie.api.definition.process.Process) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) ProcessInstanceView(org.jbpm.persistence.api.integration.model.ProcessInstanceView) TransactionManager(org.drools.persistence.api.TransactionManager) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager)

Example 15 with ProcessInstanceImpl

use of org.jbpm.process.instance.impl.ProcessInstanceImpl in project jbpm by kiegroup.

the class ProcessServiceImpl method getAvailableSignals.

@Override
public Collection<String> getAvailableSignals(String deploymentId, Long processInstanceId) {
    DeployedUnit deployedUnit = deploymentService.getDeployedUnit(deploymentId);
    if (deployedUnit == null) {
        throw new DeploymentNotFoundException("No deployments available for " + deploymentId);
    }
    RuntimeManager manager = deployedUnit.getRuntimeManager();
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
    try {
        KieSession ksession = engine.getKieSession();
        ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
        Collection<String> activeSignals = new ArrayList<>();
        if (processInstance != null) {
            ((ProcessInstanceImpl) processInstance).setProcess(ksession.getKieBase().getProcess(processInstance.getProcessId()));
            Collection<NodeInstance> activeNodes = ((WorkflowProcessInstance) processInstance).getNodeInstances();
            Collection<String> activeBoundaryNodesSignals = getActiveBoundaryNodesSignals(processInstance, activeNodes);
            activeSignals.addAll(collectActiveSignals(activeNodes));
            activeSignals.addAll(activeBoundaryNodesSignals);
        }
        return activeSignals;
    } catch (SessionNotFoundException e) {
        throw new ProcessInstanceNotFoundException("Process instance with id " + processInstanceId + " was not found", e);
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : DeploymentNotFoundException(org.jbpm.services.api.DeploymentNotFoundException) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) DeployedUnit(org.jbpm.services.api.model.DeployedUnit) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) ArrayList(java.util.ArrayList) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) KieSession(org.kie.api.runtime.KieSession) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Aggregations

ProcessInstanceImpl (org.jbpm.process.instance.impl.ProcessInstanceImpl)15 KieSession (org.kie.api.runtime.KieSession)6 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)4 Process (org.kie.api.definition.process.Process)4 ArrayList (java.util.ArrayList)3 RegistryContext (org.drools.core.command.impl.RegistryContext)3 CorrelationKey (org.kie.internal.process.CorrelationKey)3 EntityManager (javax.persistence.EntityManager)2 NodeInstanceLog (org.jbpm.process.audit.NodeInstanceLog)2 ProcessInstanceLog (org.jbpm.process.audit.ProcessInstanceLog)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 NodeInstanceImpl (org.jbpm.workflow.instance.impl.NodeInstanceImpl)2 SubProcessNodeInstance (org.jbpm.workflow.instance.node.SubProcessNodeInstance)2 WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)2 KieBase (org.kie.api.KieBase)2 Node (org.kie.api.definition.process.Node)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)2 NodeInstance (org.kie.api.runtime.process.NodeInstance)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2