Search in sources :

Example 1 with CommandBasedStatefulKnowledgeSession

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());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) ArrayList(java.util.ArrayList) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) Test(org.junit.Test)

Example 2 with CommandBasedStatefulKnowledgeSession

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);
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 3 with CommandBasedStatefulKnowledgeSession

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());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) ArrayList(java.util.ArrayList) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) Test(org.junit.Test)

Example 4 with CommandBasedStatefulKnowledgeSession

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);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 5 with CommandBasedStatefulKnowledgeSession

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();
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) Context(org.kie.api.runtime.Context) DoNothingWorkItemHandler(org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler) SingleSessionCommandService(org.drools.core.command.SingleSessionCommandService) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) KieBase(org.kie.api.KieBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) Environment(org.kie.api.runtime.Environment) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) List(java.util.List) ArrayList(java.util.ArrayList) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager) Test(org.junit.Test) RequirePersistence(org.jbpm.bpmn2.test.RequirePersistence)

Aggregations

CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)16 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)9 KieBase (org.kie.api.KieBase)7 Environment (org.kie.api.runtime.Environment)7 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)7 TriggerRulesEventListener (org.jbpm.process.instance.event.listeners.TriggerRulesEventListener)5 RegistryContext (org.drools.core.command.impl.RegistryContext)4 Context (org.kie.api.runtime.Context)4 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)3 InternalAgenda (org.drools.core.common.InternalAgenda)3 ExecutableRunner (org.kie.api.runtime.ExecutableRunner)3 KieSession (org.kie.api.runtime.KieSession)3 HashMap (java.util.HashMap)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 ClassPathResource (org.drools.core.io.impl.ClassPathResource)2 RequirePersistence (org.jbpm.bpmn2.test.RequirePersistence)2 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)2 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)2