use of org.kie.api.runtime.process.WorkItemManager in project drools by kiegroup.
the class RegisterWorkItemHandlerTest method testRegisterWorkItemHandlerWithStatelessSession.
@Test
public void testRegisterWorkItemHandlerWithStatelessSession() {
String str = "package org.kie.workitem.test \n" + "import " + DefaultWorkItemManager.class.getCanonicalName() + "\n" + "import " + WorkItem.class.getCanonicalName() + "\n" + "import " + WorkItemImpl.class.getCanonicalName() + "\n" + "rule r1 when \n" + "then \n" + " WorkItem wi = new WorkItemImpl(); \n" + " wi.setName( \"wihandler\" ); \n" + " DefaultWorkItemManager wim = ( DefaultWorkItemManager ) kcontext.getKieRuntime().getWorkItemManager(); \n" + " wim.internalExecuteWorkItem(wi); \n" + "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final boolean[] answer = new boolean[] { false };
StatelessKieSession ks = kbase.newStatelessKieSession();
ks.execute(CommandFactory.newRegisterWorkItemHandlerCommand(new WorkItemHandler() {
public void executeWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
answer[0] = true;
}
public void abortWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
// TODO Auto-generated method stub
}
}, "wihandler"));
assertTrue(answer[0]);
}
use of org.kie.api.runtime.process.WorkItemManager in project jbpm by kiegroup.
the class RequestUI method createKieSession.
private KieSession createKieSession() {
try {
final KieSession ksession = getKieSession();
KieServices.Factory.get().getLoggers().newThreadedFileLogger(ksession, "test", 1000);
UIWorkItemHandler handler = new UIWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
handler.setVisible(true);
ksession.getWorkItemManager().registerWorkItemHandler("Email", new WorkItemHandler() {
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
System.out.println("Sending email ...");
manager.completeWorkItem(workItem.getId(), null);
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
});
Person person = new Person("john", "John Doe");
person.setAge(40);
ksession.insert(person);
person = new Person("krisv", "Kris Verlaenen");
person.setAge(30);
ksession.insert(person);
person = new Person("baby", "Baby");
person.setAge(1);
ksession.insert(person);
ksession.addEventListener(new DefaultProcessEventListener() {
public void beforeProcessStarted(ProcessStartedEvent event) {
ksession.insert(event);
}
});
return ksession;
} catch (Throwable t) {
throw new RuntimeException("Could not initialize session!", t);
}
}
use of org.kie.api.runtime.process.WorkItemManager in project jbpm-work-items by kiegroup.
the class JPAWorkItemHandlerTest method queryWithParameterActionTest.
@Test
public void queryWithParameterActionTest() throws Exception {
WorkItemManager manager = new TestWorkItemManager();
String DESC = "Cheese";
Product p1 = new Product("Bread", 2f);
Product p2 = new Product("Milk", 3f);
Product p3 = new Product(DESC, 5f);
create(p1);
create(p2);
create(p3);
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter(JPAWorkItemHandler.P_ACTION, JPAWorkItemHandler.QUERY_ACTION);
Map<String, Object> params = new HashMap<>();
params.put("desc", DESC);
workItem.setParameter(JPAWorkItemHandler.P_QUERY, "SELECT p FROM Product p where p.description = :desc");
workItem.setParameter(JPAWorkItemHandler.P_QUERY_PARAMS, params);
UserTransaction ut = getUserTransaction();
ut.begin();
handler.executeWorkItem(workItem, manager);
ut.commit();
Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId());
@SuppressWarnings("unchecked") List<Product> products = (List<Product>) results.get(JPAWorkItemHandler.P_QUERY_RESULTS);
assertEquals(1, products.size());
products = getAllProducts();
assertEquals(3, products.size());
for (Product product : products) {
removeProduct(product);
}
products = getAllProducts();
assertEquals(0, products.size());
}
use of org.kie.api.runtime.process.WorkItemManager in project jbpm-work-items by kiegroup.
the class ParserWorkItemHandlerTest method testXmlToObject.
@Test
public void testXmlToObject() {
WorkItemManager manager = new TestWorkItemManager();
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter(ParserWorkItemHandler.INPUT, PERSON_XML);
workItem.setParameter(ParserWorkItemHandler.FORMAT, ParserWorkItemHandler.XML);
workItem.setParameter(ParserWorkItemHandler.TYPE, "org.jbpm.process.workitem.parser.Person");
handler.executeWorkItem(workItem, manager);
Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId());
Person result = (Person) results.get(ParserWorkItemHandler.RESULT);
assertEquals(AGE, result.getAge());
assertEquals(NAME, result.getName());
}
use of org.kie.api.runtime.process.WorkItemManager in project jbpm-work-items by kiegroup.
the class ParserWorkItemHandlerTest method testJsonToObject.
@Test
public void testJsonToObject() {
WorkItemManager manager = new TestWorkItemManager();
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter(ParserWorkItemHandler.INPUT, PERSON_JSON);
workItem.setParameter(ParserWorkItemHandler.FORMAT, ParserWorkItemHandler.JSON);
workItem.setParameter(ParserWorkItemHandler.TYPE, "org.jbpm.process.workitem.parser.Person");
handler.executeWorkItem(workItem, manager);
Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId());
Person result = (Person) results.get(ParserWorkItemHandler.RESULT);
assertEquals(AGE, result.getAge());
assertEquals(NAME, result.getName());
}
Aggregations