Search in sources :

Example 1 with ClassPathResource

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

the class AsyncWorkItemDontWaitForCompletionTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    //We are using a proces containing a single Task. The task is configured
    //to avoid waiting until the completion of the external system.
    kbuilder.add(new ClassPathResource("async-work-item-nowait.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 2 with ClassPathResource

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

the class MyFirstjBPM5ProcessTest method test.

@Test
public void test() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("firstDummyProcess.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());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    // Uncomment to see all the logs
    // KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // We have our Session, now let's play with our Dummy Process
    ProcessInstance processInstance = ksession.createProcessInstance("DummyProcess", null);
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    ksession.startProcessInstance(processInstance.getId());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ProcessInstance(org.drools.runtime.process.ProcessInstance) ClassPathResource(org.drools.io.impl.ClassPathResource) Test(org.junit.Test)

Example 3 with ClassPathResource

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

the class PersistentProcessTest method processInstancesAndLocalHTPlusFailTest.

@Test
public void processInstancesAndLocalHTPlusFailTest() 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);
    MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", localHTWorkItemHandler);
    ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
    //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");
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    ksession.startProcessInstance(processInstance.getId());
    ut.commit();
    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");
    ut.begin();
    try {
        client.complete(salaboyTask.getId(), "salaboy", null);
        ut.commit();
    } catch (WorkflowRuntimeException e) {
        System.out.println("Rolling back transaction" + e);
        ut.rollback();
    }
    System.out.println(" >>> Looking for Salaboy's Tasks");
    Task salaboysTask = client.getTask(salaboyTask.getId());
    assertNotNull(salaboysTask);
    assertEquals("InProgress", salaboysTask.getTaskData().getStatus().name());
    // We need to dispose the session, because the reference to this ksession object will no longer be valid
    //  because another thread could load the same session and execute a different command.
    System.out.println(" >>> Disposing Session");
    ksession.dispose();
}
Also used : UserTransaction(javax.transaction.UserTransaction) KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) Task(org.jbpm.task.Task) HashMap(java.util.HashMap) LocalTaskService(org.jbpm.task.service.local.LocalTaskService) TaskService(org.jbpm.task.TaskService) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ClassPathResource(org.drools.io.impl.ClassPathResource) InitialContext(javax.naming.InitialContext) 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 4 with ClassPathResource

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

the class MyFirstDrools5RulesTest method testSimpleRules.

@Test
public void testSimpleRules() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("simpleRules.drl"), ResourceType.DRL);
    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());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    // Uncomment to see all the logs
    // KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // We have our Session, now let's play with our Simple Rules
    Person person = new Person("Salaboy", 28);
    ksession.insert(person);
    int fired = ksession.fireAllRules();
    assertEquals(4, fired);
    assertEquals(true, person.isEnabledToDrive());
    assertEquals(true, person.isEnabledToVote());
    assertEquals(true, person.isHappy());
    Person person2 = new Person("John", 19);
    ksession.insert(person2);
    fired = ksession.fireAllRules();
    assertEquals(3, fired);
    assertEquals(true, person2.isEnabledToDrive());
    assertEquals(false, person2.isEnabledToVote());
    assertEquals(true, person2.isHappy());
    Person person3 = new Person("Marie", 17);
    ksession.insert(person3);
    fired = ksession.fireAllRules();
    assertEquals(0, fired);
    assertEquals(false, person3.isEnabledToDrive());
    assertEquals(false, person3.isEnabledToVote());
    assertEquals(false, person3.isHappy());
    ksession.dispose();
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) Person(com.salaboy.model.Person) ClassPathResource(org.drools.io.impl.ClassPathResource) Test(org.junit.Test)

Example 5 with ClassPathResource

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

the class TaskListJFrame method createSession.

// End of variables declaration//GEN-END:variables
private StatefulKnowledgeSession createSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    //Adds resources to the builder
    kbuilder.add(new ClassPathResource("rules/SimpleRulesAndTask.drl"), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    //Checks for errors
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.out.println(error.getMessage());
        }
        throw new IllegalStateException("Error building kbase!");
    }
    //Creates a new kbase and add all the packages from the builder
    KnowledgeBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbaseConf.setOption(EventProcessingOption.STREAM);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession myksession = kbase.newStatefulKnowledgeSession();
    HornetQHTWorkItemHandler hornetQHTWorkItemHandler = new HornetQHTWorkItemHandler(myksession);
    myksession.getWorkItemManager().registerWorkItemHandler("Human Task", hornetQHTWorkItemHandler);
    KnowledgeRuntimeLoggerFactory.newConsoleLogger(myksession);
    myksession.addEventListener(new DebugAgendaEventListener());
    return myksession;
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) DebugAgendaEventListener(org.drools.event.rule.DebugAgendaEventListener) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) HornetQHTWorkItemHandler(org.jbpm.process.workitem.wsht.HornetQHTWorkItemHandler) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) KnowledgeBaseConfiguration(org.drools.KnowledgeBaseConfiguration) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ClassPathResource(org.drools.io.impl.ClassPathResource)

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