Search in sources :

Example 66 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class DynamicUtils method internalAddDynamicSubProcess.

public static long internalAddDynamicSubProcess(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, KieRuntime ksession, final String processId, final Map<String, Object> parameters) {
    final SubProcessNodeInstance subProcessNodeInstance = new SubProcessNodeInstance();
    subProcessNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
    subProcessNodeInstance.setProcessInstance(processInstance);
    subProcessNodeInstance.setMetaData("NodeType", "SubProcessNode");
    if (ksession instanceof StatefulKnowledgeSessionImpl) {
        return executeSubProcess((StatefulKnowledgeSessionImpl) ksession, processId, parameters, processInstance, subProcessNodeInstance);
    } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
        ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
        return commandService.execute(new ExecutableCommand<Long>() {

            private static final long serialVersionUID = 5L;

            public Long execute(Context context) {
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance realProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstance.getId());
                subProcessNodeInstance.setProcessInstance(realProcessInstance);
                if (dynamicContext == null) {
                    subProcessNodeInstance.setNodeInstanceContainer(realProcessInstance);
                } else {
                    DynamicNodeInstance realDynamicContext = findDynamicContext(realProcessInstance, dynamicContext.getUniqueId());
                    subProcessNodeInstance.setNodeInstanceContainer(realDynamicContext);
                }
                return executeSubProcess((StatefulKnowledgeSessionImpl) ksession, processId, parameters, processInstance, subProcessNodeInstance);
            }
        });
    } else {
        throw new IllegalArgumentException("Unsupported ksession: " + ksession == null ? "null" : ksession.getClass().getName());
    }
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 67 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class AbstractAuditLogServiceTest method createKieSession.

public static KieSession createKieSession(KieBase kbase, Environment env) {
    // create a new session
    Properties properties = new Properties();
    properties.put("drools.processInstanceManagerFactory", "org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory");
    properties.put("drools.processSignalManagerFactory", "org.jbpm.persistence.processinstance.JPASignalManagerFactory");
    KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);
    StatefulKnowledgeSession session = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, config, env);
    return session;
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Example 68 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class ProcessRuntimeImpl method initProcessActivationListener.

private void initProcessActivationListener() {
    kruntime.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            String ruleFlowGroup = ((RuleImpl) event.getMatch().getRule()).getRuleFlowGroup();
            if ("DROOLS_SYSTEM".equals(ruleFlowGroup)) {
                // new activations of the rule associate with a state node
                // signal process instances of that state node
                String ruleName = event.getMatch().getRule().getName();
                if (ruleName.startsWith("RuleFlowStateNode-")) {
                    int index = ruleName.indexOf('-', 18);
                    index = ruleName.indexOf('-', index + 1);
                    String eventType = ruleName.substring(0, index);
                    kruntime.queueWorkingMemoryAction(new SignalManagerSignalAction(eventType, event));
                } else if (ruleName.startsWith("RuleFlowStateEventSubProcess-") || ruleName.startsWith("RuleFlowStateEvent-") || ruleName.startsWith("RuleFlow-Milestone-") || ruleName.startsWith("RuleFlow-AdHocComplete-") || ruleName.startsWith("RuleFlow-AdHocActivate-")) {
                    kruntime.queueWorkingMemoryAction(new SignalManagerSignalAction(ruleName, event));
                }
            }
        }
    });
    kruntime.addEventListener(new DefaultAgendaEventListener() {

        public void afterRuleFlowGroupDeactivated(final RuleFlowGroupDeactivatedEvent event) {
            if (kruntime instanceof StatefulKnowledgeSession) {
                signalManager.signalEvent("RuleFlowGroup_" + event.getRuleFlowGroup().getName() + "_" + ((StatefulKnowledgeSession) kruntime).getIdentifier(), null);
            } else {
                signalManager.signalEvent("RuleFlowGroup_" + event.getRuleFlowGroup().getName(), null);
            }
        }
    });
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent)

Example 69 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class MVELReturnValueEvaluator method evaluate.

public Object evaluate(ProcessContext context) throws Exception {
    int length = unit.getOtherIdentifiers().length;
    Object[] vars = new Object[length];
    if (unit.getOtherIdentifiers() != null) {
        for (int i = 0; i < length; i++) {
            vars[i] = context.getVariable(unit.getOtherIdentifiers()[i]);
        }
    }
    InternalWorkingMemory internalWorkingMemory = null;
    if (context.getKieRuntime() instanceof StatefulKnowledgeSessionImpl) {
        internalWorkingMemory = ((StatefulKnowledgeSessionImpl) context.getKieRuntime()).getInternalWorkingMemory();
    } else if (context.getKieRuntime() instanceof StatelessKnowledgeSession) {
        StatefulKnowledgeSession statefulKnowledgeSession = ((StatelessKnowledgeSessionImpl) context.getKieRuntime()).newWorkingMemory();
        internalWorkingMemory = ((StatefulKnowledgeSessionImpl) statefulKnowledgeSession).getInternalWorkingMemory();
    }
    VariableResolverFactory factory = unit.getFactory(context, // No previous declarations
    null, // No rule
    null, // No "right object"
    null, // No (left) tuples
    null, vars, internalWorkingMemory, (GlobalResolver) context.getKieRuntime().getGlobals());
    // do we have any functions for this namespace?
    KiePackage pkg = context.getKieRuntime().getKieBase().getKiePackage("MAIN");
    if (pkg instanceof KnowledgePackageImpl) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) ((KnowledgePackageImpl) pkg).getDialectRuntimeRegistry().getDialectData(id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    Object value = MVELSafeHelper.getEvaluator().executeExpression(this.expr, null, factory);
    if (!(value instanceof Boolean)) {
        throw new RuntimeException("Constraints must return boolean values: " + unit.getExpression() + " returns " + value + (value == null ? "" : " (type=" + value.getClass()));
    }
    return ((Boolean) value).booleanValue();
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) StatelessKnowledgeSession(org.kie.internal.runtime.StatelessKnowledgeSession) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) KiePackage(org.kie.api.definition.KiePackage) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl)

Example 70 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class IntermediateEventTest method testErrorBoundaryEventOnMultiInstanceSubprocess.

@Test
public void testErrorBoundaryEventOnMultiInstanceSubprocess() throws Exception {
    KieBase kbase = createKnowledgeBase("subprocess/BPMN2-MultiInstanceSubprocessWithBoundaryError.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> approvers = new ArrayList<String>();
    approvers.add("john");
    approvers.add("john");
    params.put("approvers", approvers);
    ProcessInstance processInstance = ksession.startProcess("boundary-catch-error-event", params);
    assertProcessInstanceActive(processInstance);
    List<WorkItem> workItems = handler.getWorkItems();
    assertThat(workItems).isNotNull();
    assertThat(workItems.size()).isEqualTo(2);
    ksession.signalEvent("Inside", null, processInstance.getId());
    assertProcessInstanceFinished(processInstance, ksession);
    ksession.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test)

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