use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project drools by kiegroup.
the class AgendaRuleFlowGroupsTest method testAgendaGroupAndRuleFlowGroup.
@Test
public void testAgendaGroupAndRuleFlowGroup() throws Exception {
CommandBasedStatefulKnowledgeSession ksession = createSession(-1, "agenda-groups.drl", "ruleflow-groups.drl");
org.drools.core.spi.AgendaGroup[] groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroupsManager().getAgendaGroups();
// only main is available
assertEquals(1, groups.length);
assertEquals("MAIN", groups[0].getName());
long id = ksession.getIdentifier();
List<String> list = new ArrayList<String>();
list.add("Test");
ksession.insert(list);
ksession.execute(new ActivateAgendaGroupCommand("agenda-group"));
ksession.execute(new ActivateRuleFlowCommand("ruleflow-group"));
ksession.dispose();
ksession = createSession(id, "agenda-groups.drl", "ruleflow-groups.drl");
groups = ((InternalAgenda) stripSession(ksession).getAgenda()).getAgendaGroupsManager().getAgendaGroups();
// main and agenda group is now on the agenda
assertEquals(3, groups.length);
assertEquals("MAIN", groups[0].getName());
assertEquals("ruleflow-group", groups[1].getName());
assertEquals("agenda-group", groups[2].getName());
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession 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()));
}
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.
the class DynamicProcessTest method testDynamicProcess.
@Test
public void testDynamicProcess() throws Exception {
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
factory.name("HelloWorldProcess").version("1.0").packageName("org.jbpm").imports("org.jbpm.bpmn2.objects.Person").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().splitNode(3).name("Split").type(Split.TYPE_XOR).constraint(4, "c1", "code", Dialect.JAVA, "return new Person().getName() == null;", 1).constraint(5, "c2", "code", Dialect.JAVA, "return false;", 2).done().endNode(4).name("End1").done().endNode(5).name("End2").done().connection(1, 2).connection(2, 3).connection(3, 4).connection(3, 5);
final RuleFlowProcess process = factory.validate().getProcess();
Resource resource = ResourceFactory.newByteArrayResource(XmlRuleFlowProcessDumper.INSTANCE.dump(process).getBytes());
// source path or target path must be set to be added into kbase
resource.setSourcePath("/tmp/dynamicProcess.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(resource);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
ksession.addEventListener(new ProcessEventListener() {
public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
}
public void beforeProcessStarted(ProcessStartedEvent arg0) {
logger.info("{}", arg0);
}
public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
logger.info("{}", arg0);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
logger.info("{}", arg0);
}
public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
logger.info("{}", arg0);
}
public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
}
public void afterProcessStarted(ProcessStartedEvent arg0) {
}
public void afterProcessCompleted(ProcessCompletedEvent arg0) {
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
}
public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
}
});
final ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.startProcess("org.jbpm.HelloWorld");
HumanTaskNode node = new HumanTaskNode();
node.setName("Task2");
node.setId(6);
List<DroolsAction> actions = new ArrayList<DroolsAction>();
actions.add(new DroolsConsequenceAction("java", "System.out.println(\"OnEntry\");"));
node.setActions("onEntry", actions);
insertNodeInBetween(process, 2, 3, node);
XmlBPMNProcessDumper.INSTANCE.dump(process);
((CommandBasedStatefulKnowledgeSession) ksession).getRunner().execute(new ExecutableCommand<Void>() {
public Void execute(Context context) {
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
((ProcessInstanceImpl) ksession.getProcessInstance(processInstance.getId())).updateProcess(process);
return null;
}
});
assertProcessInstanceActive(processInstance);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceActive(processInstance);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
ksession.dispose();
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.
the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromDisc.
@Test(timeout = 20000)
public void testStartTimerCycleFromDisc() throws Exception {
NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
KieBase kbase = createKnowledgeBaseFromDisc("BPMN2-StartTimerCycle.bpmn2");
try {
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.addEventListener(countDownListener);
assertEquals(0, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
long sessionId = ksession.getIdentifier();
Environment env = ksession.getEnvironment();
final List<Long> list = new ArrayList<Long>();
ksession.addEventListener(new DefaultProcessEventListener() {
public void beforeProcessStarted(ProcessStartedEvent event) {
list.add(event.getProcessInstance().getId());
}
});
((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
countDownListener.waitTillCompleted();
assertEquals(2, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
logger.info("dispose");
ksession.dispose();
countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
ksession.addEventListener(countDownListener);
AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
final List<Long> list2 = new ArrayList<Long>();
ksession.addEventListener(new DefaultProcessEventListener() {
public void beforeProcessStarted(ProcessStartedEvent event) {
list2.add(event.getProcessInstance().getId());
}
});
((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
countDownListener.waitTillCompleted();
assertEquals(4, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
abortProcessInstances(ksession);
ksession.dispose();
} finally {
ksession = createKnowledgeSession(kbase);
abortProcessInstances(ksession);
ksession.dispose();
}
}
use of org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession in project jbpm by kiegroup.
the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromClasspathDRL.
@Test
@Ignore("beta4 phreak")
public void testStartTimerCycleFromClasspathDRL() throws Exception {
KieBase kbase = createKnowledgeBaseWithoutDumper("rules-timer.drl");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
long sessionId = ksession.getIdentifier();
Environment env = ksession.getEnvironment();
final List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
ksession.fireAllRules();
Thread.sleep(5000);
assertEquals(2, list.size());
logger.info("dispose");
ksession.dispose();
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
final List<String> list2 = new ArrayList<String>();
ksession.setGlobal("list", list2);
((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
ksession.fireAllRules();
Thread.sleep(5000);
assertEquals(3, list2.size());
}
Aggregations