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"));
}
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"));
}
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));
}
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());
}
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;
}
Aggregations