use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.
the class AsyncWorkItemDontWaitForCompletionTest method initializeSession.
private void initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
//We are using a proces containing a single Task. The task is configured
//to avoid waiting until the completion of the external system.
kbuilder.add(new ClassPathResource("async-work-item-nowait.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
session.fireAllRules();
}
});
}
use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.
the class MyFirstjBPM5ProcessTest method test.
@Test
public void test() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("firstDummyProcess.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
// Uncomment to see all the logs
// KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// We have our Session, now let's play with our Dummy Process
ProcessInstance processInstance = ksession.createProcessInstance("DummyProcess", null);
assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
ksession.startProcessInstance(processInstance.getId());
assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
}
use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.
the class PersistentProcessTest method processInstancesAndLocalHTPlusFailTest.
@Test
public void processInstancesAndLocalHTPlusFailTest() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("process-async-interactions.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
Environment env = EnvironmentFactory.newEnvironment();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.runtime.ht");
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
// Let's create a Persistence Knowledge Session
System.out.println(" >>> Let's create a Persistent Knowledge Session");
final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
int sessionId = ksession.getId();
assertNotNull(sessionId);
assertTrue(sessionId != 0);
// We need to register the WorkItems and Listeners that the session will use
TaskService client = createTaskService(emf);
LocalHTWorkItemHandler localHTWorkItemHandler = new LocalHTWorkItemHandler(client, ksession);
MockFaultWorkItemHandler mockFaultWorkItemHandler = new MockFaultWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", localHTWorkItemHandler);
ksession.getWorkItemManager().registerWorkItemHandler("External Service Call", mockFaultWorkItemHandler);
//KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// Let's create a Process Instance
Person person = new Person("Salaboy", 29);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
//Each Command will generate an interaction
System.out.println(">>> Let's Create Process Instance");
ProcessInstance processInstance = ksession.createProcessInstance("com.salaboy.process.AsyncInteractions", params);
System.out.println(">>> Let's Start the Process Instance");
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
ksession.startProcessInstance(processInstance.getId());
ut.commit();
System.out.println(" >>> Looking for Salaboy's Tasks");
List<TaskSummary> salaboysTasks = client.getTasksAssignedAsPotentialOwner("salaboy", "en-UK");
assertTrue(salaboysTasks.size() == 1);
TaskSummary salaboyTask = salaboysTasks.get(0);
System.out.println(" >>> Starting Salaboy's Task");
client.start(salaboyTask.getId(), "salaboy");
System.out.println(" >>> Completing Salaboy's Task");
ut.begin();
try {
client.complete(salaboyTask.getId(), "salaboy", null);
ut.commit();
} catch (WorkflowRuntimeException e) {
System.out.println("Rolling back transaction" + e);
ut.rollback();
}
System.out.println(" >>> Looking for Salaboy's Tasks");
Task salaboysTask = client.getTask(salaboyTask.getId());
assertNotNull(salaboysTask);
assertEquals("InProgress", salaboysTask.getTaskData().getStatus().name());
// We need to dispose the session, because the reference to this ksession object will no longer be valid
// because another thread could load the same session and execute a different command.
System.out.println(" >>> Disposing Session");
ksession.dispose();
}
use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.
the class MyFirstDrools5RulesTest method testSimpleRules.
@Test
public void testSimpleRules() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("simpleRules.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
System.out.println(">>> Error:" + error.getMessage());
}
fail(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
// Uncomment to see all the logs
// KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
// We have our Session, now let's play with our Simple Rules
Person person = new Person("Salaboy", 28);
ksession.insert(person);
int fired = ksession.fireAllRules();
assertEquals(4, fired);
assertEquals(true, person.isEnabledToDrive());
assertEquals(true, person.isEnabledToVote());
assertEquals(true, person.isHappy());
Person person2 = new Person("John", 19);
ksession.insert(person2);
fired = ksession.fireAllRules();
assertEquals(3, fired);
assertEquals(true, person2.isEnabledToDrive());
assertEquals(false, person2.isEnabledToVote());
assertEquals(true, person2.isHappy());
Person person3 = new Person("Marie", 17);
ksession.insert(person3);
fired = ksession.fireAllRules();
assertEquals(0, fired);
assertEquals(false, person3.isEnabledToDrive());
assertEquals(false, person3.isEnabledToVote());
assertEquals(false, person3.isHappy());
ksession.dispose();
}
use of org.drools.io.impl.ClassPathResource in project jBPM5-Developer-Guide by Salaboy.
the class TaskListJFrame method createSession.
// End of variables declaration//GEN-END:variables
private StatefulKnowledgeSession createSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
//Adds resources to the builder
kbuilder.add(new ClassPathResource("rules/SimpleRulesAndTask.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
//Checks for errors
if (errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.out.println(error.getMessage());
}
throw new IllegalStateException("Error building kbase!");
}
//Creates a new kbase and add all the packages from the builder
KnowledgeBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbaseConf.setOption(EventProcessingOption.STREAM);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession myksession = kbase.newStatefulKnowledgeSession();
HornetQHTWorkItemHandler hornetQHTWorkItemHandler = new HornetQHTWorkItemHandler(myksession);
myksession.getWorkItemManager().registerWorkItemHandler("Human Task", hornetQHTWorkItemHandler);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(myksession);
myksession.addEventListener(new DebugAgendaEventListener());
return myksession;
}
Aggregations