Search in sources :

Example 41 with StatefulKnowledgeSession

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);
}
Also used : KieBase(org.kie.api.KieBase) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession)

Example 42 with StatefulKnowledgeSession

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();
    }
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) File(java.io.File)

Example 43 with StatefulKnowledgeSession

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;
}
Also used : ProtobufMarshaller(org.drools.serialization.protobuf.ProtobufMarshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 44 with StatefulKnowledgeSession

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);
    }
}
Also used : FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) RegistryContext(org.kie.internal.command.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand)

Example 45 with StatefulKnowledgeSession

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);
    }
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession)

Aggregations

StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)114 Test (org.junit.Test)79 KieBase (org.kie.api.KieBase)52 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)40 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)30 ArrayList (java.util.ArrayList)29 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)22 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)20 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)18 HashMap (java.util.HashMap)16 Environment (org.kie.api.runtime.Environment)12 KieSession (org.kie.api.runtime.KieSession)11 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)11 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 WorkItem (org.kie.api.runtime.process.WorkItem)9 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)8 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)8 Resource (org.kie.api.io.Resource)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7