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);
}
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;
}
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));
}
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;
}
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;
}
Aggregations