@Test public void testPersistenceRuleSet() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(new ClassPathResource("RuleSetProcess.rf"), ResourceType.DRF); kbuilder.add(new ClassPathResource("RuleSetRules.drl"), ResourceType.DRL); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); int id = ksession.getId(); ksession.insert(new ArrayList<Object>()); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess"); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.fireAllRules(); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNull(processInstance); }
@Test public void testPersistenceWorkItems() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); int origNumObjects = ksession.getObjects().size(); int id = ksession.getId(); ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess"); ksession.insert("TestString"); logger.debug("Started process instance {}", processInstance.getId()); TestWorkItemHandler handler = TestWorkItemHandler.getInstance(); WorkItem workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertEquals(origNumObjects + 1, ksession.getObjects().size()); for (Object o : ksession.getObjects()) { logger.debug(o.toString()); } assertNull(processInstance); }
@Test public void testPersistenceVariables() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(new ClassPathResource("VariablesProcess.rf"), ResourceType.DRF); for (KnowledgeBuilderError error : kbuilder.getErrors()) { logger.debug(error.toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); int id = ksession.getId(); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("name", "John Doe"); ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess", parameters); TestWorkItemHandler handler = TestWorkItemHandler.getInstance(); WorkItem workItem = handler.getWorkItem(); assertNotNull(workItem); assertEquals("John Doe", workItem.getParameter("name")); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); assertEquals("John Doe", workItem.getParameter("text")); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNull(processInstance); }
@Test public void testPersistenceSubProcess() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(new ClassPathResource("SuperProcess.rf"), ResourceType.DRF); kbuilder.add(new ClassPathResource("SubProcess.rf"), ResourceType.DRF); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); int id = ksession.getId(); ProcessInstance processInstance = ksession.startProcess("com.sample.SuperProcess"); logger.debug("Started process instance {}", processInstance.getId()); TestWorkItemHandler handler = TestWorkItemHandler.getInstance(); WorkItem workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNull(workItem); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNull("Process did not complete.", processInstance); }
@Test public void testUserTransactions() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(ruleString.getBytes()), ResourceType.DRL); KieBaseConfiguration kBaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); // ((RuleBaseConfiguration)kBaseConf).setPhreakEnabled(false); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kBaseConf); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); ut.commit(); List<?> list = new ArrayList<Object>(); // insert and commit ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); ksession.setGlobal("list", list); ksession.insert(1); ksession.insert(2); ut.commit(); // insert and rollback ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); ksession.insert(3); ut.rollback(); // check we rolled back the state changes from the 3rd insert ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); ksession.fireAllRules(); ut.commit(); assertEquals(2, list.size()); // insert and commit ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); ksession.insert(3); ksession.insert(4); ut.commit(); // rollback again, this is testing that we can do consecutive rollbacks and commits without // issue ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); ksession.insert(5); ksession.insert(6); ut.rollback(); ksession.fireAllRules(); assertEquals(4, list.size()); // now load the ksession ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(ksession.getId(), kbase, null, env); ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); ksession.insert(7); ksession.insert(8); ut.commit(); ksession.fireAllRules(); assertEquals(6, list.size()); }