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 SerializationHelper method getSerialisedStatefulKnowledgeSession.
public static StatefulKnowledgeSession getSerialisedStatefulKnowledgeSession(final KieSession ksession, final KieBase kbase, final boolean dispose) throws IOException, ClassNotFoundException {
final ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, (ObjectMarshallingStrategy[]) ksession.getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES));
final long time = ksession.getSessionClock().getCurrentTime();
// make sure globas are in the environment of the session
ksession.getEnvironment().set(EnvironmentName.GLOBALS, ksession.getGlobals());
// Serialize object
final byte[] b1;
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
marshaller.marshall(bos, ksession, time);
b1 = bos.toByteArray();
}
// Deserialize object
final StatefulKnowledgeSession ksession2;
try (final ByteArrayInputStream bais = new ByteArrayInputStream(b1)) {
ksession2 = marshaller.unmarshall(bais, ksession.getSessionConfiguration(), ksession.getEnvironment());
}
if (dispose) {
ksession.dispose();
}
return ksession2;
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method execute.
public <T> T execute(Command<T> command) {
StatefulKnowledgeSession ksession = newWorkingMemory();
RegistryContext context = new ContextImpl().register(KieSession.class, ksession);
try {
if (command instanceof BatchExecutionCommand) {
context.register(ExecutionResultImpl.class, new ExecutionResultImpl());
}
((StatefulKnowledgeSessionImpl) ksession).startBatchExecution();
Object o = ((ExecutableCommand) command).execute(context);
// did the user take control of fireAllRules, if not we will auto execute
boolean autoFireAllRules = true;
if (command instanceof FireAllRulesCommand) {
autoFireAllRules = false;
} else if (command instanceof BatchExecutionCommandImpl) {
for (Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands()) {
if (nestedCmd instanceof FireAllRulesCommand) {
autoFireAllRules = false;
break;
}
}
}
if (autoFireAllRules) {
ksession.fireAllRules();
}
if (command instanceof BatchExecutionCommand) {
return (T) context.lookup(ExecutionResultImpl.class);
} else {
return (T) o;
}
} finally {
((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
dispose(ksession);
}
}
use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.
the class StatelessKnowledgeSessionImpl method execute.
public void execute(Object object) {
StatefulKnowledgeSession ksession = newWorkingMemory();
try {
ksession.insert(object);
ksession.fireAllRules();
} finally {
dispose(ksession);
}
}
Aggregations