Search in sources :

Example 31 with ClassPathResource

use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.

the class PersistentProcessTest method processInstancesAndLocalHTTest.

@Test
public void processInstancesAndLocalHTTest() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("process-async-interactions.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());
    Environment env = EnvironmentFactory.newEnvironment();
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime.ht");
    env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
    env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
    // Let's create a Persistence Knowledge Session
    System.out.println(" >>> Let's create a Persistent Knowledge Session");
    final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    int sessionId = ksession.getId();
    assertNotNull(sessionId);
    assertTrue(sessionId != 0);
    // We need to register the WorkItems and Listeners that the session will use
    TaskService client = createTaskService(emf);
    LocalHTWorkItemHandler localHTWorkItemHandler = new LocalHTWorkItemHandler(client, ksession);
    MockExternalServiceWorkItemHandler mockExternalServiceWorkItemHandler = new MockExternalServiceWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", localHTWorkItemHandler);
    ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockExternalServiceWorkItemHandler);
    //KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // Let's create a Process Instance
    Person person = new Person("Salaboy", 29);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", person);
    //Each Command will generate an interaction
    System.out.println(">>> Let's Create Process Instance");
    ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
    System.out.println(">>> Let's Start the Process Instance");
    processInstance = ksession.startProcessInstance(processInstance.getId());
    System.out.println(" >>> Looking for Salaboy's Tasks");
    List<TaskSummary> salaboysTasks = client.getTasksAssignedAsPotentialOwner("salaboy", "en-UK");
    assertTrue(salaboysTasks.size() == 1);
    TaskSummary salaboyTask = salaboysTasks.get(0);
    System.out.println(" >>> Starting Salaboy's Task");
    client.start(salaboyTask.getId(), "salaboy");
    System.out.println(" >>> Completing Salaboy's Task");
    client.complete(salaboyTask.getId(), "salaboy", null);
    List<TaskSummary> adminTasks = client.getTasksAssignedAsPotentialOwner("Administrator", "en-UK");
    assertTrue(adminTasks.size() == 1);
    TaskSummary adminTask = adminTasks.get(0);
    client.start(adminTask.getId(), "Administrator");
    client.complete(adminTask.getId(), "Administrator", null);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    //The process instance was completed and it should be out of the Ksession.
    assertNull(processInstance);
    // We need to dispose the session, because the reference to this ksession object will no longer be valid
    // Another thread could load the same session and execute a different command.
    System.out.println(">>> Disposing Session");
    ksession.dispose();
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) HashMap(java.util.HashMap) LocalTaskService(org.jbpm.task.service.local.LocalTaskService) TaskService(org.jbpm.task.TaskService) ClassPathResource(org.drools.io.impl.ClassPathResource) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) TaskSummary(org.jbpm.task.query.TaskSummary) ProcessInstance(org.drools.runtime.process.ProcessInstance) Person(com.salaboy.model.Person) LocalHTWorkItemHandler(org.jbpm.process.workitem.wsht.LocalHTWorkItemHandler)

Example 32 with ClassPathResource

use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.

the class AsyncWorkItemWaitForCompletionTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    //We are using a proces containing a single Task. The task is configured
    //to wait until the completion of the external system. A specialized
    //callback (CompleteWorkItemCallback) is used to complete the work item 
    //after the command was executed.
    kbuilder.add(new ClassPathResource("async-work-item-wait.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    session = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
    session.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
            session.fireAllRules();
        }
    });
    //Registers the session in SessionStoreUtil. The callback we are using
    //for the process' Task will retrieve this session later and completes
    //the work item using it.
    SessionStoreUtil.sessionCache.put("sessionId=" + session.getId(), session);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 33 with ClassPathResource

use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.

the class GenericWebServiceTaskTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("HospitalInvokeInsuranceScenarioV3-data.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    session = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
    session.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
            session.fireAllRules();
        }
    });
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 34 with ClassPathResource

use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.

the class HospitalInsuranceProcessExecutorTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("InsuranceProcessV2.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    session = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
    session.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
            session.fireAllRules();
        }
    });
    //Registers the same generic handler for all the tasks. The handler will
    //schedule commands into the Executor Service. Each task in the process
    //is configured to use a different command.
    AsyncGenericWorkItemHandler genericHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
    session.getWorkItemManager().registerWorkItemHandler("Gather Patient Data", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("Insurance Service", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("External Insurance Company Service", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("Rates Service", genericHandler);
    session.getWorkItemManager().registerWorkItemHandler("Invoice Service", genericHandler);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) AsyncGenericWorkItemHandler(com.salaboy.jbpm5.dev.guide.workitems.AsyncGenericWorkItemHandler) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 35 with ClassPathResource

use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.

the class HospitalInsuranceProcessTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("InsuranceProcessV1.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        for (KnowledgeBuilderError error : errors) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    session = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
    session.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
            session.fireAllRules();
        }
    });
    //Registers an independent mocked work item handler for each task present
    //in the process.
    PatientDataServiceWorkItemHandler patientDataHandler = new PatientDataServiceWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Gather Patient Data", patientDataHandler);
    InsuranceServiceWorkItemHandler insuranceServiceHandler = new InsuranceServiceWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Insurance Service", insuranceServiceHandler);
    CompanyGatewayWorkItemHandler companyGatewayHandler = new CompanyGatewayWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("External Insurance Company Service", companyGatewayHandler);
    RatesServiceWorkItemHandler ratesServiceHandler = new RatesServiceWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Rates Service", ratesServiceHandler);
    InvoiceServiceWorkItemHandler invoiceServiceHandler = new InvoiceServiceWorkItemHandler();
    session.getWorkItemManager().registerWorkItemHandler("Invoice Service", invoiceServiceHandler);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ClassPathResource(org.drools.io.impl.ClassPathResource) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase)

Aggregations

ClassPathResource (org.drools.io.impl.ClassPathResource)46 KnowledgeBase (org.drools.KnowledgeBase)45 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)45 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)45 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)25 ProcessInstance (org.drools.runtime.process.ProcessInstance)20 HashMap (java.util.HashMap)19 Person (com.salaboy.model.Person)16 Test (org.junit.Test)13 DefaultProcessEventListener (org.drools.event.process.DefaultProcessEventListener)10 DefaultAgendaEventListener (org.drools.event.rule.DefaultAgendaEventListener)10 KnowledgeBuilderErrors (org.drools.builder.KnowledgeBuilderErrors)9 QueryResults (org.drools.runtime.rule.QueryResults)9 Resources (com.salaboy.model.Resources)6 EntityManagerFactory (javax.persistence.EntityManagerFactory)6 KnowledgeBaseConfiguration (org.drools.KnowledgeBaseConfiguration)6 ProcessNodeLeftEvent (org.drools.event.process.ProcessNodeLeftEvent)6 Customer (com.salaboy.model.Customer)5 RatesToday (com.salaboy.model.RatesToday)5 ProcessNodeTriggeredEvent (org.drools.event.process.ProcessNodeTriggeredEvent)5