use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class TestPersistenceContext method startAndPersistSomeProcess.
/**
* Starts and persists a basic simple process using current database entities.
* @param processId Process identifier. This identifier is also used to generate KieBase
* (process with this identifier is part of generated KieBase).
*/
public void startAndPersistSomeProcess(final String processId) {
testIsInitialized();
final StatefulKnowledgeSession session;
final KieBase kbase = createKieBase(processId);
session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, environment);
session.startProcess(processId);
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.
the class UpgradeScriptsTest method testPersistedProcess.
public void testPersistedProcess(String type) throws IOException, ParseException, SQLException {
// Clear schema.
TestsUtil.clearSchema();
// Prepare + upgrade schema.
final TestPersistenceContext scriptRunnerContext = new TestPersistenceContext();
scriptRunnerContext.init(PersistenceUnit.SCRIPT_RUNNER);
try {
// Prepare 6.0. schema
scriptRunnerContext.executeScripts(new File(getClass().getResource("/ddl60").getFile()));
scriptRunnerContext.persistOldProcessAndSession(TEST_SESSION_ID, TEST_PROCESS_ID, TEST_PROCESS_INSTANCE_ID);
scriptRunnerContext.createSomeTask();
// Execute upgrade scripts.
scriptRunnerContext.executeScripts(new File(getClass().getResource(DB_UPGRADE_SCRIPTS_RESOURCE_PATH).getFile()), type);
} finally {
scriptRunnerContext.clean();
}
final TestPersistenceContext dbTestingContext = new TestPersistenceContext();
dbTestingContext.init(PersistenceUnit.DB_TESTING_VALIDATE);
try {
Assert.assertTrue(dbTestingContext.getStoredProcessesCount() == 1);
Assert.assertTrue(dbTestingContext.getStoredSessionsCount() == 1);
final StatefulKnowledgeSession persistedSession = dbTestingContext.loadPersistedSession(TEST_SESSION_ID.longValue(), TEST_PROCESS_ID);
Assert.assertNotNull(persistedSession);
// Start another process.
persistedSession.startProcess(TEST_PROCESS_ID);
Assert.assertTrue(dbTestingContext.getStoredProcessesCount() == 2);
// Load old process instance.
ProcessInstance processInstance = persistedSession.getProcessInstance(TEST_PROCESS_INSTANCE_ID);
Assert.assertNotNull(processInstance);
persistedSession.signalEvent("test", null);
processInstance = persistedSession.getProcessInstance(TEST_PROCESS_INSTANCE_ID);
Assert.assertNull(processInstance);
Assert.assertTrue(dbTestingContext.getStoredProcessesCount() == 0);
persistedSession.dispose();
persistedSession.destroy();
Assert.assertTrue(dbTestingContext.getStoredSessionsCount() == 0);
} finally {
dbTestingContext.clean();
}
}
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());
}
}
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class JpaOptLockPersistentStatefulSessionTest method testOptimisticLockInterceptor.
@Test
public void testOptimisticLockInterceptor() {
String str = "";
str += "package org.kie.test\n";
str += "global java.util.List list\n";
str += "rule rule1\n";
str += "when\n";
str += " Integer(intValue > 0)\n";
str += "then\n";
str += " list.add( 1 );\n";
str += "end\n";
str += "\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);
List<?> list = new ArrayList<Object>();
for (int i = 0; i < 2; i++) {
new InsertAndFireThread(ksession.getIdentifier(), kbase, list).start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals(6, list.size());
ksession.dispose();
}
Aggregations