Search in sources :

Example 16 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project jbpm by kiegroup.

the class PersistentStatefulSessionTest method testPersistenceWorkItems.

@Test
public void testPersistenceWorkItems() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.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 = 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);
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 17 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project jbpm by kiegroup.

the class PersistentStatefulSessionTest method testPersistenceWorkItems3.

@Test
public void testPersistenceWorkItems3() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    ksession.getWorkItemManager().registerWorkItemHandler("MyWork", new SystemOutWorkItemHandler());
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    ksession.insert("TestString");
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 18 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project jbpm by kiegroup.

the class VariablePersistenceStrategyTest method createKnowledgeBase.

private KieBase createKnowledgeBase(String flowFile) {
    KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    conf.setProperty("drools.dialect.java.compiler", "JANINO");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
    kbuilder.add(new ClassPathResource(flowFile), ResourceType.DRF);
    if (kbuilder.hasErrors()) {
        StringBuilder errorMessage = new StringBuilder();
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            errorMessage.append(error.getMessage());
            errorMessage.append(System.getProperty("line.separator"));
        }
        fail(errorMessage.toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 19 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project jbpm by kiegroup.

the class ActionNodeTest method testSingleActionNode.

@Test
public void testSingleActionNode() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("ActionNodeTest.xml", ActionNodeTest.class), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.startProcess("process name");
    assertEquals(1, list.size());
    assertEquals("action node was here", list.get(0));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 20 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategyTest method testProcessInstanceResolverStrategy.

@Test
public void testProcessInstanceResolverStrategy() throws Exception {
    // Setup
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource(PROCESS_NAME, this.getClass()), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    ProcessInstance processInstance = ksession.createProcessInstance("process name", new HashMap<String, Object>());
    ksession.insert(processInstance);
    // strategy setup
    ProcessInstanceResolverStrategy strategy = new ProcessInstanceResolverStrategy();
    ObjectMarshallingStrategy[] strategies = { strategy, MarshallerFactory.newSerializeMarshallingStrategy() };
    // Test strategy.write
    org.kie.api.marshalling.MarshallingConfiguration marshallingConfig = new MarshallingConfigurationImpl(strategies, true, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ProtobufMarshallerWriteContext writerContext = new ProtobufMarshallerWriteContext(baos, ((InternalKnowledgeBase) kbase), (InternalWorkingMemory) ((StatefulKnowledgeSessionImpl) ksession), RuleBaseNodes.getNodeMap(((InternalKnowledgeBase) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), ksession.getEnvironment());
    strategy.write(writerContext, processInstance);
    baos.close();
    writerContext.close();
    byte[] bytes = baos.toByteArray();
    int numCorrectBytes = calculateNumBytesForLong(processInstance.getId());
    assertTrue("Expected " + numCorrectBytes + " bytes, not " + bytes.length, bytes.length == numCorrectBytes);
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    long serializedProcessInstanceId = ois.readLong();
    assertTrue("Expected " + processInstance.getId() + ", not " + serializedProcessInstanceId, processInstance.getId() == serializedProcessInstanceId);
    // Test other strategy stuff
    ProcessInstanceManager pim = ProcessInstanceResolverStrategy.retrieveProcessInstanceManager(writerContext);
    assertNotNull(pim);
    assertNotNull(ProcessInstanceResolverStrategy.retrieveKnowledgeRuntime(writerContext));
    assertTrue(processInstance == pim.getProcessInstance(serializedProcessInstanceId));
    // Test strategy.read
    bais = new ByteArrayInputStream(bytes);
    ProtobufMarshallerReaderContext readerContext = new ProtobufMarshallerReaderContext(bais, ((KnowledgeBaseImpl) kbase), RuleBaseNodes.getNodeMap(((KnowledgeBaseImpl) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), ProtobufMarshaller.TIMER_READERS, marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), EnvironmentFactory.newEnvironment());
    readerContext.setWorkingMemory(((StatefulKnowledgeSessionImpl) ksession).getInternalWorkingMemory());
    Object procInstObject = strategy.read(readerContext);
    assertTrue(procInstObject != null && procInstObject instanceof ProcessInstance);
    assertTrue(processInstance == procInstObject);
}
Also used : ProcessInstanceResolverStrategy(org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy) ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ProtobufMarshallerReaderContext(org.drools.serialization.protobuf.ProtobufMarshallerReaderContext) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ProtobufMarshallerWriteContext(org.drools.serialization.protobuf.ProtobufMarshallerWriteContext) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ByteArrayInputStream(java.io.ByteArrayInputStream) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

ClassPathResource (org.drools.core.io.impl.ClassPathResource)88 BpmnProcess (org.kie.kogito.process.bpmn2.BpmnProcess)45 Test (org.junit.jupiter.api.Test)38 BpmnVariables (org.kie.kogito.process.bpmn2.BpmnVariables)33 Test (org.junit.Test)24 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)21 HashMap (java.util.HashMap)16 ProcessMetaData (org.jbpm.compiler.canonical.ProcessMetaData)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)11 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)11 VerifierReport (org.drools.verifier.data.VerifierReport)10 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)10 WorkItem (org.kie.kogito.process.WorkItem)10 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)9 ArrayList (java.util.ArrayList)6 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)6 KieSession (org.kie.api.runtime.KieSession)6 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)6