Search in sources :

Example 26 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class RuntimeDataServiceEJBIntegrationTest method testGetNodeInstanceForWorkItem.

@Test
public void testGetNodeInstanceForWorkItem() {
    processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
    assertNotNull(processInstanceId);
    ProcessInstance instance = processService.getProcessInstance(processInstanceId);
    assertNotNull(instance);
    Collection<NodeInstance> activeNodes = ((WorkflowProcessInstanceImpl) instance).getNodeInstances();
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    NodeInstance node = activeNodes.iterator().next();
    assertNotNull(node);
    assertTrue(node instanceof WorkItemNodeInstance);
    Long workItemId = ((WorkItemNodeInstance) node).getWorkItemId();
    assertNotNull(workItemId);
    NodeInstanceDesc desc = runtimeDataService.getNodeInstanceForWorkItem(workItemId);
    assertNotNull(desc);
    assertEquals(processInstanceId, desc.getProcessInstanceId());
    assertEquals("Write a Document", desc.getName());
    assertEquals("HumanTaskNode", desc.getNodeType());
}
Also used : WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) Test(org.junit.Test)

Example 27 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jBPM5-Developer-Guide by Salaboy.

the class AdvancedProcessAndRulesIntegrationTest method processVariablesAutoMappingPlusCastNoSafeCheckTest.

@Test
public void processVariablesAutoMappingPlusCastNoSafeCheckTest() throws InterruptedException {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("evolution/mapping.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("evolution/scoring_processVariables_wider.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("evolution/process-variables-decision.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    Person person = new Person("Salaboy", 28);
    RatesToday ratesToday = new RatesToday(1, 100);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    params.put("ratesToday", ratesToday);
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.SimpleDecision", params);
    System.out.println("Variables: " + ((WorkflowProcessInstanceImpl) processInstance).getVariables());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    final FactHandle processHandle = ksession.insert(processInstance);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void activationCreated(ActivationCreatedEvent event) {
            System.out.println("Firing All the Rules! " + event);
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }

        @Override
        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            System.out.println("Firing All the Rules! " + event);
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }
    });
    ksession.addEventListener(new DefaultProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }

        @Override
        public void afterProcessStarted(ProcessStartedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }

        @Override
        public void afterProcessCompleted(ProcessCompletedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).retract(processHandle);
        }
    });
    ksession.startProcessInstance(processInstance.getId());
    // // If you want to query the process variables while the process Instance is running you can do:
    // //  But remember that the activities inside the process are all sync.
    // QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[]{});
    // Iterator<QueryResultsRow> iterator = queryResults.iterator();
    // 
    // QueryResultsRow ratesRow = iterator.next();
    // assertEquals(ratesToday, ((ProcessVariable) ratesRow.get("$pv")).getValue());
    // 
    // QueryResultsRow personRow = iterator.next();
    // assertEquals(person, ((ProcessVariable) personRow.get("$pv")).getValue());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
    QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[] {});
    // The Process Variables are automatically retracted when the Process Instance is Completed
    assertEquals(0, queryResults.size());
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) FactHandle(org.drools.runtime.rule.FactHandle) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ProcessStartedEvent(org.drools.event.process.ProcessStartedEvent) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) RatesToday(com.salaboy.model.RatesToday) ProcessCompletedEvent(org.drools.event.process.ProcessCompletedEvent) ClassPathResource(org.drools.io.impl.ClassPathResource) QueryResults(org.drools.runtime.rule.QueryResults) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) RuleFlowGroupActivatedEvent(org.drools.event.rule.RuleFlowGroupActivatedEvent) KnowledgeBase(org.drools.KnowledgeBase) ActivationCreatedEvent(org.drools.event.rule.ActivationCreatedEvent) DefaultProcessEventListener(org.drools.event.process.DefaultProcessEventListener) ProcessInstance(org.drools.runtime.process.ProcessInstance) Person(com.salaboy.model.Person)

Example 28 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jBPM5-Developer-Guide by Salaboy.

the class AdvancedProcessAndRulesIntegrationTest method processEventsTest.

@Test
public void processEventsTest() throws InterruptedException {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("evolution/mapping.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("evolution/scoring_processVariables_wider.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("evolution/process-events-decision.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {

        public void executeWorkItem(WorkItem wi, WorkItemManager wim) {
            System.out.println(" >>> Completing Task! -> " + wi.getName() + " - id: " + wi.getId());
            wim.completeWorkItem(wi.getId(), null);
        }

        public void abortWorkItem(WorkItem wi, WorkItemManager wim) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void activationCreated(ActivationCreatedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }

        @Override
        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }
    });
    Person person = new Person("Salaboy", 28);
    RatesToday ratesToday = new RatesToday(1, 100);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    params.put("ratesToday", ratesToday);
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.SimpleDecision", params);
    System.out.println("Variables: " + ((WorkflowProcessInstanceImpl) processInstance).getVariables());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    final FactHandle processHandle = ksession.insert(processInstance);
    ((StatefulKnowledgeSessionImpl) ksession).addEventListener(new DefaultProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }

        @Override
        public void afterProcessStarted(ProcessStartedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).fireAllRules();
        }

        @Override
        public void afterProcessCompleted(ProcessCompletedEvent event) {
            ((StatefulKnowledgeSession) event.getKnowledgeRuntime()).retract(processHandle);
        }
    });
    ksession.startProcessInstance(processInstance.getId());
    Thread.sleep(1000);
    // // If you want to query the process variables while the process Instance is running you can do:
    // QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[]{});
    // Iterator<QueryResultsRow> iterator = queryResults.iterator();
    // 
    // QueryResultsRow ratesRow = iterator.next();
    // assertEquals(ratesToday, ((ProcessVariable) ratesRow.get("$pv")).getValue());
    // 
    // QueryResultsRow personRow = iterator.next();
    // assertEquals(person, ((ProcessVariable) personRow.get("$pv")).getValue());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
    QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[] {});
    // The Process Variables are automatically retracted when the Process Instance is Completed
    assertEquals(0, queryResults.size());
}
Also used : HashMap(java.util.HashMap) FactHandle(org.drools.runtime.rule.FactHandle) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) WorkItem(org.drools.runtime.process.WorkItem) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) ActivationCreatedEvent(org.drools.event.rule.ActivationCreatedEvent) WorkItemManager(org.drools.runtime.process.WorkItemManager) KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ProcessStartedEvent(org.drools.event.process.ProcessStartedEvent) RatesToday(com.salaboy.model.RatesToday) ProcessCompletedEvent(org.drools.event.process.ProcessCompletedEvent) ClassPathResource(org.drools.io.impl.ClassPathResource) QueryResults(org.drools.runtime.rule.QueryResults) WorkItemHandler(org.drools.process.instance.WorkItemHandler) RuleFlowGroupActivatedEvent(org.drools.event.rule.RuleFlowGroupActivatedEvent) StatefulKnowledgeSessionImpl(org.drools.impl.StatefulKnowledgeSessionImpl) DefaultProcessEventListener(org.drools.event.process.DefaultProcessEventListener) ProcessInstance(org.drools.runtime.process.ProcessInstance) Person(com.salaboy.model.Person)

Example 29 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jBPM5-Developer-Guide by Salaboy.

the class AutoMappingVariablesTest method processVariablesAutoMappingTest.

@Test
public void processVariablesAutoMappingTest() throws InterruptedException {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("evolution/mapping.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("evolution/scoring_processVariables.drl"), ResourceType.DRL);
    kbuilder.add(new ClassPathResource("evolution/process-automapping-decision.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    Person person = new Person("Salaboy", 28);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.SimpleDecision", params);
    System.out.println("Variables: " + ((WorkflowProcessInstanceImpl) processInstance).getVariables());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    FactHandle processtHandle = ksession.insert(processInstance);
    ksession.fireAllRules();
    ksession.startProcessInstance(processInstance.getId());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
    QueryResults queryResults = ksession.getQueryResults("allProcessVariables", new Object[] {});
    Iterator<QueryResultsRow> iterator = queryResults.iterator();
    while (iterator.hasNext()) {
        QueryResultsRow next = iterator.next();
        assertEquals(person, ((ProcessVariable) next.get("$pv")).getValue());
    }
    ksession.retract(processtHandle);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) FactHandle(org.drools.runtime.rule.FactHandle) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ClassPathResource(org.drools.io.impl.ClassPathResource) QueryResults(org.drools.runtime.rule.QueryResults) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) QueryResultsRow(org.drools.runtime.rule.QueryResultsRow) ProcessInstance(org.drools.runtime.process.ProcessInstance) Person(com.salaboy.model.Person)

Example 30 with WorkflowProcessInstanceImpl

use of org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl in project jbpm by kiegroup.

the class AddDynamicProcessToStageCommand method execute.

@Override
public Long execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        throw new ProcessInstanceNotFoundException("No process instance found with id " + processInstanceId);
    }
    DynamicNodeInstance dynamicContext = (DynamicNodeInstance) ((WorkflowProcessInstanceImpl) processInstance).getNodeInstances(true).stream().filter(ni -> (ni instanceof DynamicNodeInstance) && stageId.equals(ni.getNode().getMetaData().get("UniqueId"))).findFirst().orElse(null);
    if (dynamicContext == null) {
        throw new StageNotFoundException("No stage found with id " + stageId);
    }
    try {
        CaseFileInstance caseFile = getCaseFile(ksession, caseId);
        CaseEventSupport caseEventSupport = getCaseEventSupport(context);
        caseEventSupport.fireBeforeDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters);
        long subProcessInstanceId = DynamicUtils.addDynamicSubProcess(dynamicContext, ksession, processId, parameters);
        if (subProcessInstanceId < 0) {
            throw new ProcessDefinitionNotFoundException("No process definition found with id: " + processId);
        }
        caseEventSupport.fireAfterDynamicProcessAdded(caseId, caseFile, processInstanceId, processId, parameters, subProcessInstanceId);
        return subProcessInstanceId;
    } catch (IllegalArgumentException e) {
        throw new ProcessDefinitionNotFoundException(e.getMessage());
    }
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) Map(java.util.Map) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicUtils(org.jbpm.workflow.instance.node.DynamicUtils) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) StageNotFoundException(org.jbpm.casemgmt.api.StageNotFoundException) DynamicNodeInstance(org.jbpm.workflow.instance.node.DynamicNodeInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessDefinitionNotFoundException(org.jbpm.services.api.ProcessDefinitionNotFoundException) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException)

Aggregations

WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)53 NodeInstance (org.kie.api.runtime.process.NodeInstance)17 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)11 WorkItemNodeInstance (org.jbpm.workflow.instance.node.WorkItemNodeInstance)11 Test (org.junit.Test)10 Node (org.kie.api.definition.process.Node)10 KieSession (org.kie.api.runtime.KieSession)9 Map (java.util.Map)7 RegistryContext (org.drools.core.command.impl.RegistryContext)6 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)6 Process (org.kie.api.definition.process.Process)6 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)5 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)5 DynamicNodeInstance (org.jbpm.workflow.instance.node.DynamicNodeInstance)5 Person (com.salaboy.model.Person)4 KnowledgeBase (org.drools.KnowledgeBase)4 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)4 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)4