Search in sources :

Example 6 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class JpaPersistenceTraitTest method testTripleBasedTraitsWithJPA.

@Test
public void testTripleBasedTraitsWithJPA() {
    String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean \n" + "  @propertyReactive \n" + "  @Traitable \n" + "  fld : String \n" + "end \n " + "" + "declare trait Mask \n" + "  @propertyReactive \n" + "  fld : String \n" + "  xyz : int  \n" + "end \n" + "\n " + "declare trait Cloak \n" + "  @propertyReactive \n" + "  fld : String \n" + "  ijk : String  \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + "  insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  Mask m = don( $b, Mask.class ); \n" + "  modify (m) { setXyz( 10 ); } \n" + "  list.add( m ); \n" + "  System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "rule Don2 \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  Cloak c = don( $b, Cloak.class ); \n" + "  modify (c) { setIjk( \"ijklmn\" ); } \n" + "  list.add( c ); \n" + "  System.out.println( \"Don result : \" + c ); \n " + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    // StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    long id = ksession.getIdentifier();
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    // StatefulKnowledgeSession ksession2 = kbase.newStatefulKnowledgeSession();
    ksession2.fireAllRules();
    Collection x = ksession2.getObjects();
    assertEquals(3, x.size());
    TraitableBean core = null;
    for (Object o : x) {
        if (o instanceof TraitableBean) {
            core = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(core);
    assertEquals(2, core._getDynamicProperties().size());
    assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
    assertNotNull(core.getTrait("org.drools.trait.test.Cloak"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class JpaPersistenceTraitTest method traitsLegacyWrapperWithJPA.

public void traitsLegacyWrapperWithJPA(VirtualPropertyMode mode) {
    String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "import org.drools.core.factmodel.traits.*; \n" + "" + "" + "declare TBean \n" + "@Traitable \n" + "  fld : String \n" + "end \n " + "" + "declare trait Mask \n" + "  @propertyReactive \n" + "  fld : String \n" + "  xyz : int  \n" + "end \n" + "\n " + "rule Init \n" + "when \n" + "then \n" + "  insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  System.out.println( \"Din Don Dan: \"  ); \n " + "  Mask m = don( $b, Mask.class ); \n" + "  modify (m) { setXyz( 10 ); } \n" + "  list.add( m ); \n" + "  System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    TraitFactory.setMode(mode, ksession.getKieBase());
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    long id = ksession.getIdentifier();
    Collection yOld = ksession.getObjects();
    TraitableBean coreOld = null;
    for (Object o : yOld) {
        if (o instanceof TraitableBean) {
            coreOld = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(coreOld);
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession2.fireAllRules();
    Collection y = ksession2.getObjects();
    assertEquals(2, y.size());
    TraitableBean core = null;
    for (Object o : y) {
        if (o instanceof TraitableBean) {
            core = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(core);
    assertEquals(1, core._getDynamicProperties().size());
    assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 8 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testSharedReferences.

@Test
public void testSharedReferences() {
    final KieBase kbase = new KieHelper().getKieContainer().getKieBase();
    final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
    final Person x = new Person("test");
    final List<Person> test = new ArrayList<>();
    final List<Person> test2 = new ArrayList<>();
    test.add(x);
    test2.add(x);
    assertThat(test.get(0)).isSameAs(test2.get(0));
    ksession.insert(test);
    ksession.insert(test2);
    ksession.fireAllRules();
    final StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(ksession.getIdentifier(), kbase, null, env);
    final Iterator c = ksession2.getObjects().iterator();
    final List ref1 = (List) c.next();
    final List ref2 = (List) c.next();
    assertThat(ref1.get(0)).isSameAs(ref2.get(0));
}
Also used : KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 9 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession 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 10 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class TimerAndCalendarTest method createSession.

private KieSession createSession(KieBase kbase) {
    final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, conf, env);
    return ksession;
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Environment(org.kie.api.runtime.Environment) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Aggregations

StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)21 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Collection (java.util.Collection)4 KieBase (org.kie.api.KieBase)4 Environment (org.kie.api.runtime.Environment)4 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 List (java.util.List)3 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)3 KieSession (org.kie.api.runtime.KieSession)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)2 RegistryContext (org.drools.core.command.impl.RegistryContext)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1