Search in sources :

Example 11 with Environment

use of org.kie.api.runtime.Environment 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 12 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class ReloadSessionTest method testListenersAfterSessionReload.

@Test
@Ignore
public void testListenersAfterSessionReload() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=826952
    Environment env = createEnvironment();
    KieBase kbase = initializeKnowledgeBase(simpleRule);
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    ksession.addEventListener(new DefaultAgendaEventListener());
    ksession.addEventListener(new DefaultRuleRuntimeEventListener());
    assertEquals(1, ksession.getRuleRuntimeEventListeners().size());
    assertEquals(1, ksession.getAgendaEventListeners().size());
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(ksession.getIdentifier(), kbase, null, env);
    assertEquals(1, ksession.getRuleRuntimeEventListeners().size());
    assertEquals(1, ksession.getAgendaEventListeners().size());
}
Also used : KieBase(org.kie.api.KieBase) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Environment(org.kie.api.runtime.Environment) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) DefaultRuleRuntimeEventListener(org.kie.api.event.rule.DefaultRuleRuntimeEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class ReloadSessionTest method testInsertIntoEntryPoint.

@Test
public void testInsertIntoEntryPoint() {
    // RHBRMS-2815
    final Environment env = createEnvironment();
    final KieBase kbase = initializeKnowledgeBase(RULE_WITH_EP);
    KieSession kieSession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    assertTrue("There should be NO facts present in a new (empty) knowledge session.", kieSession.getFactHandles().isEmpty());
    kieSession.getEntryPoint(ENTRY_POINT).insert(Integer.valueOf(10));
    kieSession = reloadSession(kieSession, env);
    Collection<? extends Object> objects = kieSession.getEntryPoint(ENTRY_POINT).getObjects();
    assertEquals("Reloaded working memory should contain the fact in the entry point.", 1, objects.size());
}
Also used : KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 14 with Environment

use of org.kie.api.runtime.Environment in project drools by kiegroup.

the class ReloadSessionTest method testInsert.

@Test
public void testInsert() {
    final Environment env = createEnvironment();
    final KieBase kbase = initializeKnowledgeBase(RULE_WITH_EP);
    KieSession kieSession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    assertTrue("There should be NO facts present in a new (empty) knowledge session.", kieSession.getFactHandles().isEmpty());
    kieSession.insert(Integer.valueOf(10));
    kieSession = reloadSession(kieSession, env);
    Collection<? extends Object> objects = kieSession.getObjects();
    assertEquals("Reloaded working memory should contain the fact.", 1, objects.size());
}
Also used : KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 15 with Environment

use of org.kie.api.runtime.Environment 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)

Aggregations

Environment (org.kie.api.runtime.Environment)31 Test (org.junit.Test)15 KieBase (org.kie.api.KieBase)9 KieSession (org.kie.api.runtime.KieSession)9 ArrayList (java.util.ArrayList)6 DroolsPersistenceUtil.createEnvironment (org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 IdentityPlaceholderResolverStrategy (org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy)4 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)4 Cheese (org.drools.compiler.Cheese)3 PersistenceContext (org.drools.persistence.api.PersistenceContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Collection (java.util.Collection)2 List (java.util.List)2 EntityManager (javax.persistence.EntityManager)2 UserTransaction (javax.transaction.UserTransaction)2 Person (org.drools.compiler.Person)2 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)2