use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class TimerAndCalendarTest method disposeAndReloadSession.
private KieSession disposeAndReloadSession(KieSession ksession, KieBase kbase) {
long ksessionId = ksession.getIdentifier();
ksession.dispose();
final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
StatefulKnowledgeSession newksession = JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase, conf, createEnvironment(context));
return newksession;
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerWithRemovingRule.
@Test
public void testTimerWithRemovingRule() throws Exception {
// DROOLS-576
// Only reproducible with RETEOO
InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
String str1 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R1\n" + " timer ( int: 5s )\n" + "when\n" + " $s : String( )\n" + "then\n" + " list.add( $s );\n" + "end\n";
Resource resource1 = ResourceFactory.newByteArrayResource(str1.getBytes());
Collection<KiePackage> kpackages1 = buildKnowledgePackage(resource1, ResourceType.DRL);
kbase1.addPackages(kpackages1);
StatefulKnowledgeSession ksession1 = JPAKnowledgeService.newStatefulKnowledgeSession(kbase1, null, createEnvironment(context));
long ksessionId = ksession1.getIdentifier();
ArrayList<String> list = new ArrayList<String>();
ksession1.setGlobal("list", list);
ksession1.insert("hello");
ksession1.fireAllRules();
// dispose before firing
ksession1.dispose();
Assert.assertEquals(0, list.size());
Thread.sleep(5000);
// A new kbase without the timer's activated rule
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
String str2 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R2\n" + "when\n" + " $s : Integer( )\n" + "then\n" + " list.add( $s );\n" + "end\n";
Resource resource2 = ResourceFactory.newByteArrayResource(str2.getBytes());
Collection<KiePackage> kpackages2 = buildKnowledgePackage(resource2, ResourceType.DRL);
kbase2.addPackages(kpackages2);
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase2, null, createEnvironment(context));
ksession2.setGlobal("list", list);
ksession2.fireAllRules();
ksession2.dispose();
Assert.assertEquals(0, list.size());
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class TransactionTestCommand method execute.
public Void execute(Context context) {
em.joinTransaction();
em.persist(mainObject);
if (subObject != null) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
// THe following 3 lines are the important ones! (See below for an explanation)
InternalKnowledgeBase cleanKBase = KnowledgeBaseFactory.newKnowledgeBase();
cleanKBase.addPackages(ksession.getKieBase().getKiePackages());
StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession(cleanKBase, null, initializeEnvironment());
/**
* Here's what's going on:
* If the PersistableRunner (SSCS) & JtaTransactionManager (JTM) were _not_ aware of transactions,
* -> then inserting the mainObject _before_ having inserted the subObject
* would cause the operation/persist to fail and the transaction to fail.
* - This is because the mainObject contains a foreign key referring to the subObject.
*
* However, the SSCS & JTM check whether or not they're owners of the transaction
* when starting and when committing the transaction they use.
* -> So that when we insert the mainObject here (via a _new_ CommandBasedStatefulKnowledgeSession),
* it does _not_ mess with the transaction state and the operation succeeds.
*/
TransactionTestCommand transactionTestSubCommand = new TransactionTestCommand(this.subObject, getPersistenceEnvironment());
commandKSession.execute(transactionTestSubCommand);
}
return null;
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class JpaPersistenceTraitTest method testMapBasedTraitsWithJPA.
@Test
public void testMapBasedTraitsWithJPA() {
String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean2 \n" + " @propertyReactive \n" + " @Traitable \n" + " fld : String \n" + "end \n " + "" + "declare trait Mask2 \n" + " @propertyReactive \n" + " fld : String \n" + " xyz : int \n" + "end \n" + "\n " + "declare trait Cloak2 \n" + " @propertyReactive \n" + " fld : String \n" + " ijk : String \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + " insert( new TBean2(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + " $b : TBean2( ) \n" + "then \n" + " Mask2 m = don( $b, Mask2.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 : TBean2( ) \n" + "then \n" + " Cloak2 c = don( $b, Cloak2.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();
TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
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);
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.Mask2"));
assertNotNull(core.getTrait("org.drools.trait.test.Cloak2"));
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class JpaPersistenceTraitTest method testTraitWithJPAOnFreshKieBase.
@Test
public void testTraitWithJPAOnFreshKieBase() {
// DROOLS-904
String str = "package org.drools.trait.test; " + "global java.util.List list; " + "declare TBean2 " + " @propertyReactive " + " @Traitable " + "end " + "declare trait Mask " + " @propertyReactive " + "end " + "query getTraits( Mask $m ) " + " $m := Mask() " + "end " + "rule Init when then don( new TBean2(), Mask.class ); end " + "rule Trig when String() then don( new TBean2(), Mask.class ); end " + "rule Main when $m : Mask() then list.add( $m ); end ";
List list = new ArrayList();
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
ksession.setGlobal("list", list);
ksession.fireAllRules();
long id = ksession.getIdentifier();
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
kbase2.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase2, null, env);
ksession.setGlobal("list", list);
ksession2.insert("go");
ksession2.fireAllRules();
assertEquals(2, list.size());
Class<?> oldProxyClass = list.get(0).getClass();
Class<?> newProxyClass = list.get(1).getClass();
assertNotSame(oldProxyClass, newProxyClass);
QueryResults qry = ksession2.getQueryResults("getTraits", Variable.v);
assertEquals(2, qry.size());
java.util.Iterator<QueryResultsRow> iter = qry.iterator();
int j = 0;
while (iter.hasNext()) {
QueryResultsRow row = iter.next();
Object entry = row.get("$m");
assertNotNull(entry);
assertSame(newProxyClass, entry.getClass());
j++;
}
assertEquals(2, j);
for (Object o : ksession2.getObjects()) {
if (o.getClass().getName().contains("Mask")) {
assertSame(newProxyClass, o.getClass());
}
}
}
Aggregations