Search in sources :

Example 1 with WorkItemManager

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]);
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) DefaultWorkItemManager(org.drools.core.process.instance.impl.DefaultWorkItemManager) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) DefaultWorkItemManager(org.drools.core.process.instance.impl.DefaultWorkItemManager) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with WorkItemManager

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);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Example 3 with WorkItemManager

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());
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) List(java.util.List) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) Test(org.junit.Test)

Example 4 with WorkItemManager

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());
}
Also used : TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) Test(org.junit.Test)

Example 5 with WorkItemManager

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());
}
Also used : TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) TestWorkItemManager(org.jbpm.process.workitem.core.TestWorkItemManager) Test(org.junit.Test)

Aggregations

WorkItemManager (org.kie.api.runtime.process.WorkItemManager)100 Test (org.junit.Test)84 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)55 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)36 WorkItem (org.kie.api.runtime.process.WorkItem)31 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)27 HashMap (java.util.HashMap)25 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)23 KieBase (org.kie.api.KieBase)21 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 KieSession (org.kie.api.runtime.KieSession)14 Map (java.util.Map)9 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)8 DefaultWorkItemManager (org.drools.core.process.instance.impl.DefaultWorkItemManager)7 AsyncWorkItemHandler (org.jbpm.executor.impl.wih.AsyncWorkItemHandler)7 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)7 QueryContext (org.kie.api.runtime.query.QueryContext)6 Document (org.w3c.dom.Document)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ArrayList (java.util.ArrayList)5