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);
}
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());
}
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;
}
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));
}
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);
}
Aggregations