Search in sources :

Example 6 with RuleFlowProcessInstance

use of org.jbpm.ruleflow.instance.RuleFlowProcessInstance in project jbpm by kiegroup.

the class TimerTest method testTimer.

@Test
@Ignore
public void testTimer() {
    // AbstractRuleBase ruleBase = (AbstractRuleBase) RuleBaseFactory.newRuleBase();
    // ExecutorService executorService = new DefaultExecutorService();
    // final StatefulSession workingMemory = new ReteooStatefulSession(1, ruleBase, executorService);
    // executorService.setCommandExecutor( new CommandExecutor( workingMemory ) );
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    final KieSession workingMemory = kbase.newKieSession();
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance() {

        private static final long serialVersionUID = 510l;

        public void signalEvent(String type, Object event) {
            if ("timerTriggered".equals(type)) {
                TimerInstance timer = (TimerInstance) event;
                logger.info("Timer {} triggered", timer.getId());
                counter++;
            }
        }
    };
    processInstance.setKnowledgeRuntime(((InternalWorkingMemory) workingMemory).getKnowledgeRuntime());
    processInstance.setId(1234);
    InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ((InternalWorkingMemory) workingMemory).getProcessRuntime());
    processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);
    new Thread(new Runnable() {

        public void run() {
            workingMemory.fireUntilHalt();
        }
    }).start();
    TimerManager timerManager = ((InternalProcessRuntime) ((InternalWorkingMemory) workingMemory).getProcessRuntime()).getTimerManager();
    TimerInstance timer = new TimerInstance();
    timerManager.registerTimer(timer, processInstance);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    timer = new TimerInstance();
    timer.setDelay(500);
    timerManager.registerTimer(timer, processInstance);
    assertEquals(0, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    timer = new TimerInstance();
    timer.setDelay(500);
    timer.setPeriod(300);
    timerManager.registerTimer(timer, processInstance);
    assertEquals(0, counter);
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    // we can't know exactly how many times this will fire as timers are not precise, but should be atleast 4
    assertTrue(counter >= 4);
    timerManager.cancelTimer(timer.getId());
    int lastCount = counter;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(lastCount, counter);
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) TimerManager(org.jbpm.process.instance.timer.TimerManager) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 7 with RuleFlowProcessInstance

use of org.jbpm.ruleflow.instance.RuleFlowProcessInstance in project jbpm by kiegroup.

the class UpdateTimerCommand method execute.

@Override
public Void execute(Context context) {
    logger.debug("About to cancel timer in process instance {} by name '{}' or id {}", processInstanceId, timerName, timerId);
    KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
    TimerManager tm = getTimerManager(kieSession);
    RuleFlowProcessInstance wfp = (RuleFlowProcessInstance) kieSession.getProcessInstance(processInstanceId);
    if (wfp == null) {
        throw new IllegalArgumentException("Process instance with id " + processInstanceId + " not found");
    }
    for (NodeInstance nodeInstance : wfp.getNodeInstances(true)) {
        if (nodeInstance instanceof TimerNodeInstance) {
            TimerNodeInstance tni = (TimerNodeInstance) nodeInstance;
            if (tni.getNodeName().equals(timerName) || tni.getTimerId() == timerId) {
                TimerInstance timer = tm.getTimerMap().get(tni.getTimerId());
                TimerInstance newTimer = rescheduleTimer(timer, tm);
                logger.debug("New timer {} about to be registered", newTimer);
                tm.registerTimer(newTimer, wfp);
                tni.internalSetTimerId(newTimer.getId());
                logger.debug("New timer {} successfully registered", newTimer);
                break;
            }
        } else if (nodeInstance instanceof StateBasedNodeInstance) {
            StateBasedNodeInstance sbni = (StateBasedNodeInstance) nodeInstance;
            List<Long> timerList = sbni.getTimerInstances();
            if (sbni.getNodeName().equals(timerName) || (timerList != null && timerList.contains(timerId))) {
                if (timerList != null && timerList.size() == 1) {
                    TimerInstance timer = tm.getTimerMap().get(timerList.get(0));
                    TimerInstance newTimer = rescheduleTimer(timer, tm);
                    logger.debug("New timer {} about to be registered", newTimer);
                    tm.registerTimer(newTimer, wfp);
                    timerList.clear();
                    timerList.add(newTimer.getId());
                    sbni.internalSetTimerInstances(timerList);
                    logger.debug("New timer {} successfully registered", newTimer);
                }
                break;
            }
        }
    }
    return null;
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) List(java.util.List) TimerManager(org.jbpm.process.instance.timer.TimerManager) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance) TimerNodeInstance(org.jbpm.workflow.instance.node.TimerNodeInstance)

Example 8 with RuleFlowProcessInstance

use of org.jbpm.ruleflow.instance.RuleFlowProcessInstance in project jbpm by kiegroup.

the class MVELReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.

@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
    ReturnValueDescr descr = new ReturnValueDescr();
    descr.setText("return value");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    PackageBuildContext context = new PackageBuildContext();
    context.init(pkgBuilder, pkg, null, dialectRegistry, mvelDialect, null);
    pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));
    ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
    final MVELReturnValueEvaluatorBuilder builder = new MVELReturnValueEvaluatorBuilder();
    builder.build(context, node, descr, null);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
    final KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("value", true);
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
    processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);
    SplitInstance splitInstance = new SplitInstance();
    splitInstance.setProcessInstance(processInstance);
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkgBuilder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
    ((MVELReturnValueEvaluator) node.getReturnValueEvaluator()).compile(data);
    assertTrue(node.evaluate(splitInstance, null, null));
    ksession.setGlobal("value", false);
    assertFalse(node.evaluate(splitInstance, null, null));
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ReturnValueDescr(org.drools.compiler.compiler.ReturnValueDescr) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELReturnValueEvaluator(org.jbpm.process.instance.impl.MVELReturnValueEvaluator) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) MVELReturnValueEvaluatorBuilder(org.jbpm.process.builder.dialect.mvel.MVELReturnValueEvaluatorBuilder) SplitInstance(org.jbpm.workflow.instance.node.SplitInstance) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) PackageBuildContext(org.drools.compiler.rule.builder.PackageBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 9 with RuleFlowProcessInstance

use of org.jbpm.ruleflow.instance.RuleFlowProcessInstance in project jbpm by kiegroup.

the class TestStatefulKnowledgeSession method getProcessInstances.

public Collection<ProcessInstance> getProcessInstances() {
    List<ProcessInstance> pis = new ArrayList<ProcessInstance>();
    pis.add(new RuleFlowProcessInstance());
    return pis;
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ArrayList(java.util.ArrayList) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 10 with RuleFlowProcessInstance

use of org.jbpm.ruleflow.instance.RuleFlowProcessInstance in project jbpm by kiegroup.

the class TestStatefulKnowledgeSession method getProcessInstance.

public ProcessInstance getProcessInstance(long arg0) {
    RuleFlowProcessInstance pi = new RuleFlowProcessInstance();
    pi.setId(arg0);
    ProcessImpl processImpl = new ProcessImpl();
    processImpl.setId("" + arg0);
    pi.setProcess(processImpl);
    return pi;
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessImpl(org.jbpm.process.core.impl.ProcessImpl)

Aggregations

RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)23 KieSession (org.kie.api.runtime.KieSession)14 Test (org.junit.Test)12 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)11 RegistryContext (org.drools.core.command.impl.RegistryContext)6 KieBase (org.kie.api.KieBase)6 ProcessInstanceNotFoundException (org.jbpm.services.api.ProcessInstanceNotFoundException)5 StringReader (java.io.StringReader)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 WorkflowProcessImpl (org.jbpm.workflow.core.impl.WorkflowProcessImpl)4 NodeInstance (org.kie.api.runtime.process.NodeInstance)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 ReturnValueDescr (org.drools.compiler.compiler.ReturnValueDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3 ReturnValueConstraintEvaluator (org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator)3 TimerManager (org.jbpm.process.instance.timer.TimerManager)3 ArrayList (java.util.ArrayList)2 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)2