Search in sources :

Example 31 with StatefulKnowledgeSession

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

the class MVELAction method execute.

public void execute(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 StatelessKnowledgeSessionImpl) {
        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());
    // KnowledgePackage pkg = context.getKnowledgeRuntime().getKnowledgeBase().getKnowledgePackage( "MAIN" );
    // if ( pkg != null && pkg instanceof KnowledgePackageImp) {
    // MVELDialectRuntimeData data = ( MVELDialectRuntimeData ) ((KnowledgePackageImp) pkg).pkg.getDialectRuntimeRegistry().getDialectData( id );
    // factory.setNextFactory( data.getFunctionFactory() );
    // }
    // 
    MVELSafeHelper.getEvaluator().executeExpression(this.expr, null, factory);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) StatelessKnowledgeSessionImpl(org.drools.core.impl.StatelessKnowledgeSessionImpl) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession)

Example 32 with StatefulKnowledgeSession

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

the class RuleSetNodeInstance method retractFacts.

public void retractFacts() {
    Map<String, Object> objects = new HashMap<String, Object>();
    KieRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
    for (Entry<String, FactHandle> entry : factHandles.entrySet()) {
        Object object = ((StatefulKnowledgeSession) kruntime).getObject(entry.getValue());
        String key = entry.getKey();
        key = key.replaceAll(getRuleFlowGroup() + "_", "");
        key = key.replaceAll(getProcessInstance().getId() + "_", "");
        objects.put(key, object);
        kruntime.delete(entry.getValue());
    }
    processOutputs(objects);
    factHandles.clear();
}
Also used : HashMap(java.util.HashMap) FactHandle(org.kie.api.runtime.rule.FactHandle) KieRuntime(org.kie.api.runtime.KieRuntime) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession)

Example 33 with StatefulKnowledgeSession

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

the class DynamicUtils method internalAddDynamicWorkItem.

private static void internalAddDynamicWorkItem(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, KieRuntime ksession, String workItemName, Map<String, Object> parameters) {
    final WorkItemImpl workItem = new WorkItemImpl();
    workItem.setState(WorkItem.ACTIVE);
    workItem.setProcessInstanceId(processInstance.getId());
    workItem.setDeploymentId((String) ksession.getEnvironment().get(EnvironmentName.DEPLOYMENT_ID));
    workItem.setName(workItemName);
    workItem.setParameters(parameters);
    for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            String s = (String) entry.getValue();
            Object variableValue = null;
            Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
            while (matcher.find()) {
                String paramName = matcher.group(1);
                variableValue = processInstance.getVariable(paramName);
                if (variableValue == null) {
                    try {
                        variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new ProcessInstanceResolverFactory(processInstance));
                    } catch (Throwable t) {
                        logger.error("Could not find variable scope for variable {}", paramName);
                        logger.error("when trying to replace variable in string for Dynamic Work Item {}", workItemName);
                        logger.error("Continuing without setting parameter.");
                    }
                }
            }
            if (variableValue != null) {
                workItem.setParameter(entry.getKey(), variableValue);
            }
        }
    }
    final WorkItemNodeInstance workItemNodeInstance = new WorkItemNodeInstance();
    workItemNodeInstance.internalSetWorkItem(workItem);
    workItemNodeInstance.setMetaData("NodeType", workItemName);
    workItem.setNodeInstanceId(workItemNodeInstance.getId());
    if (ksession instanceof StatefulKnowledgeSessionImpl) {
        workItemNodeInstance.setProcessInstance(processInstance);
        workItemNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
        workItemNodeInstance.addEventListeners();
        executeWorkItem((StatefulKnowledgeSessionImpl) ksession, workItem, workItemNodeInstance);
    } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
        ExecutableRunner runner = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
        runner.execute(new ExecutableCommand<Void>() {

            private static final long serialVersionUID = 5L;

            public Void execute(Context context) {
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance realProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstance.getId());
                workItemNodeInstance.setProcessInstance(realProcessInstance);
                if (dynamicContext == null) {
                    workItemNodeInstance.setNodeInstanceContainer(realProcessInstance);
                } else {
                    DynamicNodeInstance realDynamicContext = findDynamicContext(realProcessInstance, dynamicContext.getUniqueId());
                    workItemNodeInstance.setNodeInstanceContainer(realDynamicContext);
                }
                workItemNodeInstance.addEventListeners();
                executeWorkItem((StatefulKnowledgeSessionImpl) ksession, workItem, workItemNodeInstance);
                return null;
            }
        });
    } 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) Matcher(java.util.regex.Matcher) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) ProcessInstanceResolverFactory(org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession) Map(java.util.Map) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 34 with StatefulKnowledgeSession

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

the class JBPMHelper method loadStatefulKnowledgeSession.

@Deprecated
public static StatefulKnowledgeSession loadStatefulKnowledgeSession(KieBase kbase, int sessionId) {
    Properties properties = getProperties();
    String persistenceEnabled = properties.getProperty("persistence.enabled", "false");
    RuntimeEnvironmentBuilder builder = null;
    if ("true".equals(persistenceEnabled)) {
        String dialect = properties.getProperty("persistence.persistenceunit.dialect", "org.hibernate.dialect.H2Dialect");
        Map<String, String> map = new HashMap<String, String>();
        map.put("hibernate.dialect", dialect);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties.getProperty("persistence.persistenceunit.name", "org.jbpm.persistence.jpa"), map);
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addEnvironmentEntry(EnvironmentName.TRANSACTION_MANAGER, com.arjuna.ats.jta.TransactionManager.transactionManager());
    } else {
        builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder();
    }
    builder.knowledgeBase(kbase);
    RuntimeManager manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(builder.get());
    return (StatefulKnowledgeSession) manager.getRuntimeEngine(EmptyContext.get()).getKieSession();
}
Also used : HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) Properties(java.util.Properties) RuntimeEnvironmentBuilder(org.kie.api.runtime.manager.RuntimeEnvironmentBuilder)

Example 35 with StatefulKnowledgeSession

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

the class JbpmJUnitBaseTestCase method getRuntimeEngine.

/**
 * Returns new <code>RuntimeEngine</code> built from the manager of this test case. Common use case is to maintain
 * same session for process instance and thus <code>ProcessInstanceIdContext</code> shall be used.
 * @param context - instance of the context that shall be used to create <code>RuntimeManager</code>
 * @return new RuntimeEngine instance
 */
protected RuntimeEngine getRuntimeEngine(Context<?> context) {
    if (manager == null) {
        throw new IllegalStateException("RuntimeManager is not initialized, did you forgot to create it?");
    }
    RuntimeEngine runtimeEngine = manager.getRuntimeEngine(context);
    activeEngines.add(runtimeEngine);
    if (sessionPersistence) {
        logService = runtimeEngine.getAuditService();
    } else {
        inMemoryLogger = new WorkingMemoryInMemoryLogger((StatefulKnowledgeSession) runtimeEngine.getKieSession());
    }
    return runtimeEngine;
}
Also used : WorkingMemoryInMemoryLogger(org.drools.core.audit.WorkingMemoryInMemoryLogger) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) 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