@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 = InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); int origNumObjects = ksession.getObjects().size(); long id = ksession.getIdentifier(); 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 = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); processInstance = ksession.getProcessInstance(processInstance.getId()); assertNotNull(processInstance); ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env); ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNull(workItem); ksession = InfinispanKnowledgeService.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 testPackageImports() throws Exception { String str = ""; str += "package org.kie \n"; str += "dialect \"mvel\"\n"; str += "import org.acme.healthcare.* \n"; str += "import org.acme.insurance.* \n"; str += "import org.acme.sensors.SensorReading \n"; str += "rule rule1 \n"; str += " when \n"; str += " eval(true)\n"; str += " then \n"; str += " insert(new Claim()); // from org.acme.healthcare.* \n"; str += " insert(new Policy()); // from org.acme.insurance.* \n"; str += " insert(new SensorReading()); // from org.acme.sensor.SensorReading \n"; str += "end\n"; final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { throw new RuntimeException(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); int result = ksession.fireAllRules(); assertEquals(1, result); Collection<? extends Object> insertedObjects = ksession.getObjects(); assertEquals(3, insertedObjects.size()); }
@Test public void testPersistenceWorkItems2() throws Exception { 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 id = ksession.getId(); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); 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.getWorkItemManager().completeWorkItem(workItem.getId(), null); workItem = handler.getWorkItem(); assertNotNull(workItem); ut.commit(); 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(1, ksession.getObjects().size()); for (Object o : ksession.getObjects()) { logger.debug(o.toString()); } assertNull(processInstance); }
public Collection<VerifierComponent> getAll() { Collection<Object> list = kSession.getObjects(); return new ArrayList(list); }
public Collection<ObjectType> getObjectTypesByRuleName(String ruleName) { Collection<Object> list = kSession.getObjects(new ClassObjectFilter(Integer.class)); return new ArrayList(list); }