use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method testRuleFlowGroupOnly.
@Test
public void testRuleFlowGroupOnly() throws Exception {
CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "ruleflow-groups.drl");
org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// only main is available
assertEquals(1, groups.length);
assertEquals("MAIN", groups[0].getName());
long id = ksession.getIdentifier();
List<String> list = new ArrayList<String>();
list.add("Test");
ksession.insert(list);
ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
ksession.dispose();
ksession = createSession(id, "ruleflow-groups.drl");
groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// main and rule flow is now on the agenda
assertEquals(2, groups.length);
assertEquals("MAIN", groups[0].getName());
assertEquals("ruleflow-group", groups[1].getName());
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method createSession.
private CommandBasedStatefulKnowledgeSession createSession(long id, String... rules) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
for (String rule : rules) {
kbuilder.add(new ClassPathResource(rule), ResourceType.DRL);
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
Environment env = createEnvironment(context);
if (locking) {
env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
}
if (id == -1) {
return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
} else {
return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
}
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method testAgendaGroupOnly.
@Test
public void testAgendaGroupOnly() throws Exception {
CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "agenda-groups.drl");
org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// only main is available
assertEquals(1, groups.length);
assertEquals("MAIN", groups[0].getName());
long id = ksession.getIdentifier();
List<String> list = new ArrayList<String>();
list.add("Test");
ksession.insert(list);
ksession.execute(new ActivateAgendaGroupCommand("agenda-group"));
ksession.dispose();
ksession = createSession(id, "agenda-groups.drl");
groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroups();
// main and agenda group is now on the agenda
assertEquals(2, groups.length);
assertEquals("MAIN", groups[0].getName());
assertEquals("agenda-group", groups[1].getName());
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project drools by kiegroup.
the class RuleFlowGroupRollbackTest method createSession.
private CommandBasedStatefulKnowledgeSession createSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("ruleflowgroup_rollback.drl"), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
Environment env = createEnvironment(context);
if (locking) {
env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
}
return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.
the class IntermediateEventTest method testEventTypesLifeCycle.
@Test
@RequirePersistence
public void testEventTypesLifeCycle() throws Exception {
// JBPM-4246
KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchSignalBetweenUserTasks.bpmn2");
EntityManagerFactory separateEmf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
Environment env = createEnvironment(separateEmf);
ksession = createKnowledgeSession(kbase, null, env);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler());
ksession.startProcess("BPMN2-IntermediateCatchSignalBetweenUserTasks");
int signalListSize = ksession.execute(new ExecutableCommand<Integer>() {
public Integer execute(Context context) {
SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
InternalKnowledgeRuntime kruntime = (InternalKnowledgeRuntime) commandService.getKieSession();
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext pcontext = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = pcontext.getProcessInstancesWaitingForEvent("MySignal");
return processInstancesToSignalList.size();
}
});
// Process instance is not waiting for signal
assertThat(signalListSize).isEqualTo(0);
ksession.getWorkItemManager().completeWorkItem(1, null);
signalListSize = ksession.execute(new ExecutableCommand<Integer>() {
public Integer execute(Context context) {
SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
InternalKnowledgeRuntime kruntime = (InternalKnowledgeRuntime) commandService.getKieSession();
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext pcontext = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = pcontext.getProcessInstancesWaitingForEvent("MySignal");
return processInstancesToSignalList.size();
}
});
// Process instance is waiting for signal now
assertThat(signalListSize).isEqualTo(1);
ksession.signalEvent("MySignal", null);
signalListSize = ksession.execute(new ExecutableCommand<Integer>() {
public Integer execute(Context context) {
SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
InternalKnowledgeRuntime kruntime = (InternalKnowledgeRuntime) commandService.getKieSession();
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext pcontext = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = pcontext.getProcessInstancesWaitingForEvent("MySignal");
return processInstancesToSignalList.size();
}
});
// Process instance is not waiting for signal
assertThat(signalListSize).isEqualTo(0);
ksession.getWorkItemManager().completeWorkItem(2, null);
ksession.dispose();
ksession = null;
separateEmf.close();
}
Aggregations